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