]>
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
)
96 config
= os
.path
.join(self
.testdir
, '{}/isisd.conf'.format(rname
))
97 if os
.path
.exists(config
):
98 router
.load_config(TopoRouter
.RD_ISIS
, config
)
100 # After loading the configurations, this function loads configured daemons.
101 logger
.info('Starting routers')
104 self
.poststarthooksuccess
= customize
.ltemplatePostRouterStartHook()
105 except AttributeError:
107 logger
.debug("ltemplatePostRouterStartHook() not defined")
108 luStart(baseScriptDir
=self
.scriptdir
, baseLogDir
=self
.logdir
, net
=tgen
.net
)
110 #initialized by ltemplate_start
113 def setup_module(mod
):
115 root
= os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
)))
116 test
= mod
.__name
__[:mod
.__name
__.rfind(".")]
117 testdir
= os
.path
.join(root
, test
)
119 #don't do this for now as reload didn't work as expected
120 #fixup sys.path, want test dir there only once
122 # sys.path.remove(testdir)
124 # logger.debug(testdir+" not found in original sys.path")
126 #sys.path.append(testdir)
129 _lt
= LTemplate(test
, testdir
)
130 _lt
.setup_module(mod
)
133 #sys.path.remove(testdir)
135 def teardown_module(mod
):
137 "Teardown the pytest environment"
140 if _lt
!= None and _lt
.scriptdir
!= None:
143 # This function tears down the whole topology.
147 def ltemplateTest(script
, SkipIfFailed
=True, CallOnFail
=None, CheckFuncStr
=None):
149 if not os
.path
.isfile(script
):
150 if not os
.path
.isfile(os
.path
.join(_lt
.scriptdir
, script
)):
151 logger
.error('Could not find script file: ' + script
)
152 assert 'Could not find script file: ' + script
153 logger
.info("Starting template test: " + script
)
154 numEntry
= luNumFail()
156 if SkipIfFailed
and tgen
.routers_have_failure():
157 pytest
.skip(tgen
.errors
)
159 pytest
.skip("Have %d errors" % numEntry
)
161 if CheckFuncStr
!= None:
162 check
= eval(CheckFuncStr
)
164 pytest
.skip("Check function '"+CheckFuncStr
+"' returned: " + check
)
166 if CallOnFail
!= None:
167 CallOnFail
= eval(CallOnFail
)
168 luInclude(script
, CallOnFail
)
169 numFail
= luNumFail() - numEntry
172 fatal_error
= "%d tests failed" % numFail
173 assert "scripts/cleanup_all.py failed" == "See summary output above", fatal_error
175 # Memory leak test template
176 def test_memory_leak():
177 "Run the memory leak test and report results."
179 if not tgen
.is_memleak_enabled():
180 pytest
.skip('Memory leak test/report is disabled')
182 tgen
.report_memory_leaks()
184 class ltemplateRtrCmd():
188 def doCmd(self
, tgen
, rtr
, cmd
, checkstr
= None):
189 output
= tgen
.net
[rtr
].cmd(cmd
).strip()
193 ret
= re
.search(checkstr
, output
)
199 logger
.info('command: {} {}'.format(rtr
, cmd
))
200 logger
.info('output: ' + output
)
204 def resetCounts(self
):
213 def getNoMatch(self
):
222 def ltemplateVersionCheck(vstr
, rname
='r1', compstr
='<',cli
=False, kernel
='4.9', iproute2
=None, mpls
=True):
224 router
= tgen
.gears
[rname
]
227 logger
.info('calling mininet CLI')
229 logger
.info('exited mininet CLI')
232 ret
= 'Template not initialized'
235 if _lt
.prestarthooksuccess
!= True:
236 ret
= 'ltemplatePreRouterStartHook failed'
239 if _lt
.poststarthooksuccess
!= True:
240 ret
= 'ltemplatePostRouterStartHook failed'
243 if mpls
== True and tgen
.hasmpls
!= True:
244 ret
= 'MPLS not initialized'
248 krel
= platform
.release()
249 if topotest
.version_cmp(krel
, kernel
) < 0:
250 ret
= 'Skipping tests, old kernel ({} < {})'.format(krel
, kernel
)
254 if _lt
.iproute2Ver
== None:
255 #collect/log info on iproute2
256 cc
= ltemplateRtrCmd()
257 found
= cc
.doCmd(tgen
, rname
, 'apt-cache policy iproute2', 'Installed: ([\d\.]*)')
259 iproute2Ver
= found
.group(1)
261 iproute2Ver
= '0-unknown'
262 logger
.info('Have iproute2 version=' + iproute2Ver
)
264 if topotest
.version_cmp(iproute2Ver
, iproute2
) < 0:
265 ret
= 'Skipping tests, old iproute2 ({} < {})'.format(iproute2Ver
, iproute2
)
270 if router
.has_version(compstr
, vstr
):
271 ret
= 'Skipping tests, old FRR version {} {}'.format(compstr
, vstr
)
279 if __name__
== '__main__':
280 args
= ["-s"] + sys
.argv
[1:]
281 sys
.exit(pytest
.main(args
))