2 # SPDX-License-Identifier: ISC
4 # Copyright (c) 2022 by VMware, Inc. ("VMware")
5 # Used Copyright (c) 2018 by Network Device Education Foundation,
6 # Inc. ("NetDEF") in this file.
10 1. Verify the BGP Local AS functionality by adding new AS when dynamically import routes
11 from default vrf to non-default vrf with route map by adding AS by as-prepend command.
12 2. Verify the BGP Local AS functionality by adding new AS when dynamically import routes
13 from non-default vrf to default vrf and further advertised to eBGP peers.
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 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
26 # pylint: disable=C0413
27 # Import topogen and topotest helpers
28 from lib
.topogen
import Topogen
, get_topogen
29 from lib
.topotest
import version_cmp
31 from lib
.common_config
import (
36 reset_config_on_routers
,
46 from lib
.topolog
import logger
48 verify_bgp_convergence
,
52 from lib
.topojson
import build_config_from_json
54 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
57 BGP_CONVERGENCE
= False
58 ADDR_TYPES
= check_address_types()
59 NETWORK
= {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
60 NEXT_HOP_IP
= {"ipv4": "Null0", "ipv6": "Null0"}
63 def setup_module(mod
):
65 Sets up the pytest environment
70 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
71 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
74 logger
.info("Running setup_module to create topology")
76 # This function initiates the topology build with Topogen...
77 json_file
= "{}/bgp_local_asn_vrf_topo1.json".format(CWD
)
78 tgen
= Topogen(json_file
, mod
.__name
__)
81 # ... and here it calls Mininet initialization functions.
83 # Starting topology, create tmp files which are loaded to routers
84 # to start daemons and then start routers
87 # Creating configuration from JSON
88 build_config_from_json(tgen
, topo
)
90 global BGP_CONVERGENCE
92 ADDR_TYPES
= check_address_types()
94 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
95 assert BGP_CONVERGENCE
is True, "setup_module : Failed \n Error: {}".format(
99 logger
.info("Running setup_module() done")
102 def teardown_module():
103 """Teardown the pytest environment"""
105 logger
.info("Running teardown_module to delete topology")
109 # Stop toplogy and Remove tmp files
113 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
115 logger
.info("=" * 40)
118 ################################################################################################
122 ###############################################################################################
125 def test_verify_local_asn_ipv4_import_from_default_to_non_default_VRF_p0(request
):
127 Verify the BGP Local AS functionality by adding new AS when dynamically import routes
128 from default vrf to non-default vrf with route map by adding AS by as-prepend command.
131 global BGP_CONVERGENCE
132 if BGP_CONVERGENCE
!= True:
133 pytest
.skip("skipped because of BGP Convergence failure")
136 tc_name
= request
.node
.name
137 write_test_header(tc_name
)
138 if tgen
.routers_have_failure():
139 check_router_status(tgen
)
140 reset_config_on_routers(tgen
)
142 step("Base config is done as part of JSON")
144 # configure static routes
145 step("Advertise prefix 10.0.0.1/32 from Router-1(AS-100).")
146 step("Advertise an ipv6 prefix 10::1/128 from Router-1(AS-100).")
148 for addr_type
in ADDR_TYPES
:
149 # Enable static routes
150 input_dict_static_route
= {
153 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
158 logger
.info("Configure static routes")
159 result
= create_static_routes(tgen
, input_dict_static_route
)
160 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
164 step("configure redistribute static in Router BGP in R2")
165 input_dict_static_route_redist
= {
171 "unicast": {"redistribute": [{"redist_type": "static"}]}
178 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
179 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
183 step("Configure import vrf BLUE on R3 under IPv4 Address Family")
184 input_import_vrf_ipv4
= {
191 "ipv4": {"unicast": {"import": {"vrf": "default"}}},
192 "ipv6": {"unicast": {"import": {"vrf": "default"}}},
198 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
199 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
201 step("Verify IPv4 and IPv6 static routes received on R2")
202 for addr_type
in ADDR_TYPES
:
203 input_dict_static_route
= {
206 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
210 result
= verify_rib(tgen
, addr_type
, "r2", input_dict_static_route
)
211 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
214 result
= verify_bgp_rib(tgen
, addr_type
, "r2", input_dict_static_route
)
215 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
218 result
= verify_fib_routes(tgen
, addr_type
, "r2", input_dict_static_route
)
219 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
223 step("Configure local-as at R3 towards R2.")
224 for addr_type
in ADDR_TYPES
:
225 input_dict_r3_to_r2
= {
236 "r3": {"local_asn": {"local_as": "110"}}
247 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
248 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
252 step("Configure local-as at R3 towards R4.")
253 for addr_type
in ADDR_TYPES
:
254 input_dict_r3_to_r4
= {
266 "r3": {"local_asn": {"local_as": "110"}}
277 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
278 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
282 step("Configure remote-as at R2 towards R3.")
283 for addr_type
in ADDR_TYPES
:
284 input_dict_r2_to_r3
= {
296 "local_asn": {"remote_as": "110"}
308 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
309 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
313 step("Configure remote-as at R4 towards R3.")
314 for addr_type
in ADDR_TYPES
:
315 input_dict_r4_to_r3
= {
328 "local_asn": {"remote_as": "110"}
340 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
341 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
345 step("BGP neighborship is verified by following commands in R3 routers")
346 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
347 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
351 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
352 for addr_type
in ADDR_TYPES
:
353 static_routes_input
= {
357 "network": NETWORK
[addr_type
],
358 "next_hop": NEXT_HOP_IP
[addr_type
],
364 for dut
in ["r3", "r4"]:
365 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
366 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
370 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
371 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
376 "Verify that AS-110 is got added in the AS list 110 200 100 by following "
377 " commands at R3 router."
381 for addr_type
in ADDR_TYPES
:
383 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
385 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
386 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
390 step("Configure a route-map on R3 to prepend AS 2 times.")
391 for addr_type
in ADDR_TYPES
:
395 "ASP_{}".format(addr_type
): [
399 "path": {"as_num": "1000 1000", "as_action": "prepend"}
406 result
= create_route_maps(tgen
, input_dict_4
)
407 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
411 step("configure route map in out direction on R4")
412 # Configure neighbor for route map
427 "name": "ASP_{}".format(
444 result
= create_router_bgp(tgen
, topo
, input_dict_7
)
445 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
449 step("Configure local-as with no-prepend at R3 towards R2.")
450 for addr_type
in ADDR_TYPES
:
451 input_dict_no_prep_r3_to_r2
= {
478 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
479 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
483 step("Configure local-as with no-prepend at R3 towards R4.")
484 for addr_type
in ADDR_TYPES
:
485 input_dict_no_prep_r3_to_r4
= {
513 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
514 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
518 step("BGP neighborship is verified by following commands in R3 routers")
519 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
520 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
526 for addr_type
in ADDR_TYPES
:
528 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
530 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
531 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
535 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
536 for addr_type
in ADDR_TYPES
:
537 input_dict_no_prep_rep_as_r3_to_r2
= {
565 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
566 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
570 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
571 for addr_type
in ADDR_TYPES
:
572 input_dict_no_prep_rep_as_r3_to_r4
= {
601 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
602 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
606 step("BGP neighborship is verified by following commands in R3 routers")
607 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
608 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
613 aspath
= "110 1000 1000 200"
614 for addr_type
in ADDR_TYPES
:
616 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
618 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
619 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
623 write_test_footer(tc_name
)
626 def test_verify_local_asn_ipv4_import_from_non_default_to_default_VRF_p0(request
):
628 Verify the BGP Local AS functionality by adding new AS when dynamically import routes
629 from non-default vrf to default vrf and further advertised to eBGP peers.
632 global BGP_CONVERGENCE
633 if BGP_CONVERGENCE
!= True:
634 pytest
.skip("skipped because of BGP Convergence failure")
637 tc_name
= request
.node
.name
638 write_test_header(tc_name
)
639 if tgen
.routers_have_failure():
640 check_router_status(tgen
)
641 reset_config_on_routers(tgen
)
643 step("Resetting the config from JSON")
644 reset_config_on_routers(tgen
)
646 step("Base config is done as part of JSON")
647 # configure static routes
648 step("Advertise prefix 10.0.0.1/32 from Router-1(AS-100).")
649 step("Advertise an ipv6 prefix 10::1/128 from Router-1(AS-100).")
651 for addr_type
in ADDR_TYPES
:
652 # Enable static routes
653 input_dict_static_route
= {
657 "network": NETWORK
[addr_type
],
658 "next_hop": NEXT_HOP_IP
[addr_type
],
665 logger
.info("Configure static routes")
666 result
= create_static_routes(tgen
, input_dict_static_route
)
667 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
671 step("configure redistribute static in Router BGP in R4")
672 input_dict_static_route_redist
= {
681 {"redist_type": "static"},
682 {"redist_type": "connected"},
689 {"redist_type": "static"},
690 {"redist_type": "connected"},
698 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
699 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
702 "Configure import from BLUE vrf to default vrf on R3 under IPv4 Address Family"
704 input_import_vrf_ipv4
= {
711 "ipv4": {"unicast": {"import": {"vrf": "BLUE"}}},
712 "ipv6": {"unicast": {"import": {"vrf": "BLUE"}}},
718 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
719 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
721 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
722 for addr_type
in ADDR_TYPES
:
723 static_routes_input
= {
727 "network": NETWORK
[addr_type
],
728 "next_hop": NEXT_HOP_IP
[addr_type
],
734 for dut
in ["r3", "r4"]:
735 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
736 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
740 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
741 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
745 step("Configure local-as at R3 towards R2.")
746 for addr_type
in ADDR_TYPES
:
747 input_dict_r3_to_r2
= {
758 "r3": {"local_asn": {"local_as": "110"}}
769 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
770 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
774 step("Configure local-as at R3 towards R4.")
775 for addr_type
in ADDR_TYPES
:
776 input_dict_r3_to_r4
= {
788 "r3": {"local_asn": {"local_as": "110"}}
799 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
800 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
804 step("Configure remote-as at R2 towards R3.")
805 for addr_type
in ADDR_TYPES
:
806 input_dict_r2_to_r3
= {
818 "local_asn": {"remote_as": "110"}
830 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
831 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
835 step("Configure remote-as at R4 towards R3.")
836 for addr_type
in ADDR_TYPES
:
837 input_dict_r4_to_r3
= {
850 "local_asn": {"remote_as": "110"}
862 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
863 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
867 step("BGP neighborship is verified by following commands in R3 routers")
868 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
869 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
873 step("Verify IPv4 and IPv6 static routes received on R2")
874 for addr_type
in ADDR_TYPES
:
875 input_dict_static_route_from_r4
= {
879 "network": NETWORK
[addr_type
],
880 "next_hop": NEXT_HOP_IP
[addr_type
],
885 result
= verify_rib(tgen
, addr_type
, "r2", input_dict_static_route_from_r4
)
886 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
889 result
= verify_bgp_rib(tgen
, addr_type
, "r2", input_dict_static_route_from_r4
)
890 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
893 result
= verify_fib_routes(
894 tgen
, addr_type
, "r2", input_dict_static_route_from_r4
896 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
901 "Verify that AS-110 is got added in the AS list 110 400 by following "
902 " commands at R3 router."
906 for addr_type
in ADDR_TYPES
:
907 input_static_r2
= {"r4": {"static_routes": [{"network": NETWORK
[addr_type
]}]}}
908 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
909 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
913 step("Configure local-as with no-prepend at R3 towards R2.")
914 for addr_type
in ADDR_TYPES
:
915 input_dict_no_prep_r3_to_r2
= {
942 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
943 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
947 step("Configure local-as with no-prepend at R3 towards R4.")
948 for addr_type
in ADDR_TYPES
:
949 input_dict_no_prep_r3_to_r4
= {
977 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
978 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
982 step("BGP neighborship is verified by following commands in R3 routers")
983 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
984 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
989 for addr_type
in ADDR_TYPES
:
990 input_static_r2
= {"r4": {"static_routes": [{"network": NETWORK
[addr_type
]}]}}
991 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
992 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
996 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
997 for addr_type
in ADDR_TYPES
:
998 input_dict_no_prep_rep_as_r3_to_r2
= {
1026 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
1027 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
1031 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
1032 for addr_type
in ADDR_TYPES
:
1033 input_dict_no_prep_rep_as_r3_to_r4
= {
1062 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
1063 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
1067 step("BGP neighborship is verified by following commands in R3 routers")
1068 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
1069 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
1075 for addr_type
in ADDR_TYPES
:
1080 "network": NETWORK
[addr_type
],
1085 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
1086 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
1090 write_test_footer(tc_name
)
1093 if __name__
== "__main__":
1094 args
= ["-s"] + sys
.argv
[1:]
1095 sys
.exit(pytest
.main(args
))