blob: 3942f84b535601162a445124afd9f645332de44f [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
macauley_cheng420ddce2015-10-26 13:42:23 +080017import subprocess
18import json
19import os
20from pprint import pprint
macauley_cheng0a0a7f62015-11-06 11:36:50 +080021import time
macauley_cheng420ddce2015-10-26 13:42:23 +080022
23test_tmp_file=os.getcwd()+os.sep+"dpctloutputtmp.tmp"
24
25
macauley_chengdc5eb582015-10-26 14:29:17 +080026def apply_dpctl(test, config, cmd):
macauley_cheng420ddce2015-10-26 13:42:23 +080027 switch_ip = config["switch_ip"]
28 if len(switch_ip) == 0:
29 assert(0)
macauley_cheng420ddce2015-10-26 13:42:23 +080030
31 #apply dpctl command
macauley_cheng31507392015-11-06 13:26:50 +080032 #subprocess.call(os.getcwd()+"/dpctl tcp:"+switch_ip+":6633 "+cmd+" > "+test_tmp_file, shell=True)
33 subprocess.call("dpctl tcp:"+switch_ip+":6633 "+cmd+" > "+test_tmp_file, shell=True)
macauley_cheng0a0a7f62015-11-06 11:36:50 +080034 time.sleep(0.2)
macauley_chengdc5eb582015-10-26 14:29:17 +080035
36def apply_dpctl_get_cmd(test, config, cmd):
37
38 #create the tmp file
39 if not os.path.isfile(test_tmp_file):
40 open(test_tmp_file, "w").close()
41 subprocess.call(["sudo", "chmod", "a+w", test_tmp_file])
42
43 apply_dpctl(test, config, cmd)
macauley_cheng0a0a7f62015-11-06 11:36:50 +080044
macauley_chengdc5eb582015-10-26 14:29:17 +080045 #parse result
macauley_cheng420ddce2015-10-26 13:42:23 +080046 with open(test_tmp_file) as tmp_file:
47 try:
48 json_result=json.loads(tmp_file.read(), encoding='utf-8')
49 except ValueError:
50 subprocess.call("cat "+ test_tmp_file, shell=True)
51 """
52 https://docs.python.org/2/library/unittest.html#
53 """
54 test.assertTrue(False, "NO json format, dpctl may fail")
55
macauley_chengdc5eb582015-10-26 14:29:17 +080056 return json_result
57
58def apply_dpctl_mod(test, config, cmd):
59 apply_dpctl(test, config, cmd)
60
61