]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/ldp_oc_topo1/test_ldp_oc_topo1.py
Merge pull request #9497 from opensourcerouting/cli-better-no
[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 functools import partial
66
67 # Save the Current Working Directory to find configuration files.
68 CWD = os.path.dirname(os.path.realpath(__file__))
69 sys.path.append(os.path.join(CWD, "../"))
70
71 # pylint: disable=C0413
72 # Import topogen and topotest helpers
73 from lib import topotest
74 from lib.topogen import Topogen, TopoRouter, get_topogen
75 from lib.topolog import logger
76
77 # Required to instantiate the topology builder class.
78
79 pytestmark = [pytest.mark.ldpd, pytest.mark.ospfd]
80
81
82 def build_topo(tgen):
83 "Build function"
84
85 #
86 # Define FRR Routers
87 #
88 for router in ["r1", "r2", "r3", "r4"]:
89 tgen.add_router(router)
90
91 #
92 # Define connections
93 #
94 switch = tgen.add_switch("s0")
95 switch.add_link(tgen.gears["r1"])
96 switch.add_link(tgen.gears["r2"])
97
98 switch = tgen.add_switch("s1")
99 switch.add_link(tgen.gears["r2"])
100 switch.add_link(tgen.gears["r3"])
101 switch.add_link(tgen.gears["r4"])
102
103 switch = tgen.add_switch("s2")
104 switch.add_link(tgen.gears["r2"])
105 switch.add_link(tgen.gears["r3"])
106
107
108 def setup_module(mod):
109 "Sets up the pytest environment"
110 tgen = Topogen(build_topo, mod.__name__)
111 tgen.start_topology()
112
113 router_list = tgen.routers()
114
115 # For all registered routers, load the zebra configuration file
116 for rname, router in router_list.items():
117 router.load_config(
118 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
119 )
120 # Don't start ospfd and ldpd in the CE nodes
121 if router.name[0] == "r":
122 router.load_config(
123 TopoRouter.RD_OSPF, os.path.join(CWD, "{}/ospfd.conf".format(rname))
124 )
125 router.load_config(
126 TopoRouter.RD_LDP, os.path.join(CWD, "{}/ldpd.conf".format(rname))
127 )
128
129 tgen.start_router()
130
131
132 def teardown_module(mod):
133 "Teardown the pytest environment"
134 tgen = get_topogen()
135
136 # This function tears down the whole topology.
137 tgen.stop_topology()
138
139
140 def router_compare_json_output(rname, command, reference):
141 "Compare router JSON output"
142
143 logger.info('Comparing router "%s" "%s" output', rname, command)
144
145 tgen = get_topogen()
146 filename = "{}/{}/{}".format(CWD, rname, reference)
147 expected = json.loads(open(filename).read())
148
149 # Run test function until we get an result. Wait at most 80 seconds.
150 test_func = partial(topotest.router_json_cmp, tgen.gears[rname], command, expected)
151 _, diff = topotest.run_and_expect(test_func, None, count=160, wait=0.5)
152
153 assertmsg = '"{}" JSON output mismatches the expected result'.format(rname)
154 assert diff is None, assertmsg
155
156
157 def test_ospf_convergence():
158 logger.info("Test: check OSPF adjacencies")
159
160 tgen = get_topogen()
161
162 # Skip if previous fatal error condition is raised
163 if tgen.routers_have_failure():
164 pytest.skip(tgen.errors)
165
166 for rname in ["r1", "r2", "r3", "r4"]:
167 router_compare_json_output(
168 rname, "show ip ospf neighbor json", "show_ip_ospf_neighbor.json"
169 )
170
171
172 def test_rib():
173 logger.info("Test: verify RIB")
174 tgen = get_topogen()
175
176 # Skip if previous fatal error condition is raised
177 if tgen.routers_have_failure():
178 pytest.skip(tgen.errors)
179
180 for rname in ["r1", "r2", "r3", "r4"]:
181 router_compare_json_output(rname, "show ip route json", "show_ip_route.ref")
182
183
184 def test_ldp_adjacencies():
185 logger.info("Test: verify LDP adjacencies")
186 tgen = get_topogen()
187
188 # Skip if previous fatal error condition is raised
189 if tgen.routers_have_failure():
190 pytest.skip(tgen.errors)
191
192 for rname in ["r1", "r2", "r3", "r4"]:
193 router_compare_json_output(
194 rname, "show mpls ldp discovery json", "show_ldp_discovery.ref"
195 )
196
197
198 def test_ldp_neighbors():
199 logger.info("Test: verify LDP neighbors")
200 tgen = get_topogen()
201
202 # Skip if previous fatal error condition is raised
203 if tgen.routers_have_failure():
204 pytest.skip(tgen.errors)
205
206 for rname in ["r1", "r2", "r3", "r4"]:
207 router_compare_json_output(
208 rname, "show mpls ldp neighbor json", "show_ldp_neighbor.ref"
209 )
210
211
212 def test_ldp_bindings():
213 logger.info("Test: verify LDP bindings")
214 tgen = get_topogen()
215
216 # Skip if previous fatal error condition is raised
217 if tgen.routers_have_failure():
218 pytest.skip(tgen.errors)
219
220 for rname in ["r1", "r2", "r3", "r4"]:
221 router_compare_json_output(
222 rname, "show mpls ldp binding json", "show_ldp_binding.ref"
223 )
224
225
226 # Memory leak test template
227 def test_memory_leak():
228 "Run the memory leak test and report results."
229 tgen = get_topogen()
230 if not tgen.is_memleak_enabled():
231 pytest.skip("Memory leak test/report is disabled")
232
233 tgen.report_memory_leaks()
234
235
236 if __name__ == "__main__":
237 args = ["-s"] + sys.argv[1:]
238 sys.exit(pytest.main(args))