connectTest.py: several changes:

- whitespace, prefer indenting dicts
- moved subroutines out to LFUtils
- renamed a few variables
- fixes inspecting possibly null return values
This commit is contained in:
Jed Reynolds
2020-05-26 17:10:14 -07:00
parent e199faaefb
commit 4f3bdde8ed

View File

@@ -11,36 +11,33 @@ import json
import pprint
from LANforge import LFRequest
from LANforge import LFUtils
from LANforge.LFUtils import *
import create_genlink as genl
debugOn = True
if sys.version_info[0] != 3:
print("This script requires Python 3")
exit(1)
mgrURL = "http://localhost:8080/"
def execWrap(cmd):
if os.system(cmd) != 0:
print("\nError with " + cmd + ",bye\n")
exit(1)
def jsonReq(mgrURL, reqURL, data, debug=False):
def jsonReq(mgrURL, reqURL, data, exitWhenCalled=False):
lf_r = LFRequest.LFRequest(mgrURL + reqURL)
lf_r.addPostData(data)
if debug:
json_response = lf_r.jsonPost(debug)
LFUtils.debug_printer.pprint(json_response)
if exitWhenCalled:
json_response = lf_r.jsonPost(True)
print("jsonReq: debugdie Response: ")
LFUtils.debug_printer.pprint(vars(json_response))
print("jsonReq: bye")
sys.exit(1)
else:
lf_r.jsonPost(debug)
lf_r.jsonPost(exitWhenCalled)
def getJsonInfo(mgrURL, reqURL):
def getJsonInfo(mgrURL, reqURL, debug=False):
lf_r = LFRequest.LFRequest(mgrURL + reqURL)
json_response = lf_r.getAsJson(debugOn)
json_response = lf_r.getAsJson(debug)
return json_response
#print(name)
#j_printer = pprint.PrettyPrinter(indent=2)
@@ -48,44 +45,35 @@ def getJsonInfo(mgrURL, reqURL):
#for record in json_response[key]:
# j_printer.pprint(record)
def removeEndps(mgrURL, endpNames):
for name in endpNames:
#print(f"Removing endp {name}")
data = {
"endp_name":name
}
jsonReq(mgrURL, "cli-json/rm_endp", data)
def removeCX(mgrURL, cxNames):
for name in cxNames:
#print(f"Removing CX {name}")
data = {
"test_mgr":"all",
"cx_name":name
}
jsonReq(mgrURL,"cli-json/rm_cx", data)
print("Checking for LANforge Client")
response = getJsonInfo(mgrURL, 'port/1/1/wiphy0')
timeout = 0
while response == None and timeout != 300:
duration = 0
while ((response == None) and (duration < 300)):
print("LANforge Client not found sleeping 5 seconds")
timeout += 5
time.sleep(5)
duration += 2
time.sleep(2)
response = getJsonInfo(mgrURL, 'port/1/1/wiphy0')
#print(response)
if timeout == 300:
if duration >= 300:
print("Could not connect to LANforge Client")
sys.exit(1)
print("See home/lanforge/Documents/connectTestLogs/connectTestLatest for specific values on latest test")
#Create stations and turn dhcp on
print("Creating station and turning on dhcp")
url = "cli-json/add_sta"
url = "port/1/1/sta00000"
debugOn = True
response = getJsonInfo(mgrURL, url)
if (response is not None):
if (response["interface"] is not None):
print("removing old station")
LFUtils.removePortByName("1.1.sta00000", mgrURL)
time.sleep(1)
url = "cli-json/add_sta"
data = {
"shelf":1,
"resource":1,
@@ -97,9 +85,10 @@ data = {
"mac":"xx:xx:xx:xx:*:xx",
"flags":1024 #0x400 | 1024
}
print("adding new station")
jsonReq(mgrURL, url, data)
time.sleep(5)
time.sleep(1)
reqURL = "cli-json/set_port"
data = {
@@ -109,9 +98,10 @@ data = {
"current_flags": 2147483648, #0x80000000 | 2147483648
"interest":16386 # 0x4002 | 16386
}
print("configuring port")
jsonReq(mgrURL, reqURL, data)
time.sleep(10)
time.sleep(5)
eth1IP = getJsonInfo(mgrURL, "port/1/1/eth1")
if eth1IP['interface']['ip'] == "0.0.0.0":
@@ -124,25 +114,24 @@ data = { "shelf":1,
"probe_flags":1 }
jsonReq(mgrURL, reqURL, data)
staIP = getJsonInfo(mgrURL, "port/1/1/sta00000")
timeout = 0
station_info = getJsonInfo(mgrURL, "port/1/1/sta00000?fields=port,ip")
duration = 0
maxTime = 300
while staIP['interface']['ip'] == "0.0.0.0" and timeout != maxTime:
ip = "0.0.0.0"
while ((ip == "0.0.0.0") and (duration < maxTime)):
print("Station failed to get IP. Waiting 10 seconds...")
staIP = getJsonInfo(mgrURL, "port/1/1/sta00000")
timeout += 10
time.sleep(10)
if timeout == maxTime:
station_info = getJsonInfo(mgrURL, "port/1/1/sta00000?fields=port,ip")
LFUtils.debug_printer.pprint(station_info)
if ((station_info is not None) and ("interface" in station_info) and ("ip" in station_info["interface"])):
ip = station_info["interface"]["ip"]
duration += 2
time.sleep(2)
if duration >= maxTime:
print("sta00000 failed to get an ip. Ending test")
print("Cleaning up...")
reqURL = "cli-json/rm_vlan"
data = {
"shelf":1,
"resource":1,
"port":"sta00000"
}
jsonReq(mgrURL, reqURL, data)
removePortByName("1.sta00000", mgrURL)
sys.exit(1)
@@ -349,14 +338,7 @@ except Exception as e:
print("Something went wrong")
print(e)
print("Cleaning up...")
reqURL = "cli-json/rm_vlan"
data = {
"shelf":1,
"resource":1,
"port":"sta00000"
}
jsonReq(mgrURL, reqURL, data)
LFUtils.removePortByName("1.sta00000", mgrURL)
endpNames = ["testTCP-A", "testTCP-B",
"testUDP-A", "testUDP-B",
@@ -522,14 +504,7 @@ print("\n")
#remove all endpoints and cxs
print("Cleaning up...")
reqURL = "cli-json/rm_vlan"
data = {
"shelf":1,
"resource":1,
"port":"sta00000"
}
jsonReq(mgrURL, reqURL, data)
LFUtils.removePortByName("1.sta00000", mgrURL)
endpNames = ["testTCP-A", "testTCP-B",
"testUDP-A", "testUDP-B",