]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_communities_topo1/test_bgp_communities.py
Merge pull request #12791 from taspelund/loc_rib_json_fix
[mirror_frr.git] / tests / topotests / bgp_communities_topo1 / test_bgp_communities.py
1 #!/usr/bin/python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2020 by VMware, Inc. ("VMware")
6 # Used Copyright (c) 2018 by Network Device Education Foundation,
7 # Inc. ("NetDEF") in this file.
8 #
9
10 """
11 Following tests are covered to test bgp community functionality:
12 - Verify routes are not advertised when NO-ADVERTISE Community is applied
13
14 """
15
16 import os
17 import sys
18 import time
19 import pytest
20
21 # Save the Current Working Directory to find configuration files.
22 CWD = os.path.dirname(os.path.realpath(__file__))
23 sys.path.append(os.path.join(CWD, "../"))
24
25 # pylint: disable=C0413
26 # Import topogen and topotest helpers
27 from lib.topogen import Topogen, get_topogen
28
29 # Import topoJson from lib, to create topology and initial configuration
30 from lib.common_config import (
31 start_topology,
32 write_test_header,
33 write_test_footer,
34 reset_config_on_routers,
35 verify_rib,
36 create_static_routes,
37 check_address_types,
38 step,
39 create_route_maps,
40 create_prefix_lists,
41 create_route_maps,
42 required_linux_kernel_version,
43 )
44 from lib.topolog import logger
45 from lib.bgp import (
46 verify_bgp_convergence,
47 create_router_bgp,
48 verify_bgp_rib,
49 )
50 from lib.topojson import build_config_from_json
51 from copy import deepcopy
52
53 pytestmark = [pytest.mark.bgpd, pytest.mark.staticd]
54
55
56 # Global variables
57 BGP_CONVERGENCE = False
58 ADDR_TYPES = check_address_types()
59 NETWORK = {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
60 NEXT_HOP_IP = {}
61
62
63 def setup_module(mod):
64 """
65 Sets up the pytest environment
66
67 * `mod`: module name
68 """
69
70 # Required linux kernel version for this suite to run.
71 result = required_linux_kernel_version("4.15")
72 if result is not True:
73 pytest.skip("Kernel requirements are not met, kernel version should be >= 4.15")
74
75 testsuite_run_time = time.asctime(time.localtime(time.time()))
76 logger.info("Testsuite start time: {}".format(testsuite_run_time))
77 logger.info("=" * 40)
78
79 logger.info("Running setup_module to create topology")
80
81 # This function initiates the topology build with Topogen...
82 json_file = "{}/bgp_communities.json".format(CWD)
83 tgen = Topogen(json_file, mod.__name__)
84 global topo
85 topo = tgen.json_topo
86 # ... and here it calls Mininet initialization functions.
87
88 # Starting topology, create tmp files which are loaded to routers
89 # to start daemons and then start routers
90 start_topology(tgen)
91
92 # Creating configuration from JSON
93 build_config_from_json(tgen, topo)
94
95 # Checking BGP convergence
96 global BGP_CONVERGENCE
97 global ADDR_TYPES
98
99 # Don't run this test if we have any failure.
100 if tgen.routers_have_failure():
101 pytest.skip(tgen.errors)
102
103 # Api call verify whether BGP is converged
104 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
105 assert BGP_CONVERGENCE is True, "setup_module :Failed \n Error:" " {}".format(
106 BGP_CONVERGENCE
107 )
108
109 logger.info("Running setup_module() done")
110
111
112 def teardown_module(mod):
113 """
114 Teardown the pytest environment
115
116 * `mod`: module name
117 """
118
119 logger.info("Running teardown_module to delete topology")
120
121 tgen = get_topogen()
122
123 # Stop toplogy and Remove tmp files
124 tgen.stop_topology()
125
126 logger.info(
127 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
128 )
129 logger.info("=" * 40)
130
131
132 #####################################################
133 #
134 # Tests starting
135 #
136 #####################################################
137
138
139 def test_bgp_no_advertise_community_p0(request):
140 """
141 Verify routes are not advertised when NO-ADVERTISE Community is applied
142
143 """
144
145 tc_name = request.node.name
146 write_test_header(tc_name)
147 tgen = get_topogen()
148 reset_config_on_routers(tgen)
149
150 # Don't run this test if we have any failure.
151 if tgen.routers_have_failure():
152 pytest.skip(tgen.errors)
153
154 NEXT_HOP_IP = {
155 "ipv4": topo["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
156 "ipv6": topo["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
157 }
158
159 # configure static routes
160 dut = "r3"
161 protocol = "bgp"
162
163 for addr_type in ADDR_TYPES:
164 # Enable static routes
165 input_dict = {
166 "r1": {
167 "static_routes": [
168 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
169 ]
170 }
171 }
172
173 logger.info("Configure static routes")
174 result = create_static_routes(tgen, input_dict)
175 assert result is True, "Testcase {} : Failed \n Error: {}".format(
176 tc_name, result
177 )
178
179 step("configure redistribute static and connected in Router BGP " "in R1")
180
181 input_dict_2 = {
182 "r1": {
183 "bgp": {
184 "address_family": {
185 addr_type: {
186 "unicast": {
187 "redistribute": [
188 {"redist_type": "static"},
189 {"redist_type": "connected"},
190 ]
191 }
192 }
193 }
194 }
195 }
196 }
197 result = create_router_bgp(tgen, topo, input_dict_2)
198 assert result is True, "Testcase {} : Failed \n Error: {}".format(
199 tc_name, result
200 )
201
202 step(
203 "BGP neighbors are up, static and connected route advertised from"
204 " R1 are present on R2 BGP table and RIB using show ip bgp and "
205 " show ip route"
206 )
207 step(
208 "Static and connected route advertised from R1 are present on R3"
209 " BGP table and RIB using show ip bgp and show ip route"
210 )
211
212 dut = "r3"
213 protocol = "bgp"
214 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
215 assert result is True, "Testcase {} : Failed \n Error: {}".format(
216 tc_name, result
217 )
218
219 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
220 assert result is True, "Testcase {} : Failed \n Error: {}".format(
221 tc_name, result
222 )
223
224 step("Configure prefix list P1 on R2 to permit route coming from R1")
225 # Create ip prefix list
226 input_dict_2 = {
227 "r2": {
228 "prefix_lists": {
229 addr_type: {
230 "pf_list_1_{}".format(addr_type): [
231 {"seqid": 10, "network": "any", "action": "permit"}
232 ]
233 }
234 }
235 }
236 }
237 result = create_prefix_lists(tgen, input_dict_2)
238 assert result is True, "Testcase {} : Failed \n Error: {}".format(
239 tc_name, result
240 )
241
242 # Create route map
243 input_dict_3 = {
244 "r2": {
245 "route_maps": {
246 "rmap_match_pf_1_{}".format(addr_type): [
247 {
248 "action": "permit",
249 "seq_id": "5",
250 "match": {
251 addr_type: {"prefix_lists": "pf_list_1_" + addr_type}
252 },
253 "set": {"community": {"num": "no-advertise"}},
254 }
255 ]
256 }
257 }
258 }
259 result = create_route_maps(tgen, input_dict_3)
260 assert result is True, "Testcase {} : Failed \n Error: {}".format(
261 tc_name, result
262 )
263 step(
264 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
265 " advertise community"
266 )
267 # Configure neighbor for route map
268 input_dict_4 = {
269 "r2": {
270 "bgp": {
271 "address_family": {
272 addr_type: {
273 "unicast": {
274 "neighbor": {
275 "r1": {
276 "dest_link": {
277 "r2": {
278 "route_maps": [
279 {
280 "name": "rmap_match_pf_1_"
281 + addr_type,
282 "direction": "in",
283 }
284 ]
285 }
286 }
287 }
288 }
289 }
290 }
291 }
292 }
293 }
294 }
295 result = create_router_bgp(tgen, topo, input_dict_4)
296 assert result is True, "Testcase {} : Failed \n Error: {}".format(
297 tc_name, result
298 )
299
300 step(
301 "After advertising no advertise community to BGP neighbor "
302 "static and connected router got removed from R3 verify using "
303 "show ip bgp & show ip route"
304 )
305
306 result = verify_bgp_rib(tgen, addr_type, dut, input_dict, expected=False)
307 assert result is not True, (
308 "Testcase {} : Failed \n Expected: "
309 "Routes still present in {} router. Found: {}".format(tc_name, dut, result)
310 )
311
312 result = verify_rib(
313 tgen, addr_type, dut, input_dict, protocol=protocol, expected=False
314 )
315 assert (
316 result is not True
317 ), "Testcase {} : Failed \n Expected: Routes still present in {} router. Found: {}".format(
318 tc_name, dut, result
319 )
320
321 step("Remove and Add no advertise community")
322 # Configure neighbor for route map
323 input_dict_4 = {
324 "r2": {
325 "bgp": {
326 "address_family": {
327 addr_type: {
328 "unicast": {
329 "neighbor": {
330 "r1": {
331 "dest_link": {
332 "r2": {
333 "route_maps": [
334 {
335 "name": "rmap_match_pf_1_"
336 + addr_type,
337 "direction": "in",
338 "delete": True,
339 }
340 ]
341 }
342 }
343 }
344 }
345 }
346 }
347 }
348 }
349 }
350 }
351 result = create_router_bgp(tgen, topo, input_dict_4)
352 assert result is True, "Testcase {} : Failed \n Error: {}".format(
353 tc_name, result
354 )
355
356 step(
357 "After removing no advertise community from BGP neighbor "
358 "static and connected router got advertised to R3 and "
359 "removing route-map, verify route using show ip bgp"
360 " and show ip route"
361 )
362
363 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
364 assert (
365 result is True
366 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
367 tc_name, result
368 )
369
370 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
371 assert (
372 result is True
373 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
374 tc_name, result
375 )
376
377 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
378 topo1 = deepcopy(topo)
379
380 topo1["routers"]["r1"]["bgp"]["local_as"] = "100"
381 topo1["routers"]["r2"]["bgp"]["local_as"] = "100"
382 topo1["routers"]["r3"]["bgp"]["local_as"] = "100"
383
384 for rtr in ["r1", "r2", "r3"]:
385 if "bgp" in topo1["routers"][rtr].keys():
386 delete_bgp = {rtr: {"bgp": {"delete": True}}}
387 result = create_router_bgp(tgen, topo1, delete_bgp)
388 assert result is True, "Testcase {} : Failed \n Error: {}".format(
389 tc_name, result
390 )
391 config_bgp = {
392 rtr: {"bgp": {"local_as": topo1["routers"][rtr]["bgp"]["local_as"]}}
393 }
394 result = create_router_bgp(tgen, topo1, config_bgp)
395 assert result is True, "Testcase {} : Failed \n Error: {}".format(
396 tc_name, result
397 )
398
399 build_config_from_json(tgen, topo1, save_bkup=False)
400
401 step("verify bgp convergence before starting test case")
402
403 bgp_convergence = verify_bgp_convergence(tgen, topo1)
404 assert bgp_convergence is True, "Testcase {} : Failed \n Error: {}".format(
405 tc_name, bgp_convergence
406 )
407
408 # configure static routes
409 dut = "r3"
410 protocol = "bgp"
411
412 for addr_type in ADDR_TYPES:
413 # Enable static routes
414 input_dict = {
415 "r1": {
416 "static_routes": [
417 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
418 ]
419 }
420 }
421
422 logger.info("Configure static routes")
423 result = create_static_routes(tgen, input_dict)
424 assert result is True, "Testcase {} : Failed \n Error: {}".format(
425 tc_name, result
426 )
427
428 step("configure redistribute static and connected in Router " "BGP in R1")
429
430 input_dict_2 = {
431 "r1": {
432 "bgp": {
433 "address_family": {
434 addr_type: {
435 "unicast": {
436 "redistribute": [
437 {"redist_type": "static"},
438 {"redist_type": "connected"},
439 ]
440 }
441 }
442 }
443 }
444 }
445 }
446 result = create_router_bgp(tgen, topo, input_dict_2)
447 assert result is True, "Testcase {} : Failed \n Error: {}".format(
448 tc_name, result
449 )
450
451 step(
452 "BGP neighbors are up, static and connected route advertised from"
453 " R1 are present on R2 BGP table and RIB using show ip bgp and "
454 " show ip route"
455 )
456 step(
457 "Static and connected route advertised from R1 are present on R3"
458 " BGP table and RIB using show ip bgp and show ip route"
459 )
460
461 dut = "r2"
462 protocol = "bgp"
463 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
464 assert result is True, "Testcase {} : Failed \n Error: {}".format(
465 tc_name, result
466 )
467
468 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
469 assert result is True, "Testcase {} : Failed \n Error: {}".format(
470 tc_name, result
471 )
472
473 step("Configure prefix list P1 on R2 to permit route coming from R1")
474 # Create ip prefix list
475 input_dict_2 = {
476 "r2": {
477 "prefix_lists": {
478 addr_type: {
479 "pf_list_1_{}".format(addr_type): [
480 {"seqid": 10, "network": "any", "action": "permit"}
481 ]
482 }
483 }
484 }
485 }
486 result = create_prefix_lists(tgen, input_dict_2)
487 assert result is True, "Testcase {} : Failed \n Error: {}".format(
488 tc_name, result
489 )
490
491 # Create route map
492 input_dict_3 = {
493 "r2": {
494 "route_maps": {
495 "rmap_match_pf_1_{}".format(addr_type): [
496 {
497 "action": "permit",
498 "seq_id": "5",
499 "match": {
500 addr_type: {"prefix_lists": "pf_list_1_" + addr_type}
501 },
502 "set": {"community": {"num": "no-advertise"}},
503 }
504 ]
505 }
506 }
507 }
508 result = create_route_maps(tgen, input_dict_3)
509 assert result is True, "Testcase {} : Failed \n Error: {}".format(
510 tc_name, result
511 )
512 step(
513 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
514 " advertise community"
515 )
516
517 # Configure neighbor for route map
518 input_dict_4 = {
519 "r2": {
520 "bgp": {
521 "address_family": {
522 addr_type: {
523 "unicast": {
524 "neighbor": {
525 "r1": {
526 "dest_link": {
527 "r2": {
528 "route_maps": [
529 {
530 "name": "rmap_match_pf_1_"
531 + addr_type,
532 "direction": "in",
533 }
534 ]
535 }
536 }
537 }
538 }
539 }
540 }
541 }
542 }
543 }
544 }
545 result = create_router_bgp(tgen, topo, input_dict_4)
546 assert result is True, "Testcase {} : Failed \n Error: {}".format(
547 tc_name, result
548 )
549
550 step(
551 "After advertising no advertise community to BGP neighbor "
552 "static and connected router got removed from R3 verify using "
553 "show ip bgp & show ip route"
554 )
555
556 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
557 assert (
558 result is True
559 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
560 tc_name, result
561 )
562
563 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
564 assert (
565 result is True
566 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
567 tc_name, result
568 )
569
570 step("Remove and Add no advertise community")
571 # Configure neighbor for route map
572 input_dict_4 = {
573 "r2": {
574 "bgp": {
575 "address_family": {
576 addr_type: {
577 "unicast": {
578 "neighbor": {
579 "r1": {
580 "dest_link": {
581 "r2": {
582 "route_maps": [
583 {
584 "name": "rmap_match_pf_1_"
585 + addr_type,
586 "direction": "in",
587 "delete": True,
588 }
589 ]
590 }
591 }
592 }
593 }
594 }
595 }
596 }
597 }
598 }
599 }
600 result = create_router_bgp(tgen, topo, input_dict_4)
601 assert result is True, "Testcase {} : Failed \n Error: {}".format(
602 tc_name, result
603 )
604
605 step(
606 "After removing no advertise community from BGP neighbor "
607 "static and connected router got advertised to R3 and "
608 "removing route verify using show ip bgp and "
609 " show ip route"
610 )
611
612 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
613 assert (
614 result is True
615 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
616 tc_name, result
617 )
618
619 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
620 assert (
621 result is True
622 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
623 tc_name, result
624 )
625
626 write_test_footer(tc_name)
627
628
629 if __name__ == "__main__":
630 args = ["-s"] + sys.argv[1:]
631 sys.exit(pytest.main(args))