]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/ldp_oc_topo1/test_ldp_oc_topo1.py
Merge pull request #8649 from ton31337/fix/unify-naming-for-topotests-directory
[mirror_frr.git] / tests / topotests / ldp_oc_topo1 / test_ldp_oc_topo1.py
1 #!/usr/bin/env python
2
3 #
4 # test_ldp_oc_topo1.py
5 # Part of NetDEF Topology Tests
6 #
7 # Copyright (c) 2020 by by Volta Networks
8 #
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
12 # in all copies.
13 #
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
21 # OF THIS SOFTWARE.
22 #
23
24 """
25 test_ldp_oc_topo1.py: Simple FRR LDP Test
26
27 +---------+
28 | r1 |
29 | 1.1.1.1 |
30 +----+----+
31 | .1 r1-eth0
32 |
33 ~~~~~~~~~~~~~
34 ~~ sw0 ~~
35 ~~ 10.0.1.0/24 ~~
36 ~~~~~~~~~~~~~
37 |10.0.1.0/24
38 |
39 | .2 r2-eth0
40 +----+----+
41 | r2 |
42 | 2.2.2.2 |
43 +--+---+--+
44 r2-eth2 .2 | | .2 r2-eth1
45 ______/ \______
46 / \
47 ~~~~~~~~~~~~~ ~~~~~~~~~~~~~
48 ~~ sw2 ~~ ~~ sw1 ~~
49 ~~ 10.0.3.0/24 ~~ ~~ 10.0.2.0/24 ~~
50 ~~~~~~~~~~~~~ ~~~~~~~~~~~~~
51 | / |
52 \ _________/ |
53 \ / \
54 r3-eth1 .3 | | .3 r3-eth0 | .4 r4-eth0
55 +----+--+---+ +----+----+
56 | r3 | | r4 |
57 | 3.3.3.3 | | 4.4.4.4 |
58 +-----------+ +---------+
59 """
60
61 import os
62 import sys
63 import pytest
64 import json
65 from time import sleep
66 from functools import partial
67
68 # Save the Current Working Directory to find configuration files.
69 CWD = os.path.dirname(os.path.realpath(__file__))
70 sys.path.append(os.path.join(CWD, "../"))
71
72 # pylint: disable=C0413
73 # Import topogen and topotest helpers
74 from lib import topotest
75 from lib.topogen import Topogen, TopoRouter, get_topogen
76 from lib.topolog import logger
77
78 # Required to instantiate the topology builder class.
79 from mininet.topo import Topo
80
81 pytestmark = [pytest.mark.ldpd, pytest.mark.ospfd]
82
83
84 class TemplateTopo(Topo):
85 "Test topology builder"
86
87 def build(self, *_args, **_opts):
88 "Build function"
89 tgen = get_topogen(self)
90
91 #
92 # Define FRR Routers
93 #
94 for router in ["r1", "r2", "r3", "r4"]:
95 tgen.add_router(router)
96
97 #
98 # Define connections
99 #
100 switch = tgen.add_switch("s0")
101 switch.add_link(tgen.gears["r1"])
102 switch.add_link(tgen.gears["r2"])
103
104 switch = tgen.add_switch("s1")
105 switch.add_link(tgen.gears["r2"])
106 switch.add_link(tgen.gears["r3"])
107 switch.add_link(tgen.gears["r4"])
108
109 switch = tgen.add_switch("s2")
110 switch.add_link(tgen.gears["r2"])
111 switch.add_link(tgen.gears["r3"])
112
113
114 def setup_module(mod):
115 "Sets up the pytest environment"
116 tgen = Topogen(TemplateTopo, mod.__name__)
117 tgen.start_topology()
118
119 router_list = tgen.routers()
120
121 # For all registered routers, load the zebra configuration file
122 for rname, router in router_list.items():
123 router.load_config(
124 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
125 )
126 # Don't start ospfd and ldpd in the CE nodes
127 if router.name[0] == "r":
128 router.load_config(
129 TopoRouter.RD_OSPF, os.path.join(CWD, "{}/ospfd.conf".format(rname))
130 )
131 router.load_config(
132 TopoRouter.RD_LDP, os.path.join(CWD, "{}/ldpd.conf".format(rname))
133 )
134
135 tgen.start_router()
136
137
138 def teardown_module(mod):
139 "Teardown the pytest environment"
140 tgen = get_topogen()
141
142 # This function tears down the whole topology.
143 tgen.stop_topology()
144
145
146 def router_compare_json_output(rname, command, reference):
147 "Compare router JSON output"
148
149 logger.info('Comparing router "%s" "%s" output', rname, command)
150
151 tgen = get_topogen()
152 filename = "{}/{}/{}".format(CWD, rname, reference)
153 expected = json.loads(open(filename).read())
154
155 # Run test function until we get an result. Wait at most 80 seconds.
156 test_func = partial(topotest.router_json_cmp, tgen.gears[rname], command, expected)
157 _, diff = topotest.run_and_expect(test_func, None, count=160, wait=0.5)
158
159 assertmsg = '"{}" JSON output mismatches the expected result'.format(rname)
160 assert diff is None, assertmsg
161
162
163 def test_ospf_convergence():
164 logger.info("Test: check OSPF adjacencies")
165
166 tgen = get_topogen()
167
168 # Skip if previous fatal error condition is raised
169 if tgen.routers_have_failure():
170 pytest.skip(tgen.errors)
171
172 for rname in ["r1", "r2", "r3", "r4"]:
173 router_compare_json_output(
174 rname, "show ip ospf neighbor json", "show_ip_ospf_neighbor.json"
175 )
176
177
178 def test_rib():
179 logger.info("Test: verify RIB")
180 tgen = get_topogen()
181
182 # Skip if previous fatal error condition is raised
183 if tgen.routers_have_failure():
184 pytest.skip(tgen.errors)
185
186 for rname in ["r1", "r2", "r3", "r4"]:
187 router_compare_json_output(rname, "show ip route json", "show_ip_route.ref")
188
189
190 def test_ldp_adjacencies():
191 logger.info("Test: verify LDP adjacencies")
192 tgen = get_topogen()
193
194 # Skip if previous fatal error condition is raised
195 if tgen.routers_have_failure():
196 pytest.skip(tgen.errors)
197
198 for rname in ["r1", "r2", "r3", "r4"]:
199 router_compare_json_output(
200 rname, "show mpls ldp discovery json", "show_ldp_discovery.ref"
201 )
202
203
204 def test_ldp_neighbors():
205 logger.info("Test: verify LDP neighbors")
206 tgen = get_topogen()
207
208 # Skip if previous fatal error condition is raised
209 if tgen.routers_have_failure():
210 pytest.skip(tgen.errors)
211
212 for rname in ["r1", "r2", "r3", "r4"]:
213 router_compare_json_output(
214 rname, "show mpls ldp neighbor json", "show_ldp_neighbor.ref"
215 )
216
217
218 def test_ldp_bindings():
219 logger.info("Test: verify LDP bindings")
220 tgen = get_topogen()
221
222 # Skip if previous fatal error condition is raised
223 if tgen.routers_have_failure():
224 pytest.skip(tgen.errors)
225
226 for rname in ["r1", "r2", "r3", "r4"]:
227 router_compare_json_output(
228 rname, "show mpls ldp binding json", "show_ldp_binding.ref"
229 )
230
231
232 # Memory leak test template
233 def test_memory_leak():
234 "Run the memory leak test and report results."
235 tgen = get_topogen()
236 if not tgen.is_memleak_enabled():
237 pytest.skip("Memory leak test/report is disabled")
238
239 tgen.report_memory_leaks()
240
241
242 if __name__ == "__main__":
243 args = ["-s"] + sys.argv[1:]
244 sys.exit(pytest.main(args))