]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/lib/topogen.py
5c8d1ddf36e53a45761474047b28538b17774736
3 # Library of helper functions for NetDEF Topology Tests
5 # Copyright (c) 2017 by
6 # Network Device Education Foundation, Inc. ("NetDEF")
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
13 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 Topogen (Topology Generator) is an abstraction around Topotest and Mininet to
25 help reduce boilerplate code and provide a stable interface to build topology
28 Basic usage instructions:
30 * Define a Topology class with a build method using mininet.topo.Topo.
31 See examples/test_template.py.
32 * Use Topogen inside the build() method with get_topogen.
33 e.g. get_topogen(self).
34 * Start up your topology with: Topogen(YourTopology)
35 * Initialize the Mininet with your topology with: tgen.start_topology()
36 * Configure your routers/hosts and start them
37 * Run your tests / mininet cli.
38 * After running stop Mininet with: tgen.stop_topology()
53 from mininet
.net
import Mininet
54 from mininet
.log
import setLogLevel
55 from mininet
.cli
import CLI
57 from lib
import topotest
58 from lib
.topolog
import logger
, logger_config
60 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
62 # pylint: disable=C0103
63 # Global Topogen variable. This is being used to keep the Topogen available on
64 # all test functions without declaring a test local variable.
67 def get_topogen(topo
=None):
69 Helper function to retrieve Topogen. Must be called with `topo` when called
70 inside the build() method of Topology class.
73 global_tgen
.topo
= topo
76 def set_topogen(tgen
):
77 "Helper function to set Topogen"
78 # pylint: disable=W0603
83 # Main class: topology builder
86 # Topogen configuration defaults
89 'frrdir': '/usr/lib/frr',
90 'quaggadir': '/usr/lib/quagga',
95 class Topogen(object):
96 "A topology test builder helper."
98 CONFIG_SECTION
= 'topogen'
100 def __init__(self
, cls
, modname
='unnamed'):
102 Topogen initialization function, takes the following arguments:
103 * `cls`: the topology class that is child of mininet.topo
104 * `modname`: module name must be a unique name to identify logs later.
112 self
.modname
= modname
117 logger
.info('loading topology: {}'.format(self
.modname
))
120 def _mininet_reset():
121 "Reset the mininet environment"
122 # Clean up the mininet environment
123 os
.system('mn -c > /dev/null 2>&1')
125 def _init_topo(self
, cls
):
127 Initialize the topogily provided by the user. The user topology class
128 must call get_topogen() during build() to get the topogen object.
130 # Set the global variable so the test cases can access it anywhere
133 # Test for MPLS Kernel modules available
135 if os
.system('/sbin/modprobe mpls-router') != 0:
136 logger
.info('MPLS tests will not run (missing mpls-router kernel module)')
137 elif os
.system('/sbin/modprobe mpls-iptunnel') != 0:
138 logger
.info('MPLS tests will not run (missing mpls-iptunnel kernel module)')
141 # Load the default topology configurations
145 self
._mininet
_reset
()
147 self
.net
= Mininet(controller
=None, topo
=self
.topo
)
148 for gear
in self
.gears
.values():
151 def _load_config(self
):
153 Loads the configuration file `pytest.ini` located at the root dir of
156 self
.config
= ConfigParser
.ConfigParser(tgen_defaults
)
157 pytestini_path
= os
.path
.join(CWD
, '../pytest.ini')
158 self
.config
.read(pytestini_path
)
160 def add_router(self
, name
=None, cls
=topotest
.Router
, **params
):
162 Adds a new router to the topology. This function has the following
164 * `name`: (optional) select the router name
165 * `daemondir`: (optional) custom daemon binary directory
166 * `routertype`: (optional) `quagga` or `frr`
167 Returns a TopoRouter.
170 name
= 'r{}'.format(self
.routern
)
171 if name
in self
.gears
:
172 raise KeyError('router already exists')
174 params
['frrdir'] = self
.config
.get(self
.CONFIG_SECTION
, 'frrdir')
175 params
['quaggadir'] = self
.config
.get(self
.CONFIG_SECTION
, 'quaggadir')
176 params
['memleak_path'] = self
.config
.get(self
.CONFIG_SECTION
, 'memleak_path')
177 if not params
.has_key('routertype'):
178 params
['routertype'] = self
.config
.get(self
.CONFIG_SECTION
, 'routertype')
180 self
.gears
[name
] = TopoRouter(self
, cls
, name
, **params
)
182 return self
.gears
[name
]
184 def add_switch(self
, name
=None, cls
=topotest
.LegacySwitch
):
186 Adds a new switch to the topology. This function has the following
188 name: (optional) select the switch name
189 Returns the switch name and number.
192 name
= 's{}'.format(self
.switchn
)
193 if name
in self
.gears
:
194 raise KeyError('switch already exists')
196 self
.gears
[name
] = TopoSwitch(self
, cls
, name
)
198 return self
.gears
[name
]
200 def add_exabgp_peer(self
, name
, ip
, defaultRoute
):
202 Adds a new ExaBGP peer to the topology. This function has the following
204 * `ip`: the peer address (e.g. '1.2.3.4/24')
205 * `defaultRoute`: the peer default route (e.g. 'via 1.2.3.1')
208 name
= 'peer{}'.format(self
.peern
)
209 if name
in self
.gears
:
210 raise KeyError('exabgp peer already exists')
212 self
.gears
[name
] = TopoExaBGP(self
, name
, ip
=ip
, defaultRoute
=defaultRoute
)
214 return self
.gears
[name
]
216 def add_link(self
, node1
, node2
, ifname1
=None, ifname2
=None):
218 Creates a connection between node1 and node2. The nodes can be the
224 if not isinstance(node1
, TopoGear
):
225 raise ValueError('invalid node1 type')
226 if not isinstance(node2
, TopoGear
):
227 raise ValueError('invalid node2 type')
230 ifname1
= node1
.new_link()
232 ifname2
= node2
.new_link()
234 node1
.register_link(ifname1
, node2
, ifname2
)
235 node2
.register_link(ifname2
, node1
, ifname1
)
236 self
.topo
.addLink(node1
.name
, node2
.name
,
237 intfName1
=ifname1
, intfName2
=ifname2
)
239 def get_gears(self
, geartype
):
241 Returns a dictionary of all gears of type `geartype`.
244 * Dictionary iteration:
247 router_dict = tgen.get_gears(TopoRouter)
248 for router_name, router in router_dict.iteritems():
254 peer_list = tgen.get_gears(TopoExaBGP).values()
255 for peer in peer_list:
259 return dict((name
, gear
) for name
, gear
in self
.gears
.iteritems()
260 if isinstance(gear
, geartype
))
264 Returns the router dictionary (key is the router name and value is the
265 router object itself).
267 return self
.get_gears(TopoRouter
)
269 def exabgp_peers(self
):
271 Returns the exabgp peer dictionary (key is the peer name and value is
272 the peer object itself).
274 return self
.get_gears(TopoExaBGP
)
276 def start_topology(self
, log_level
=None):
278 Starts the topology class. Possible `log_level`s are:
279 'debug': all information possible
280 'info': informational messages
281 'output': default logging level defined by Mininet
282 'warning': only warning, error and critical messages
283 'error': only error and critical messages
284 'critical': only critical messages
286 # If log_level is not specified use the configuration.
287 if log_level
is None:
288 log_level
= self
.config
.get(self
.CONFIG_SECTION
, 'verbosity')
290 # Set python logger level
291 logger_config
.set_log_level(log_level
)
294 if log_level
== 'debug':
295 setLogLevel(log_level
)
297 logger
.info('starting topology: {}'.format(self
.modname
))
300 def start_router(self
, router
=None):
302 Call the router startRouter method.
303 If no router is specified it is called for all registred routers.
306 # pylint: disable=r1704
307 for _
, router
in self
.routers().iteritems():
310 if isinstance(router
, str):
311 router
= self
.gears
[router
]
315 def stop_topology(self
):
317 Stops the network topology. This function will call the stop() function
318 of all gears before calling the mininet stop function, so they can have
319 their oportunity to do a graceful shutdown. stop() is called twice. The
320 first is a simple kill with no sleep, the second will sleep if not
321 killed and try with a different signal.
323 logger
.info('stopping topology: {}'.format(self
.modname
))
325 for gear
in self
.gears
.values():
327 for gear
in self
.gears
.values():
332 def mininet_cli(self
):
334 Interrupt the test and call the command line interface for manual
335 inspection. Should be only used on non production code.
337 if not sys
.stdin
.isatty():
338 raise EnvironmentError(
339 'you must run pytest with \'-s\' in order to use mininet CLI')
343 def is_memleak_enabled(self
):
344 "Returns `True` if memory leak report is enable, otherwise `False`."
345 # On router failure we can't run the memory leak test
346 if self
.routers_have_failure():
349 memleak_file
= (os
.environ
.get('TOPOTESTS_CHECK_MEMLEAK') or
350 self
.config
.get(self
.CONFIG_SECTION
, 'memleak_path'))
351 if memleak_file
is None:
355 def report_memory_leaks(self
, testname
=None):
356 "Run memory leak test and reports."
357 if not self
.is_memleak_enabled():
360 # If no name was specified, use the test module name
362 testname
= self
.modname
364 router_list
= self
.routers().values()
365 for router
in router_list
:
366 router
.report_memory_leaks(self
.modname
)
368 def set_error(self
, message
, code
=None):
369 "Sets an error message and signal other tests to skip."
372 # If no code is defined use a sequential number
374 code
= len(self
.errorsd
)
376 self
.errorsd
[code
] = message
377 self
.errors
+= '\n{}: {}'.format(code
, message
)
379 def has_errors(self
):
380 "Returns whether errors exist or not."
381 return len(self
.errorsd
) > 0
383 def routers_have_failure(self
):
384 "Runs an assertion to make sure that all routers are running."
385 if self
.has_errors():
389 router_list
= self
.routers().values()
390 for router
in router_list
:
391 result
= router
.check_router_running()
393 errors
+= result
+ '\n'
396 self
.set_error(errors
, 'router_error')
402 # Topology gears (equipment)
405 class TopoGear(object):
406 "Abstract class for type checking"
417 for myif
, dest
in self
.links
.iteritems():
421 links
+= '"{}"<->"{}"'.format(myif
, destif
)
423 return 'TopoGear<name="{}",links=[{}]>'.format(self
.name
, links
)
426 "Basic start function that just reports equipment start"
427 logger
.info('starting "{}"'.format(self
.name
))
429 def stop(self
, wait
=True):
430 "Basic start function that just reports equipment stop"
431 logger
.info('stopping "{}"'.format(self
.name
))
433 def run(self
, command
):
435 Runs the provided command string in the router and returns a string
438 return self
.tgen
.net
[self
.name
].cmd(command
)
440 def add_link(self
, node
, myif
=None, nodeif
=None):
442 Creates a link (connection) between myself and the specified node.
443 Interfaces name can be speficied with:
444 myif: the interface name that will be created in this node
445 nodeif: the target interface name that will be created on the remote node.
447 self
.tgen
.add_link(self
, node
, myif
, nodeif
)
449 def link_enable(self
, myif
, enabled
=True):
451 Set this node interface administrative state.
452 myif: this node interface name
453 enabled: whether we should enable or disable the interface
455 if myif
not in self
.links
.keys():
456 raise KeyError('interface doesn\'t exists')
463 logger
.info('setting node "{}" link "{}" to state "{}"'.format(
464 self
.name
, myif
, operation
466 return self
.run('ip link set dev {} {}'.format(myif
, operation
))
468 def peer_link_enable(self
, myif
, enabled
=True):
470 Set the peer interface administrative state.
471 myif: this node interface name
472 enabled: whether we should enable or disable the interface
474 NOTE: this is used to simulate a link down on this node, since when the
475 peer disables their interface our interface status changes to no link.
477 if myif
not in self
.links
.keys():
478 raise KeyError('interface doesn\'t exists')
480 node
, nodeif
= self
.links
[myif
]
481 node
.link_enable(nodeif
, enabled
)
485 Generates a new unique link name.
487 NOTE: This function should only be called by Topogen.
489 ifname
= '{}-eth{}'.format(self
.name
, self
.linkn
)
493 def register_link(self
, myif
, node
, nodeif
):
495 Register link between this node interface and outside node.
497 NOTE: This function should only be called by Topogen.
499 if myif
in self
.links
.keys():
500 raise KeyError('interface already exists')
502 self
.links
[myif
] = (node
, nodeif
)
504 class TopoRouter(TopoGear
):
509 # The default required directories by Quagga/FRR
518 # Router Daemon enumeration definition.
544 def __init__(self
, tgen
, cls
, name
, **params
):
546 The constructor has the following parameters:
547 * tgen: Topogen object
548 * cls: router class that will be used to instantiate
550 * daemondir: daemon binary directory
551 * routertype: 'quagga' or 'frr'
553 super(TopoRouter
, self
).__init
__()
559 self
.routertype
= params
.get('routertype', 'frr')
560 if not params
.has_key('privateDirs'):
561 params
['privateDirs'] = self
.PRIVATE_DIRS
563 self
.options
['memleak_path'] = params
.get('memleak_path', None)
565 # Create new log directory
566 self
.logdir
= '/tmp/topotests/{}'.format(self
.tgen
.modname
)
567 # Clean up before starting new log files: avoids removing just created
569 self
._prepare
_tmpfiles
()
570 # Propagate the router log directory
571 params
['logdir'] = self
.logdir
573 #setup the per node directory
574 dir = '{}/{}'.format(self
.logdir
, self
.name
)
575 os
.system('mkdir -p ' + dir)
576 os
.system('chmod 775 ' + dir)
577 os
.system('chgrp {} {}'.format(self
.routertype
, dir))
579 # Open router log file
580 logfile
= '{0}/{1}.log'.format(dir, name
)
582 self
.logger
= logger_config
.get_logger(name
=name
, target
=logfile
)
583 self
.tgen
.topo
.addNode(self
.name
, cls
=self
.cls
, **params
)
586 gear
= super(TopoRouter
, self
).__str
__()
587 gear
+= ' TopoRouter<>'
590 def _prepare_tmpfiles(self
):
591 # Create directories if they don't exist
593 os
.makedirs(self
.logdir
, 0755)
597 # Allow unprivileged daemon user (frr/quagga) to create log files
599 # Only allow group, if it exist.
600 gid
= grp
.getgrnam(self
.routertype
)[2]
601 os
.chown(self
.logdir
, 0, gid
)
602 os
.chmod(self
.logdir
, 0775)
604 # Allow anyone, but set the sticky bit to avoid file deletions
605 os
.chmod(self
.logdir
, 01777)
607 # Try to find relevant old logfiles in /tmp and delete them
608 map(os
.remove
, glob
.glob('{}/{}/*.log'.format(self
.logdir
, self
.name
)))
609 # Remove old core files
610 map(os
.remove
, glob
.glob('{}/{}/*.dmp'.format(self
.logdir
, self
.name
)))
612 def check_capability(self
, daemon
, param
):
614 Checks a capability daemon against an argument option
615 Return True if capability available. False otherwise
617 daemonstr
= self
.RD
.get(daemon
)
618 self
.logger
.info('check capability {} for "{}"'.format(param
, daemonstr
))
619 return self
.tgen
.net
[self
.name
].checkCapability(daemonstr
, param
)
621 def load_config(self
, daemon
, source
=None, param
=None):
623 Loads daemon configuration from the specified source
624 Possible daemon values are: TopoRouter.RD_ZEBRA, TopoRouter.RD_RIP,
625 TopoRouter.RD_RIPNG, TopoRouter.RD_OSPF, TopoRouter.RD_OSPF6,
626 TopoRouter.RD_ISIS, TopoRouter.RD_BGP, TopoRouter.RD_LDP,
629 daemonstr
= self
.RD
.get(daemon
)
630 self
.logger
.info('loading "{}" configuration: {}'.format(daemonstr
, source
))
631 self
.tgen
.net
[self
.name
].loadConf(daemonstr
, source
, param
)
633 def check_router_running(self
):
635 Run a series of checks and returns a status string.
637 self
.logger
.info('checking if daemons are running')
638 return self
.tgen
.net
[self
.name
].checkRouterRunning()
645 * Configure interfaces
646 * Start daemons (e.g. FRR/Quagga)
647 * Configure daemon logging files
649 self
.logger
.debug('starting')
650 nrouter
= self
.tgen
.net
[self
.name
]
651 result
= nrouter
.startRouter(self
.tgen
)
653 # Enable all daemon command logging, logging files
654 # and set them to the start dir.
655 for daemon
, enabled
in nrouter
.daemons
.iteritems():
658 self
.vtysh_cmd('configure terminal\nlog commands\nlog file {}.log'.format(
659 daemon
), daemon
=daemon
)
662 self
.tgen
.set_error(result
)
666 def stop(self
, wait
=True):
671 self
.logger
.debug('stopping')
672 return self
.tgen
.net
[self
.name
].stopRouter(wait
)
674 def vtysh_cmd(self
, command
, isjson
=False, daemon
=None):
676 Runs the provided command string in the vty shell and returns a string
679 This function also accepts multiple commands, but this mode does not
680 return output for each command. See vtysh_multicmd() for more details.
682 # Detect multi line commands
683 if command
.find('\n') != -1:
684 return self
.vtysh_multicmd(command
, daemon
=daemon
)
687 if daemon
is not None:
688 dparam
+= '-d {}'.format(daemon
)
690 vtysh_command
= 'vtysh {} -c "{}" 2>/dev/null'.format(dparam
, command
)
692 output
= self
.run(vtysh_command
)
693 self
.logger
.info('\nvtysh command => {}\nvtysh output <= {}'.format(
699 return json
.loads(output
)
701 logger
.warning('vtysh_cmd: failed to convert json output')
704 def vtysh_multicmd(self
, commands
, pretty_output
=True, daemon
=None):
706 Runs the provided commands in the vty shell and return the result of
709 pretty_output: defines how the return value will be presented. When
710 True it will show the command as they were executed in the vty shell,
711 otherwise it will only show lines that failed.
713 # Prepare the temporary file that will hold the commands
714 fname
= topotest
.get_file(commands
)
717 if daemon
is not None:
718 dparam
+= '-d {}'.format(daemon
)
720 # Run the commands and delete the temporary file
722 vtysh_command
= 'vtysh {} < {}'.format(dparam
, fname
)
724 vtysh_command
= 'vtysh {} -f {}'.format(dparam
, fname
)
726 res
= self
.run(vtysh_command
)
729 self
.logger
.info('\nvtysh command => "{}"\nvtysh output <= "{}"'.format(
734 def report_memory_leaks(self
, testname
):
736 Runs the router memory leak check test. Has the following parameter:
737 testname: the test file name for identification
739 NOTE: to run this you must have the environment variable
740 TOPOTESTS_CHECK_MEMLEAK set or memleak_path configured in `pytest.ini`.
742 memleak_file
= os
.environ
.get('TOPOTESTS_CHECK_MEMLEAK') or self
.options
['memleak_path']
743 if memleak_file
is None:
747 self
.logger
.info('running memory leak report')
748 self
.tgen
.net
[self
.name
].report_memory_leaks(memleak_file
, testname
)
750 def version_info(self
):
751 "Get equipment information from 'show version'."
752 output
= self
.vtysh_cmd('show version').split('\n')[0]
753 columns
= topotest
.normalize_text(output
).split(' ')
757 'version': columns
[1],
765 def has_version(self
, cmpop
, version
):
767 Compares router version using operation `cmpop` with `version`.
768 Valid `cmpop` values:
769 * `>=`: has the same version or greater
770 * '>': has greater version
771 * '=': has the same version
772 * '<': has a lesser version
773 * '<=': has the same version or lesser
775 Usage example: router.has_version('>', '1.0')
777 rversion
= self
.version_info()['version']
781 result
= topotest
.version_cmp(rversion
, version
)
795 def has_type(self
, rtype
):
797 Compares router type with `rtype`. Returns `True` if the type matches,
800 curtype
= self
.version_info()['type']
801 return rtype
== curtype
804 nrouter
= self
.tgen
.net
[self
.name
]
805 return nrouter
.hasmpls
807 class TopoSwitch(TopoGear
):
809 Switch abstraction. Has the following properties:
810 * cls: switch class that will be used to instantiate
813 # pylint: disable=too-few-public-methods
815 def __init__(self
, tgen
, cls
, name
):
816 super(TopoSwitch
, self
).__init
__()
821 self
.tgen
.topo
.addSwitch(name
, cls
=self
.cls
)
824 gear
= super(TopoSwitch
, self
).__str
__()
825 gear
+= ' TopoSwitch<>'
828 class TopoHost(TopoGear
):
830 # pylint: disable=too-few-public-methods
832 def __init__(self
, tgen
, name
, **params
):
834 Mininet has the following known `params` for hosts:
835 * `ip`: the IP address (string) for the host interface
836 * `defaultRoute`: the default route that will be installed
837 (e.g. 'via 10.0.0.1')
838 * `privateDirs`: directories that will be mounted on a different domain
839 (e.g. '/etc/important_dir').
841 super(TopoHost
, self
).__init
__()
845 self
.options
= params
846 self
.tgen
.topo
.addHost(name
, **params
)
849 gear
= super(TopoHost
, self
).__str
__()
850 gear
+= ' TopoHost<ip="{}",defaultRoute="{}",privateDirs="{}">'.format(
851 self
.options
['ip'], self
.options
['defaultRoute'],
852 str(self
.options
['privateDirs']))
855 class TopoExaBGP(TopoHost
):
856 "ExaBGP peer abstraction."
857 # pylint: disable=too-few-public-methods
865 def __init__(self
, tgen
, name
, **params
):
867 ExaBGP usually uses the following parameters:
868 * `ip`: the IP address (string) for the host interface
869 * `defaultRoute`: the default route that will be installed
870 (e.g. 'via 10.0.0.1')
872 Note: the different between a host and a ExaBGP peer is that this class
873 has a privateDirs already defined and contains functions to handle ExaBGP
876 params
['privateDirs'] = self
.PRIVATE_DIRS
877 super(TopoExaBGP
, self
).__init
__(tgen
, name
, **params
)
878 self
.tgen
.topo
.addHost(name
, **params
)
881 gear
= super(TopoExaBGP
, self
).__str
__()
882 gear
+= ' TopoExaBGP<>'.format()
885 def start(self
, peer_dir
, env_file
=None):
887 Start running ExaBGP daemon:
888 * Copy all peer* folder contents into /etc/exabgp
889 * Copy exabgp env file if specified
890 * Make all python files runnable
891 * Run ExaBGP with env file `env_file` and configuration peer*/exabgp.cfg
893 self
.run('mkdir /etc/exabgp')
894 self
.run('chmod 755 /etc/exabgp')
895 self
.run('cp {}/* /etc/exabgp/'.format(peer_dir
))
896 if env_file
is not None:
897 self
.run('cp {} /etc/exabgp/exabgp.env'.format(env_file
))
898 self
.run('chmod 644 /etc/exabgp/*')
899 self
.run('chmod a+x /etc/exabgp/*.py')
900 self
.run('chown -R exabgp:exabgp /etc/exabgp')
901 output
= self
.run('exabgp -e /etc/exabgp/exabgp.env /etc/exabgp/exabgp.cfg')
902 if output
== None or len(output
) == 0:
904 logger
.info('{} exabgp started, output={}'.format(self
.name
, output
))
906 def stop(self
, wait
=True):
907 "Stop ExaBGP peer and kill the daemon"
908 self
.run('kill `cat /var/run/exabgp/exabgp.pid`')
912 # Diagnostic function
915 # Disable linter branch warning. It is expected to have these here.
916 # pylint: disable=R0912
919 Run diagnostics in the running environment. Returns `True` when everything
920 is ok, otherwise `False`.
924 # Test log path exists before installing handler.
925 if not os
.path
.isdir('/tmp'):
926 logger
.warning('could not find /tmp for logs')
928 os
.system('mkdir /tmp/topotests')
929 # Log diagnostics to file so it can be examined later.
930 fhandler
= logging
.FileHandler(filename
='/tmp/topotests/diagnostics.txt')
931 fhandler
.setLevel(logging
.DEBUG
)
932 fhandler
.setFormatter(
933 logging
.Formatter(fmt
='%(asctime)s %(levelname)s: %(message)s')
935 logger
.addHandler(fhandler
)
937 logger
.info('Running environment diagnostics')
940 config
= ConfigParser
.ConfigParser(tgen_defaults
)
941 pytestini_path
= os
.path
.join(CWD
, '../pytest.ini')
942 config
.read(pytestini_path
)
944 # Assert that we are running as root
946 logger
.error('you must run topotest as root')
949 # Assert that we have mininet
950 if os
.system('which mn >/dev/null 2>/dev/null') != 0:
951 logger
.error('could not find mininet binary (mininet is not installed)')
954 # Assert that we have iproute installed
955 if os
.system('which ip >/dev/null 2>/dev/null') != 0:
956 logger
.error('could not find ip binary (iproute is not installed)')
959 # Assert that we have gdb installed
960 if os
.system('which gdb >/dev/null 2>/dev/null') != 0:
961 logger
.error('could not find gdb binary (gdb is not installed)')
964 # Assert that FRR utilities exist
965 frrdir
= config
.get('topogen', 'frrdir')
967 if not os
.path
.isdir(frrdir
):
968 logger
.error('could not find {} directory'.format(frrdir
))
973 pwd
.getpwnam('frr')[2]
975 logger
.warning('could not find "frr" user')
978 grp
.getgrnam('frr')[2]
980 logger
.warning('could not find "frr" group')
983 if 'frr' not in grp
.getgrnam('frrvty').gr_mem
:
984 logger
.error('"frr" user and group exist, but user is not under "frrvty"')
986 logger
.warning('could not find "frrvty" group')
988 for fname
in ['zebra', 'ospfd', 'ospf6d', 'bgpd', 'ripd', 'ripngd',
989 'isisd', 'pimd', 'ldpd']:
990 path
= os
.path
.join(frrdir
, fname
)
991 if not os
.path
.isfile(path
):
992 # LDPd is an exception
994 logger
.info('could not find {} in {}'.format(fname
, frrdir
) +
995 '(LDPd tests will not run)')
998 logger
.warning('could not find {} in {}'.format(fname
, frrdir
))
1001 if fname
!= 'zebra':
1005 '{} -v 2>&1 >/tmp/topotests/frr_zebra.txt'.format(path
)
1008 # Assert that Quagga utilities exist
1009 quaggadir
= config
.get('topogen', 'quaggadir')
1011 # if we have frr, don't check for quagga
1013 elif not os
.path
.isdir(quaggadir
):
1014 logger
.info('could not find {} directory (quagga tests will not run)'.format(quaggadir
))
1018 pwd
.getpwnam('quagga')[2]
1020 logger
.info('could not find "quagga" user')
1023 grp
.getgrnam('quagga')[2]
1025 logger
.info('could not find "quagga" group')
1028 if 'quagga' not in grp
.getgrnam('quaggavty').gr_mem
:
1029 logger
.error('"quagga" user and group exist, but user is not under "quaggavty"')
1031 logger
.warning('could not find "quaggavty" group')
1033 for fname
in ['zebra', 'ospfd', 'ospf6d', 'bgpd', 'ripd', 'ripngd',
1035 path
= os
.path
.join(quaggadir
, fname
)
1036 if not os
.path
.isfile(path
):
1037 logger
.warning('could not find {} in {}'.format(fname
, quaggadir
))
1040 if fname
!= 'zebra':
1044 '{} -v 2>&1 >/tmp/topotests/quagga_zebra.txt'.format(path
)
1047 # Test MPLS availability
1048 krel
= platform
.release()
1049 if topotest
.version_cmp(krel
, '4.5') < 0:
1050 logger
.info('LDPd tests will not run (have kernel "{}", but it requires 4.5)'.format(krel
))
1052 # Test for MPLS Kernel modules available
1053 if os
.system('/sbin/modprobe -n mpls-router') != 0:
1054 logger
.info('LDPd tests will not run (missing mpls-router kernel module)')
1055 if os
.system('/sbin/modprobe -n mpls-iptunnel') != 0:
1056 logger
.info('LDPd tests will not run (missing mpls-iptunnel kernel module)')
1058 # TODO remove me when we start supporting exabgp >= 4
1060 output
= subprocess
.check_output(['exabgp', '-v'])
1061 line
= output
.split('\n')[0]
1062 version
= line
.split(' ')[2]
1063 if topotest
.version_cmp(version
, '4') >= 0:
1064 logger
.warning('BGP topologies are still using exabgp version 3, expect failures')
1066 # We want to catch all exceptions
1067 # pylint: disable=W0702
1069 logger
.warning('failed to find exabgp or returned error')
1071 # After we logged the output to file, remove the handler.
1072 logger
.removeHandler(fhandler
)