diff options
Diffstat (limited to 'tests/topotests/lib/lutil.py')
| -rw-r--r-- | tests/topotests/lib/lutil.py | 265 |
1 files changed, 169 insertions, 96 deletions
diff --git a/tests/topotests/lib/lutil.py b/tests/topotests/lib/lutil.py index 05ed9c007d..1fb4f48b0f 100644 --- a/tests/topotests/lib/lutil.py +++ b/tests/topotests/lib/lutil.py @@ -32,46 +32,53 @@ from mininet.net import Mininet # These functions are inteneted to provide support for CI testing within MiniNet # environments. + class lUtil: - #to be made configurable in the future - base_script_dir = '.' - base_log_dir = '.' - fout_name = 'output.log' - fsum_name = 'summary.txt' + # to be made configurable in the future + base_script_dir = "." + base_log_dir = "." + fout_name = "output.log" + fsum_name = "summary.txt" l_level = 6 CallOnFail = False l_total = 0 l_pass = 0 l_fail = 0 - l_filename = '' + l_filename = "" l_last = None l_line = 0 l_dotall_experiment = False l_last_nl = None - fout = '' - fsum = '' - net = '' + fout = "" + fsum = "" + net = "" def log(self, str, level=6): if self.l_level > 0: - if self.fout == '': - self.fout = open(self.fout_name, 'w', 0) - self.fout.write(str+'\n') + if self.fout == "": + self.fout = open(self.fout_name, "w", 0) + self.fout.write(str + "\n") if level <= self.l_level: print(str) def summary(self, str): - if self.fsum == '': - self.fsum = open(self.fsum_name, 'w', 0) - self.fsum.write('\ -******************************************************************************\n') - self.fsum.write('\ -Test Target Summary Pass Fail\n') - self.fsum.write('\ -******************************************************************************\n') - self.fsum.write(str+'\n') + if self.fsum == "": + self.fsum = open(self.fsum_name, "w", 0) + self.fsum.write( + "\ +******************************************************************************\n" + ) + self.fsum.write( + "\ +Test Target Summary Pass Fail\n" + ) + self.fsum.write( + "\ +******************************************************************************\n" + ) + self.fsum.write(str + "\n") def result(self, target, success, str, logstr=None): if success: @@ -88,32 +95,34 @@ Test Target Summary Pass Fail\n if logstr != None: self.log("R:%d %s: %s" % (self.l_total, sstr, logstr)) res = "%-4d %-6s %-56s %-4d %d" % (self.l_total, target, str, p, f) - self.log ('R:'+res) + self.log("R:" + res) self.summary(res) if f == 1 and self.CallOnFail != False: self.CallOnFail() def closeFiles(self): - ret = '\ + ret = ( + "\ ******************************************************************************\n\ Total %-4d %-4d %d\n\ -******************************************************************************'\ -% (self.l_total, self.l_pass, self.l_fail) - if self.fsum != '': - self.fsum.write(ret + '\n') +******************************************************************************" + % (self.l_total, self.l_pass, self.l_fail) + ) + if self.fsum != "": + self.fsum.write(ret + "\n") self.fsum.close() - self.fsum = '' - if self.fout != '': + self.fsum = "" + if self.fout != "": if os.path.isfile(self.fsum_name): - r = open(self.fsum_name, 'r') + r = open(self.fsum_name, "r") self.fout.write(r.read()) r.close() self.fout.close() - self.fout = '' + self.fout = "" return ret def setFilename(self, name): - str = 'FILE: ' + name + str = "FILE: " + name self.log(str) self.summary(str) self.l_filename = name @@ -128,19 +137,19 @@ Total %-4d %-4d %d\n\ def strToArray(self, string): a = [] c = 0 - end = '' + end = "" words = string.split() - if len(words) < 1 or words[0].startswith('#'): + if len(words) < 1 or words[0].startswith("#"): return a words = string.split() for word in words: if len(end) == 0: a.append(word) else: - a[c] += str(' '+word) - if end == '\\': - end = '' - if not word.endswith('\\'): + a[c] += str(" " + word) + if end == "\\": + end = "" + if not word.endswith("\\"): if end != '"': if word.startswith('"'): end = '"' @@ -148,14 +157,14 @@ Total %-4d %-4d %d\n\ c += 1 else: if word.endswith('"'): - end = '' + end = "" c += 1 else: c += 1 else: - end = '\\' - # if len(end) == 0: - # print('%d:%s:' % (c, a[c-1])) + end = "\\" + # if len(end) == 0: + # print('%d:%s:' % (c, a[c-1])) return a @@ -169,27 +178,37 @@ Total %-4d %-4d %d\n\ luCommand(a[1], a[2], a[3], a[4], a[5]) else: self.l_line += 1 - self.log('%s:%s %s' % (self.l_filename, self.l_line , line)) + self.log("%s:%s %s" % (self.l_filename, self.l_line, line)) if len(a) >= 2: - if a[0] == 'sleep': + if a[0] == "sleep": time.sleep(int(a[1])) - elif a[0] == 'include': + elif a[0] == "include": self.execTestFile(a[1]) f.close() else: - self.log('unable to read: ' + tstFile) + self.log("unable to read: " + tstFile) sys.exit(1) def command(self, target, command, regexp, op, result, returnJson): global net - if op != 'wait': - self.l_line += 1 - self.log('(#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:' % \ - (self.l_total+1, - self.l_filename, self.l_line, target, command, regexp, op, result)) - if self.net == '': + if op != "wait": + self.l_line += 1 + self.log( + "(#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:" + % ( + self.l_total + 1, + self.l_filename, + self.l_line, + target, + command, + regexp, + op, + result, + ) + ) + if self.net == "": return False - #self.log("Running %s %s" % (target, command)) + # self.log("Running %s %s" % (target, command)) js = None out = self.net[target].cmd(command).rstrip() if len(out) == 0: @@ -201,13 +220,15 @@ Total %-4d %-4d %d\n\ js = json.loads(out) except: js = None - self.log('WARNING: JSON load failed -- confirm command output is in JSON format.') - self.log('COMMAND OUTPUT:%s:' % report) + self.log( + "WARNING: JSON load failed -- confirm command output is in JSON format." + ) + self.log("COMMAND OUTPUT:%s:" % report) # Experiment: can we achieve the same match behavior via DOTALL # without converting newlines to spaces? out_nl = out - search_nl = re.search(regexp, out_nl, re.DOTALL); + search_nl = re.search(regexp, out_nl, re.DOTALL) self.l_last_nl = search_nl # Set up for comparison if search_nl != None: @@ -220,32 +241,50 @@ Total %-4d %-4d %d\n\ search = re.search(regexp, out) self.l_last = search if search == None: - if op == 'fail': + if op == "fail": success = True else: success = False ret = success else: ret = search.group() - if op != 'fail': + if op != "fail": success = True level = 7 else: success = False level = 5 - self.log('found:%s:' % ret, level) + self.log("found:%s:" % ret, level) # Experiment: compare matched strings obtained each way if self.l_dotall_experiment and (group_nl_converted != ret): - self.log('DOTALL experiment: strings differ dotall=[%s] orig=[%s]' % (group_nl_converted, ret), 9) - if op == 'pass' or op == 'fail': + self.log( + "DOTALL experiment: strings differ dotall=[%s] orig=[%s]" + % (group_nl_converted, ret), + 9, + ) + if op == "pass" or op == "fail": self.result(target, success, result) if js != None: return js return ret - def wait(self, target, command, regexp, op, result, wait, returnJson, wait_time=0.5): - self.log('%s:%s WAIT:%s:%s:%s:%s:%s:%s:%s:' % \ - (self.l_filename, self.l_line, target, command, regexp, op, result,wait,wait_time)) + def wait( + self, target, command, regexp, op, result, wait, returnJson, wait_time=0.5 + ): + self.log( + "%s:%s WAIT:%s:%s:%s:%s:%s:%s:%s:" + % ( + self.l_filename, + self.l_line, + target, + command, + regexp, + op, + result, + wait, + wait_time, + ) + ) found = False n = 0 startt = time.time() @@ -264,103 +303,137 @@ Total %-4d %-4d %d\n\ time.sleep(wait_time) delta = time.time() - startt - self.log('Done after %d loops, time=%s, Found=%s' % (n, delta, found)) - found = self.command(target, command, regexp, 'pass', '%s +%4.2f secs' % (result, delta), returnJson) + self.log("Done after %d loops, time=%s, Found=%s" % (n, delta, found)) + found = self.command( + target, + command, + regexp, + "pass", + "%s +%4.2f secs" % (result, delta), + returnJson, + ) return found -#initialized by luStart -LUtil=None -#entry calls -def luStart(baseScriptDir='.', baseLogDir='.', net='', - fout='output.log', fsum='summary.txt', level=None): +# initialized by luStart +LUtil = None + +# entry calls +def luStart( + baseScriptDir=".", + baseLogDir=".", + net="", + fout="output.log", + fsum="summary.txt", + level=None, +): global LUtil - #init class - LUtil=lUtil() + # init class + LUtil = lUtil() LUtil.base_script_dir = baseScriptDir LUtil.base_log_dir = baseLogDir LUtil.net = net - if fout != '': - LUtil.fout_name = baseLogDir + '/' + fout + if fout != "": + LUtil.fout_name = baseLogDir + "/" + fout if fsum != None: - LUtil.fsum_name = baseLogDir + '/' + fsum + LUtil.fsum_name = baseLogDir + "/" + fsum if level != None: LUtil.l_level = level LUtil.l_dotall_experiment = False LUtil.l_dotall_experiment = True -def luCommand(target, command, regexp='.', op='none', result='', time=10, returnJson=False, wait_time=0.5): - if op != 'wait': + +def luCommand( + target, + command, + regexp=".", + op="none", + result="", + time=10, + returnJson=False, + wait_time=0.5, +): + if op != "wait": return LUtil.command(target, command, regexp, op, result, returnJson) else: - return LUtil.wait(target, command, regexp, op, result, time, returnJson, wait_time) + return LUtil.wait( + target, command, regexp, op, result, time, returnJson, wait_time + ) + def luLast(usenl=False): if usenl: if LUtil.l_last_nl != None: - LUtil.log('luLast:%s:' % LUtil.l_last_nl.group(), 7) + LUtil.log("luLast:%s:" % LUtil.l_last_nl.group(), 7) return LUtil.l_last_nl else: if LUtil.l_last != None: - LUtil.log('luLast:%s:' % LUtil.l_last.group(), 7) + LUtil.log("luLast:%s:" % LUtil.l_last.group(), 7) return LUtil.l_last + def luInclude(filename, CallOnFail=None): - tstFile = LUtil.base_script_dir + '/' + filename + tstFile = LUtil.base_script_dir + "/" + filename LUtil.setFilename(filename) if CallOnFail != None: oldCallOnFail = LUtil.getCallOnFail() LUtil.setCallOnFail(CallOnFail) - if filename.endswith('.py'): - LUtil.log("luInclude: execfile "+tstFile) + if filename.endswith(".py"): + LUtil.log("luInclude: execfile " + tstFile) execfile(tstFile) else: - LUtil.log("luInclude: execTestFile "+tstFile) + LUtil.log("luInclude: execTestFile " + tstFile) LUtil.execTestFile(tstFile) if CallOnFail != None: LUtil.setCallOnFail(oldCallOnFail) + def luFinish(): global LUtil ret = LUtil.closeFiles() - #done + # done LUtil = None - return ret; + return ret + def luNumFail(): return LUtil.l_fail + def luNumPass(): return LUtil.l_pass + def luResult(target, success, str, logstr=None): return LUtil.result(target, success, str, logstr) + def luShowResults(prFunction): printed = 0 - sf = open(LUtil.fsum_name, 'r') + sf = open(LUtil.fsum_name, "r") for line in sf: - printed+=1 + printed += 1 prFunction(line.rstrip()) sf.close() + def luShowFail(): printed = 0 - sf = open(LUtil.fsum_name, 'r') + sf = open(LUtil.fsum_name, "r") for line in sf: if line[-2] != "0": - printed+=1 + printed += 1 logger.error(line.rstrip()) sf.close() if printed > 0: - logger.error("See %s for details of errors" % LUtil.fout_name) + logger.error("See %s for details of errors" % LUtil.fout_name) + -#for testing -if __name__ == '__main__': - print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '/lib') +# for testing +if __name__ == "__main__": + print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + "/lib") luStart() for arg in sys.argv[1:]: luInclude(arg) luFinish() sys.exit(0) - |
