Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 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_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 17 | import subprocess
|
| 18 | import json
|
| 19 | import os
|
| 20 | from pprint import pprint
|
macauley_cheng | 0a0a7f6 | 2015-11-06 11:36:50 +0800 | [diff] [blame] | 21 | import time
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 22 |
|
| 23 | test_tmp_file=os.getcwd()+os.sep+"dpctloutputtmp.tmp"
|
| 24 |
|
| 25 |
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 26 | def apply_dpctl(test, config, cmd):
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 27 | switch_ip = config["switch_ip"]
|
| 28 | if len(switch_ip) == 0:
|
| 29 | assert(0)
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 30 |
|
| 31 | #apply dpctl command
|
macauley_cheng | 3150739 | 2015-11-06 13:26:50 +0800 | [diff] [blame] | 32 | #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_cheng | 0a0a7f6 | 2015-11-06 11:36:50 +0800 | [diff] [blame] | 34 | time.sleep(0.2)
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 35 |
|
| 36 | def 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_cheng | 0a0a7f6 | 2015-11-06 11:36:50 +0800 | [diff] [blame] | 44 |
|
macauley_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 45 | #parse result
|
macauley_cheng | 420ddce | 2015-10-26 13:42:23 +0800 | [diff] [blame] | 46 | 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_cheng | dc5eb58 | 2015-10-26 14:29:17 +0800 | [diff] [blame] | 56 | return json_result
|
| 57 |
|
| 58 | def apply_dpctl_mod(test, config, cmd):
|
| 59 | apply_dpctl(test, config, cmd)
|
| 60 |
|
| 61 | |