]>
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 mininet
.net
import Mininet
33 # These functions are inteneted to provide support for CI testing within MiniNet
38 # to be made configurable in the future
41 fout_name
= "output.log"
42 fsum_name
= "summary.txt"
52 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
):
198 "%s (#%d) %s:%s COMMAND:%s:%s:%s:%s:%s:"
213 # self.log("Running %s %s" % (target, command))
215 out
= self
.net
[target
].cmd(command
).rstrip()
217 report
= "<no output>"
220 if returnJson
== True:
226 "WARNING: JSON load failed -- confirm command output is in JSON format."
228 self
.log("COMMAND OUTPUT:%s:" % report
)
230 # Experiment: can we achieve the same match behavior via DOTALL
231 # without converting newlines to spaces?
233 search_nl
= re
.search(regexp
, out_nl
, re
.DOTALL
)
234 self
.l_last_nl
= search_nl
235 # Set up for comparison
236 if search_nl
!= None:
237 group_nl
= search_nl
.group()
238 group_nl_converted
= " ".join(group_nl
.splitlines())
240 group_nl_converted
= None
242 out
= " ".join(out
.splitlines())
243 search
= re
.search(regexp
, out
)
259 self
.log("found:%s:" % ret
, level
)
260 # Experiment: compare matched strings obtained each way
261 if self
.l_dotall_experiment
and (group_nl_converted
!= ret
):
263 "DOTALL experiment: strings differ dotall=[%s] orig=[%s]"
264 % (group_nl_converted
, ret
),
267 if op
== "pass" or op
== "fail":
268 self
.result(target
, success
, result
)
274 self
, target
, command
, regexp
, op
, result
, wait
, returnJson
, wait_time
=0.5
277 "%s:%s WAIT:%s:%s:%s:%s:%s:%s:%s:"
294 # Calculate the amount of `sleep`s we are going to peform.
295 wait_count
= int(math
.ceil(wait
/ wait_time
)) + 1
297 while wait_count
> 0:
299 found
= self
.command(target
, command
, regexp
, op
, result
, returnJson
)
300 if found
is not False:
305 time
.sleep(wait_time
)
307 delta
= time
.time() - startt
308 self
.log("Done after %d loops, time=%s, Found=%s" % (n
, delta
, found
))
309 found
= self
.command(
314 "%s +%4.2f secs" % (result
, delta
),
320 # initialized by luStart
335 LUtil
.base_script_dir
= baseScriptDir
336 LUtil
.base_log_dir
= baseLogDir
339 LUtil
.fout_name
= baseLogDir
+ "/" + fout
341 LUtil
.fsum_name
= baseLogDir
+ "/" + fsum
343 LUtil
.l_level
= level
344 LUtil
.l_dotall_experiment
= False
345 LUtil
.l_dotall_experiment
= True
359 return LUtil
.command(target
, command
, regexp
, op
, result
, returnJson
)
362 target
, command
, regexp
, op
, result
, time
, returnJson
, wait_time
366 def luLast(usenl
=False):
368 if LUtil
.l_last_nl
!= None:
369 LUtil
.log("luLast:%s:" % LUtil
.l_last_nl
.group(), 7)
370 return LUtil
.l_last_nl
372 if LUtil
.l_last
!= None:
373 LUtil
.log("luLast:%s:" % LUtil
.l_last
.group(), 7)
377 def luInclude(filename
, CallOnFail
=None):
378 tstFile
= LUtil
.base_script_dir
+ "/" + filename
379 LUtil
.setFilename(filename
)
380 if CallOnFail
!= None:
381 oldCallOnFail
= LUtil
.getCallOnFail()
382 LUtil
.setCallOnFail(CallOnFail
)
383 if filename
.endswith(".py"):
384 LUtil
.log("luInclude: execfile " + tstFile
)
385 with
open(tstFile
) as infile
:
388 LUtil
.log("luInclude: execTestFile " + tstFile
)
389 LUtil
.execTestFile(tstFile
)
390 if CallOnFail
!= None:
391 LUtil
.setCallOnFail(oldCallOnFail
)
396 ret
= LUtil
.closeFiles()
410 def luResult(target
, success
, str, logstr
=None):
411 return LUtil
.result(target
, success
, str, logstr
)
414 def luShowResults(prFunction
):
416 sf
= open(LUtil
.fsum_name
, "r")
419 prFunction(line
.rstrip())
425 sf
= open(LUtil
.fsum_name
, "r")
429 logger
.error(line
.rstrip())
432 logger
.error("See %s for details of errors" % LUtil
.fout_name
)
436 if __name__
== "__main__":
437 print(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))) + "/lib")
439 for arg
in sys
.argv
[1:]: