1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
#!/usr/bin/env python
# SPDX-License-Identifier: ISC
#
# test_ospf_metric_propagation.py
#
# Copyright (c) 2023 ATCorp
# Jafar Al-Gharaibeh
#
import os
import sys
import json
from functools import partial
import pytest
# pylint: disable=C0413
# Import topogen and topotest helpers
from lib import topotest
from lib.topogen import Topogen, get_topogen
from lib.topolog import logger
"""
test_ospf_metric_propagation.py: Test OSPF/BGP metric propagation
"""
TOPOLOGY = """
+-----+ +-----+
eth1 | | eth0 | | eth2
+-------------+ rA +---------------------------+ rB +---------------+
| .5 | | .5 .6 | | .6 |
| +--+--+ 10.0.50.0/24 +--+--+ .6 |
| |.5 |.6 |
| eth2| eth1| |
10.0.10.0/24 | | |
| 10.0.20.0/24 10.0.30.0/24 10.0.40.0/24
|blue |blue |blue |blue
| | | |
eth1|.1 eth1|.2 eth1|.3 eth1|.4
+-----+ +--+--+ +--+--+ +-----+ +-+---+ +-+---+ +------+
| |eth0 eth2| | eth0 | |eth2 eth1| |eth2 eth3| | eth0 | |eth2 eth0| |
| h1 +----------+ R1 +----------+ R2 +-----------+ rC +----------+ R3 +------------+ R4 +---------+ h2 |
| | | | | | | | | | | | | |
+-----+.2 .1 +-----+.1 .2+-----+.2 .7 +-----+.7 .3+-----+.3 .4+-----+.4 .2+------+
green green green green
10.0.91.0/24 10.0.1.0/24 10.0.70.0/24 10.0.80.0/24 10.0.3.0/24 10.0.94.0/24
"""
# Save the Current Working Directory to find configuration files.
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(CWD, "../"))
# Required to instantiate the topology builder class.
pytestmark = [pytest.mark.ospfd, pytest.mark.bgpd]
def build_topo(tgen):
"Build function"
# Create 4 routers
for routern in range(1, 5):
tgen.add_router("r{}".format(routern))
tgen.add_router("ra")
tgen.add_router("rb")
tgen.add_router("rc")
tgen.add_router("h1")
tgen.add_router("h2")
# Interconect router 1, 2
switch = tgen.add_switch("s1-2")
switch.add_link(tgen.gears["r1"])
switch.add_link(tgen.gears["r2"])
# Interconect router 3, 4
switch = tgen.add_switch("s3-4")
switch.add_link(tgen.gears["r3"])
switch.add_link(tgen.gears["r4"])
# Interconect router a, b
switch = tgen.add_switch("sa-b")
switch.add_link(tgen.gears["ra"])
switch.add_link(tgen.gears["rb"])
# Interconect router 1, a
switch = tgen.add_switch("s1-a")
switch.add_link(tgen.gears["r1"])
switch.add_link(tgen.gears["ra"])
# Interconect router 2, a
switch = tgen.add_switch("s2-a")
switch.add_link(tgen.gears["r2"])
switch.add_link(tgen.gears["ra"])
# Interconect router 3, b
switch = tgen.add_switch("s3-b")
switch.add_link(tgen.gears["r3"])
switch.add_link(tgen.gears["rb"])
# Interconect router 4, b
switch = tgen.add_switch("s4-b")
switch.add_link(tgen.gears["r4"])
switch.add_link(tgen.gears["rb"])
# Interconect router 1, h1
switch = tgen.add_switch("s1-h1")
switch.add_link(tgen.gears["r1"])
switch.add_link(tgen.gears["h1"])
# Interconect router 4, h2
switch = tgen.add_switch("s4-h2")
switch.add_link(tgen.gears["r4"])
switch.add_link(tgen.gears["h2"])
# Interconect router 2, c
switch = tgen.add_switch("s2-c")
switch.add_link(tgen.gears["r2"])
switch.add_link(tgen.gears["rc"])
# Interconect router 3, c
switch = tgen.add_switch("s3-c")
switch.add_link(tgen.gears["r3"])
switch.add_link(tgen.gears["rc"])
def setup_module(mod):
logger.info("OSPF Metric Propagation:\n {}".format(TOPOLOGY))
tgen = Topogen(build_topo, mod.__name__)
tgen.start_topology()
vrf_setup_cmds = [
"ip link add name blue type vrf table 11",
"ip link set dev blue up",
"ip link add name green type vrf table 12",
"ip link set dev green up",
]
# Starting Routers
router_list = tgen.routers()
# Create VRFs and bind to interfaces
for routern in range(1, 5):
for cmd in vrf_setup_cmds:
tgen.net["r{}".format(routern)].cmd(cmd)
for routern in range(1, 5):
tgen.net["r{}".format(routern)].cmd(
"ip link set dev r{}-eth1 vrf blue up".format(routern)
)
tgen.net["r{}".format(routern)].cmd(
"ip link set dev r{}-eth2 vrf green up".format(routern)
)
for rname, router in router_list.items():
logger.info("Loading router %s" % rname)
router.load_frr_config(os.path.join(CWD, "{}/frr.conf".format(rname)))
# Initialize all routers.
tgen.start_router()
for router in router_list.values():
if router.has_version("<", "4.20"):
tgen.set_error("unsupported version")
def teardown_module():
"Teardown the pytest environment"
tgen = get_topogen()
tgen.stop_topology()
def test_all_links_up():
"Test path R1 -> Ra -> Rb -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-1.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_down():
"Test path R1 -> R2 -> Ra -> Rb -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
tgen.net["r1"].cmd("ip link set dev r1-eth1 down")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-2.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_2_down():
"Test path R1 -> R2 -> Rc -> R3 -> Rb -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
tgen.net["r2"].cmd("ip link set dev r2-eth1 down")
tgen.net["r1"].cmd("ip link set dev r1-eth0 down")
tgen.net["r2"].cmd("ip link set dev r2-eth2 down")
tgen.net["r2"].cmd("ip link set dev r2-eth2 up")
tgen.net["r1"].cmd("ip link set dev r1-eth0 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-3.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_2_3_down():
"Test path R1 -> R2 -> Rc -> R3 -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
tgen.net["r3"].cmd("ip link set dev r3-eth1 down")
tgen.net["r1"].cmd("ip link set dev r1-eth0 down")
tgen.net["r3"].cmd("ip link set dev r3-eth0 down")
tgen.net["r3"].cmd("ip link set dev r3-eth0 up")
tgen.net["r1"].cmd("ip link set dev r1-eth0 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-4.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_2_3_4_down():
"Test path R1 -> R2 -> Rc -> R3 -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
tgen.net["r4"].cmd("ip link set dev r4-eth1 down")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-4.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_2_4_down_3_up():
"Test path R1 -> R2 -> Rc -> R3 -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
# bring link 3 back up
tgen.net["r3"].cmd("ip link set dev r3-eth1 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-4.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=60, wait=1)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_4_down_2_up():
"Test path R1 -> R2 -> Ra -> Rb -> R3 -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
# bring back link 2 up
tgen.net["r2"].cmd("ip link set dev r2-eth1 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-5.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=120, wait=2)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_4_down_1_up():
"Test path R1 -> Ra -> Rb -> R3 -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
# bring back link 1 up
tgen.net["r1"].cmd("ip link set dev r1-eth1 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-6.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=120, wait=2)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_link_1_2_3_4_up():
"Test path R1 -> Ra -> Rb -> R4"
tgen = get_topogen()
if tgen.routers_have_failure():
pytest.skip("skipped because of router(s) failure")
# bring back link 4 up
tgen.net["r4"].cmd("ip link set dev r4-eth1 up")
r1 = tgen.gears["r1"]
json_file = "{}/r1/show_ip_route-1.json".format(CWD)
expected = json.loads(open(json_file).read())
test_func = partial(
topotest.router_json_cmp, r1, "show ip route vrf green 10.0.94.2 json", expected
)
_, result = topotest.run_and_expect(test_func, None, count=120, wait=2)
assertmsg = "r1 JSON output mismatches"
assert result is None, assertmsg
def test_memory_leak():
"Run the memory leak test and report results."
tgen = get_topogen()
if not tgen.is_memleak_enabled():
pytest.skip("Memory leak test/report is disabled")
tgen.report_memory_leaks()
if __name__ == "__main__":
args = ["-s"] + sys.argv[1:]
sys.exit(pytest.main(args))
|