]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/ltemplate.py
4 # Part of NetDEF Topology Tests
6 # Copyright (c) 2017 by
7 # Network Device Education Foundation, Inc. ("NetDEF")
9 # Permission to use, copy, modify, and/or distribute this software
10 # for any purpose with or without fee is hereby granted, provided
11 # that the above copyright notice and this permission notice appear
14 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
15 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
17 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
19 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
20 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
25 ltemplate.py: LabN template for FRR tests.
34 # pylint: disable=C0413
35 # Import topogen and topotest helpers
36 from lib
import topotest
37 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
38 from lib
.topolog
import logger
39 from lib
.lutil
import *
41 # Required to instantiate the topology builder class.
42 from mininet
.topo
import Topo
51 prestarthooksuccess
= True
52 poststarthooksuccess
= True
55 def __init__(self
, test
, testdir
):
57 customize
= imp
.load_source('customize', os
.path
.join(testdir
, 'customize.py'))
59 self
.testdir
= testdir
60 self
.scriptdir
= testdir
61 self
.logdir
= '/tmp/topotests/{0}.test_{0}'.format(test
)
62 logger
.info('LTemplate: '+test
)
64 def setup_module(self
, mod
):
65 "Sets up the pytest environment"
66 # This function initiates the topology build with Topogen...
67 tgen
= Topogen(customize
.ThisTestTopo
, mod
.__name
__)
68 # ... and here it calls Mininet initialization functions.
71 logger
.info('Topology started')
73 self
.prestarthooksuccess
= customize
.ltemplatePreRouterStartHook()
74 except AttributeError:
76 logger
.debug("ltemplatePreRouterStartHook() not defined")
78 # This is a sample of configuration loading.
79 router_list
= tgen
.routers()
81 # For all registred routers, load the zebra configuration file
82 for rname
, router
in router_list
.iteritems():
83 print("Setting up %s" % rname
)
84 config
= os
.path
.join(self
.testdir
, '{}/zebra.conf'.format(rname
))
85 if os
.path
.exists(config
):
86 router
.load_config(TopoRouter
.RD_ZEBRA
, config
)
87 config
= os
.path
.join(self
.testdir
, '{}/ospfd.conf'.format(rname
))
88 if os
.path
.exists(config
):
89 router
.load_config(TopoRouter
.RD_OSPF
, config
)
90 config
= os
.path
.join(self
.testdir
, '{}/ldpd.conf'.format(rname
))
91 if os
.path
.exists(config
):
92 router
.load_config(TopoRouter
.RD_LDP
, config
)
93 config
= os
.path
.join(self
.testdir
, '{}/bgpd.conf'.format(rname
))
94 if os
.path
.exists(config
):
95 router
.load_config(TopoRouter
.RD_BGP
, config
)
97 # After loading the configurations, this function loads configured daemons.
98 logger
.info('Starting routers')
101 self
.poststarthooksuccess
= customize
.ltemplatePostRouterStartHook()
102 except AttributeError:
104 logger
.debug("ltemplatePostRouterStartHook() not defined")
105 luStart(baseScriptDir
=self
.scriptdir
, baseLogDir
=self
.logdir
, net
=tgen
.net
)
107 #initialized by ltemplate_start
110 def setup_module(mod
):
112 root
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
113 test
= mod
.__name
__[:mod
.__name
__.rfind(".")]
114 testdir
= os
.path
.join(root
, test
)
116 #don't do this for now as reload didn't work as expected
117 #fixup sys.path, want test dir there only once
119 # sys.path.remove(testdir)
121 # logger.debug(testdir+" not found in original sys.path")
123 #sys.path.append(testdir)
126 _lt
= LTemplate(test
, testdir
)
127 _lt
.setup_module(mod
)
130 #sys.path.remove(testdir)
132 def teardown_module(mod
):
134 "Teardown the pytest environment"
137 if _lt
!= None and _lt
.scriptdir
!= None:
140 # This function tears down the whole topology.
144 def ltemplateTest(script
, SkipIfFailed
=True, CallOnFail
=None, CheckFuncStr
=None):
146 if not os
.path
.isfile(script
):
147 if not os
.path
.isfile(os
.path
.join(_lt
.scriptdir
, script
)):
148 logger
.error('Could not find script file: ' + script
)
149 assert 'Could not find script file: ' + script
150 logger
.info("Starting template test: " + script
)
151 numEntry
= luNumFail()
153 if SkipIfFailed
and tgen
.routers_have_failure():
154 pytest
.skip(tgen
.errors
)
156 pytest
.skip("Have %d errors" % numEntry
)
158 if CheckFuncStr
!= None:
159 check
= eval(CheckFuncStr
)
161 pytest
.skip("Check function '"+CheckFuncStr
+"' returned: " + check
)
163 if CallOnFail
!= None:
164 CallOnFail
= eval(CallOnFail
)
165 luInclude(script
, CallOnFail
)
166 numFail
= luNumFail() - numEntry
169 fatal_error
= "%d tests failed" % numFail
170 assert "scripts/cleanup_all.py failed" == "See summary output above", fatal_error
172 # Memory leak test template
173 def test_memory_leak():
174 "Run the memory leak test and report results."
176 if not tgen
.is_memleak_enabled():
177 pytest
.skip('Memory leak test/report is disabled')
179 tgen
.report_memory_leaks()
181 class ltemplateRtrCmd():
185 def doCmd(self
, tgen
, rtr
, cmd
, checkstr
= None):
186 output
= tgen
.net
[rtr
].cmd(cmd
).strip()
190 ret
= re
.search(checkstr
, output
)
196 logger
.info('command: {} {}'.format(rtr
, cmd
))
197 logger
.info('output: ' + output
)
201 def resetCounts(self
):
210 def getNoMatch(self
):
219 def ltemplateVersionCheck(vstr
, rname
='r1', compstr
='<',cli
=False, kernel
='4.9', iproute2
=None, mpls
=True):
221 router
= tgen
.gears
[rname
]
224 logger
.info('calling mininet CLI')
226 logger
.info('exited mininet CLI')
229 ret
= 'Template not initialized'
232 if _lt
.prestarthooksuccess
!= True:
233 ret
= 'ltemplatePreRouterStartHook failed'
236 if _lt
.poststarthooksuccess
!= True:
237 ret
= 'ltemplatePostRouterStartHook failed'
240 if mpls
== True and tgen
.hasmpls
!= True:
241 ret
= 'MPLS not initialized'
245 krel
= platform
.release()
246 if topotest
.version_cmp(krel
, kernel
) < 0:
247 ret
= 'Skipping tests, old kernel ({} < {})'.format(krel
, kernel
)
251 if _lt
.iproute2Ver
== None:
252 #collect/log info on iproute2
253 cc
= ltemplateRtrCmd()
254 found
= cc
.doCmd(tgen
, rname
, 'apt-cache policy iproute2', 'Installed: ([\d\.]*)')
256 iproute2Ver
= found
.group(1)
258 iproute2Ver
= '0-unknown'
259 logger
.info('Have iproute2 version=' + iproute2Ver
)
261 if topotest
.version_cmp(iproute2Ver
, iproute2
) < 0:
262 ret
= 'Skipping tests, old iproute2 ({} < {})'.format(iproute2Ver
, iproute2
)
267 if router
.has_version(compstr
, vstr
):
268 ret
= 'Skipping tests, old FRR version {} {}'.format(compstr
, vstr
)
276 if __name__
== '__main__':
277 args
= ["-s"] + sys
.argv
[1:]
278 sys
.exit(pytest
.main(args
))