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 1. Verify that BGP well known communities work fine for
27 Well known communities tested: no-export, local-AS, internet
36 # Save the Current Working Directory to find configuration files.
37 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
38 sys
.path
.append(os
.path
.join(CWD
, "../"))
40 # pylint: disable=C0413
41 # Import topogen and topotest helpers
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
,
56 required_linux_kernel_version
,
59 from lib
.topolog
import logger
61 verify_bgp_convergence
,
66 from lib
.topojson
import build_config_from_json
68 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.staticd
]
72 BGP_CONVERGENCE
= False
73 ADDR_TYPES
= check_address_types()
75 "ipv4": ["192.0.2.1/32", "192.0.2.2/32"],
76 "ipv6": ["2001:DB8::1:1/128", "2001:DB8::1:2/128"],
80 def setup_module(mod
):
82 Sets up the pytest environment
87 # Required linux kernel version for this suite to run.
88 result
= required_linux_kernel_version("4.14")
89 if result
is not True:
90 pytest
.skip("Kernel requirements are not met, kernel version should be >= 4.14")
92 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
93 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
96 logger
.info("Running setup_module to create topology")
98 # This function initiates the topology build with Topogen...
99 json_file
= "{}/bgp_communities_topo2.json".format(CWD
)
100 tgen
= Topogen(json_file
, mod
.__name
__)
102 topo
= tgen
.json_topo
103 # ... and here it calls Mininet initialization functions.
105 # Starting topology, create tmp files which are loaded to routers
106 # to start daemons and then start routers
109 # Creating configuration from JSON
110 build_config_from_json(tgen
, topo
)
112 # Checking BGP convergence
113 global BGP_CONVERGENCE
116 # Don't run this test if we have any failure.
117 if tgen
.routers_have_failure():
118 pytest
.skip(tgen
.errors
)
120 # Api call verify whether BGP is converged
121 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
122 assert BGP_CONVERGENCE
is True, "setup_module :Failed \n Error:" " {}".format(
126 logger
.info("Running setup_module() done")
129 def teardown_module(mod
):
131 Teardown the pytest environment
136 logger
.info("Running teardown_module to delete topology")
140 # Stop toplogy and Remove tmp files
144 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
146 logger
.info("=" * 40)
149 #####################################################
153 #####################################################
156 def test_bgp_no_export_local_as_and_internet_communities_p0(request
):
158 Verify that BGP well known communities work fine for
160 Well known communities tested: no-export, local-AS, internet
163 tc_name
= request
.node
.name
164 write_test_header(tc_name
)
167 # Don"t run this test if we have any failure.
168 if tgen
.routers_have_failure():
169 pytest
.skip(tgen
.errors
)
171 step("Initial config: Configure BGP neighborship between R1 and R3.")
172 reset_config_on_routers(tgen
)
174 step("Configure static routes on R1 with next-hop as null0")
175 for addr_type
in ADDR_TYPES
:
178 "static_routes": [{"network": NETWORK
[addr_type
], "next_hop": "null0"}]
181 result
= create_static_routes(tgen
, input_dict_4
)
182 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
186 for comm_type
in ["no-export", "local-AS", "internet"]:
188 step("Create a route-map on R1 to set community as {}".format(comm_type
))
198 "set": {"community": {"num": "{}".format(comm_type
)}},
204 result
= create_route_maps(tgen
, input_rmap
)
205 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
209 step("Apply route-map while redistributing static routes into BGP")
218 "redist_type": "static",
219 "attribute": {"route-map": "rmap_wkc"},
228 "redist_type": "static",
229 "attribute": {"route-map": "rmap_wkc"},
238 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
240 step("Verify that BGP prefixes on R1 have community: {}".format(comm_type
))
241 input_dict_4
= {"community": "{}".format(comm_type
)}
242 for addr_type
in ADDR_TYPES
:
243 result
= verify_bgp_community(
244 tgen
, addr_type
, "r1", NETWORK
[addr_type
], input_dict_4
246 assert result
is True, "Test case {} : Should fail \n Error: {}".format(
250 for addr_type
in ADDR_TYPES
:
255 "network": NETWORK
[addr_type
],
256 "next_hop": topo
["routers"]["r2"]["links"]["r1"][
263 result
= verify_bgp_rib(
268 next_hop
=topo
["routers"]["r1"]["links"]["r2"][addr_type
].split("/")[0],
270 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
274 if comm_type
== "internet":
276 "Verify that these prefixes, originated on R1, are"
277 "received on both R2 and R3"
285 next_hop
=topo
["routers"]["r1"]["links"]["r3"][addr_type
].split("/")[
289 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
294 "Verify that these prefixes, originated on R1, are not"
295 "received on R3 but received on R2"
303 next_hop
=topo
["routers"]["r1"]["links"]["r3"][addr_type
].split("/")[
308 assert result
is not True, (
309 "Testcase {} : Failed \n "
310 "Expected: Routes are still present in rib of r3 \n "
311 "Found: {}".format(tc_name
, result
)
314 step("Remove route-map from redistribute static on R1")
322 {"redist_type": "static", "delete": True}
329 {"redist_type": "static", "delete": True}
337 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
338 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
342 step("Configure redistribute static")
348 "unicast": {"redistribute": [{"redist_type": "static"}]}
351 "unicast": {"redistribute": [{"redist_type": "static"}]}
357 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
358 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
363 "Verify that these prefixes, originated on R1, are now"
364 "received on both routers R2 and R3"
366 for addr_type
in ADDR_TYPES
:
371 "network": NETWORK
[addr_type
],
372 "next_hop": topo
["routers"]["r2"]["links"]["r1"][
379 result
= verify_bgp_rib(
384 next_hop
=topo
["routers"]["r1"]["links"]["r2"][addr_type
].split("/")[0],
386 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
390 result
= verify_bgp_rib(
395 next_hop
=topo
["routers"]["r1"]["links"]["r3"][addr_type
].split("/")[0],
397 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
401 write_test_footer(tc_name
)
404 if __name__
== "__main__":
405 args
= ["-s"] + sys
.argv
[1:]
406 sys
.exit(pytest
.main(args
))