]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/lutil.py
3 # Copyright 2017, LabN Consulting, L.L.C.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; see the file COPYING; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 from lib
.topolog
import logger
27 from lib
.topotest
import json_cmp
32 # These functions are inteneted to provide support for CI testing within MiniNet
37 # to be made configurable in the future
40 fout_name
= "output.log"
41 fsum_name
= "summary.txt"
51 l_dotall_experiment
= False
59 def log(self
, str, level
=6):
62 self
.fout
= open(self
.fout_name
, "w")
63 self
.fout
.write(str + "\n")
64 if level
<= self
.l_level
:
67 def summary(self
, str):
69 self
.fsum
= open(self
.fsum_name
, "w")
72 ******************************************************************************\n"
76 Test Target Summary Pass Fail\n"
80 ******************************************************************************\n"
82 self
.fsum
.write(str + "\n")
84 def result(self
, target
, success
, str, logstr
=None):
97 self
.log("R:%d %s: %s" % (self
.l_total
, sstr
, logstr
))
98 res
= "%-4d %-6s %-56s %-4d %d" % (self
.l_total
, target
, str, p
, f
)
101 if f
== 1 and self
.CallOnFail
!= False:
104 def closeFiles(self
):
107 ******************************************************************************\n\
108 Total %-4d %-4d %d\n\
109 ******************************************************************************"
110 % (self
.l_total
, self
.l_pass
, self
.l_fail
)
113 self
.fsum
.write(ret
+ "\n")
117 if os
.path
.isfile(self
.fsum_name
):
118 r
= open(self
.fsum_name
, "r")
119 self
.fout
.write(r
.read())
125 def setFilename(self
, name
):
126 str = "FILE: " + name
129 self
.l_filename
= name
132 def getCallOnFail(self
):
133 return self
.CallOnFail
135 def setCallOnFail(self
, CallOnFail
):
136 self
.CallOnFail
= CallOnFail
138 def strToArray(self
, string
):
142 words
= string
.split()
143 if len(words
) < 1 or words
[0].startswith("#"):
145 words
= string
.split()
150 a
[c
] += str(" " + word
)
153 if not word
.endswith("\\"):
155 if word
.startswith('"'):
160 if word
.endswith('"'):
168 # print('%d:%s:' % (c, a[c-1]))
172 def execTestFile(self
, tstFile
):
173 if os
.path
.isfile(tstFile
):
177 a
= self
.strToArray(line
)
179 luCommand(a
[1], a
[2], a
[3], a
[4], a
[5])
182 self
.log("%s:%s %s" % (self
.l_filename
, self
.l_line
, line
))
185 time
.sleep(int(a
[1]))
186 elif a
[0] == "include":
187 self
.execTestFile(a
[1])
190 self
.log("unable to read: " + tstFile
)
193 def command(self
, target
, command
, regexp
, op
, result
, returnJson
, startt
=None, force_result
=False):
195 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
199 "%s (#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:"
214 # self.log("Running %s %s" % (target, command))
216 out
= self
.net
[target
].cmd(command
).rstrip()
218 report
= "<no output>"
221 if returnJson
== True:
227 "WARNING: JSON load failed -- confirm command output is in JSON format."
229 self
.log("COMMAND OUTPUT:%s:" % report
)
232 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
234 expect
= json
.loads(regexp
)
238 "WARNING: JSON load failed -- confirm regex input is in JSON format."
240 json_diff
= json_cmp(js
, expect
)
241 if json_diff
!= None:
242 if op
== "jsoncmp_fail":
246 self
.log("JSON DIFF:%s:" % json_diff
)
249 if op
== "jsoncmp_fail":
253 self
.result(target
, success
, result
)
258 # Experiment: can we achieve the same match behavior via DOTALL
259 # without converting newlines to spaces?
261 search_nl
= re
.search(regexp
, out_nl
, re
.DOTALL
)
262 self
.l_last_nl
= search_nl
263 # Set up for comparison
264 if search_nl
!= None:
265 group_nl
= search_nl
.group()
266 group_nl_converted
= " ".join(group_nl
.splitlines())
268 group_nl_converted
= None
270 out
= " ".join(out
.splitlines())
271 search
= re
.search(regexp
, out
)
287 self
.log("found:%s:" % ret
, level
)
288 # Experiment: compare matched strings obtained each way
289 if self
.l_dotall_experiment
and (group_nl_converted
!= ret
):
291 "DOTALL experiment: strings differ dotall=[%s] orig=[%s]"
292 % (group_nl_converted
, ret
),
296 if js
!= None or ret
is not False or force_result
is not False:
297 delta
= time
.time() - startt
298 self
.result(target
, success
, "%s +%4.2f secs" % (result
, delta
))
299 elif op
== "pass" or op
== "fail":
300 self
.result(target
, success
, result
)
306 self
, target
, command
, regexp
, op
, result
, wait
, returnJson
, wait_time
=0.5
309 "%s:%s WAIT:%s:%s:%s:%s:%s:%s:%s:"
326 if (op
== "wait-strict") or ((op
== "wait") and self
.l_wait_strict
):
331 # Calculate the amount of `sleep`s we are going to peform.
332 wait_count
= int(math
.ceil(wait
/ wait_time
)) + 1
335 while wait_count
> 0:
338 # log a failure on last iteration if we don't get desired regexp
339 if strict
and (wait_count
== 1):
342 found
= self
.command(target
, command
, regexp
, op
, result
, returnJson
, startt
, force_result
)
343 if found
is not False:
348 time
.sleep(wait_time
)
350 delta
= time
.time() - startt
351 self
.log("Done after %d loops, time=%s, Found=%s" % (n
, delta
, found
))
355 # 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
)
472 # Sets default wait type for luCommand(op="wait) (may be overridden by
473 # specifying luCommand(op="wait-strict") or luCommand(op="wait-nostrict")).
475 # "nostrict" is the historical default behavior, which is to ignore
476 # failures to match the specified regexp in the specified time.
478 # "strict" means that failure to match the specified regexp in the
479 # specified time yields an explicit, logged failure result
481 def luSetWaitType(waittype
):
482 if waittype
== "strict":
483 LUtil
.l_wait_strict
= 1
485 if waittype
== "nostrict":
486 LUtil
.l_wait_strict
= 0
488 raise ValueError('waittype must be one of "strict" or "nostrict"')
492 if __name__
== "__main__":
493 print(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))) + "/lib")
495 for arg
in sys
.argv
[1:]: