]>
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
24 from topolog
import logger
25 from mininet
.net
import Mininet
30 # These functions are inteneted to provide support for CI testing within MiniNet
34 #to be made configurable in the future
37 fout_name
= 'output.log'
38 fsum_name
= 'summary.txt'
56 self
.fout
= open(self
.fout_name
, 'w', 0)
57 self
.fout
.write(str+'\n')
61 def result(self
, target
, success
, str):
70 res
= "%-4d %-6s %-56s %-4d %d" % (self
.l_total
, target
, str, p
, f
)
73 self
.fsum
= open(self
.fsum_name
, 'w', 0)
75 ******************************************************************************\n')
77 Test Target Summary Pass Fail\n')
79 ******************************************************************************\n')
80 self
.fsum
.write(res
+'\n')
81 if f
== 1 and self
.CallOnFail
!= False:
86 ******************************************************************************\n\
88 ******************************************************************************'\
89 % (self
.l_total
, self
.l_pass
, self
.l_fail
)
91 self
.fsum
.write(ret
+ '\n')
95 if os
.path
.isfile(self
.fsum_name
):
96 r
= open(self
.fsum_name
, 'r')
97 self
.fout
.write(r
.read())
103 def setFilename(self
, name
):
104 self
.log('FILE: ' + name
)
105 self
.l_filename
= name
108 def getCallOnFail(self
):
109 return self
.CallOnFail
111 def setCallOnFail(self
, CallOnFail
):
112 self
.CallOnFail
= CallOnFail
114 def strToArray(self
, string
):
118 words
= string
.split()
119 if len(words
) < 1 or words
[0].startswith('#'):
121 words
= string
.split()
126 a
[c
] += str(' '+word
)
129 if not word
.endswith('\\'):
131 if word
.startswith('"'):
136 if word
.endswith('"'):
144 # print('%d:%s:' % (c, a[c-1]))
148 def execTestFile(self
, tstFile
):
149 if os
.path
.isfile(tstFile
):
153 a
= self
.strToArray(line
)
155 luCommand(a
[1], a
[2], a
[3], a
[4], a
[5])
158 self
.log('%s:%s %s' % (self
.l_filename
, self
.l_line
, line
))
161 time
.sleep(int(a
[1]))
162 elif a
[0] == 'include':
163 self
.execTestFile(a
[1])
166 self
.log('unable to read: ' + tstFile
)
169 def command(self
, target
, command
, regexp
, op
, result
):
174 self
.log('%s:%s COMMAND:%s:%s:%s:%s:%s:' % \
175 (self
.l_filename
, self
.l_line
, target
, command
, regexp
, op
, result
))
178 #self.log("Running %s %s" % (target, command))
179 out
= self
.net
[target
].cmd(command
).rstrip()
181 report
= "<no output>"
184 self
.log('COMMAND OUTPUT:%s:' % report
)
185 out
= " ".join(out
.splitlines())
186 search
= re
.search(regexp
, out
)
196 self
.log('found:%s:' % ret
)
201 if op
== 'pass' or op
== 'fail':
202 self
.result(target
, success
, result
)
205 def wait(self
, target
, command
, regexp
, op
, result
, wait
):
206 self
.log('%s:%s WAIT:%s:%s:%s:%s:%s:%s:' % \
207 (self
.l_filename
, self
.l_line
, target
, command
, regexp
, op
, result
,wait
))
208 llevel
= LUtil
.l_level
212 delta
= time
.time() - startt
213 while delta
< wait
and found
is False:
214 found
= self
.command(target
, command
, regexp
, op
, result
)
217 delta
= time
.time() - startt
218 if delta
< wait
and found
is False:
220 LUtil
.l_level
= llevel
221 self
.log('Done after %d loops, time=%s, Found=%s' % (n
, delta
, found
))
222 found
= self
.command(target
, command
, regexp
, 'pass', '%s +%4.2f secs' % (result
, delta
))
225 #initialized by luStart
229 def luStart(baseScriptDir
='.', baseLogDir
='.', net
='',
230 fout
='output.log', fsum
='summary.txt', level
=9):
234 LUtil
.base_script_dir
= baseScriptDir
235 LUtil
.base_log_dir
= baseLogDir
238 LUtil
.fout_name
= baseLogDir
+ '/' + fout
240 LUtil
.fsum_name
= baseLogDir
+ '/' + fsum
241 LUtil
.l_level
= level
243 def luCommand(target
, command
, regexp
='.', op
='none', result
='', time
=10):
245 return LUtil
.command(target
, command
, regexp
, op
, result
)
247 return LUtil
.wait(target
, command
, regexp
, op
, result
, time
)
250 if LUtil
.l_last
!= None:
251 LUtil
.log('luLast:%s:' % LUtil
.l_last
.group())
254 def luInclude(filename
, CallOnFail
=None):
255 tstFile
= LUtil
.base_script_dir
+ '/' + filename
256 LUtil
.setFilename(filename
)
257 if CallOnFail
!= None:
258 oldCallOnFail
= LUtil
.getCallOnFail()
259 LUtil
.setCallOnFail(CallOnFail
)
260 if filename
.endswith('.py'):
261 LUtil
.log("luInclude: execfile "+tstFile
)
264 LUtil
.log("luInclude: execTestFile "+tstFile
)
265 LUtil
.execTestFile(tstFile
)
266 if CallOnFail
!= None:
267 LUtil
.setCallOnFail(oldCallOnFail
)
271 ret
= LUtil
.closeFiles()
284 sf
= open(LUtil
.fsum_name
, 'r')
288 logger
.error(line
.rstrip())
291 logger
.error("See %s for details of errors" % LUtil
.fout_name
)
294 if __name__
== '__main__':
295 print(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))) + '/lib')
297 for arg
in sys
.argv
[1:]: