2 # SPDX-License-Identifier: ISC
5 # Copyright (c) 2022 by VMware, Inc. ("VMware")
6 # Used Copyright (c) 2018 by Network Device Education Foundation,
7 # Inc. ("NetDEF") in this file.
16 """Following tests are covered to test bgp admin distance functionality.
18 Verify bgp admin distance functionality when static route is configured
19 same as bgp learnt route in user vrf.
21 TC_6: Verify bgp admin distance functionality with ECMP in user vrf.
24 Verify bgp admin distance functionality when routes are
25 imported between VRFs.
28 #################################
30 #################################
41 | iBGP +-------+ eBGP +-------+
42 +---------- | R3 |----------| R4 |
54 # Save the Current Working Directory to find configuration files.
55 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
56 sys
.path
.append(os
.path
.join(CWD
, "../"))
57 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
59 # pylint: disable=C0413
60 # Import topogen and topotest helpers
61 from lib
.topogen
import Topogen
, get_topogen
63 # Required to instantiate the topology builder class.
64 from lib
.common_config
import (
72 reset_config_on_routers
,
75 from lib
.topolog
import logger
77 verify_bgp_convergence
,
79 verify_best_path_as_per_admin_distance
,
82 # pylint: disable=C0413
83 # Import topogen and topotest helpers
84 from lib
.topogen
import Topogen
, get_topogen
85 from lib
.topojson
import build_config_from_json
86 from lib
.topolog
import logger
90 bgp_convergence
= False
91 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
111 ADDR_TYPES
= check_address_types()
114 def setup_module(mod
):
116 Sets up the pytest environment
123 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
124 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
125 logger
.info("=" * 40)
127 logger
.info("Running setup_module to create topology")
129 # This function initiates the topology build with Topogen...
130 json_file
= "{}/bgp_admin_dist_vrf.json".format(CWD
)
131 tgen
= Topogen(json_file
, mod
.__name
__)
133 topo
= tgen
.json_topo
135 # Starting topology, create tmp files which are loaded to routers
136 # to start deamons and then start routers
139 # Creating configuration from JSON
140 build_config_from_json(tgen
, topo
)
142 # Checking BGP convergence
143 global bgp_convergence
146 # Don't run this test if we have any failure.
147 if tgen
.routers_have_failure():
148 pytest
.skip(tgen
.errors
)
150 # Api call verify whether BGP is converged
151 bgp_convergence
= verify_bgp_convergence(tgen
, topo
)
152 assert bgp_convergence
is True, "setup_module :Failed \n Error:" " {}".format(
155 logger
.info("Running setup_module() done")
158 def teardown_module(mod
):
161 Teardown the pytest environment.
164 logger
.info("Running teardown_module to delete topology")
167 # Stop toplogy and Remove tmp files
171 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
173 logger
.info("=" * 40)
176 #####################################################
178 #####################################################
181 def test_bgp_admin_distance_ebgp_vrf_p0():
184 Verify bgp admin distance functionality when static route is
185 configured same as ebgp learnt route
188 global bgp_convergence
190 if bgp_convergence
is not True:
191 pytest
.skip("skipping test case because of BGP Convergence failure at setup")
194 tc_name
= inspect
.stack()[0][3]
195 write_test_header(tc_name
)
196 if tgen
.routers_have_failure():
197 check_router_status(tgen
)
199 step("Configure base config as per the topology")
200 reset_config_on_routers(tgen
)
202 step("Configure bgp admin distance 200 with CLI in dut.")
213 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
218 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
227 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
228 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
230 step("Verify bgp routes have admin distance of 200 in dut.")
231 # Verifying best path
233 attribute
= "admin_distance"
240 "network": NETWORK
["ipv4"][0],
241 "admin_distance": 200,
245 "network": NETWORK
["ipv4"][1],
246 "admin_distance": 200,
256 "network": NETWORK
["ipv6"][0],
257 "admin_distance": 200,
261 "network": NETWORK
["ipv6"][1],
262 "admin_distance": 200,
270 for addr_type
in ADDR_TYPES
:
271 result
= verify_best_path_as_per_admin_distance(
272 tgen
, addr_type
, dut
, input_dict
[addr_type
], attribute
, vrf
="RED"
274 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
278 step("Modify the admin distance value to 150.")
288 "distance": {"ebgp": 150, "ibgp": 150, "local": 150}
293 "distance": {"ebgp": 150, "ibgp": 150, "local": 150}
302 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
303 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
305 step("Verify bgp routes have admin distance of 150 in dut.")
306 # Verifying best path
308 attribute
= "admin_distance"
315 "network": NETWORK
["ipv4"][0],
316 "admin_distance": 150,
320 "network": NETWORK
["ipv4"][1],
321 "admin_distance": 150,
331 "network": NETWORK
["ipv6"][0],
332 "admin_distance": 150,
336 "network": NETWORK
["ipv6"][1],
337 "admin_distance": 150,
345 for addr_type
in ADDR_TYPES
:
346 result
= verify_best_path_as_per_admin_distance(
347 tgen
, addr_type
, dut
, input_dict
[addr_type
], attribute
, vrf
="RED"
349 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
353 step("Un configure the admin distance value on DUT")
387 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
388 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
390 step("Verify bgp routes have default admin distance in dut.")
391 # Verifying best path
393 attribute
= "admin_distance"
399 {"network": NETWORK
["ipv4"][0], "admin_distance": 20, "vrf": "RED"},
400 {"network": NETWORK
["ipv4"][1], "admin_distance": 20, "vrf": "RED"},
407 {"network": NETWORK
["ipv6"][0], "admin_distance": 20, "vrf": "RED"},
408 {"network": NETWORK
["ipv6"][1], "admin_distance": 20, "vrf": "RED"},
414 for addr_type
in ADDR_TYPES
:
415 result
= verify_best_path_as_per_admin_distance(
416 tgen
, addr_type
, dut
, input_dict
[addr_type
], attribute
, vrf
="RED"
418 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
422 step("Configure static route Without any admin distance")
424 for addr_type
in ADDR_TYPES
:
425 # Create Static routes
429 {"network": NETWORK
[addr_type
], "next_hop": "Null0", "vrf": "RED"}
434 result
= create_static_routes(tgen
, input_dict
)
435 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
439 step("Verify that zebra selects static route.")
442 for addr_type
in ADDR_TYPES
:
446 {"network": NETWORK
[addr_type
], "next_hop": "Null0", "vrf": "RED"}
450 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
451 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
455 step("Configure static route with admin distance of 253")
456 for addr_type
in ADDR_TYPES
:
457 # Create Static routes
462 "network": NETWORK
[addr_type
],
464 "admin_distance": 253,
471 result
= create_static_routes(tgen
, input_dict
)
472 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
476 step("Verify that zebra selects bgp route.")
479 for addr_type
in ADDR_TYPES
:
484 "network": NETWORK
[addr_type
],
486 "admin_distance": 253,
492 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
493 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
497 step("Configure admin distance of 254 in bgp for route .")
507 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
512 "distance": {"ebgp": 254, "ibgp": 254, "local": 254}
521 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
522 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
524 step("Verify that zebra selects static route.")
527 for addr_type
in ADDR_TYPES
:
532 "network": NETWORK
[addr_type
],
534 "admin_distance": 253,
541 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
542 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
546 step("Configure admin distance of 255 in bgp for route in vrf red")
556 "distance": {"ebgp": 255, "ibgp": 255, "local": 255}
561 "distance": {"ebgp": 255, "ibgp": 255, "local": 255}
570 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
571 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
573 step("Verify that zebra selects static route.")
576 for addr_type
in ADDR_TYPES
:
581 "network": NETWORK
[addr_type
],
583 "admin_distance": 253,
590 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
591 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
595 step("Delete the static route.")
596 for addr_type
in ADDR_TYPES
:
597 # Create Static routes
602 "network": NETWORK
[addr_type
],
604 "admin_distance": 253,
612 result
= create_static_routes(tgen
, input_dict
)
613 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
617 step("Verify that zebra selects bgp route.")
620 for addr_type
in ADDR_TYPES
:
621 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
622 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
626 write_test_footer(tc_name
)
629 def test_bgp_admin_distance_ebgp_with_imported_rtes_vrf_p0():
632 Verify bgp admin distance functionality when static route is configured
633 same as bgp learnt route in user vrf.
636 global bgp_convergence
638 if bgp_convergence
is not True:
639 pytest
.skip("skipping test case because of BGP Convergence failure at setup")
642 tc_name
= inspect
.stack()[0][3]
643 write_test_header(tc_name
)
644 if tgen
.routers_have_failure():
645 check_router_status(tgen
)
647 step("Configure base config as per the topology")
648 reset_config_on_routers(tgen
)
649 step("Configure bgp admin distance 200 with CLI in dut.")
650 step(" Import route from vrf to default vrf")
660 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
665 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
675 "distance": {"ebgp": 200, "ibgp": 200, "local": 200},
676 "import": {"vrf": "RED"},
681 "distance": {"ebgp": 200, "ibgp": 200, "local": 200},
693 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
694 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
696 step("Verify bgp routes have admin distance of 200 in dut.")
697 # Verifying best path
699 attribute
= "admin_distance"
706 "network": NETWORK
["ipv4"][0],
707 "admin_distance": 200,
711 "network": NETWORK
["ipv4"][1],
712 "admin_distance": 200,
722 "network": NETWORK
["ipv6"][0],
723 "admin_distance": 200,
727 "network": NETWORK
["ipv6"][1],
728 "admin_distance": 200,
736 for addr_type
in ADDR_TYPES
:
737 result
= verify_best_path_as_per_admin_distance(
738 tgen
, addr_type
, dut
, input_dict
[addr_type
], attribute
, vrf
="RED"
740 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
745 "Verify that routes are getting imported without any issues and "
746 "routes are calculated and installed in rib."
754 "network": NETWORK
["ipv4"][0],
755 "admin_distance": 200,
758 "network": NETWORK
["ipv4"][1],
759 "admin_distance": 200,
768 "network": NETWORK
["ipv6"][0],
769 "admin_distance": 200,
772 "network": NETWORK
["ipv6"][1],
773 "admin_distance": 200,
780 step("Verify that zebra selects bgp route.")
783 for addr_type
in ADDR_TYPES
:
784 result4
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
785 assert result4
is True, "Testcase {} : Failed \n Error: {}".format(
789 step(" Un configure import route vrf red inside default vrf.")
799 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
804 "distance": {"ebgp": 200, "ibgp": 200, "local": 200}
814 "distance": {"ebgp": 200, "ibgp": 200, "local": 200},
815 "import": {"vrf": "RED", "delete": True},
820 "distance": {"ebgp": 200, "ibgp": 200, "local": 200},
821 "import": {"vrf": "RED", "delete": True},
830 result
= create_router_bgp(tgen
, topo
, input_dict_1
)
831 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
838 "network": NETWORK
["ipv4"][0],
839 "admin_distance": 200,
842 "network": NETWORK
["ipv4"][1],
843 "admin_distance": 200,
852 "network": NETWORK
["ipv6"][0],
853 "admin_distance": 200,
856 "network": NETWORK
["ipv6"][1],
857 "admin_distance": 200,
864 step("Verify that route withdrawal happens properly.")
867 for addr_type
in ADDR_TYPES
:
868 result4
= verify_rib(
872 input_dict
[addr_type
],
878 ), "Testcase {} : Failed \n Route is not withdrawn. Error: {}".format(
882 write_test_footer(tc_name
)
885 if __name__
== "__main__":
886 args
= ["-s"] + sys
.argv
[1:]
887 sys
.exit(pytest
.main(args
))