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")
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 deamons 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, "Testcase {} : Failed \n ".format(
322 ) + " Routes still present in R3 router. Error: {}".format(result
)
325 tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
, expected
=False
329 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
333 step("Remove and Add no advertise community")
334 # Configure neighbor for route map
347 "name": "rmap_match_pf_1_"
363 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
364 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
369 "After removing no advertise community from BGP neighbor "
370 "static and connected router got advertised to R3 and "
371 "removing route-map, verify route using show ip bgp"
375 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
378 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
382 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
385 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
389 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
390 topo1
= deepcopy(topo
)
392 topo1
["routers"]["r1"]["bgp"]["local_as"] = "100"
393 topo1
["routers"]["r2"]["bgp"]["local_as"] = "100"
394 topo1
["routers"]["r3"]["bgp"]["local_as"] = "100"
396 for rtr
in ["r1", "r2", "r3"]:
397 if "bgp" in topo1
["routers"][rtr
].keys():
398 delete_bgp
= {rtr
: {"bgp": {"delete": True}}}
399 result
= create_router_bgp(tgen
, topo1
, delete_bgp
)
400 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
404 rtr
: {"bgp": {"local_as": topo1
["routers"][rtr
]["bgp"]["local_as"]}}
406 result
= create_router_bgp(tgen
, topo1
, config_bgp
)
407 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
411 build_config_from_json(tgen
, topo1
, save_bkup
=False)
413 step("verify bgp convergence before starting test case")
415 bgp_convergence
= verify_bgp_convergence(tgen
, topo1
)
416 assert bgp_convergence
is True, "Testcase {} : Failed \n Error: {}".format(
417 tc_name
, bgp_convergence
420 # configure static routes
424 for addr_type
in ADDR_TYPES
:
425 # Enable static routes
429 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
434 logger
.info("Configure static routes")
435 result
= create_static_routes(tgen
, input_dict
)
436 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
440 step("configure redistribute static and connected in Router " "BGP in R1")
449 {"redist_type": "static"},
450 {"redist_type": "connected"},
458 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
459 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
464 "BGP neighbors are up, static and connected route advertised from"
465 " R1 are present on R2 BGP table and RIB using show ip bgp and "
469 "Static and connected route advertised from R1 are present on R3"
470 " BGP table and RIB using show ip bgp and show ip route"
475 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
476 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
480 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
481 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
485 step("Configure prefix list P1 on R2 to permit route coming from R1")
486 # Create ip prefix list
491 "pf_list_1_{}".format(addr_type
): [
492 {"seqid": 10, "network": "any", "action": "permit"}
498 result
= create_prefix_lists(tgen
, input_dict_2
)
499 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
507 "rmap_match_pf_1_{}".format(addr_type
): [
512 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
514 "set": {"community": {"num": "no-advertise"}},
520 result
= create_route_maps(tgen
, input_dict_3
)
521 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
525 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
526 " advertise community"
529 # Configure neighbor for route map
542 "name": "rmap_match_pf_1_"
557 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
558 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
563 "After advertising no advertise community to BGP neighbor "
564 "static and connected router got removed from R3 verify using "
565 "show ip bgp & show ip route"
568 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
571 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
575 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
578 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
582 step("Remove and Add no advertise community")
583 # Configure neighbor for route map
596 "name": "rmap_match_pf_1_"
612 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
613 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
618 "After removing no advertise community from BGP neighbor "
619 "static and connected router got advertised to R3 and "
620 "removing route verify using show ip bgp and "
624 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
627 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
631 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
634 ), "Testcase {} : Failed \n Routes still present in R3 router. Error: {}".format(
638 write_test_footer(tc_name
)
641 if __name__
== "__main__":
642 args
= ["-s"] + sys
.argv
[1:]
643 sys
.exit(pytest
.main(args
))