]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/lutil.py
2 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Copyright 2017, LabN Consulting, L.L.C.
13 from lib
.topolog
import logger
14 from lib
.topotest
import json_cmp
19 # These functions are inteneted to provide support for CI testing within MiniNet
24 # to be made configurable in the future
27 fout_name
= "output.log"
28 fsum_name
= "summary.txt"
38 l_dotall_experiment
= False
46 def log(self
, str, level
=6):
49 self
.fout
= open(self
.fout_name
, "w")
50 self
.fout
.write(str + "\n")
51 if level
<= self
.l_level
:
54 def summary(self
, str):
56 self
.fsum
= open(self
.fsum_name
, "w")
59 ******************************************************************************\n"
63 Test Target Summary Pass Fail\n"
67 ******************************************************************************\n"
69 self
.fsum
.write(str + "\n")
71 def result(self
, target
, success
, str, logstr
=None):
84 self
.log("R:%d %s: %s" % (self
.l_total
, sstr
, logstr
))
85 res
= "%-4d %-6s %-56s %-4d %d" % (self
.l_total
, target
, str, p
, f
)
88 if f
== 1 and self
.CallOnFail
!= False:
94 ******************************************************************************\n\
96 ******************************************************************************"
97 % (self
.l_total
, self
.l_pass
, self
.l_fail
)
100 self
.fsum
.write(ret
+ "\n")
104 if os
.path
.isfile(self
.fsum_name
):
105 r
= open(self
.fsum_name
, "r")
106 self
.fout
.write(r
.read())
112 def setFilename(self
, name
):
113 str = "FILE: " + name
116 self
.l_filename
= name
119 def getCallOnFail(self
):
120 return self
.CallOnFail
122 def setCallOnFail(self
, CallOnFail
):
123 self
.CallOnFail
= CallOnFail
125 def strToArray(self
, string
):
129 words
= string
.split()
130 if len(words
) < 1 or words
[0].startswith("#"):
132 words
= string
.split()
137 a
[c
] += str(" " + word
)
140 if not word
.endswith("\\"):
142 if word
.startswith('"'):
147 if word
.endswith('"'):
155 # print('%d:%s:' % (c, a[c-1]))
159 def execTestFile(self
, tstFile
):
160 if os
.path
.isfile(tstFile
):
164 a
= self
.strToArray(line
)
166 luCommand(a
[1], a
[2], a
[3], a
[4], a
[5])
169 self
.log("%s:%s %s" % (self
.l_filename
, self
.l_line
, line
))
172 time
.sleep(int(a
[1]))
173 elif a
[0] == "include":
174 self
.execTestFile(a
[1])
177 self
.log("unable to read: " + tstFile
)
192 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
196 "%s (#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:"
211 # self.log("Running %s %s" % (target, command))
213 out
= self
.net
[target
].cmd(command
).rstrip()
215 report
= "<no output>"
218 if returnJson
== True:
224 "WARNING: JSON load failed -- confirm command output is in JSON format."
226 self
.log("COMMAND OUTPUT:%s:" % report
)
229 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
231 expect
= json
.loads(regexp
)
235 "WARNING: JSON load failed -- confirm regex input is in JSON format."
237 json_diff
= json_cmp(js
, expect
)
238 if json_diff
!= None:
239 if op
== "jsoncmp_fail":
243 self
.log("JSON DIFF:%s:" % json_diff
)
246 if op
== "jsoncmp_fail":
250 self
.result(target
, success
, result
)
255 # Experiment: can we achieve the same match behavior via DOTALL
256 # without converting newlines to spaces?
258 search_nl
= re
.search(regexp
, out_nl
, re
.DOTALL
)
259 self
.l_last_nl
= search_nl
260 # Set up for comparison
261 if search_nl
!= None:
262 group_nl
= search_nl
.group()
263 group_nl_converted
= " ".join(group_nl
.splitlines())
265 group_nl_converted
= None
267 out
= " ".join(out
.splitlines())
268 search
= re
.search(regexp
, out
)
284 self
.log("found:%s:" % ret
, level
)
285 # Experiment: compare matched strings obtained each way
286 if self
.l_dotall_experiment
and (group_nl_converted
!= ret
):
288 "DOTALL experiment: strings differ dotall=[%s] orig=[%s]"
289 % (group_nl_converted
, ret
),
293 if js
!= None or ret
is not False or force_result
is not False:
294 delta
= time
.time() - startt
295 self
.result(target
, success
, "%s +%4.2f secs" % (result
, delta
))
296 elif op
== "pass" or op
== "fail":
297 self
.result(target
, success
, result
)
303 self
, target
, command
, regexp
, op
, result
, wait
, returnJson
, wait_time
=0.5
306 "%s:%s WAIT:%s:%s:%s:%s:%s:%s:%s:"
323 if (op
== "wait-strict") or ((op
== "wait") and self
.l_wait_strict
):
328 # Calculate the amount of `sleep`s we are going to peform.
329 wait_count
= int(math
.ceil(wait
/ wait_time
)) + 1
332 while wait_count
> 0:
335 # log a failure on last iteration if we don't get desired regexp
336 if strict
and (wait_count
== 1):
339 found
= self
.command(
340 target
, command
, regexp
, op
, result
, returnJson
, startt
, force_result
342 if found
is not False:
347 time
.sleep(wait_time
)
349 delta
= time
.time() - startt
350 self
.log("Done after %d loops, time=%s, Found=%s" % (n
, delta
, found
))
354 # initialized by luStart
370 LUtil
.base_script_dir
= baseScriptDir
371 LUtil
.base_log_dir
= baseLogDir
374 LUtil
.fout_name
= baseLogDir
+ "/" + fout
376 LUtil
.fsum_name
= baseLogDir
+ "/" + fsum
378 LUtil
.l_level
= level
379 LUtil
.l_dotall_experiment
= False
380 LUtil
.l_dotall_experiment
= True
393 waitops
= ["wait", "wait-strict", "wait-nostrict"]
397 target
, command
, regexp
, op
, result
, time
, returnJson
, wait_time
400 return LUtil
.command(target
, command
, regexp
, op
, result
, returnJson
)
403 def luLast(usenl
=False):
405 if LUtil
.l_last_nl
!= None:
406 LUtil
.log("luLast:%s:" % LUtil
.l_last_nl
.group(), 7)
407 return LUtil
.l_last_nl
409 if LUtil
.l_last
!= None:
410 LUtil
.log("luLast:%s:" % LUtil
.l_last
.group(), 7)
414 def luInclude(filename
, CallOnFail
=None):
415 tstFile
= LUtil
.base_script_dir
+ "/" + filename
416 LUtil
.setFilename(filename
)
417 if CallOnFail
!= None:
418 oldCallOnFail
= LUtil
.getCallOnFail()
419 LUtil
.setCallOnFail(CallOnFail
)
420 if filename
.endswith(".py"):
421 LUtil
.log("luInclude: execfile " + tstFile
)
422 with
open(tstFile
) as infile
:
425 LUtil
.log("luInclude: execTestFile " + tstFile
)
426 LUtil
.execTestFile(tstFile
)
427 if CallOnFail
!= None:
428 LUtil
.setCallOnFail(oldCallOnFail
)
433 ret
= LUtil
.closeFiles()
447 def luResult(target
, success
, str, logstr
=None):
448 return LUtil
.result(target
, success
, str, logstr
)
451 def luShowResults(prFunction
):
453 sf
= open(LUtil
.fsum_name
, "r")
456 prFunction(line
.rstrip())
462 sf
= open(LUtil
.fsum_name
, "r")
466 logger
.error(line
.rstrip())
469 logger
.error("See %s for details of errors" % LUtil
.fout_name
)
473 # Sets default wait type for luCommand(op="wait) (may be overridden by
474 # specifying luCommand(op="wait-strict") or luCommand(op="wait-nostrict")).
476 # "nostrict" is the historical default behavior, which is to ignore
477 # failures to match the specified regexp in the specified time.
479 # "strict" means that failure to match the specified regexp in the
480 # specified time yields an explicit, logged failure result
482 def luSetWaitType(waittype
):
483 if waittype
== "strict":
484 LUtil
.l_wait_strict
= 1
486 if waittype
== "nostrict":
487 LUtil
.l_wait_strict
= 0
489 raise ValueError('waittype must be one of "strict" or "nostrict"')
493 if __name__
== "__main__":
494 print(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))) + "/lib")
496 for arg
in sys
.argv
[1:]: