]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_communities_topo1/test_bgp_communities.py
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 mininet
.topo
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
,
58 from lib
.topolog
import logger
60 verify_bgp_convergence
,
65 from lib
.topojson
import build_topo_from_json
, build_config_from_json
66 from copy
import deepcopy
68 # Reading the data from JSON File for topology creation
69 jsonFile
= "{}/bgp_communities.json".format(CWD
)
71 with
open(jsonFile
, "r") as topoJson
:
72 topo
= json
.load(topoJson
)
74 assert False, "Could not read file {}".format(jsonFile
)
77 BGP_CONVERGENCE
= False
78 ADDR_TYPES
= check_address_types()
79 NETWORK
= {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
83 class BGPCOMMUNITIES(Topo
):
85 Test BGPCOMMUNITIES - topology 1
87 * `Topo`: Topology object
90 def build(self
, *_args
, **_opts
):
92 tgen
= get_topogen(self
)
94 # Building topology from json file
95 build_topo_from_json(tgen
, topo
)
98 def setup_module(mod
):
100 Sets up the pytest environment
105 testsuite_run_time
= time
.asctime(time
.localtime(time
.time()))
106 logger
.info("Testsuite start time: {}".format(testsuite_run_time
))
107 logger
.info("=" * 40)
109 logger
.info("Running setup_module to create topology")
111 # This function initiates the topology build with Topogen...
112 tgen
= Topogen(BGPCOMMUNITIES
, mod
.__name
__)
113 # ... and here it calls Mininet initialization functions.
115 # Starting topology, create tmp files which are loaded to routers
116 # to start deamons and then start routers
119 # Creating configuration from JSON
120 build_config_from_json(tgen
, topo
)
122 # Checking BGP convergence
123 global BGP_CONVERGENCE
126 # Don't run this test if we have any failure.
127 if tgen
.routers_have_failure():
128 pytest
.skip(tgen
.errors
)
130 # Api call verify whether BGP is converged
131 BGP_CONVERGENCE
= verify_bgp_convergence(tgen
, topo
)
132 assert BGP_CONVERGENCE
is True, "setup_module :Failed \n Error:" " {}".format(
136 logger
.info("Running setup_module() done")
139 def teardown_module(mod
):
141 Teardown the pytest environment
146 logger
.info("Running teardown_module to delete topology")
150 # Stop toplogy and Remove tmp files
154 "Testsuite end time: {}".format(time
.asctime(time
.localtime(time
.time())))
156 logger
.info("=" * 40)
159 #####################################################
163 #####################################################
166 def test_bgp_no_advertise_community_p0(request
):
168 Verify routes are not advertised when NO-ADVERTISE Community is applied
172 tc_name
= request
.node
.name
173 write_test_header(tc_name
)
175 reset_config_on_routers(tgen
)
177 # Don't run this test if we have any failure.
178 if tgen
.routers_have_failure():
179 pytest
.skip(tgen
.errors
)
182 "ipv4": topo
["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
183 "ipv6": topo
["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
186 # configure static routes
190 for addr_type
in ADDR_TYPES
:
191 # Enable static routes
195 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
200 logger
.info("Configure static routes")
201 result
= create_static_routes(tgen
, input_dict
)
202 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
206 step("configure redistribute static and connected in Router BGP " "in R1")
215 {"redist_type": "static"},
216 {"redist_type": "connected"},
224 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
225 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
230 "BGP neighbors are up, static and connected route advertised from"
231 " R1 are present on R2 BGP table and RIB using show ip bgp and "
235 "Static and connected route advertised from R1 are present on R3"
236 " BGP table and RIB using show ip bgp and show ip route"
241 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
242 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
246 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
247 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
251 step("Configure prefix list P1 on R2 to permit route coming from R1")
252 # Create ip prefix list
257 "pf_list_1_{}".format(addr_type
): [
258 {"seqid": 10, "network": "any", "action": "permit"}
264 result
= create_prefix_lists(tgen
, input_dict_2
)
265 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
273 "rmap_match_pf_1_{}".format(addr_type
): [
278 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
280 "set": {"community": {"num": "no-advertise"}},
286 result
= create_route_maps(tgen
, input_dict_3
)
287 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
291 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
292 " advertise community"
294 # Configure neighbor for route map
307 "name": "rmap_match_pf_1_"
322 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
323 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
328 "After advertising no advertise community to BGP neighbor "
329 "static and connected router got removed from R3 verify using "
330 "show ip bgp & show ip route"
333 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
, expected
=False)
334 assert result
is not True, "Testcase {} : Failed \n "
335 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
338 tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
, expected
=False
340 assert result
is not True, "Testcase {} : Failed \n "
341 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
343 step("Remove and Add no advertise community")
344 # Configure neighbor for route map
357 "name": "rmap_match_pf_1_"
373 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
374 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
379 "After removing no advertise community from BGP neighbor "
380 "static and connected router got advertised to R3 and "
381 "removing route-map, verify route using show ip bgp"
385 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
386 assert result
is True, "Testcase {} : Failed \n "
387 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
389 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
390 assert result
is True, "Testcase {} : Failed \n "
391 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
393 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
394 topo1
= deepcopy(topo
)
396 topo1
["routers"]["r1"]["bgp"]["local_as"] = "100"
397 topo1
["routers"]["r2"]["bgp"]["local_as"] = "100"
398 topo1
["routers"]["r3"]["bgp"]["local_as"] = "100"
400 for rtr
in ["r1", "r2", "r3"]:
401 if "bgp" in topo1
["routers"][rtr
].keys():
402 delete_bgp
= {rtr
: {"bgp": {"delete": True}}}
403 result
= create_router_bgp(tgen
, topo1
, delete_bgp
)
404 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
408 rtr
: {"bgp": {"local_as": topo1
["routers"][rtr
]["bgp"]["local_as"]}}
410 result
= create_router_bgp(tgen
, topo1
, config_bgp
)
411 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
415 build_config_from_json(tgen
, topo1
, save_bkup
=False)
417 step("verify bgp convergence before starting test case")
419 bgp_convergence
= verify_bgp_convergence(tgen
, topo1
)
420 assert bgp_convergence
is True, "Testcase {} : Failed \n Error: {}".format(
421 tc_name
, bgp_convergence
424 # configure static routes
428 for addr_type
in ADDR_TYPES
:
429 # Enable static routes
433 {"network": NETWORK
[addr_type
], "next_hop": NEXT_HOP_IP
[addr_type
]}
438 logger
.info("Configure static routes")
439 result
= create_static_routes(tgen
, input_dict
)
440 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
444 step("configure redistribute static and connected in Router " "BGP in R1")
453 {"redist_type": "static"},
454 {"redist_type": "connected"},
462 result
= create_router_bgp(tgen
, topo
, input_dict_2
)
463 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
468 "BGP neighbors are up, static and connected route advertised from"
469 " R1 are present on R2 BGP table and RIB using show ip bgp and "
473 "Static and connected route advertised from R1 are present on R3"
474 " BGP table and RIB using show ip bgp and show ip route"
479 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
480 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
484 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
485 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
489 step("Configure prefix list P1 on R2 to permit route coming from R1")
490 # Create ip prefix list
495 "pf_list_1_{}".format(addr_type
): [
496 {"seqid": 10, "network": "any", "action": "permit"}
502 result
= create_prefix_lists(tgen
, input_dict_2
)
503 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
511 "rmap_match_pf_1_{}".format(addr_type
): [
516 addr_type
: {"prefix_lists": "pf_list_1_" + addr_type
}
518 "set": {"community": {"num": "no-advertise"}},
524 result
= create_route_maps(tgen
, input_dict_3
)
525 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
529 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
530 " advertise community"
533 # Configure neighbor for route map
546 "name": "rmap_match_pf_1_"
561 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
562 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
567 "After advertising no advertise community to BGP neighbor "
568 "static and connected router got removed from R3 verify using "
569 "show ip bgp & show ip route"
572 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
573 assert result
is True, "Testcase {} : Failed \n "
574 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
576 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
577 assert result
is True, "Testcase {} : Failed \n "
578 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
580 step("Remove and Add no advertise community")
581 # Configure neighbor for route map
594 "name": "rmap_match_pf_1_"
610 result
= create_router_bgp(tgen
, topo
, input_dict_4
)
611 assert result
is True, "Testcase {} : Failed \n Error: {}".format(
616 "After removing no advertise community from BGP neighbor "
617 "static and connected router got advertised to R3 and "
618 "removing route verify using show ip bgp and "
622 result
= verify_bgp_rib(tgen
, addr_type
, dut
, input_dict
)
623 assert result
is True, "Testcase {} : Failed \n "
624 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
626 result
= verify_rib(tgen
, addr_type
, dut
, input_dict
, protocol
=protocol
)
627 assert result
is True, "Testcase {} : Failed \n "
628 " Routes still present in R3 router. Error: {}".format(tc_name
, result
)
630 write_test_footer(tc_name
)
633 if __name__
== "__main__":
634 args
= ["-s"] + sys
.argv
[1:]
635 sys
.exit(pytest
.main(args
))