]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bfd-topo3/test_bfd_topo3.py
Merge pull request #6882 from vishaldhingra/static
[mirror_frr.git] / tests / topotests / bfd-topo3 / test_bfd_topo3.py
1 #!/usr/bin/env python
2
3 #
4 # test_bfd_topo3.py
5 # Part of NetDEF Topology Tests
6 #
7 # Copyright (c) 2020 by
8 # Network Device Education Foundation, Inc. ("NetDEF")
9 #
10 # Permission to use, copy, modify, and/or distribute this software
11 # for any purpose with or without fee is hereby granted, provided
12 # that the above copyright notice and this permission notice appear
13 # in all copies.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
16 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
18 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
19 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
21 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 # OF THIS SOFTWARE.
23 #
24
25 """
26 test_bfd_topo3.py: Test the FRR BFD daemon multi hop.
27 """
28
29 import os
30 import sys
31 import json
32 from functools import partial
33 import pytest
34
35 # Save the Current Working Directory to find configuration files.
36 CWD = os.path.dirname(os.path.realpath(__file__))
37 sys.path.append(os.path.join(CWD, "../"))
38
39 # pylint: disable=C0413
40 # Import topogen and topotest helpers
41 from lib import topotest
42 from lib.topogen import Topogen, TopoRouter, get_topogen
43 from lib.topolog import logger
44
45 # Required to instantiate the topology builder class.
46 from mininet.topo import Topo
47
48
49 class BFDTopo(Topo):
50 "Test topology builder"
51
52 def build(self, *_args, **_opts):
53 "Build function"
54 tgen = get_topogen(self)
55
56 # Create 4 routers
57 for routern in range(1, 5):
58 tgen.add_router("r{}".format(routern))
59
60 switch = tgen.add_switch("s1")
61 switch.add_link(tgen.gears["r1"])
62 switch.add_link(tgen.gears["r2"])
63
64 switch = tgen.add_switch("s2")
65 switch.add_link(tgen.gears["r2"])
66 switch.add_link(tgen.gears["r3"])
67
68 switch = tgen.add_switch("s3")
69 switch.add_link(tgen.gears["r3"])
70 switch.add_link(tgen.gears["r4"])
71
72
73 def setup_module(mod):
74 "Sets up the pytest environment"
75 tgen = Topogen(BFDTopo, mod.__name__)
76 tgen.start_topology()
77
78 router_list = tgen.routers()
79 for rname, router in router_list.items():
80 daemon_file = "{}/{}/bfdd.conf".format(CWD, rname)
81 if os.path.isfile(daemon_file):
82 router.load_config(TopoRouter.RD_BFD, daemon_file)
83
84 daemon_file = "{}/{}/zebra.conf".format(CWD, rname)
85 if os.path.isfile(daemon_file):
86 router.load_config(TopoRouter.RD_ZEBRA, daemon_file)
87
88 daemon_file = "{}/{}/bgpd.conf".format(CWD, rname)
89 if os.path.isfile(daemon_file):
90 router.load_config(TopoRouter.RD_BGP, daemon_file)
91
92 # Initialize all routers.
93 tgen.start_router()
94
95
96 def test_wait_bgp_convergence():
97 "Wait for BGP to converge"
98 tgen = get_topogen()
99 if tgen.routers_have_failure():
100 pytest.skip(tgen.errors)
101
102 logger.info("waiting for protocols to converge")
103
104 def expect_loopback_route(router, iptype, route, proto):
105 "Wait until route is present on RIB for protocol."
106 logger.info('waiting route {} in {}'.format(route, router))
107 test_func = partial(
108 topotest.router_json_cmp,
109 tgen.gears[router],
110 'show {} route json'.format(iptype),
111 { route: [{ 'protocol': proto }] }
112 )
113 _, result = topotest.run_and_expect(test_func, None, count=130, wait=1)
114 assertmsg = '"{}" OSPF convergence failure'.format(router)
115 assert result is None, assertmsg
116
117 # Wait for R1 <-> R2 convergence.
118 expect_loopback_route('r1', 'ip', '10.254.254.2/32', 'bgp')
119 # Wait for R1 <-> R3 convergence.
120 expect_loopback_route('r1', 'ip', '10.254.254.3/32', 'bgp')
121 # Wait for R1 <-> R4 convergence.
122 expect_loopback_route('r1', 'ip', '10.254.254.4/32', 'bgp')
123
124 # Wait for R2 <-> R1 convergence.
125 expect_loopback_route('r2', 'ip', '10.254.254.1/32', 'bgp')
126 # Wait for R2 <-> R3 convergence.
127 expect_loopback_route('r2', 'ip', '10.254.254.3/32', 'bgp')
128 # Wait for R2 <-> R4 convergence.
129 expect_loopback_route('r2', 'ip', '10.254.254.4/32', 'bgp')
130
131 # Wait for R3 <-> R1 convergence.
132 expect_loopback_route('r3', 'ip', '10.254.254.1/32', 'bgp')
133 # Wait for R3 <-> R2 convergence.
134 expect_loopback_route('r3', 'ip', '10.254.254.2/32', 'bgp')
135 # Wait for R3 <-> R4 convergence.
136 expect_loopback_route('r3', 'ip', '10.254.254.4/32', 'bgp')
137
138 # Wait for R4 <-> R1 convergence.
139 expect_loopback_route('r4', 'ip', '10.254.254.1/32', 'bgp')
140 # Wait for R4 <-> R2 convergence.
141 expect_loopback_route('r4', 'ip', '10.254.254.2/32', 'bgp')
142 # Wait for R4 <-> R3 convergence.
143 expect_loopback_route('r4', 'ip', '10.254.254.3/32', 'bgp')
144
145
146 def test_wait_bfd_convergence():
147 "Wait for BFD to converge"
148 tgen = get_topogen()
149 if tgen.routers_have_failure():
150 pytest.skip(tgen.errors)
151
152 logger.info("test BFD configurations")
153
154 def expect_bfd_configuration(router):
155 "Load JSON file and compare with 'show bfd peer json'"
156 logger.info('waiting BFD configuration on router {}'.format(router))
157 bfd_config = json.loads(open('{}/{}/bfd-peers.json'.format(CWD, router)).read())
158 test_func = partial(
159 topotest.router_json_cmp,
160 tgen.gears[router],
161 'show bfd peers json',
162 bfd_config
163 )
164 _, result = topotest.run_and_expect(test_func, None, count=130, wait=1)
165 assertmsg = '"{}" BFD configuration failure'.format(router)
166 assert result is None, assertmsg
167
168 expect_bfd_configuration('r1')
169 expect_bfd_configuration('r2')
170 expect_bfd_configuration('r3')
171 expect_bfd_configuration('r4')
172
173
174 def teardown_module(_mod):
175 "Teardown the pytest environment"
176 tgen = get_topogen()
177 tgen.stop_topology()
178
179
180 def test_memory_leak():
181 "Run the memory leak test and report results."
182 tgen = get_topogen()
183 if not tgen.is_memleak_enabled():
184 pytest.skip("Memory leak test/report is disabled")
185
186 tgen.report_memory_leaks()
187
188
189 if __name__ == "__main__":
190 args = ["-s"] + sys.argv[1:]
191 sys.exit(pytest.main(args))