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 |
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 9 | def apply_dpctl(test, config, cmd):
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 10 | switch_ip = config["switch_ip"]
|
| 11 | if len(switch_ip) == 0:
|
| 12 | assert(0)
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 13 |
|
| 14 | #apply dpctl command
|
| 15 | subprocess.call("dpctl tcp:"+switch_ip+":6633 "+cmd+" > "+test_tmp_file, shell=True)
|
| 16 |
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 17 |
|
| 18 | def apply_dpctl_get_cmd(test, config, cmd):
|
| 19 |
|
| 20 | #create the tmp file
|
| 21 | if not os.path.isfile(test_tmp_file):
|
| 22 | open(test_tmp_file, "w").close()
|
| 23 | subprocess.call(["sudo", "chmod", "a+w", test_tmp_file])
|
| 24 |
|
| 25 | apply_dpctl(test, config, cmd)
|
| 26 |
|
| 27 | #parse result
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 28 | with open(test_tmp_file) as tmp_file:
|
| 29 | try:
|
| 30 | json_result=json.loads(tmp_file.read(), encoding='utf-8')
|
| 31 | except ValueError:
|
| 32 | subprocess.call("cat "+ test_tmp_file, shell=True)
|
| 33 | """
|
| 34 | https://docs.python.org/2/library/unittest.html#
|
| 35 | """
|
| 36 | test.assertTrue(False, "NO json format, dpctl may fail")
|
| 37 |
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 38 | return json_result
|
| 39 |
|
| 40 | def apply_dpctl_mod(test, config, cmd):
|
| 41 | apply_dpctl(test, config, cmd)
|
| 42 |
|
| 43 | |