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 dynamically import routes
24 from default vrf to non-default vrf with route map by adding AS by as-prepend command.
25 2. Verify the BGP Local AS functionality by adding new AS when dynamically import routes
26 from non-default vrf to default vrf and further advertised to eBGP peers.
34 # Save the Current Working Directory to find configuration files.
35 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
36 sys
.path
.append(os
.path
.join(CWD
, "../"))
37 sys
.path
.append(os
.path
.join(CWD
, "../lib/"))
39 # pylint: disable=C0413
40 # Import topogen and topotest helpers
41 from lib
.topogen
import Topogen
, get_topogen
42 from lib
.topotest
import version_cmp
44 from lib
.common_config
import (
49 reset_config_on_routers
,
59 from lib
.topolog
import logger
61 verify_bgp_convergence
,
65 from lib
.topojson
import build_config_from_json
67 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
70 BGP_CONVERGENCE
= False
71 ADDR_TYPES
= check_address_types()
72 NETWORK
= {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
73 NEXT_HOP_IP
= {"ipv4": "Null0", "ipv6": "Null0"}
76 def setup_module(mod
):
78 Sets up the pytest environment
83 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
84 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
87 logger
.info("Running setup_module to create topology")
89 # This function initiates the topology build with Topogen...
90 json_file
= "{}/bgp_local_asn_vrf_topo1.json".format(CWD
)
91 tgen
= Topogen(json_file
, mod
.__name
__)
94 # ... and here it calls Mininet initialization functions.
96 # Starting topology, create tmp files which are loaded to routers
97 # to start daemons and then start routers
100 # Creating configuration from JSON
101 build_config_from_json(tgen
, topo
)
103 global BGP_CONVERGENCE
105 ADDR_TYPES
= check_address_types()
107 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
108 assert BGP_CONVERGENCE
is True, "setup_module : Failed \n Error: {}".format(
112 logger
.info("Running setup_module() done")
115 def teardown_module():
116 """Teardown the pytest environment"""
118 logger
.info("Running teardown_module to delete topology")
122 # Stop toplogy and Remove tmp files
126 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
128 logger
.info("=" * 40)
131 ################################################################################################
135 ###############################################################################################
138 def test_verify_local_asn_ipv4_import_from_default_to_non_default_VRF_p0(request
):
140 Verify the BGP Local AS functionality by adding new AS when dynamically import routes
141 from default vrf to non-default vrf with route map by adding AS by as-prepend command.
144 global BGP_CONVERGENCE
145 if BGP_CONVERGENCE
!= True:
146 pytest
.skip("skipped because of BGP Convergence failure")
149 tc_name
= request
.node
.name
150 write_test_header(tc_name
)
151 if tgen
.routers_have_failure():
152 check_router_status(tgen
)
153 reset_config_on_routers(tgen
)
155 step("Base config is done as part of JSON")
157 # configure static routes
158 step("Advertise prefix 10.0.0.1/32 from Router-1(AS-100).")
159 step("Advertise an ipv6 prefix 10::1/128 from Router-1(AS-100).")
161 for addr_type
in ADDR_TYPES
:
162 # Enable static routes
163 input_dict_static_route
= {
166 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
171 logger
.info("Configure static routes")
172 result
= create_static_routes(tgen
, input_dict_static_route
)
173 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
177 step("configure redistribute static in Router BGP in R2")
178 input_dict_static_route_redist
= {
184 "unicast": {"redistribute": [{"redist_type": "static"}]}
191 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
192 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
196 step("Configure import vrf BLUE on R3 under IPv4 Address Family")
197 input_import_vrf_ipv4
= {
204 "ipv4": {"unicast": {"import": {"vrf": "default"}}},
205 "ipv6": {"unicast": {"import": {"vrf": "default"}}},
211 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
212 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
214 step("Verify IPv4 and IPv6 static routes received on R2")
215 for addr_type
in ADDR_TYPES
:
216 input_dict_static_route
= {
219 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
223 result
= verify_rib(tgen
, addr_type
, "r2", input_dict_static_route
)
224 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
227 result
= verify_bgp_rib(tgen
, addr_type
, "r2", input_dict_static_route
)
228 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
231 result
= verify_fib_routes(tgen
, addr_type
, "r2", input_dict_static_route
)
232 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
236 step("Configure local-as at R3 towards R2.")
237 for addr_type
in ADDR_TYPES
:
238 input_dict_r3_to_r2
= {
249 "r3": {"local_asn": {"local_as": "110"}}
260 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
261 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
265 step("Configure local-as at R3 towards R4.")
266 for addr_type
in ADDR_TYPES
:
267 input_dict_r3_to_r4
= {
279 "r3": {"local_asn": {"local_as": "110"}}
290 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
291 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
295 step("Configure remote-as at R2 towards R3.")
296 for addr_type
in ADDR_TYPES
:
297 input_dict_r2_to_r3
= {
309 "local_asn": {"remote_as": "110"}
321 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
322 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
326 step("Configure remote-as at R4 towards R3.")
327 for addr_type
in ADDR_TYPES
:
328 input_dict_r4_to_r3
= {
341 "local_asn": {"remote_as": "110"}
353 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
354 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
358 step("BGP neighborship is verified by following commands in R3 routers")
359 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
360 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
364 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
365 for addr_type
in ADDR_TYPES
:
366 static_routes_input
= {
370 "network": NETWORK
[addr_type
],
371 "next_hop": NEXT_HOP_IP
[addr_type
],
377 for dut
in ["r3", "r4"]:
378 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
379 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
383 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
384 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
389 "Verify that AS-110 is got added in the AS list 110 200 100 by following "
390 " commands at R3 router."
394 for addr_type
in ADDR_TYPES
:
396 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
398 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
399 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
403 step("Configure a route-map on R3 to prepend AS 2 times.")
404 for addr_type
in ADDR_TYPES
:
408 "ASP_{}".format(addr_type
): [
412 "path": {"as_num": "1000 1000", "as_action": "prepend"}
419 result
= create_route_maps(tgen
, input_dict_4
)
420 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
424 step("configure route map in out direction on R4")
425 # Configure neighbor for route map
440 "name": "ASP_{}".format(
457 result
= create_router_bgp(tgen
, topo
, input_dict_7
)
458 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
462 step("Configure local-as with no-prepend at R3 towards R2.")
463 for addr_type
in ADDR_TYPES
:
464 input_dict_no_prep_r3_to_r2
= {
491 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
492 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
496 step("Configure local-as with no-prepend at R3 towards R4.")
497 for addr_type
in ADDR_TYPES
:
498 input_dict_no_prep_r3_to_r4
= {
526 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
527 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
531 step("BGP neighborship is verified by following commands in R3 routers")
532 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
533 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
539 for addr_type
in ADDR_TYPES
:
541 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
543 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
544 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
548 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
549 for addr_type
in ADDR_TYPES
:
550 input_dict_no_prep_rep_as_r3_to_r2
= {
578 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
579 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
583 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
584 for addr_type
in ADDR_TYPES
:
585 input_dict_no_prep_rep_as_r3_to_r4
= {
614 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
615 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
619 step("BGP neighborship is verified by following commands in R3 routers")
620 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
621 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
626 aspath
= "110 1000 1000 200"
627 for addr_type
in ADDR_TYPES
:
629 "r2": {"static_routes": [{"network": NETWORK
[addr_type
], "vrf": "BLUE"}]}
631 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
632 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
636 write_test_footer(tc_name
)
639 def test_verify_local_asn_ipv4_import_from_non_default_to_default_VRF_p0(request
):
641 Verify the BGP Local AS functionality by adding new AS when dynamically import routes
642 from non-default vrf to default vrf and further advertised to eBGP peers.
645 global BGP_CONVERGENCE
646 if BGP_CONVERGENCE
!= True:
647 pytest
.skip("skipped because of BGP Convergence failure")
650 tc_name
= request
.node
.name
651 write_test_header(tc_name
)
652 if tgen
.routers_have_failure():
653 check_router_status(tgen
)
654 reset_config_on_routers(tgen
)
656 step("Resetting the config from JSON")
657 reset_config_on_routers(tgen
)
659 step("Base config is done as part of JSON")
660 # configure static routes
661 step("Advertise prefix 10.0.0.1/32 from Router-1(AS-100).")
662 step("Advertise an ipv6 prefix 10::1/128 from Router-1(AS-100).")
664 for addr_type
in ADDR_TYPES
:
665 # Enable static routes
666 input_dict_static_route
= {
670 "network": NETWORK
[addr_type
],
671 "next_hop": NEXT_HOP_IP
[addr_type
],
678 logger
.info("Configure static routes")
679 result
= create_static_routes(tgen
, input_dict_static_route
)
680 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
684 step("configure redistribute static in Router BGP in R4")
685 input_dict_static_route_redist
= {
694 {"redist_type": "static"},
695 {"redist_type": "connected"},
702 {"redist_type": "static"},
703 {"redist_type": "connected"},
711 result
= create_router_bgp(tgen
, topo
, input_dict_static_route_redist
)
712 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
715 "Configure import from BLUE vrf to default vrf on R3 under IPv4 Address Family"
717 input_import_vrf_ipv4
= {
724 "ipv4": {"unicast": {"import": {"vrf": "BLUE"}}},
725 "ipv6": {"unicast": {"import": {"vrf": "BLUE"}}},
731 result
= create_router_bgp(tgen
, topo
, input_import_vrf_ipv4
)
732 assert result
is True, "Testcase {} : Failed \n Error: {}".format(tc_name
, result
)
734 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
735 for addr_type
in ADDR_TYPES
:
736 static_routes_input
= {
740 "network": NETWORK
[addr_type
],
741 "next_hop": NEXT_HOP_IP
[addr_type
],
747 for dut
in ["r3", "r4"]:
748 result
= verify_fib_routes(tgen
, addr_type
, dut
, static_routes_input
)
749 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
753 result
= verify_bgp_rib(tgen
, addr_type
, dut
, static_routes_input
)
754 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
758 step("Configure local-as at R3 towards R2.")
759 for addr_type
in ADDR_TYPES
:
760 input_dict_r3_to_r2
= {
771 "r3": {"local_asn": {"local_as": "110"}}
782 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r2
)
783 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
787 step("Configure local-as at R3 towards R4.")
788 for addr_type
in ADDR_TYPES
:
789 input_dict_r3_to_r4
= {
801 "r3": {"local_asn": {"local_as": "110"}}
812 result
= create_router_bgp(tgen
, topo
, input_dict_r3_to_r4
)
813 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
817 step("Configure remote-as at R2 towards R3.")
818 for addr_type
in ADDR_TYPES
:
819 input_dict_r2_to_r3
= {
831 "local_asn": {"remote_as": "110"}
843 result
= create_router_bgp(tgen
, topo
, input_dict_r2_to_r3
)
844 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
848 step("Configure remote-as at R4 towards R3.")
849 for addr_type
in ADDR_TYPES
:
850 input_dict_r4_to_r3
= {
863 "local_asn": {"remote_as": "110"}
875 result
= create_router_bgp(tgen
, topo
, input_dict_r4_to_r3
)
876 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
880 step("BGP neighborship is verified by following commands in R3 routers")
881 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
882 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
886 step("Verify IPv4 and IPv6 static routes received on R2")
887 for addr_type
in ADDR_TYPES
:
888 input_dict_static_route_from_r4
= {
892 "network": NETWORK
[addr_type
],
893 "next_hop": NEXT_HOP_IP
[addr_type
],
898 result
= verify_rib(tgen
, addr_type
, "r2", input_dict_static_route_from_r4
)
899 assert result
is True, "Testcase {}: Failed \n Error: {}".format(
902 result
= verify_bgp_rib(tgen
, addr_type
, "r2", input_dict_static_route_from_r4
)
903 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
906 result
= verify_fib_routes(
907 tgen
, addr_type
, "r2", input_dict_static_route_from_r4
909 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
914 "Verify that AS-110 is got added in the AS list 110 400 by following "
915 " commands at R3 router."
919 for addr_type
in ADDR_TYPES
:
920 input_static_r2
= {"r4": {"static_routes": [{"network": NETWORK
[addr_type
]}]}}
921 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
922 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
926 step("Configure local-as with no-prepend at R3 towards R2.")
927 for addr_type
in ADDR_TYPES
:
928 input_dict_no_prep_r3_to_r2
= {
955 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r2
)
956 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
960 step("Configure local-as with no-prepend at R3 towards R4.")
961 for addr_type
in ADDR_TYPES
:
962 input_dict_no_prep_r3_to_r4
= {
990 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_r3_to_r4
)
991 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
995 step("BGP neighborship is verified by following commands in R3 routers")
996 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
997 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
1002 for addr_type
in ADDR_TYPES
:
1003 input_static_r2
= {"r4": {"static_routes": [{"network": NETWORK
[addr_type
]}]}}
1004 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
1005 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
1009 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
1010 for addr_type
in ADDR_TYPES
:
1011 input_dict_no_prep_rep_as_r3_to_r2
= {
1039 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r2
)
1040 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
1044 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
1045 for addr_type
in ADDR_TYPES
:
1046 input_dict_no_prep_rep_as_r3_to_r4
= {
1075 result
= create_router_bgp(tgen
, topo
, input_dict_no_prep_rep_as_r3_to_r4
)
1076 assert result
is True, "Testcase {} :Failed \n Error: {}".format(
1080 step("BGP neighborship is verified by following commands in R3 routers")
1081 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
1082 assert BGP_CONVERGENCE
is True, "BGP convergence :Failed \n Error: {}".format(
1088 for addr_type
in ADDR_TYPES
:
1093 "network": NETWORK
[addr_type
],
1098 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_static_r2
, aspath
=aspath
)
1099 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
1103 write_test_footer(tc_name
)
1106 if __name__
== "__main__":
1107 args
= ["-s"] + sys
.argv
[1:]
1108 sys
.exit(pytest
.main(args
))