2 # SPDX-License-Identifier: ISC
5 # Copyright (c) 2020 by VMware, Inc. ("VMware")
6 # Used Copyright (c) 2018 by Network Device Education Foundation,
7 # Inc. ("NetDEF") in this file.
11 Following tests are covered to test bgp community functionality:
12 - Verify routes are not advertised when NO-ADVERTISE Community is applied
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
, "../"))
25 # pylint: disable=C0413
26 # Import topogen and topotest helpers
27 from lib
.topogen
import Topogen
, get_topogen
29 # Import topoJson from lib, to create topology and initial configuration
30 from lib
.common_config
import (
34 reset_config_on_routers
,
42 required_linux_kernel_version
,
44 from lib
.topolog
import logger
46 verify_bgp_convergence
,
50 from lib
.topojson
import build_config_from_json
51 from copy
import deepcopy
53 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
57 BGP_CONVERGENCE
= False
58 ADDR_TYPES
= check_address_types()
59 NETWORK
= {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
63 def setup_module(mod
):
65 Sets up the pytest environment
70 # Required linux kernel version for this suite to run.
71 result
= required_linux_kernel_version("4.15")
72 if result
is not True:
73 pytest
.skip("Kernel requirements are not met, kernel version should be >= 4.15")
75 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
76 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
79 logger
.info("Running setup_module to create topology")
81 # This function initiates the topology build with Topogen...
82 json_file
= "{}/bgp_communities.json".format(CWD
)
83 tgen
= Topogen(json_file
, mod
.__name
__)
86 # ... and here it calls Mininet initialization functions.
88 # Starting topology, create tmp files which are loaded to routers
89 # to start daemons and then start routers
92 # Creating configuration from JSON
93 build_config_from_json(tgen
, topo
)
95 # Checking BGP convergence
96 global BGP_CONVERGENCE
99 # Don't run this test if we have any failure.
100 if tgen
.routers_have_failure():
101 pytest
.skip(tgen
.errors
)
103 # Api call verify whether BGP is converged
104 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
105 assert BGP_CONVERGENCE
is True, "setup_module :Failed \n Error:" " {}".format(
109 logger
.info("Running setup_module() done")
112 def teardown_module(mod
):
114 Teardown the pytest environment
119 logger
.info("Running teardown_module to delete topology")
123 # Stop toplogy and Remove tmp files
127 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
129 logger
.info("=" * 40)
132 #####################################################
136 #####################################################
139 def test_bgp_no_advertise_community_p0(request
):
141 Verify routes are not advertised when NO-ADVERTISE Community is applied
145 tc_name
= request
.node
.name
146 write_test_header(tc_name
)
148 reset_config_on_routers(tgen
)
150 # Don't run this test if we have any failure.
151 if tgen
.routers_have_failure():
152 pytest
.skip(tgen
.errors
)
155 "ipv4": topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
156 "ipv6": topo
["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
159 # configure static routes
163 for addr_type
in ADDR_TYPES
:
164 # Enable static routes
168 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
173 logger
.info("Configure static routes")
174 result
= create_static_routes(tgen
, input_dict
)
175 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
179 step("configure redistribute static and connected in Router BGP " "in R1")
188 {"redist_type": "static"},
189 {"redist_type": "connected"},
197 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
198 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
203 "BGP neighbors are up, static and connected route advertised from"
204 " R1 are present on R2 BGP table and RIB using show ip bgp and "
208 "Static and connected route advertised from R1 are present on R3"
209 " BGP table and RIB using show ip bgp and show ip route"
214 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
215 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
219 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
220 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
224 step("Configure prefix list P1 on R2 to permit route coming from R1")
225 # Create ip prefix list
230 "pf_list_1_{}".format(addr_type
): [
231 {"seqid": 10, "network": "any", "action": "permit"}
237 result
= create_prefix_lists(tgen
, input_dict_2
)
238 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
246 "rmap_match_pf_1_{}".format(addr_type
): [
251 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
253 "set": {"community": {"num": "no-advertise"}},
259 result
= create_route_maps(tgen
, input_dict_3
)
260 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
264 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
265 " advertise community"
267 # Configure neighbor for route map
280 "name": "rmap_match_pf_1_"
295 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
296 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
301 "After advertising no advertise community to BGP neighbor "
302 "static and connected router got removed from R3 verify using "
303 "show ip bgp & show ip route"
306 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
, expected
=False)
307 assert result
is not True, (
308 "Testcase {} : Failed \n Expected: "
309 "Routes still present in {} router. Found: {}".format(tc_name
, dut
, result
)
313 tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
, expected
=False
317 ), "Testcase {} : Failed \n Expected: Routes still present in {} router. Found: {}".format(
321 step("Remove and Add no advertise community")
322 # Configure neighbor for route map
335 "name": "rmap_match_pf_1_"
351 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
352 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
357 "After removing no advertise community from BGP neighbor "
358 "static and connected router got advertised to R3 and "
359 "removing route-map, verify route using show ip bgp"
363 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
366 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
370 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
373 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
377 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
378 topo1
= deepcopy(topo
)
380 topo1
["routers"]["r1"]["bgp"]["local_as"] = "100"
381 topo1
["routers"]["r2"]["bgp"]["local_as"] = "100"
382 topo1
["routers"]["r3"]["bgp"]["local_as"] = "100"
384 for rtr
in ["r1", "r2", "r3"]:
385 if "bgp" in topo1
["routers"][rtr
].keys():
386 delete_bgp
= {rtr
: {"bgp": {"delete": True}}}
387 result
= create_router_bgp(tgen
, topo1
, delete_bgp
)
388 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
392 rtr
: {"bgp": {"local_as": topo1
["routers"][rtr
]["bgp"]["local_as"]}}
394 result
= create_router_bgp(tgen
, topo1
, config_bgp
)
395 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
399 build_config_from_json(tgen
, topo1
, save_bkup
=False)
401 step("verify bgp convergence before starting test case")
403 bgp_convergence
= verify_bgp_convergence(tgen
, topo1
)
404 assert bgp_convergence
is True, "Testcase {} : Failed \n Error: {}".format(
405 tc_name
, bgp_convergence
408 # configure static routes
412 for addr_type
in ADDR_TYPES
:
413 # Enable static routes
417 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
422 logger
.info("Configure static routes")
423 result
= create_static_routes(tgen
, input_dict
)
424 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
428 step("configure redistribute static and connected in Router " "BGP in R1")
437 {"redist_type": "static"},
438 {"redist_type": "connected"},
446 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
447 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
452 "BGP neighbors are up, static and connected route advertised from"
453 " R1 are present on R2 BGP table and RIB using show ip bgp and "
457 "Static and connected route advertised from R1 are present on R3"
458 " BGP table and RIB using show ip bgp and show ip route"
463 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
464 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
468 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
469 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
473 step("Configure prefix list P1 on R2 to permit route coming from R1")
474 # Create ip prefix list
479 "pf_list_1_{}".format(addr_type
): [
480 {"seqid": 10, "network": "any", "action": "permit"}
486 result
= create_prefix_lists(tgen
, input_dict_2
)
487 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
495 "rmap_match_pf_1_{}".format(addr_type
): [
500 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
502 "set": {"community": {"num": "no-advertise"}},
508 result
= create_route_maps(tgen
, input_dict_3
)
509 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
513 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
514 " advertise community"
517 # Configure neighbor for route map
530 "name": "rmap_match_pf_1_"
545 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
546 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
551 "After advertising no advertise community to BGP neighbor "
552 "static and connected router got removed from R3 verify using "
553 "show ip bgp & show ip route"
556 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
559 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
563 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
566 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
570 step("Remove and Add no advertise community")
571 # Configure neighbor for route map
584 "name": "rmap_match_pf_1_"
600 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
601 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
606 "After removing no advertise community from BGP neighbor "
607 "static and connected router got advertised to R3 and "
608 "removing route verify using show ip bgp and "
612 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
615 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
619 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
622 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
626 write_test_footer(tc_name
)
629 if __name__
== "__main__":
630 args
= ["-s"] + sys
.argv
[1:]
631 sys
.exit(pytest
.main(args
))