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 leaking routes
11 from non-default VRF to non-default with route map by prefix lists.
19 # Save the Current Working Directory to find configuration files.
20 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
21 sys
.path
.append(os
.path
.join(CWD
, "../"))
22 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
24 # pylint: disable=C0413
25 # Import topogen and topotest helpers
26 from lib
.topogen
import Topogen
, get_topogen
27 from lib
.topotest
import version_cmp
29 from lib
.common_config
import (
34 reset_config_on_routers
,
45 from lib
.topolog
import logger
47 verify_bgp_convergence
,
51 from lib
.topojson
import build_config_from_json
53 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
56 BGP_CONVERGENCE
= False
57 ADDR_TYPES
= check_address_types()
58 NETWORK
= {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
59 NEXT_HOP_IP
= {"ipv4": "Null0", "ipv6": "Null0"}
62 def setup_module(mod
):
64 Sets up the pytest environment
69 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
70 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
73 logger
.info("Running setup_module to create topology")
75 # This function initiates the topology build with Topogen...
76 json_file
= "{}/bgp_local_asn_vrf_topo2.json".format(CWD
)
77 tgen
= Topogen(json_file
, mod
.__name
__)
80 # ... and here it calls Mininet initialization functions.
82 # Starting topology, create tmp files which are loaded to routers
83 # to start daemons and then start routers
86 # Creating configuration from JSON
87 build_config_from_json(tgen
, topo
)
89 global BGP_CONVERGENCE
91 ADDR_TYPES
= check_address_types()
93 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
94 assert BGP_CONVERGENCE
is True, "setup_module : Failed \n Error: {}".format(
98 logger
.info("Running setup_module() done")
101 def teardown_module():
102 """Teardown the pytest environment"""
104 logger
.info("Running teardown_module to delete topology")
108 # Stop toplogy and Remove tmp files
112 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
114 logger
.info("=" * 40)
117 ################################################################################################
121 ###############################################################################################
124 def test_verify_local_asn_ipv4_import_from_non_default_to_non_default_VRF_p0(request
):
126 Verify the BGP Local AS functionality by adding new AS when leaking routes
127 from non-default VRF to non-default with route map by prefix lists.
130 global BGP_CONVERGENCE
131 if BGP_CONVERGENCE
!= True:
132 pytest
.skip("skipped because of BGP Convergence failure")
135 tc_name
= request
.node
.name
136 write_test_header(tc_name
)
137 if tgen
.routers_have_failure():
138 check_router_status(tgen
)
139 reset_config_on_routers(tgen
)
141 step("Base config is done as part of JSON")
143 # configure static routes
144 step("Advertise prefix 10.1.1.0/32 from Router-1(AS-100).")
145 step("Advertise an ipv6 prefix 10:1::1:0/128 from Router-1(AS-100).")
147 for addr_type
in ADDR_TYPES
:
148 # Enable static routes
149 input_dict_static_route
= {
153 "network": NETWORK
[addr_type
],
154 "next_hop": NEXT_HOP_IP
[addr_type
],
161 logger
.info("Configure static routes")
162 result
= create_static_routes(tgen
, input_dict_static_route
)
163 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
167 step("configure redistribute static in Router BGP in R2")
168 input_dict_static_route_redist
= {
177 {"redist_type": "static"},
178 {"redist_type": "connected"},
185 {"redist_type": "static"},
186 {"redist_type": "connected"},
194 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
195 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
197 step("Configure import vrf BLUE from vrf RED on R3 under IPv4 Address Family")
198 input_import_vrf_ipv4
= {
205 "ipv4": {"unicast": {"import": {"vrf": "RED"}}},
206 "ipv6": {"unicast": {"import": {"vrf": "RED"}}},
212 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
213 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
215 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
216 for addr_type
in ADDR_TYPES
:
217 static_routes_input
= {
221 "network": NETWORK
[addr_type
],
222 "next_hop": NEXT_HOP_IP
[addr_type
],
228 for dut
in ["r3", "r4"]:
229 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
230 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
233 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
234 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
238 step("Configure local-as at R3 towards R2.")
239 for addr_type
in ADDR_TYPES
:
240 input_dict_r3_to_r2
= {
242 "vrfs": [{"name": "RED", "id": "1"}],
253 "r3": {"local_asn": {"local_as": "110"}}
264 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
265 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
269 step("Configure local-as at R3 towards R4.")
270 for addr_type
in ADDR_TYPES
:
271 input_dict_r3_to_r4
= {
273 "vrfs": [{"name": "BLUE", "id": "1"}],
284 "r3": {"local_asn": {"local_as": "110"}}
295 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
296 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
300 step("Configure remote-as at R2 towards R3.")
301 for addr_type
in ADDR_TYPES
:
302 input_dict_r2_to_r3
= {
304 "vrfs": [{"name": "RED", "id": "1"}],
316 "local_asn": {"remote_as": "110"}
328 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
329 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
333 step("Configure remote-as at R4 towards R3.")
334 for addr_type
in ADDR_TYPES
:
335 input_dict_r4_to_r3
= {
337 "vrfs": [{"name": "BLUE", "id": "1"}],
349 "local_asn": {"remote_as": "110"}
361 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
362 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
366 step("BGP neighborship is verified by following commands in R3 routers")
367 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
368 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
372 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
373 for addr_type
in ADDR_TYPES
:
374 static_routes_input
= {
378 "network": NETWORK
[addr_type
],
379 "next_hop": NEXT_HOP_IP
[addr_type
],
385 for dut
in ["r3", "r4"]:
386 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
387 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
391 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
392 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
397 "Verify that AS-110 is got added in the AS list 110 200 100 by following "
398 " commands at R3 router."
402 for addr_type
in ADDR_TYPES
:
404 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
406 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
407 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
411 # configure the prefix-list and route-maps.
412 for adt
in ADDR_TYPES
:
413 # Create Static routes
418 "network": NETWORK
[adt
],
420 "next_hop": NEXT_HOP_IP
[adt
],
427 result
= create_static_routes(tgen
, input_dict_rm1
)
428 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
432 # Api call to redistribute static routes
441 {"redist_type": "static"},
442 {"redist_type": "connected"},
449 {"redist_type": "static"},
450 {"redist_type": "connected"},
459 result
= create_router_bgp(tgen
, topo
, input_dict_rm_rd
)
460 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
472 "network": NETWORK
["ipv4"],
481 "network": NETWORK
["ipv6"],
488 result
= create_prefix_lists(tgen
, input_dict_rm_pl
)
489 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
494 for addr_type
in ADDR_TYPES
:
498 "rmap_match_tag_1_{}".format(addr_type
): [
503 "prefix_lists": "pf_list_1_{}".format(addr_type
)
508 "rmap_match_tag_2_{}".format(addr_type
): [
513 "prefix_lists": "pf_list_2_{}".format(addr_type
)
521 result
= create_route_maps(tgen
, input_dict_rm_r3
)
522 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
526 # Configure neighbor for route map
527 input_dict_conf_neighbor_rm
= {
542 "name": "rmap_match_tag_1_ipv4",
546 "name": "rmap_match_tag_1_ipv4",
564 "name": "rmap_match_tag_1_ipv6",
568 "name": "rmap_match_tag_1_ipv6",
584 result
= create_router_bgp(tgen
, topo
, input_dict_conf_neighbor_rm
)
585 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
589 for adt
in ADDR_TYPES
:
590 # Verifying RIB routes
592 input_dict_r2_rib
= {
596 "network": [NETWORK
[adt
]],
598 "next_hop": NEXT_HOP_IP
[adt
],
604 result
= verify_rib(tgen
, adt
, dut
, input_dict_r2_rib
)
605 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
609 # Verifying RIB routes
611 input_dict_r2_rib
= {
615 "network": [NETWORK
[adt
]],
617 "next_hop": NEXT_HOP_IP
[adt
],
623 result
= verify_rib(tgen
, adt
, dut
, input_dict_r2_rib
)
624 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
628 step("Configure local-as with no-prepend at R3 towards R2.")
629 for addr_type
in ADDR_TYPES
:
630 input_dict_no_prep_r3_to_r2
= {
632 "vrfs": [{"name": "RED", "id": "1"}],
659 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
660 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
664 step("Configure local-as with no-prepend at R3 towards R4.")
665 for addr_type
in ADDR_TYPES
:
666 input_dict_no_prep_r3_to_r4
= {
668 "vrfs": [{"name": "BLUE", "id": "1"}],
695 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
696 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
700 step("BGP neighborship is verified by following commands in R3 routers")
701 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
702 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
708 for addr_type
in ADDR_TYPES
:
710 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
712 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
713 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
717 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
718 for addr_type
in ADDR_TYPES
:
719 input_dict_no_prep_rep_as_r3_to_r2
= {
721 "vrfs": [{"name": "RED", "id": "1"}],
749 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
750 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
754 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
755 for addr_type
in ADDR_TYPES
:
756 input_dict_no_prep_rep_as_r3_to_r4
= {
758 "vrfs": [{"name": "BLUE", "id": "1"}],
786 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
787 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
791 step("BGP neighborship is verified by following commands in R3 routers")
792 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
793 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
799 for addr_type
in ADDR_TYPES
:
801 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
803 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
804 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
808 write_test_footer(tc_name
)
811 if __name__
== "__main__":
812 args
= ["-s"] + sys
.argv
[1:]
813 sys
.exit(pytest
.main(args
))