4 # Copyright (c) 2020 by VMware, Inc. ("VMware")
5 # Used Copyright (c) 2018 by Network Device Education Foundation,
6 # Inc. ("NetDEF") in this file.
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
13 # THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 Following tests are covered to test bgp community functionality:
25 - Verify routes are not advertised when NO-ADVERTISE Community is applied
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
, "../"))
38 # pylint: disable=C0413
39 # Import topogen and topotest helpers
40 from lib
.topogen
import Topogen
, get_topogen
42 # Import topoJson from lib, to create topology and initial configuration
43 from lib
.common_config
import (
47 reset_config_on_routers
,
55 required_linux_kernel_version
,
57 from lib
.topolog
import logger
59 verify_bgp_convergence
,
63 from lib
.topojson
import build_config_from_json
64 from copy
import deepcopy
66 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
70 BGP_CONVERGENCE
= False
71 ADDR_TYPES
= check_address_types()
72 NETWORK
= {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
76 def setup_module(mod
):
78 Sets up the pytest environment
83 # Required linux kernel version for this suite to run.
84 result
= required_linux_kernel_version("4.15")
85 if result
is not True:
86 pytest
.skip("Kernel requirements are not met, kernel version should be >= 4.15")
88 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
89 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
92 logger
.info("Running setup_module to create topology")
94 # This function initiates the topology build with Topogen...
95 json_file
= "{}/bgp_communities.json".format(CWD
)
96 tgen
= Topogen(json_file
, mod
.__name
__)
99 # ... and here it calls Mininet initialization functions.
101 # Starting topology, create tmp files which are loaded to routers
102 # to start daemons and then start routers
105 # Creating configuration from JSON
106 build_config_from_json(tgen
, topo
)
108 # Checking BGP convergence
109 global BGP_CONVERGENCE
112 # Don't run this test if we have any failure.
113 if tgen
.routers_have_failure():
114 pytest
.skip(tgen
.errors
)
116 # Api call verify whether BGP is converged
117 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
118 assert BGP_CONVERGENCE
is True, "setup_module :Failed \n Error:" " {}".format(
122 logger
.info("Running setup_module() done")
125 def teardown_module(mod
):
127 Teardown the pytest environment
132 logger
.info("Running teardown_module to delete topology")
136 # Stop toplogy and Remove tmp files
140 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
142 logger
.info("=" * 40)
145 #####################################################
149 #####################################################
152 def test_bgp_no_advertise_community_p0(request
):
154 Verify routes are not advertised when NO-ADVERTISE Community is applied
158 tc_name
= request
.node
.name
159 write_test_header(tc_name
)
161 reset_config_on_routers(tgen
)
163 # Don't run this test if we have any failure.
164 if tgen
.routers_have_failure():
165 pytest
.skip(tgen
.errors
)
168 "ipv4": topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
169 "ipv6": topo
["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
172 # configure static routes
176 for addr_type
in ADDR_TYPES
:
177 # Enable static routes
181 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
186 logger
.info("Configure static routes")
187 result
= create_static_routes(tgen
, input_dict
)
188 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
192 step("configure redistribute static and connected in Router BGP " "in R1")
201 {"redist_type": "static"},
202 {"redist_type": "connected"},
210 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
211 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
216 "BGP neighbors are up, static and connected route advertised from"
217 " R1 are present on R2 BGP table and RIB using show ip bgp and "
221 "Static and connected route advertised from R1 are present on R3"
222 " BGP table and RIB using show ip bgp and show ip route"
227 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
228 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
232 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
233 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
237 step("Configure prefix list P1 on R2 to permit route coming from R1")
238 # Create ip prefix list
243 "pf_list_1_{}".format(addr_type
): [
244 {"seqid": 10, "network": "any", "action": "permit"}
250 result
= create_prefix_lists(tgen
, input_dict_2
)
251 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
259 "rmap_match_pf_1_{}".format(addr_type
): [
264 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
266 "set": {"community": {"num": "no-advertise"}},
272 result
= create_route_maps(tgen
, input_dict_3
)
273 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
277 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
278 " advertise community"
280 # Configure neighbor for route map
293 "name": "rmap_match_pf_1_"
308 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
309 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
314 "After advertising no advertise community to BGP neighbor "
315 "static and connected router got removed from R3 verify using "
316 "show ip bgp & show ip route"
319 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
, expected
=False)
320 assert result
is not True, (
321 "Testcase {} : Failed \n Expected: "
322 "Routes still present in {} router. Found: {}".format(tc_name
, dut
, result
)
326 tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
, expected
=False
330 ), "Testcase {} : Failed \n Expected: Routes still present in {} router. Found: {}".format(
334 step("Remove and Add no advertise community")
335 # Configure neighbor for route map
348 "name": "rmap_match_pf_1_"
364 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
365 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
370 "After removing no advertise community from BGP neighbor "
371 "static and connected router got advertised to R3 and "
372 "removing route-map, verify route using show ip bgp"
376 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
379 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
383 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
386 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
390 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
391 topo1
= deepcopy(topo
)
393 topo1
["routers"]["r1"]["bgp"]["local_as"] = "100"
394 topo1
["routers"]["r2"]["bgp"]["local_as"] = "100"
395 topo1
["routers"]["r3"]["bgp"]["local_as"] = "100"
397 for rtr
in ["r1", "r2", "r3"]:
398 if "bgp" in topo1
["routers"][rtr
].keys():
399 delete_bgp
= {rtr
: {"bgp": {"delete": True}}}
400 result
= create_router_bgp(tgen
, topo1
, delete_bgp
)
401 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
405 rtr
: {"bgp": {"local_as": topo1
["routers"][rtr
]["bgp"]["local_as"]}}
407 result
= create_router_bgp(tgen
, topo1
, config_bgp
)
408 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
412 build_config_from_json(tgen
, topo1
, save_bkup
=False)
414 step("verify bgp convergence before starting test case")
416 bgp_convergence
= verify_bgp_convergence(tgen
, topo1
)
417 assert bgp_convergence
is True, "Testcase {} : Failed \n Error: {}".format(
418 tc_name
, bgp_convergence
421 # configure static routes
425 for addr_type
in ADDR_TYPES
:
426 # Enable static routes
430 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
435 logger
.info("Configure static routes")
436 result
= create_static_routes(tgen
, input_dict
)
437 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
441 step("configure redistribute static and connected in Router " "BGP in R1")
450 {"redist_type": "static"},
451 {"redist_type": "connected"},
459 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
460 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
465 "BGP neighbors are up, static and connected route advertised from"
466 " R1 are present on R2 BGP table and RIB using show ip bgp and "
470 "Static and connected route advertised from R1 are present on R3"
471 " BGP table and RIB using show ip bgp and show ip route"
476 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
477 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
481 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
482 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
486 step("Configure prefix list P1 on R2 to permit route coming from R1")
487 # Create ip prefix list
492 "pf_list_1_{}".format(addr_type
): [
493 {"seqid": 10, "network": "any", "action": "permit"}
499 result
= create_prefix_lists(tgen
, input_dict_2
)
500 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
508 "rmap_match_pf_1_{}".format(addr_type
): [
513 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
515 "set": {"community": {"num": "no-advertise"}},
521 result
= create_route_maps(tgen
, input_dict_3
)
522 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
526 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
527 " advertise community"
530 # Configure neighbor for route map
543 "name": "rmap_match_pf_1_"
558 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
559 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
564 "After advertising no advertise community to BGP neighbor "
565 "static and connected router got removed from R3 verify using "
566 "show ip bgp & show ip route"
569 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
572 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
576 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
579 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
583 step("Remove and Add no advertise community")
584 # Configure neighbor for route map
597 "name": "rmap_match_pf_1_"
613 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
614 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
619 "After removing no advertise community from BGP neighbor "
620 "static and connected router got advertised to R3 and "
621 "removing route verify using show ip bgp and "
625 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
628 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
632 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
635 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
639 write_test_footer(tc_name
)
642 if __name__
== "__main__":
643 args
= ["-s"] + sys
.argv
[1:]
644 sys
.exit(pytest
.main(args
))