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
35 # Save the Current Working Directory to find configuration files.
36 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
37 sys
.path
.append(os
.path
.join(CWD
, "../"))
39 # pylint: disable=C0413
40 # Import topogen and topotest helpers
41 from lib
.micronet_compat
import Topo
42 from lib
.topogen
import Topogen
, get_topogen
44 # Import topoJson from lib, to create topology and initial configuration
45 from lib
.common_config
import (
49 reset_config_on_routers
,
57 required_linux_kernel_version
,
59 from lib
.topolog
import logger
61 verify_bgp_convergence
,
66 from lib
.topojson
import build_topo_from_json
, build_config_from_json
67 from copy
import deepcopy
69 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
72 # Reading the data from JSON File for topology creation
73 jsonFile
= "{}/bgp_communities.json".format(CWD
)
75 with
open(jsonFile
, "r") as topoJson
:
76 topo
= json
.load(topoJson
)
78 assert False, "Could not read file {}".format(jsonFile
)
81 BGP_CONVERGENCE
= False
82 ADDR_TYPES
= check_address_types()
83 NETWORK
= {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
87 class BGPCOMMUNITIES(Topo
):
89 Test BGPCOMMUNITIES - topology 1
91 * `Topo`: Topology object
94 def build(self
, *_args
, **_opts
):
96 tgen
= get_topogen(self
)
98 # Building topology from json file
99 build_topo_from_json(tgen
, topo
)
102 def setup_module(mod
):
104 Sets up the pytest environment
109 # Required linux kernel version for this suite to run.
110 result
= required_linux_kernel_version("4.15")
111 if result
is not True:
112 pytest
.skip("Kernel requirements are not met")
114 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
115 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
116 logger
.info("=" * 40)
118 logger
.info("Running setup_module to create topology")
120 # This function initiates the topology build with Topogen...
121 tgen
= Topogen(BGPCOMMUNITIES
, mod
.__name
__)
122 # ... and here it calls Mininet initialization functions.
124 # Starting topology, create tmp files which are loaded to routers
125 # to start deamons and then start routers
128 # Creating configuration from JSON
129 build_config_from_json(tgen
, topo
)
131 # Checking BGP convergence
132 global BGP_CONVERGENCE
135 # Don't run this test if we have any failure.
136 if tgen
.routers_have_failure():
137 pytest
.skip(tgen
.errors
)
139 # Api call verify whether BGP is converged
140 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
141 assert BGP_CONVERGENCE
is True, "setup_module :Failed \n Error:" " {}".format(
145 logger
.info("Running setup_module() done")
148 def teardown_module(mod
):
150 Teardown the pytest environment
155 logger
.info("Running teardown_module to delete topology")
159 # Stop toplogy and Remove tmp files
163 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
165 logger
.info("=" * 40)
168 #####################################################
172 #####################################################
175 def test_bgp_no_advertise_community_p0(request
):
177 Verify routes are not advertised when NO-ADVERTISE Community is applied
181 tc_name
= request
.node
.name
182 write_test_header(tc_name
)
184 reset_config_on_routers(tgen
)
186 # Don't run this test if we have any failure.
187 if tgen
.routers_have_failure():
188 pytest
.skip(tgen
.errors
)
191 "ipv4": topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
192 "ipv6": topo
["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
195 # configure static routes
199 for addr_type
in ADDR_TYPES
:
200 # Enable static routes
204 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
209 logger
.info("Configure static routes")
210 result
= create_static_routes(tgen
, input_dict
)
211 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
215 step("configure redistribute static and connected in Router BGP " "in R1")
224 {"redist_type": "static"},
225 {"redist_type": "connected"},
233 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
234 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
239 "BGP neighbors are up, static and connected route advertised from"
240 " R1 are present on R2 BGP table and RIB using show ip bgp and "
244 "Static and connected route advertised from R1 are present on R3"
245 " BGP table and RIB using show ip bgp and show ip route"
250 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
251 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
255 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
256 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
260 step("Configure prefix list P1 on R2 to permit route coming from R1")
261 # Create ip prefix list
266 "pf_list_1_{}".format(addr_type
): [
267 {"seqid": 10, "network": "any", "action": "permit"}
273 result
= create_prefix_lists(tgen
, input_dict_2
)
274 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
282 "rmap_match_pf_1_{}".format(addr_type
): [
287 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
289 "set": {"community": {"num": "no-advertise"}},
295 result
= create_route_maps(tgen
, input_dict_3
)
296 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
300 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
301 " advertise community"
303 # Configure neighbor for route map
316 "name": "rmap_match_pf_1_"
331 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
332 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
337 "After advertising no advertise community to BGP neighbor "
338 "static and connected router got removed from R3 verify using "
339 "show ip bgp & show ip route"
342 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
, expected
=False)
343 assert result
is not True, "Testcase {} : Failed \n "
344 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
347 tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
, expected
=False
349 assert result
is not True, "Testcase {} : Failed \n "
350 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
352 step("Remove and Add no advertise community")
353 # Configure neighbor for route map
366 "name": "rmap_match_pf_1_"
382 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
383 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
388 "After removing no advertise community from BGP neighbor "
389 "static and connected router got advertised to R3 and "
390 "removing route-map, verify route using show ip bgp"
394 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
395 assert result
is True, "Testcase {} : Failed \n "
396 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
398 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
399 assert result
is True, "Testcase {} : Failed \n "
400 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
402 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
403 topo1
= deepcopy(topo
)
405 topo1
["routers"]["r1"]["bgp"]["local_as"] = "100"
406 topo1
["routers"]["r2"]["bgp"]["local_as"] = "100"
407 topo1
["routers"]["r3"]["bgp"]["local_as"] = "100"
409 for rtr
in ["r1", "r2", "r3"]:
410 if "bgp" in topo1
["routers"][rtr
].keys():
411 delete_bgp
= {rtr
: {"bgp": {"delete": True}}}
412 result
= create_router_bgp(tgen
, topo1
, delete_bgp
)
413 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
417 rtr
: {"bgp": {"local_as": topo1
["routers"][rtr
]["bgp"]["local_as"]}}
419 result
= create_router_bgp(tgen
, topo1
, config_bgp
)
420 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
424 build_config_from_json(tgen
, topo1
, save_bkup
=False)
426 step("verify bgp convergence before starting test case")
428 bgp_convergence
= verify_bgp_convergence(tgen
, topo1
)
429 assert bgp_convergence
is True, "Testcase {} : Failed \n Error: {}".format(
430 tc_name
, bgp_convergence
433 # configure static routes
437 for addr_type
in ADDR_TYPES
:
438 # Enable static routes
442 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
447 logger
.info("Configure static routes")
448 result
= create_static_routes(tgen
, input_dict
)
449 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
453 step("configure redistribute static and connected in Router " "BGP in R1")
462 {"redist_type": "static"},
463 {"redist_type": "connected"},
471 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
472 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
477 "BGP neighbors are up, static and connected route advertised from"
478 " R1 are present on R2 BGP table and RIB using show ip bgp and "
482 "Static and connected route advertised from R1 are present on R3"
483 " BGP table and RIB using show ip bgp and show ip route"
488 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
489 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
493 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
494 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
498 step("Configure prefix list P1 on R2 to permit route coming from R1")
499 # Create ip prefix list
504 "pf_list_1_{}".format(addr_type
): [
505 {"seqid": 10, "network": "any", "action": "permit"}
511 result
= create_prefix_lists(tgen
, input_dict_2
)
512 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
520 "rmap_match_pf_1_{}".format(addr_type
): [
525 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
527 "set": {"community": {"num": "no-advertise"}},
533 result
= create_route_maps(tgen
, input_dict_3
)
534 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
538 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
539 " advertise community"
542 # Configure neighbor for route map
555 "name": "rmap_match_pf_1_"
570 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
571 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
576 "After advertising no advertise community to BGP neighbor "
577 "static and connected router got removed from R3 verify using "
578 "show ip bgp & show ip route"
581 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
582 assert result
is True, "Testcase {} : Failed \n "
583 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
585 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
586 assert result
is True, "Testcase {} : Failed \n "
587 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
589 step("Remove and Add no advertise community")
590 # Configure neighbor for route map
603 "name": "rmap_match_pf_1_"
619 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
620 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
625 "After removing no advertise community from BGP neighbor "
626 "static and connected router got advertised to R3 and "
627 "removing route verify using show ip bgp and "
631 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
632 assert result
is True, "Testcase {} : Failed \n "
633 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
635 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
636 assert result
is True, "Testcase {} : Failed \n "
637 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
639 write_test_footer(tc_name
)
642 if __name__
== "__main__":
643 args
= ["-s"] + sys
.argv
[1:]
644 sys
.exit(pytest
.main(args
))