Sreeju Sreedhar | e3fefd9 | 2019-04-02 15:57:15 -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 | |
| 17 | import subprocess
|
| 18 | import json
|
| 19 | import os
|
| 20 | from pprint import pprint
|
| 21 | import time
|
| 22 |
|
| 23 | test_tmp_file=os.getcwd()+os.sep+"dpctloutputtmp.tmp"
|
| 24 |
|
| 25 |
|
| 26 | def apply_dpctl(test, config, cmd):
|
| 27 | switch_ip = config["switch_ip"]
|
| 28 | if len(switch_ip) == 0:
|
| 29 | assert(0)
|
| 30 |
|
| 31 | #apply dpctl command
|
| 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)
|
| 34 | time.sleep(0.2)
|
| 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)
|
| 44 |
|
| 45 | #parse result
|
| 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 |
|
| 56 | return json_result
|
| 57 |
|
| 58 | def apply_dpctl_mod(test, config, cmd):
|
| 59 | apply_dpctl(test, config, cmd)
|
| 60 |
|
| 61 | |