]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_distance_change/test_bgp_admin_dist.py
Merge pull request #12851 from sri-mohan1/sri-mohan-ldp
[mirror_frr.git] / tests / topotests / bgp_distance_change / test_bgp_admin_dist.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2022 by VMware, Inc. ("VMware")
6 # Used Copyright (c) 2018 by Network Device Education Foundation,
7 # Inc. ("NetDEF") in this file.
8 #
9
10 import sys
11 import time
12 import pytest
13 import inspect
14 import os
15
16
17 """Following tests are covered to test bgp admin distance functionality.
18 TC_1:
19 Verify bgp admin distance functionality when static route is
20 configured same as ebgp learnt route
21
22 TC_2:
23 Verify ebgp admin distance functionality with ECMP.
24
25 TC_3:
26 Verify ibgp admin distance functionality when static route is
27 configured same as bgp learnt route.
28 TC_4:
29 Verify ibgp admin distance functionality with ECMP.
30
31 TC_7: Chaos - Verify bgp admin distance functionality with chaos.
32 """
33
34 #################################
35 # TOPOLOGY
36 #################################
37 """
38
39 +-------+
40 +--------- | R2 |
41 | +-------+
42 |iBGP |
43 +-------+ |
44 | R1 | |iBGP
45 +-------+ |
46 | |
47 | iBGP +-------+ eBGP +-------+
48 +---------- | R3 |----------| R4 |
49 +-------+ +-------+
50 |
51 |eBGP
52 |
53 +-------+
54 | R5 |
55 +-------+
56
57
58 """
59
60 # Save the Current Working Directory to find configuration files.
61 CWD = os.path.dirname(os.path.realpath(__file__))
62 sys.path.append(os.path.join(CWD, "../"))
63 sys.path.append(os.path.join(CWD, "../lib/"))
64
65 # pylint: disable=C0413
66 # Import topogen and topotest helpers
67 from lib.topogen import Topogen, get_topogen
68
69 # Required to instantiate the topology builder class.
70 from lib.common_config import (
71 start_topology,
72 write_test_header,
73 step,
74 write_test_footer,
75 create_static_routes,
76 verify_rib,
77 create_route_maps,
78 create_prefix_lists,
79 check_address_types,
80 reset_config_on_routers,
81 check_router_status,
82 stop_router,
83 kill_router_daemons,
84 start_router_daemons,
85 start_router,
86 get_frr_ipv6_linklocal,
87 verify_fib_routes,
88 )
89 from lib.topolog import logger
90 from lib.bgp import (
91 verify_bgp_convergence,
92 create_router_bgp,
93 verify_best_path_as_per_admin_distance,
94 clear_bgp,
95 )
96
97 # pylint: disable=C0413
98 # Import topogen and topotest helpers
99 from lib.topogen import Topogen, get_topogen
100 from lib.topojson import build_config_from_json
101 from lib.topolog import logger
102
103 # Global variables
104 topo = None
105 bgp_convergence = False
106 pytestmark = [pytest.mark.bgpd, pytest.mark.staticd]
107
108 NETWORK = {
109 "ipv4": [
110 "192.168.20.1/32",
111 "192.168.20.2/32",
112 "192.168.21.1/32",
113 "192.168.21.2/32",
114 "192.168.22.1/32",
115 "192.168.22.2/32",
116 ],
117 "ipv6": [
118 "fc07:50::1/128",
119 "fc07:50::2/128",
120 "fc07:150::1/128",
121 "fc07:150::2/128",
122 "fc07:1::1/128",
123 "fc07:1::2/128",
124 ],
125 }
126
127 ADDR_TYPES = check_address_types()
128
129
130 def setup_module(mod):
131 """
132 Sets up the pytest environment
133
134 * `mod`: module name
135 """
136
137 global topo
138
139 testsuite_run_time = time.asctime(time.localtime(time.time()))
140 logger.info("Testsuite start time: {}".format(testsuite_run_time))
141 logger.info("=" * 40)
142
143 logger.info("Running setup_module to create topology")
144
145 # This function initiates the topology build with Topogen...
146 json_file = "{}/bgp_admin_dist.json".format(CWD)
147 tgen = Topogen(json_file, mod.__name__)
148 global topo
149 topo = tgen.json_topo
150
151 # Starting topology, create tmp files which are loaded to routers
152 # to start deamons and then start routers
153 start_topology(tgen)
154
155 # Creating configuration from JSON
156 build_config_from_json(tgen, topo)
157
158 # Checking BGP convergence
159 global bgp_convergence
160 global ADDR_TYPES
161
162 # Don't run this test if we have any failure.
163 if tgen.routers_have_failure():
164 pytest.skip(tgen.errors)
165
166 # Api call verify whether BGP is converged
167 bgp_convergence = verify_bgp_convergence(tgen, topo)
168 assert bgp_convergence is True, "setup_module :Failed \n Error:" " {}".format(
169 bgp_convergence
170 )
171 logger.info("Running setup_module() done")
172
173
174 def teardown_module(mod):
175 """teardown_module.
176
177 Teardown the pytest environment.
178 * `mod`: module name
179 """
180 logger.info("Running teardown_module to delete topology")
181 tgen = get_topogen()
182
183 # Stop toplogy and Remove tmp files
184 tgen.stop_topology()
185
186 logger.info(
187 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
188 )
189 logger.info("=" * 40)
190
191
192 #####################################################
193 # Tests starting
194 #####################################################
195 def test_bgp_admin_distance_ebgp_ecmp_p0():
196 """
197 TC: 2
198 Verify ebgp admin distance functionality with ECMP.
199 """
200 tgen = get_topogen()
201 global bgp_convergence
202
203 if bgp_convergence is not True:
204 pytest.skip("skipping test case because of BGP Convergence failure at setup")
205
206 # test case name
207 tc_name = inspect.stack()[0][3]
208 write_test_header(tc_name)
209 if tgen.routers_have_failure():
210 check_router_status(tgen)
211
212 step("Configure base config as per the topology")
213 reset_config_on_routers(tgen)
214
215 step("Configure static route in R4 and R5, redistribute in bgp")
216
217 for addr_type in ADDR_TYPES:
218
219 input_dict = {
220 "r4": {
221 "static_routes": [{"network": NETWORK[addr_type], "next_hop": "Null0"}]
222 }
223 }
224
225 result = create_static_routes(tgen, input_dict)
226 assert result is True, "Testcase {} : Failed \n Error: {}".format(
227 tc_name, result
228 )
229
230 for addr_type in ADDR_TYPES:
231
232 input_dict = {
233 "r5": {
234 "static_routes": [{"network": NETWORK[addr_type], "next_hop": "Null0"}]
235 }
236 }
237
238 result = create_static_routes(tgen, input_dict)
239 assert result is True, "Testcase {} : Failed \n Error: {}".format(
240 tc_name, result
241 )
242
243 step("Verify that route is learnt in DUT via ebgp")
244
245 # Verifying RIB routes
246 protocol = "bgp"
247 input_dict = topo["routers"]
248 dut = "r3"
249 nhop = {"ipv4": [], "ipv6": []}
250 nhop["ipv4"].append(topo["routers"]["r4"]["links"]["r3"]["ipv4"].split("/")[0])
251 nhop["ipv4"].append(topo["routers"]["r5"]["links"]["r3"]["ipv4"].split("/")[0])
252 nhop["ipv6"].append(get_frr_ipv6_linklocal(tgen, "r4", "r3-r4-eth1"))
253 nhop["ipv6"].append(get_frr_ipv6_linklocal(tgen, "r5", "r1-r3-eth1"))
254
255 for addr_type in ADDR_TYPES:
256 input_dict = {
257 "r3": {
258 "static_routes": [
259 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
260 ]
261 }
262 }
263 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
264 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
265 tc_name, result4
266 )
267
268 step("Configure the static route in R3 (Dut).")
269
270 for addr_type in ADDR_TYPES:
271
272 input_dict = {
273 "r3": {
274 "static_routes": [
275 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
276 ]
277 }
278 }
279
280 result = create_static_routes(tgen, input_dict)
281 assert result is True, "Testcase {} : Failed \n Error: {}".format(
282 tc_name, result
283 )
284
285 step("Verify that static route is selected as best route in zebra.")
286
287 # Verifying RIB routes
288 protocol = "static"
289 dut = "r3"
290
291 for addr_type in ADDR_TYPES:
292 input_dict = {
293 "r3": {
294 "static_routes": [
295 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
296 ]
297 }
298 }
299
300 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
301 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
302 tc_name, result4
303 )
304
305 step(" Configure the admin distance of 254 to static route in R3.")
306
307 for addr_type in ADDR_TYPES:
308
309 input_dict = {
310 "r3": {
311 "static_routes": [
312 {
313 "network": NETWORK[addr_type][0],
314 "next_hop": "Null0",
315 "admin_distance": 254,
316 }
317 ]
318 }
319 }
320
321 result = create_static_routes(tgen, input_dict)
322 assert result is True, "Testcase {} : Failed \n Error: {}".format(
323 tc_name, result
324 )
325
326 step("Verify that bgp routes are selected as best routes in zebra.")
327 protocol = "bgp"
328 dut = "r3"
329
330 for addr_type in ADDR_TYPES:
331 input_dict = {
332 "r3": {
333 "static_routes": [
334 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
335 ]
336 }
337 }
338 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
339 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
340 tc_name, result4
341 )
342
343 input_dict_1 = {
344 "r3": {
345 "bgp": {
346 "local_as": 100,
347 "address_family": {
348 "ipv4": {
349 "unicast": {
350 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
351 }
352 },
353 "ipv6": {
354 "unicast": {
355 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
356 }
357 },
358 },
359 }
360 }
361 }
362
363 result = create_router_bgp(tgen, topo, input_dict_1)
364 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
365
366 step("Verify that bgp routes are selected as best routes in zebra.")
367 # Verifying RIB routes
368 protocol = "bgp"
369 dut = "r3"
370
371 for addr_type in ADDR_TYPES:
372 input_dict = {
373 "r3": {
374 "static_routes": [
375 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
376 ]
377 }
378 }
379 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
380 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
381 tc_name, result4
382 )
383
384 step("Configure bgp admin distance 10 with CLI in dut.")
385 input_dict_1 = {
386 "r3": {
387 "bgp": {
388 "local_as": 100,
389 "address_family": {
390 "ipv4": {
391 "unicast": {"distance": {"ebgp": 10, "ibgp": 254, "local": 254}}
392 },
393 "ipv6": {
394 "unicast": {"distance": {"ebgp": 10, "ibgp": 254, "local": 254}}
395 },
396 },
397 }
398 }
399 }
400
401 result = create_router_bgp(tgen, topo, input_dict_1)
402 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
403
404 step("Verify ebgp routes have admin distance of 10 in dut.")
405
406 protocol = "bgp"
407 input_dict = topo["routers"]
408 dut = "r3"
409
410 for addr_type in ADDR_TYPES:
411 input_dict = {
412 "r3": {
413 "static_routes": [
414 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
415 ]
416 }
417 }
418 result4 = verify_rib(
419 tgen, addr_type, dut, input_dict, protocol=protocol, admin_distance=10
420 )
421 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
422 tc_name, result4
423 )
424
425 step(
426 "Configure route map with weight as 200 and apply to one of the "
427 "neighbor (R4 neighbor)."
428 )
429
430 # Create Prefix list
431 input_dict_2 = {
432 "r3": {
433 "prefix_lists": {
434 "ipv4": {
435 "pf_ls_1": [
436 {
437 "seqid": 10,
438 "network": NETWORK["ipv4"][0],
439 "le": "32",
440 "action": "permit",
441 }
442 ]
443 },
444 "ipv6": {
445 "pf_ls_1_ipv6": [
446 {
447 "seqid": 100,
448 "network": NETWORK["ipv6"][0],
449 "le": "128",
450 "action": "permit",
451 }
452 ]
453 },
454 }
455 }
456 }
457 result = create_prefix_lists(tgen, input_dict_2)
458 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
459
460 # Create route map
461 input_dict_3 = {
462 "r3": {
463 "route_maps": {
464 "RMAP_WEIGHT": [
465 {
466 "action": "permit",
467 "match": {"ipv4": {"prefix_lists": "pf_ls_1"}},
468 "set": {"weight": 200},
469 },
470 {
471 "action": "permit",
472 "match": {"ipv6": {"prefix_lists": "pf_ls_1_ipv6"}},
473 "set": {"weight": 200},
474 },
475 ]
476 }
477 }
478 }
479 result = create_route_maps(tgen, input_dict_3)
480 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
481
482 # Configure neighbor for route map
483 input_dict_4 = {
484 "r3": {
485 "bgp": {
486 "address_family": {
487 "ipv4": {
488 "unicast": {
489 "neighbor": {
490 "r4": {
491 "dest_link": {
492 "r3": {
493 "route_maps": [
494 {
495 "name": "RMAP_WEIGHT",
496 "direction": "in",
497 }
498 ]
499 }
500 }
501 }
502 }
503 }
504 },
505 "ipv6": {
506 "unicast": {
507 "neighbor": {
508 "r4": {
509 "dest_link": {
510 "r3": {
511 "route_maps": [
512 {
513 "name": "RMAP_WEIGHT",
514 "direction": "in",
515 }
516 ]
517 }
518 }
519 }
520 }
521 }
522 },
523 }
524 }
525 }
526 }
527 result = create_router_bgp(tgen, topo, input_dict_4)
528 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
529
530 step("Verify that bgp route is selected as best on by zebra in r3.")
531
532 protocol = "bgp"
533 dut = "r3"
534
535 for addr_type in ADDR_TYPES:
536 input_dict = {
537 "r3": {
538 "static_routes": [
539 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
540 ]
541 }
542 }
543 result4 = verify_rib(
544 tgen, addr_type, dut, input_dict, protocol=protocol, admin_distance=10
545 )
546 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
547 tc_name, result4
548 )
549
550 step("Static route should not be selected as best route.")
551 protocol = "static"
552 dut = "r3"
553
554 for addr_type in ADDR_TYPES:
555 input_dict = {
556 "r3": {
557 "static_routes": [
558 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
559 ]
560 }
561 }
562 result4 = verify_fib_routes(
563 tgen, addr_type, dut, input_dict, protocol=protocol, expected=False
564 )
565 assert (
566 result4 is not True
567 ), "Testcase {} : Failed. Wrong route is selected as best route.\n Error: {}".format(
568 tc_name, result4
569 )
570
571 step("Reconfigure the static route without admin distance")
572
573 for addr_type in ADDR_TYPES:
574
575 input_dict = {
576 "r3": {
577 "static_routes": [
578 {
579 "network": NETWORK[addr_type][0],
580 "next_hop": "Null0",
581 "admin_distance": 254,
582 "delete": True,
583 }
584 ]
585 }
586 }
587
588 result = create_static_routes(tgen, input_dict)
589 assert result is True, "Testcase {} : Failed \n Error: {}".format(
590 tc_name, result
591 )
592
593 for addr_type in ADDR_TYPES:
594 input_dict = {
595 "r3": {
596 "static_routes": [
597 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
598 ]
599 }
600 }
601
602 result = create_static_routes(tgen, input_dict)
603 assert result is True, "Testcase {} : Failed \n Error: {}".format(
604 tc_name, result
605 )
606
607 step("Verify that static route is installed as best route.")
608 protocol = "static"
609 dut = "r3"
610
611 for addr_type in ADDR_TYPES:
612 input_dict = {
613 "r3": {
614 "static_routes": [
615 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
616 ]
617 }
618 }
619 result4 = verify_rib(
620 tgen, addr_type, dut, input_dict, protocol=protocol, fib=True
621 )
622 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
623 tc_name, result4
624 )
625
626 step("Unconfigure the static route in R3.")
627
628 for addr_type in ADDR_TYPES:
629 input_dict = {
630 "r3": {
631 "static_routes": [
632 {
633 "network": NETWORK[addr_type][0],
634 "next_hop": "Null0",
635 "delete": True,
636 }
637 ]
638 }
639 }
640
641 result = create_static_routes(tgen, input_dict)
642 assert result is True, "Testcase {} : Failed \n Error: {}".format(
643 tc_name, result
644 )
645
646 step("Verify that bgp route is selected as best on by zebra in r3.")
647
648 protocol = "bgp"
649 dut = "r3"
650
651 for addr_type in ADDR_TYPES:
652 input_dict = {
653 "r3": {
654 "static_routes": [
655 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
656 ]
657 }
658 }
659 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
660 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
661 tc_name, result4
662 )
663
664 step("Un configure the route map on R3.")
665
666 # Configure neighbor for route map
667 input_dict_4 = {
668 "r3": {
669 "bgp": {
670 "address_family": {
671 "ipv4": {
672 "unicast": {
673 "neighbor": {
674 "r4": {
675 "dest_link": {
676 "r3": {
677 "route_maps": [
678 {
679 "name": "RMAP_WEIGHT",
680 "direction": "in",
681 "delete": True,
682 }
683 ]
684 }
685 }
686 }
687 }
688 }
689 },
690 "ipv6": {
691 "unicast": {
692 "neighbor": {
693 "r4": {
694 "dest_link": {
695 "r3": {
696 "route_maps": [
697 {
698 "name": "RMAP_WEIGHT",
699 "direction": "in",
700 "delete": True,
701 }
702 ]
703 }
704 }
705 }
706 }
707 }
708 },
709 }
710 }
711 }
712 }
713 result = create_router_bgp(tgen, topo, input_dict_4)
714 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
715
716 step("verify bgp routes installed in zebra.")
717
718 # Verifying RIB routes
719 protocol = "bgp"
720 input_dict = topo["routers"]
721 dut = "r3"
722 for addr_type in ADDR_TYPES:
723 input_dict = {
724 "r3": {
725 "static_routes": [
726 {"network": NETWORK[addr_type][0], "next_hop": "Null0"}
727 ]
728 }
729 }
730 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
731 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
732 tc_name, result4
733 )
734
735 write_test_footer(tc_name)
736
737
738 def test_bgp_admin_distance_ibgp_p0():
739 """
740 TC: 3
741 Verify bgp admin distance functionality when static route is
742 configured same as ibgp learnt route
743 """
744 tgen = get_topogen()
745 global bgp_convergence
746
747 if bgp_convergence is not True:
748 pytest.skip("skipping test case because of BGP Convergence failure at setup")
749
750 # test case name
751 tc_name = inspect.stack()[0][3]
752 write_test_header(tc_name)
753 if tgen.routers_have_failure():
754 check_router_status(tgen)
755
756 step("Configure base config as per the topology")
757 reset_config_on_routers(tgen)
758
759 step("Configure bgp admin distance 200 with CLI in dut.")
760
761 input_dict_1 = {
762 "r3": {
763 "bgp": {
764 "local_as": 100,
765 "address_family": {
766 "ipv4": {
767 "unicast": {
768 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
769 }
770 },
771 "ipv6": {
772 "unicast": {
773 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
774 }
775 },
776 },
777 }
778 }
779 }
780
781 result = create_router_bgp(tgen, topo, input_dict_1)
782 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
783
784 step("Verify bgp routes have admin distance of 200 in dut.")
785 # Verifying best path
786 dut = "r3"
787 attribute = "admin_distance"
788
789 input_dict = {
790 "ipv4": {
791 "r3": {
792 "static_routes": [
793 {
794 "network": "192.168.22.1/32",
795 "admin_distance": 200,
796 },
797 {
798 "network": "192.168.22.2/32",
799 "admin_distance": 200,
800 },
801 ]
802 }
803 },
804 "ipv6": {
805 "r3": {
806 "static_routes": [
807 {
808 "network": "fc07:1::1/128",
809 "admin_distance": 200,
810 },
811 {
812 "network": "fc07:1::2/128",
813 "admin_distance": 200,
814 },
815 ]
816 }
817 },
818 }
819
820 for addr_type in ADDR_TYPES:
821 result = verify_best_path_as_per_admin_distance(
822 tgen, addr_type, dut, input_dict[addr_type], attribute
823 )
824 assert result is True, "Testcase {} : Failed \n Error: {}".format(
825 tc_name, result
826 )
827
828 step("Modify the admin distance value to 150.")
829
830 input_dict_1 = {
831 "r3": {
832 "bgp": {
833 "local_as": 100,
834 "address_family": {
835 "ipv4": {
836 "unicast": {
837 "distance": {"ebgp": 150, "ibgp": 150, "local": 150}
838 }
839 },
840 "ipv6": {
841 "unicast": {
842 "distance": {"ebgp": 150, "ibgp": 150, "local": 150}
843 }
844 },
845 },
846 }
847 }
848 }
849
850 result = create_router_bgp(tgen, topo, input_dict_1)
851 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
852
853 step("Verify bgp routes have admin distance of 150 in dut.")
854 # Verifying best path
855 dut = "r3"
856 attribute = "admin_distance"
857
858 input_dict = {
859 "ipv4": {
860 "r3": {
861 "static_routes": [
862 {
863 "network": "192.168.22.1/32",
864 "admin_distance": 150,
865 },
866 {
867 "network": "192.168.22.2/32",
868 "admin_distance": 150,
869 },
870 ]
871 }
872 },
873 "ipv6": {
874 "r3": {
875 "static_routes": [
876 {
877 "network": "fc07:1::1/128",
878 "admin_distance": 150,
879 },
880 {
881 "network": "fc07:1::2/128",
882 "admin_distance": 150,
883 },
884 ]
885 }
886 },
887 }
888
889 for addr_type in ADDR_TYPES:
890 result = verify_best_path_as_per_admin_distance(
891 tgen, addr_type, dut, input_dict[addr_type], attribute
892 )
893 assert result is True, "Testcase {} : Failed \n Error: {}".format(
894 tc_name, result
895 )
896
897 step("Un configure the admin distance value on DUT")
898
899 input_dict_1 = {
900 "r3": {
901 "bgp": {
902 "local_as": 100,
903 "address_family": {
904 "ipv4": {
905 "unicast": {
906 "distance": {
907 "ebgp": 150,
908 "ibgp": 150,
909 "local": 150,
910 "delete": True,
911 }
912 }
913 },
914 "ipv6": {
915 "unicast": {
916 "distance": {
917 "ebgp": 150,
918 "ibgp": 150,
919 "local": 150,
920 "delete": True,
921 }
922 }
923 },
924 },
925 }
926 }
927 }
928
929 result = create_router_bgp(tgen, topo, input_dict_1)
930 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
931
932 step("Verify bgp routes have default admin distance in dut.")
933 # Verifying best path
934 dut = "r3"
935 attribute = "admin_distance"
936
937 input_dict = {
938 "ipv4": {
939 "r3": {
940 "static_routes": [
941 {
942 "network": "192.168.22.1/32",
943 "admin_distance": 20,
944 },
945 {
946 "network": "192.168.22.2/32",
947 "admin_distance": 20,
948 },
949 ]
950 }
951 },
952 "ipv6": {
953 "r3": {
954 "static_routes": [
955 {
956 "network": "fc07:1::1/128",
957 "admin_distance": 20,
958 },
959 {
960 "network": "fc07:1::2/128",
961 "admin_distance": 20,
962 },
963 ]
964 }
965 },
966 }
967
968 for addr_type in ADDR_TYPES:
969 result = verify_best_path_as_per_admin_distance(
970 tgen, addr_type, dut, input_dict[addr_type], attribute
971 )
972 assert result is True, "Testcase {} : Failed \n Error: {}".format(
973 tc_name, result
974 )
975
976 step(
977 "Learn the same route via ebgp and ibgp peer. Configure admin "
978 "distance of 200 in DUT for both ebgp and ibgp peer. "
979 )
980
981 step("Verify that ebgp route is preferred over ibgp.")
982
983 # Verifying RIB routes
984 protocol = "bgp"
985 input_dict = topo["routers"]
986
987 for addr_type in ADDR_TYPES:
988 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
989 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
990 tc_name, result4
991 )
992
993 step("Configure static route Without any admin distance")
994
995 for addr_type in ADDR_TYPES:
996
997 input_dict = {
998 "r3": {
999 "static_routes": [{"network": NETWORK[addr_type], "next_hop": "Null0"}]
1000 }
1001 }
1002
1003 result = create_static_routes(tgen, input_dict)
1004 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1005 tc_name, result
1006 )
1007
1008 step("Verify that zebra selects static route.")
1009 protocol = "static"
1010
1011 for addr_type in ADDR_TYPES:
1012
1013 input_dict = {
1014 "r3": {
1015 "static_routes": [{"network": NETWORK[addr_type], "next_hop": "Null0"}]
1016 }
1017 }
1018
1019 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
1020 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
1021 tc_name, result4
1022 )
1023
1024 step("Configure static route with admin distance of 253")
1025 for addr_type in ADDR_TYPES:
1026
1027 input_dict = {
1028 "r3": {
1029 "static_routes": [
1030 {
1031 "network": NETWORK[addr_type],
1032 "next_hop": "Null0",
1033 "admin_distance": 253,
1034 }
1035 ]
1036 }
1037 }
1038
1039 result = create_static_routes(tgen, input_dict)
1040 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1041 tc_name, result
1042 )
1043
1044 step("Verify that zebra selects bgp route.")
1045 protocol = "bgp"
1046
1047 for addr_type in ADDR_TYPES:
1048 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
1049 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
1050 tc_name, result4
1051 )
1052
1053 step("Configure admin distance of 254 in bgp for route.")
1054
1055 input_dict_1 = {
1056 "r3": {
1057 "bgp": {
1058 "local_as": 100,
1059 "address_family": {
1060 "ipv4": {
1061 "unicast": {
1062 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
1063 }
1064 },
1065 "ipv6": {
1066 "unicast": {
1067 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
1068 }
1069 },
1070 },
1071 }
1072 }
1073 }
1074
1075 result = create_router_bgp(tgen, topo, input_dict_1)
1076 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1077
1078 step("Verify that zebra selects static route.")
1079 protocol = "static"
1080
1081 for addr_type in ADDR_TYPES:
1082 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
1083 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
1084 tc_name, result4
1085 )
1086
1087 step("Delete the static route.")
1088 for addr_type in ADDR_TYPES:
1089
1090 input_dict = {
1091 "r3": {
1092 "static_routes": [
1093 {
1094 "network": NETWORK[addr_type],
1095 "next_hop": "Null0",
1096 "admin_distance": 253,
1097 "delete": True,
1098 }
1099 ]
1100 }
1101 }
1102
1103 result = create_static_routes(tgen, input_dict)
1104 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1105 tc_name, result
1106 )
1107
1108 step("Verify that zebra selects bgp route.")
1109 protocol = "bgp"
1110
1111 for addr_type in ADDR_TYPES:
1112 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
1113 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
1114 tc_name, result4
1115 )
1116
1117 write_test_footer(tc_name)
1118
1119
1120 def test_bgp_admin_distance_chaos_p2():
1121 """
1122 TC: 7
1123 Chaos - Verify bgp admin distance functionality with chaos.
1124 """
1125 tgen = get_topogen()
1126 global bgp_convergence
1127
1128 if bgp_convergence is not True:
1129 pytest.skip("skipping test case because of BGP Convergence failure at setup")
1130
1131 # test case name
1132 tc_name = inspect.stack()[0][3]
1133 write_test_header(tc_name)
1134 if tgen.routers_have_failure():
1135 check_router_status(tgen)
1136
1137 step("Configure base config as per the topology")
1138 reset_config_on_routers(tgen)
1139
1140 step("Configure bgp admin distance 200 with CLI in dut.")
1141
1142 input_dict_1 = {
1143 "r3": {
1144 "bgp": {
1145 "local_as": 100,
1146 "address_family": {
1147 "ipv4": {
1148 "unicast": {
1149 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
1150 }
1151 },
1152 "ipv6": {
1153 "unicast": {
1154 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
1155 }
1156 },
1157 },
1158 }
1159 }
1160 }
1161
1162 result = create_router_bgp(tgen, topo, input_dict_1)
1163 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1164
1165 step("Verify bgp routes have admin distance of 200 in dut.")
1166 # Verifying best path
1167 dut = "r3"
1168 attribute = "admin_distance"
1169
1170 input_dict = {
1171 "ipv4": {
1172 "r3": {
1173 "static_routes": [
1174 {
1175 "network": NETWORK["ipv4"][0],
1176 "admin_distance": 200,
1177 },
1178 {
1179 "network": NETWORK["ipv4"][1],
1180 "admin_distance": 200,
1181 },
1182 ]
1183 }
1184 },
1185 "ipv6": {
1186 "r3": {
1187 "static_routes": [
1188 {
1189 "network": NETWORK["ipv6"][0],
1190 "admin_distance": 200,
1191 },
1192 {
1193 "network": NETWORK["ipv6"][1],
1194 "admin_distance": 200,
1195 },
1196 ]
1197 }
1198 },
1199 }
1200
1201 for addr_type in ADDR_TYPES:
1202 result = verify_best_path_as_per_admin_distance(
1203 tgen, addr_type, dut, input_dict[addr_type], attribute
1204 )
1205 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1206 tc_name, result
1207 )
1208
1209 step("Restart frr on R3")
1210 stop_router(tgen, "r3")
1211 start_router(tgen, "r3")
1212
1213 bgp_convergence = verify_bgp_convergence(tgen, topo)
1214 assert bgp_convergence is True, "Testcase {} : Failed \n Error: {}".format(
1215 tc_name, bgp_convergence
1216 )
1217
1218 step("Verify ebgp and ibgp routes have admin distance of 200 in dut.")
1219 for addr_type in ADDR_TYPES:
1220 result = verify_best_path_as_per_admin_distance(
1221 tgen, addr_type, dut, input_dict[addr_type], attribute
1222 )
1223 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1224 tc_name, result
1225 )
1226
1227 step("Restart bgpd process on R3")
1228 kill_router_daemons(tgen, "r3", ["bgpd"])
1229 start_router_daemons(tgen, "r3", ["bgpd"])
1230
1231 bgp_convergence = verify_bgp_convergence(tgen, topo)
1232 assert bgp_convergence is True, "Testcase {} : Failed \n Error: {}".format(
1233 tc_name, bgp_convergence
1234 )
1235
1236 step("Verify ebgp and ibgp routes have admin distance of 200 in dut.")
1237 for addr_type in ADDR_TYPES:
1238 result = verify_best_path_as_per_admin_distance(
1239 tgen, addr_type, dut, input_dict[addr_type], attribute
1240 )
1241 assert result is True, "Testcase {} : Failed \n Error: {}".format(
1242 tc_name, result
1243 )
1244
1245 step("Clear BGP")
1246 for rtr in topo["routers"]:
1247 clear_bgp(tgen, "ipv4", rtr)
1248 clear_bgp(tgen, "ipv6", rtr)
1249
1250 bgp_convergence = verify_bgp_convergence(tgen, topo)
1251 assert bgp_convergence is True, "Testcase {} : Failed \n Error: {}".format(
1252 tc_name, bgp_convergence
1253 )
1254
1255 step("Verify that zebra selects bgp route.")
1256 protocol = "bgp"
1257
1258 for addr_type in ADDR_TYPES:
1259 result4 = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
1260 assert result4 is True, "Testcase {} : Failed \n Error: {}".format(
1261 tc_name, result4
1262 )
1263
1264 write_test_footer(tc_name)
1265
1266
1267 if __name__ == "__main__":
1268 args = ["-s"] + sys.argv[1:]
1269 sys.exit(pytest.main(args))