macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame^] | 1 | import subprocess
|
| 2 | import json
|
| 3 | import os
|
| 4 | from pprint import pprint
|
| 5 |
|
| 6 | test_tmp_file=os.getcwd()+os.sep+"dpctloutputtmp.tmp"
|
| 7 |
|
| 8 |
|
| 9 | def apply_dpctl_cmd(test, config, cmd):
|
| 10 | switch_ip = config["switch_ip"]
|
| 11 | if len(switch_ip) == 0:
|
| 12 | assert(0)
|
| 13 |
|
| 14 | #create the tmp file
|
| 15 | try:
|
| 16 | subprocess.check_call(["ls", test_tmp_file])
|
| 17 | except subprocess.CalledProcessError:
|
| 18 | open(test_tmp_file, "w").close()
|
| 19 | subprocess.call(["sudo", "chmod", "a+w", test_tmp_file])
|
| 20 |
|
| 21 | #apply dpctl command
|
| 22 | subprocess.call("dpctl tcp:"+switch_ip+":6633 "+cmd+" > "+test_tmp_file, shell=True)
|
| 23 |
|
| 24 | with open(test_tmp_file) as tmp_file:
|
| 25 | try:
|
| 26 | json_result=json.loads(tmp_file.read(), encoding='utf-8')
|
| 27 | except ValueError:
|
| 28 | subprocess.call("cat "+ test_tmp_file, shell=True)
|
| 29 | """
|
| 30 | https://docs.python.org/2/library/unittest.html#
|
| 31 | """
|
| 32 | test.assertTrue(False, "NO json format, dpctl may fail")
|
| 33 |
|
| 34 | return json_result |