]>
Commit | Line | Data |
---|---|---|
418b2885 KS |
1 | #!/usr/bin/env python |
2 | ||
3 | # | |
4 | # test_ldp_oc_acl_topo1.py | |
5 | # Part of NetDEF Topology Tests | |
6 | # | |
7 | # Copyright (c) 2020 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 | ||
21b5cd1d | 24 | r""" |
622c4996 | 25 | test_ldp_oc_acl_topo1.py: Simple FRR LDP Test |
418b2885 KS |
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__)) | |
787e7624 | 70 | sys.path.append(os.path.join(CWD, "../")) |
418b2885 KS |
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. | |
418b2885 | 79 | |
3dedee4f | 80 | pytestmark = [pytest.mark.ldpd, pytest.mark.ospfd] |
787e7624 | 81 | |
5980ad0a | 82 | |
e82b531d CH |
83 | def build_topo(tgen): |
84 | "Build function" | |
787e7624 | 85 | |
e82b531d CH |
86 | # |
87 | # Define FRR Routers | |
88 | # | |
89 | for router in ["r1", "r2", "r3", "r4"]: | |
90 | tgen.add_router(router) | |
418b2885 | 91 | |
e82b531d CH |
92 | # |
93 | # Define connections | |
94 | # | |
95 | switch = tgen.add_switch("s0") | |
96 | switch.add_link(tgen.gears["r1"]) | |
97 | switch.add_link(tgen.gears["r2"]) | |
418b2885 | 98 | |
e82b531d CH |
99 | switch = tgen.add_switch("s1") |
100 | switch.add_link(tgen.gears["r2"]) | |
101 | switch.add_link(tgen.gears["r3"]) | |
102 | switch.add_link(tgen.gears["r4"]) | |
787e7624 | 103 | |
e82b531d CH |
104 | switch = tgen.add_switch("s2") |
105 | switch.add_link(tgen.gears["r2"]) | |
106 | switch.add_link(tgen.gears["r3"]) | |
418b2885 | 107 | |
418b2885 KS |
108 | |
109 | def setup_module(mod): | |
110 | "Sets up the pytest environment" | |
e82b531d | 111 | tgen = Topogen(build_topo, mod.__name__) |
418b2885 KS |
112 | tgen.start_topology() |
113 | ||
114 | router_list = tgen.routers() | |
115 | ||
116 | # For all registered routers, load the zebra configuration file | |
e5f0ed14 | 117 | for rname, router in router_list.items(): |
418b2885 | 118 | router.load_config( |
787e7624 | 119 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) |
418b2885 KS |
120 | ) |
121 | # Don't start ospfd and ldpd in the CE nodes | |
787e7624 | 122 | if router.name[0] == "r": |
418b2885 | 123 | router.load_config( |
787e7624 | 124 | TopoRouter.RD_OSPF, os.path.join(CWD, "{}/ospfd.conf".format(rname)) |
418b2885 KS |
125 | ) |
126 | router.load_config( | |
787e7624 | 127 | TopoRouter.RD_LDP, os.path.join(CWD, "{}/ldpd.conf".format(rname)) |
418b2885 KS |
128 | ) |
129 | ||
130 | tgen.start_router() | |
131 | ||
787e7624 | 132 | |
418b2885 KS |
133 | def teardown_module(mod): |
134 | "Teardown the pytest environment" | |
135 | tgen = get_topogen() | |
136 | ||
137 | # This function tears down the whole topology. | |
138 | tgen.stop_topology() | |
139 | ||
140 | ||
141 | def router_compare_json_output(rname, command, reference): | |
142 | "Compare router JSON output" | |
143 | ||
144 | logger.info('Comparing router "%s" "%s" output', rname, command) | |
145 | ||
146 | tgen = get_topogen() | |
787e7624 | 147 | filename = "{}/{}/{}".format(CWD, rname, reference) |
418b2885 KS |
148 | expected = json.loads(open(filename).read()) |
149 | ||
150 | # Run test function until we get an result. Wait at most 80 seconds. | |
787e7624 | 151 | test_func = partial(topotest.router_json_cmp, tgen.gears[rname], command, expected) |
418b2885 KS |
152 | _, diff = topotest.run_and_expect(test_func, None, count=160, wait=0.5) |
153 | ||
154 | assertmsg = '"{}" JSON output mismatches the expected result'.format(rname) | |
155 | assert diff is None, assertmsg | |
156 | ||
787e7624 | 157 | |
418b2885 KS |
158 | def test_ospf_convergence(): |
159 | logger.info("Test: check OSPF adjacencies") | |
160 | ||
161 | tgen = get_topogen() | |
162 | ||
163 | # Skip if previous fatal error condition is raised | |
164 | if tgen.routers_have_failure(): | |
165 | pytest.skip(tgen.errors) | |
166 | ||
787e7624 | 167 | for rname in ["r1", "r2", "r3", "r4"]: |
168 | router_compare_json_output( | |
169 | rname, "show ip ospf neighbor json", "show_ip_ospf_neighbor.json" | |
170 | ) | |
171 | ||
418b2885 KS |
172 | |
173 | def test_rib(): | |
174 | logger.info("Test: verify RIB") | |
175 | tgen = get_topogen() | |
176 | ||
177 | # Skip if previous fatal error condition is raised | |
178 | if tgen.routers_have_failure(): | |
179 | pytest.skip(tgen.errors) | |
180 | ||
787e7624 | 181 | for rname in ["r1", "r2", "r3", "r4"]: |
418b2885 KS |
182 | router_compare_json_output(rname, "show ip route json", "show_ip_route.ref") |
183 | ||
787e7624 | 184 | |
418b2885 KS |
185 | def test_ldp_adjacencies(): |
186 | logger.info("Test: verify LDP adjacencies") | |
187 | tgen = get_topogen() | |
188 | ||
189 | # Skip if previous fatal error condition is raised | |
190 | if tgen.routers_have_failure(): | |
191 | pytest.skip(tgen.errors) | |
192 | ||
787e7624 | 193 | for rname in ["r1", "r2", "r3", "r4"]: |
194 | router_compare_json_output( | |
195 | rname, "show mpls ldp discovery json", "show_ldp_discovery.ref" | |
196 | ) | |
197 | ||
418b2885 KS |
198 | |
199 | def test_ldp_neighbors(): | |
200 | logger.info("Test: verify LDP neighbors") | |
201 | tgen = get_topogen() | |
202 | ||
203 | # Skip if previous fatal error condition is raised | |
204 | if tgen.routers_have_failure(): | |
205 | pytest.skip(tgen.errors) | |
206 | ||
787e7624 | 207 | for rname in ["r1", "r2", "r3", "r4"]: |
208 | router_compare_json_output( | |
209 | rname, "show mpls ldp neighbor json", "show_ldp_neighbor.ref" | |
210 | ) | |
211 | ||
418b2885 KS |
212 | |
213 | def test_ldp_bindings(): | |
214 | logger.info("Test: verify LDP bindings") | |
215 | tgen = get_topogen() | |
216 | ||
217 | # Skip if previous fatal error condition is raised | |
218 | if tgen.routers_have_failure(): | |
219 | pytest.skip(tgen.errors) | |
220 | ||
787e7624 | 221 | for rname in ["r1", "r2", "r3", "r4"]: |
222 | router_compare_json_output( | |
223 | rname, "show mpls ldp binding json", "show_ldp_binding.ref" | |
224 | ) | |
225 | ||
418b2885 | 226 | |
6b4830dc | 227 | def test_ldp_bindings_all_routes(): |
228 | logger.info("Test: verify LDP bindings after host filter removed") | |
229 | tgen = get_topogen() | |
230 | ||
231 | # Skip if previous fatal error condition is raised | |
232 | if tgen.routers_have_failure(): | |
233 | pytest.skip(tgen.errors) | |
234 | ||
235 | # remove ACL that blocks advertising everything but host routes */ | |
787e7624 | 236 | cmd = 'vtysh -c "configure terminal" -c "mpls ldp" -c "address-family ipv4" -c "no label local allocate host-routes"' |
237 | tgen.net["r1"].cmd(cmd) | |
6b4830dc | 238 | sleep(2) |
239 | ||
787e7624 | 240 | for rname in ["r1", "r2", "r3", "r4"]: |
241 | router_compare_json_output( | |
242 | rname, "show mpls ldp binding json", "show_ldp_all_binding.ref" | |
243 | ) | |
244 | ||
6b4830dc | 245 | |
418b2885 KS |
246 | # Memory leak test template |
247 | def test_memory_leak(): | |
248 | "Run the memory leak test and report results." | |
249 | tgen = get_topogen() | |
250 | if not tgen.is_memleak_enabled(): | |
787e7624 | 251 | pytest.skip("Memory leak test/report is disabled") |
418b2885 KS |
252 | |
253 | tgen.report_memory_leaks() | |
254 | ||
787e7624 | 255 | |
256 | if __name__ == "__main__": | |
418b2885 KS |
257 | args = ["-s"] + sys.argv[1:] |
258 | sys.exit(pytest.main(args)) |