3 # Copyright (c) 2022 by VMware, Inc. ("VMware")
4 # Used Copyright (c) 2018 by Network Device Education Foundation,
5 # Inc. ("NetDEF") in this file.
7 # Permission to use, copy, modify, and/or distribute this software
8 # for any purpose with or without fee is hereby granted, provided
9 # that the above copyright notice and this permission notice appear
12 # THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 1. Verify the BGP Local AS functionality by adding new AS when leaking routes
24 from non-default VRF to non-default with route map by prefix lists.
32 # Save the Current Working Directory to find configuration files.
33 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
34 sys
.path
.append(os
.path
.join(CWD
, "../"))
35 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
37 # pylint: disable=C0413
38 # Import topogen and topotest helpers
39 from lib
.topogen
import Topogen
, get_topogen
40 from lib
.topotest
import version_cmp
42 from lib
.common_config
import (
47 reset_config_on_routers
,
58 from lib
.topolog
import logger
60 verify_bgp_convergence
,
64 from lib
.topojson
import build_config_from_json
66 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
69 BGP_CONVERGENCE
= False
70 ADDR_TYPES
= check_address_types()
71 NETWORK
= {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
72 NEXT_HOP_IP
= {"ipv4": "Null0", "ipv6": "Null0"}
75 def setup_module(mod
):
77 Sets up the pytest environment
82 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
83 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
86 logger
.info("Running setup_module to create topology")
88 # This function initiates the topology build with Topogen...
89 json_file
= "{}/bgp_local_asn_vrf_topo2.json".format(CWD
)
90 tgen
= Topogen(json_file
, mod
.__name
__)
93 # ... and here it calls Mininet initialization functions.
95 # Starting topology, create tmp files which are loaded to routers
96 # to start daemons and then start routers
99 # Creating configuration from JSON
100 build_config_from_json(tgen
, topo
)
102 global BGP_CONVERGENCE
104 ADDR_TYPES
= check_address_types()
106 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
107 assert BGP_CONVERGENCE
is True, "setup_module : Failed \n Error: {}".format(
111 logger
.info("Running setup_module() done")
114 def teardown_module():
115 """Teardown the pytest environment"""
117 logger
.info("Running teardown_module to delete topology")
121 # Stop toplogy and Remove tmp files
125 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
127 logger
.info("=" * 40)
130 ################################################################################################
134 ###############################################################################################
137 def test_verify_local_asn_ipv4_import_from_non_default_to_non_default_VRF_p0(request
):
139 Verify the BGP Local AS functionality by adding new AS when leaking routes
140 from non-default VRF to non-default with route map by prefix lists.
143 global BGP_CONVERGENCE
144 if BGP_CONVERGENCE
!= True:
145 pytest
.skip("skipped because of BGP Convergence failure")
148 tc_name
= request
.node
.name
149 write_test_header(tc_name
)
150 if tgen
.routers_have_failure():
151 check_router_status(tgen
)
152 reset_config_on_routers(tgen
)
154 step("Base config is done as part of JSON")
156 # configure static routes
157 step("Advertise prefix 10.1.1.0/32 from Router-1(AS-100).")
158 step("Advertise an ipv6 prefix 10:1::1:0/128 from Router-1(AS-100).")
160 for addr_type
in ADDR_TYPES
:
161 # Enable static routes
162 input_dict_static_route
= {
166 "network": NETWORK
[addr_type
],
167 "next_hop": NEXT_HOP_IP
[addr_type
],
174 logger
.info("Configure static routes")
175 result
= create_static_routes(tgen
, input_dict_static_route
)
176 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
180 step("configure redistribute static in Router BGP in R2")
181 input_dict_static_route_redist
= {
190 {"redist_type": "static"},
191 {"redist_type": "connected"},
198 {"redist_type": "static"},
199 {"redist_type": "connected"},
207 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
208 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
210 step("Configure import vrf BLUE from vrf RED on R3 under IPv4 Address Family")
211 input_import_vrf_ipv4
= {
218 "ipv4": {"unicast": {"import": {"vrf": "RED"}}},
219 "ipv6": {"unicast": {"import": {"vrf": "RED"}}},
225 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
226 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
228 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
229 for addr_type
in ADDR_TYPES
:
230 static_routes_input
= {
234 "network": NETWORK
[addr_type
],
235 "next_hop": NEXT_HOP_IP
[addr_type
],
241 for dut
in ["r3", "r4"]:
242 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
243 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
246 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
247 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
251 step("Configure local-as at R3 towards R2.")
252 for addr_type
in ADDR_TYPES
:
253 input_dict_r3_to_r2
= {
255 "vrfs": [{"name": "RED", "id": "1"}],
266 "r3": {"local_asn": {"local_as": "110"}}
277 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
278 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
282 step("Configure local-as at R3 towards R4.")
283 for addr_type
in ADDR_TYPES
:
284 input_dict_r3_to_r4
= {
286 "vrfs": [{"name": "BLUE", "id": "1"}],
297 "r3": {"local_asn": {"local_as": "110"}}
308 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
309 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
313 step("Configure remote-as at R2 towards R3.")
314 for addr_type
in ADDR_TYPES
:
315 input_dict_r2_to_r3
= {
317 "vrfs": [{"name": "RED", "id": "1"}],
329 "local_asn": {"remote_as": "110"}
341 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
342 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
346 step("Configure remote-as at R4 towards R3.")
347 for addr_type
in ADDR_TYPES
:
348 input_dict_r4_to_r3
= {
350 "vrfs": [{"name": "BLUE", "id": "1"}],
362 "local_asn": {"remote_as": "110"}
374 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
375 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
379 step("BGP neighborship is verified by following commands in R3 routers")
380 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
381 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
385 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
386 for addr_type
in ADDR_TYPES
:
387 static_routes_input
= {
391 "network": NETWORK
[addr_type
],
392 "next_hop": NEXT_HOP_IP
[addr_type
],
398 for dut
in ["r3", "r4"]:
399 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
400 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
404 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
405 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
410 "Verify that AS-110 is got added in the AS list 110 200 100 by following "
411 " commands at R3 router."
415 for addr_type
in ADDR_TYPES
:
417 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
419 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
420 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
424 # configure the prefix-list and route-maps.
425 for adt
in ADDR_TYPES
:
426 # Create Static routes
431 "network": NETWORK
[adt
],
433 "next_hop": NEXT_HOP_IP
[adt
],
440 result
= create_static_routes(tgen
, input_dict_rm1
)
441 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
445 # Api call to redistribute static routes
454 {"redist_type": "static"},
455 {"redist_type": "connected"},
462 {"redist_type": "static"},
463 {"redist_type": "connected"},
472 result
= create_router_bgp(tgen
, topo
, input_dict_rm_rd
)
473 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
485 "network": NETWORK
["ipv4"],
494 "network": NETWORK
["ipv6"],
501 result
= create_prefix_lists(tgen
, input_dict_rm_pl
)
502 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
507 for addr_type
in ADDR_TYPES
:
511 "rmap_match_tag_1_{}".format(addr_type
): [
516 "prefix_lists": "pf_list_1_{}".format(addr_type
)
521 "rmap_match_tag_2_{}".format(addr_type
): [
526 "prefix_lists": "pf_list_2_{}".format(addr_type
)
534 result
= create_route_maps(tgen
, input_dict_rm_r3
)
535 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
539 # Configure neighbor for route map
540 input_dict_conf_neighbor_rm
= {
555 "name": "rmap_match_tag_1_ipv4",
559 "name": "rmap_match_tag_1_ipv4",
577 "name": "rmap_match_tag_1_ipv6",
581 "name": "rmap_match_tag_1_ipv6",
597 result
= create_router_bgp(tgen
, topo
, input_dict_conf_neighbor_rm
)
598 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
602 for adt
in ADDR_TYPES
:
603 # Verifying RIB routes
605 input_dict_r2_rib
= {
609 "network": [NETWORK
[adt
]],
611 "next_hop": NEXT_HOP_IP
[adt
],
617 result
= verify_rib(tgen
, adt
, dut
, input_dict_r2_rib
)
618 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
622 # Verifying RIB routes
624 input_dict_r2_rib
= {
628 "network": [NETWORK
[adt
]],
630 "next_hop": NEXT_HOP_IP
[adt
],
636 result
= verify_rib(tgen
, adt
, dut
, input_dict_r2_rib
)
637 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
641 step("Configure local-as with no-prepend at R3 towards R2.")
642 for addr_type
in ADDR_TYPES
:
643 input_dict_no_prep_r3_to_r2
= {
645 "vrfs": [{"name": "RED", "id": "1"}],
672 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
673 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
677 step("Configure local-as with no-prepend at R3 towards R4.")
678 for addr_type
in ADDR_TYPES
:
679 input_dict_no_prep_r3_to_r4
= {
681 "vrfs": [{"name": "BLUE", "id": "1"}],
708 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
709 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
713 step("BGP neighborship is verified by following commands in R3 routers")
714 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
715 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
721 for addr_type
in ADDR_TYPES
:
723 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
725 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
726 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
730 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
731 for addr_type
in ADDR_TYPES
:
732 input_dict_no_prep_rep_as_r3_to_r2
= {
734 "vrfs": [{"name": "RED", "id": "1"}],
762 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
763 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
767 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
768 for addr_type
in ADDR_TYPES
:
769 input_dict_no_prep_rep_as_r3_to_r4
= {
771 "vrfs": [{"name": "BLUE", "id": "1"}],
799 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
800 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
804 step("BGP neighborship is verified by following commands in R3 routers")
805 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
806 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
812 for addr_type
in ADDR_TYPES
:
814 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
816 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
817 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
821 write_test_footer(tc_name
)
824 if __name__
== "__main__":
825 args
= ["-s"] + sys
.argv
[1:]
826 sys
.exit(pytest
.main(args
))