]>
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
27 from lib
.topolog
import logger
28 from lib
.topotest
import json_cmp
29 from mininet
.net
import Mininet
34 # These functions are inteneted to provide support for CI testing within MiniNet
39 # to be made configurable in the future
42 fout_name
= "output.log"
43 fsum_name
= "summary.txt"
53 l_dotall_experiment
= False
60 def log(self
, str, level
=6):
63 self
.fout
= open(self
.fout_name
, "w")
64 self
.fout
.write(str + "\n")
65 if level
<= self
.l_level
:
68 def summary(self
, str):
70 self
.fsum
= open(self
.fsum_name
, "w")
73 ******************************************************************************\n"
77 Test Target Summary Pass Fail\n"
81 ******************************************************************************\n"
83 self
.fsum
.write(str + "\n")
85 def result(self
, target
, success
, str, logstr
=None):
98 self
.log("R:%d %s: %s" % (self
.l_total
, sstr
, logstr
))
99 res
= "%-4d %-6s %-56s %-4d %d" % (self
.l_total
, target
, str, p
, f
)
102 if f
== 1 and self
.CallOnFail
!= False:
105 def closeFiles(self
):
108 ******************************************************************************\n\
109 Total %-4d %-4d %d\n\
110 ******************************************************************************"
111 % (self
.l_total
, self
.l_pass
, self
.l_fail
)
114 self
.fsum
.write(ret
+ "\n")
118 if os
.path
.isfile(self
.fsum_name
):
119 r
= open(self
.fsum_name
, "r")
120 self
.fout
.write(r
.read())
126 def setFilename(self
, name
):
127 str = "FILE: " + name
130 self
.l_filename
= name
133 def getCallOnFail(self
):
134 return self
.CallOnFail
136 def setCallOnFail(self
, CallOnFail
):
137 self
.CallOnFail
= CallOnFail
139 def strToArray(self
, string
):
143 words
= string
.split()
144 if len(words
) < 1 or words
[0].startswith("#"):
146 words
= string
.split()
151 a
[c
] += str(" " + word
)
154 if not word
.endswith("\\"):
156 if word
.startswith('"'):
161 if word
.endswith('"'):
169 # print('%d:%s:' % (c, a[c-1]))
173 def execTestFile(self
, tstFile
):
174 if os
.path
.isfile(tstFile
):
178 a
= self
.strToArray(line
)
180 luCommand(a
[1], a
[2], a
[3], a
[4], a
[5])
183 self
.log("%s:%s %s" % (self
.l_filename
, self
.l_line
, line
))
186 time
.sleep(int(a
[1]))
187 elif a
[0] == "include":
188 self
.execTestFile(a
[1])
191 self
.log("unable to read: " + tstFile
)
194 def command(self
, target
, command
, regexp
, op
, result
, returnJson
):
199 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
203 "%s (#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:"
218 # self.log("Running %s %s" % (target, command))
220 out
= self
.net
[target
].cmd(command
).rstrip()
222 report
= "<no output>"
225 if returnJson
== True:
231 "WARNING: JSON load failed -- confirm command output is in JSON format."
233 self
.log("COMMAND OUTPUT:%s:" % report
)
236 if op
== "jsoncmp_pass" or op
== "jsoncmp_fail":
238 expect
= json
.loads(regexp
)
242 "WARNING: JSON load failed -- confirm regex input is in JSON format."
244 json_diff
= json_cmp(js
, expect
)
245 if json_diff
!= None:
246 if op
== "jsoncmp_fail":
250 self
.log("JSON DIFF:%s:" % json_diff
)
253 if op
== "jsoncmp_fail":
257 self
.result(target
, success
, result
)
262 # Experiment: can we achieve the same match behavior via DOTALL
263 # without converting newlines to spaces?
265 search_nl
= re
.search(regexp
, out_nl
, re
.DOTALL
)
266 self
.l_last_nl
= search_nl
267 # Set up for comparison
268 if search_nl
!= None:
269 group_nl
= search_nl
.group()
270 group_nl_converted
= " ".join(group_nl
.splitlines())
272 group_nl_converted
= None
274 out
= " ".join(out
.splitlines())
275 search
= re
.search(regexp
, out
)
291 self
.log("found:%s:" % ret
, level
)
292 # Experiment: compare matched strings obtained each way
293 if self
.l_dotall_experiment
and (group_nl_converted
!= ret
):
295 "DOTALL experiment: strings differ dotall=[%s] orig=[%s]"
296 % (group_nl_converted
, ret
),
299 if 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 # Calculate the amount of `sleep`s we are going to peform.
327 wait_count
= int(math
.ceil(wait
/ wait_time
)) + 1
329 while wait_count
> 0:
331 found
= self
.command(target
, command
, regexp
, op
, result
, returnJson
)
332 if found
is not False:
337 time
.sleep(wait_time
)
339 delta
= time
.time() - startt
340 self
.log("Done after %d loops, time=%s, Found=%s" % (n
, delta
, found
))
341 found
= self
.command(
346 "%s +%4.2f secs" % (result
, delta
),
352 # initialized by luStart
367 LUtil
.base_script_dir
= baseScriptDir
368 LUtil
.base_log_dir
= baseLogDir
371 LUtil
.fout_name
= baseLogDir
+ "/" + fout
373 LUtil
.fsum_name
= baseLogDir
+ "/" + fsum
375 LUtil
.l_level
= level
376 LUtil
.l_dotall_experiment
= False
377 LUtil
.l_dotall_experiment
= True
391 return LUtil
.command(target
, command
, regexp
, op
, result
, returnJson
)
394 target
, command
, regexp
, op
, result
, time
, returnJson
, wait_time
398 def luLast(usenl
=False):
400 if LUtil
.l_last_nl
!= None:
401 LUtil
.log("luLast:%s:" % LUtil
.l_last_nl
.group(), 7)
402 return LUtil
.l_last_nl
404 if LUtil
.l_last
!= None:
405 LUtil
.log("luLast:%s:" % LUtil
.l_last
.group(), 7)
409 def luInclude(filename
, CallOnFail
=None):
410 tstFile
= LUtil
.base_script_dir
+ "/" + filename
411 LUtil
.setFilename(filename
)
412 if CallOnFail
!= None:
413 oldCallOnFail
= LUtil
.getCallOnFail()
414 LUtil
.setCallOnFail(CallOnFail
)
415 if filename
.endswith(".py"):
416 LUtil
.log("luInclude: execfile " + tstFile
)
417 with
open(tstFile
) as infile
:
420 LUtil
.log("luInclude: execTestFile " + tstFile
)
421 LUtil
.execTestFile(tstFile
)
422 if CallOnFail
!= None:
423 LUtil
.setCallOnFail(oldCallOnFail
)
428 ret
= LUtil
.closeFiles()
442 def luResult(target
, success
, str, logstr
=None):
443 return LUtil
.result(target
, success
, str, logstr
)
446 def luShowResults(prFunction
):
448 sf
= open(LUtil
.fsum_name
, "r")
451 prFunction(line
.rstrip())
457 sf
= open(LUtil
.fsum_name
, "r")
461 logger
.error(line
.rstrip())
464 logger
.error("See %s for details of errors" % LUtil
.fout_name
)
468 if __name__
== "__main__":
469 print(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))) + "/lib")
471 for arg
in sys
.argv
[1:]: