]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_communities_topo1/test_bgp_communities.py
tests: Adding new test suite bgp_communities_topo1
[mirror_frr.git] / tests / topotests / bgp_communities_topo1 / test_bgp_communities.py
1 #!/usr/bin/python
2
3 #
4 # Copyright (c) 2020 by VMware, Inc. ("VMware")
5 # Used Copyright (c) 2018 by Network Device Education Foundation,
6 # Inc. ("NetDEF") in this file.
7 #
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
11 # in all copies.
12 #
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
20 # OF THIS SOFTWARE.
21 #
22
23 """
24 Following tests are covered to test bgp community functionality:
25 - Verify routes are not advertised when NO-ADVERTISE Community is applied
26
27 """
28
29 import os
30 import sys
31 import time
32 import json
33 import pytest
34
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, "../"))
38
39 # pylint: disable=C0413
40 # Import topogen and topotest helpers
41 from mininet.topo import Topo
42 from lib.topogen import Topogen, get_topogen
43
44 # Import topoJson from lib, to create topology and initial configuration
45 from lib.common_config import (
46 start_topology,
47 write_test_header,
48 write_test_footer,
49 reset_config_on_routers,
50 verify_rib,
51 create_static_routes,
52 check_address_types,
53 step,
54 create_route_maps,
55 create_prefix_lists,
56 create_route_maps,
57 )
58 from lib.topolog import logger
59 from lib.bgp import (
60 verify_bgp_convergence,
61 create_router_bgp,
62 clear_bgp_and_verify,
63 verify_bgp_rib,
64 )
65 from lib.topojson import build_topo_from_json, build_config_from_json
66 from copy import deepcopy
67
68 # Reading the data from JSON File for topology creation
69 jsonFile = "{}/bgp_communities.json".format(CWD)
70 try:
71 with open(jsonFile, "r") as topoJson:
72 topo = json.load(topoJson)
73 except IOError:
74 assert False, "Could not read file {}".format(jsonFile)
75
76 # Global variables
77 BGP_CONVERGENCE = False
78 ADDR_TYPES = check_address_types()
79 NETWORK = {"ipv4": "2.2.2.2/32", "ipv6": "22:22::2/128"}
80 NEXT_HOP_IP = {}
81
82
83 class BGPCOMMUNITIES(Topo):
84 """
85 Test BGPCOMMUNITIES - topology 1
86
87 * `Topo`: Topology object
88 """
89
90 def build(self, *_args, **_opts):
91 """Build function"""
92 tgen = get_topogen(self)
93
94 # Building topology from json file
95 build_topo_from_json(tgen, topo)
96
97
98 def setup_module(mod):
99 """
100 Sets up the pytest environment
101
102 * `mod`: module name
103 """
104
105 testsuite_run_time = time.asctime(time.localtime(time.time()))
106 logger.info("Testsuite start time: {}".format(testsuite_run_time))
107 logger.info("=" * 40)
108
109 logger.info("Running setup_module to create topology")
110
111 # This function initiates the topology build with Topogen...
112 tgen = Topogen(BGPCOMMUNITIES, mod.__name__)
113 # ... and here it calls Mininet initialization functions.
114
115 # Starting topology, create tmp files which are loaded to routers
116 # to start deamons and then start routers
117 start_topology(tgen)
118
119 # Creating configuration from JSON
120 build_config_from_json(tgen, topo)
121
122 # Checking BGP convergence
123 global BGP_CONVERGENCE
124 global ADDR_TYPES
125
126 # Don't run this test if we have any failure.
127 if tgen.routers_have_failure():
128 pytest.skip(tgen.errors)
129
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(
133 BGP_CONVERGENCE
134 )
135
136 logger.info("Running setup_module() done")
137
138
139 def teardown_module(mod):
140 """
141 Teardown the pytest environment
142
143 * `mod`: module name
144 """
145
146 logger.info("Running teardown_module to delete topology")
147
148 tgen = get_topogen()
149
150 # Stop toplogy and Remove tmp files
151 tgen.stop_topology()
152
153 logger.info(
154 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
155 )
156 logger.info("=" * 40)
157
158
159 #####################################################
160 #
161 # Tests starting
162 #
163 #####################################################
164
165
166 def test_bgp_no_advertise_community_p0(request):
167 """
168 Verify routes are not advertised when NO-ADVERTISE Community is applied
169
170 """
171
172 tc_name = request.node.name
173 write_test_header(tc_name)
174 tgen = get_topogen()
175 reset_config_on_routers(tgen)
176
177 # Don't run this test if we have any failure.
178 if tgen.routers_have_failure():
179 pytest.skip(tgen.errors)
180
181 NEXT_HOP_IP = {
182 "ipv4": topo["routers"]["r0"]["links"]["r1"]["ipv4"].split("/")[0],
183 "ipv6": topo["routers"]["r0"]["links"]["r1"]["ipv6"].split("/")[0],
184 }
185
186 # configure static routes
187 dut = "r3"
188 protocol = "bgp"
189
190 for addr_type in ADDR_TYPES:
191 # Enable static routes
192 input_dict = {
193 "r1": {
194 "static_routes": [
195 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
196 ]
197 }
198 }
199
200 logger.info("Configure static routes")
201 result = create_static_routes(tgen, input_dict)
202 assert result is True, "Testcase {} : Failed \n Error: {}".format(
203 tc_name, result
204 )
205
206 step("configure redistribute static and connected in Router BGP " "in R1")
207
208 input_dict_2 = {
209 "r1": {
210 "bgp": {
211 "address_family": {
212 addr_type: {
213 "unicast": {
214 "redistribute": [
215 {"redist_type": "static"},
216 {"redist_type": "connected"},
217 ]
218 }
219 }
220 }
221 }
222 }
223 }
224 result = create_router_bgp(tgen, topo, input_dict_2)
225 assert result is True, "Testcase {} : Failed \n Error: {}".format(
226 tc_name, result
227 )
228
229 step(
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 "
232 " show ip route"
233 )
234 step(
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"
237 )
238
239 dut = "r3"
240 protocol = "bgp"
241 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
242 assert result is True, "Testcase {} : Failed \n Error: {}".format(
243 tc_name, result
244 )
245
246 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
247 assert result is True, "Testcase {} : Failed \n Error: {}".format(
248 tc_name, result
249 )
250
251 step("Configure prefix list P1 on R2 to permit route coming from R1")
252 # Create ip prefix list
253 input_dict_2 = {
254 "r2": {
255 "prefix_lists": {
256 addr_type: {
257 "pf_list_1_{}".format(addr_type): [
258 {"seqid": 10, "network": "any", "action": "permit"}
259 ]
260 }
261 }
262 }
263 }
264 result = create_prefix_lists(tgen, input_dict_2)
265 assert result is True, "Testcase {} : Failed \n Error: {}".format(
266 tc_name, result
267 )
268
269 # Create route map
270 input_dict_3 = {
271 "r2": {
272 "route_maps": {
273 "rmap_match_pf_1_{}".format(addr_type): [
274 {
275 "action": "permit",
276 "seq_id": "5",
277 "match": {
278 addr_type: {"prefix_lists": "pf_list_1_" + addr_type}
279 },
280 "set": {"community": {"num": "no-advertise"}},
281 }
282 ]
283 }
284 }
285 }
286 result = create_route_maps(tgen, input_dict_3)
287 assert result is True, "Testcase {} : Failed \n Error: {}".format(
288 tc_name, result
289 )
290 step(
291 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
292 " advertise community"
293 )
294 # Configure neighbor for route map
295 input_dict_4 = {
296 "r2": {
297 "bgp": {
298 "address_family": {
299 addr_type: {
300 "unicast": {
301 "neighbor": {
302 "r1": {
303 "dest_link": {
304 "r2": {
305 "route_maps": [
306 {
307 "name": "rmap_match_pf_1_"
308 + addr_type,
309 "direction": "in",
310 }
311 ]
312 }
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 }
321 }
322 result = create_router_bgp(tgen, topo, input_dict_4)
323 assert result is True, "Testcase {} : Failed \n Error: {}".format(
324 tc_name, result
325 )
326
327 step(
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"
331 )
332
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)
336
337 result = verify_rib(
338 tgen, addr_type, dut, input_dict, protocol=protocol, expected=False
339 )
340 assert result is not True, "Testcase {} : Failed \n "
341 " Routes still present in R3 router. Error: {}".format(tc_name, result)
342
343 step("Remove and Add no advertise community")
344 # Configure neighbor for route map
345 input_dict_4 = {
346 "r2": {
347 "bgp": {
348 "address_family": {
349 addr_type: {
350 "unicast": {
351 "neighbor": {
352 "r1": {
353 "dest_link": {
354 "r2": {
355 "route_maps": [
356 {
357 "name": "rmap_match_pf_1_"
358 + addr_type,
359 "direction": "in",
360 "delete": True,
361 }
362 ]
363 }
364 }
365 }
366 }
367 }
368 }
369 }
370 }
371 }
372 }
373 result = create_router_bgp(tgen, topo, input_dict_4)
374 assert result is True, "Testcase {} : Failed \n Error: {}".format(
375 tc_name, result
376 )
377
378 step(
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"
382 " and show ip route"
383 )
384
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)
388
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)
392
393 step("Repeat above steps when IBGP nbr configured between R1, R2 & R2, R3")
394 topo1 = deepcopy(topo)
395
396 topo1["routers"]["r1"]["bgp"]["local_as"] = "100"
397 topo1["routers"]["r2"]["bgp"]["local_as"] = "100"
398 topo1["routers"]["r3"]["bgp"]["local_as"] = "100"
399
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(
405 tc_name, result
406 )
407 config_bgp = {
408 rtr: {"bgp": {"local_as": topo1["routers"][rtr]["bgp"]["local_as"]}}
409 }
410 result = create_router_bgp(tgen, topo1, config_bgp)
411 assert result is True, "Testcase {} : Failed \n Error: {}".format(
412 tc_name, result
413 )
414
415 build_config_from_json(tgen, topo1, save_bkup=False)
416
417 step("verify bgp convergence before starting test case")
418
419 bgp_convergence = verify_bgp_convergence(tgen, topo1)
420 assert bgp_convergence is True, "Testcase {} : Failed \n Error: {}".format(
421 tc_name, bgp_convergence
422 )
423
424 # configure static routes
425 dut = "r3"
426 protocol = "bgp"
427
428 for addr_type in ADDR_TYPES:
429 # Enable static routes
430 input_dict = {
431 "r1": {
432 "static_routes": [
433 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
434 ]
435 }
436 }
437
438 logger.info("Configure static routes")
439 result = create_static_routes(tgen, input_dict)
440 assert result is True, "Testcase {} : Failed \n Error: {}".format(
441 tc_name, result
442 )
443
444 step("configure redistribute static and connected in Router " "BGP in R1")
445
446 input_dict_2 = {
447 "r1": {
448 "bgp": {
449 "address_family": {
450 addr_type: {
451 "unicast": {
452 "redistribute": [
453 {"redist_type": "static"},
454 {"redist_type": "connected"},
455 ]
456 }
457 }
458 }
459 }
460 }
461 }
462 result = create_router_bgp(tgen, topo, input_dict_2)
463 assert result is True, "Testcase {} : Failed \n Error: {}".format(
464 tc_name, result
465 )
466
467 step(
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 "
470 " show ip route"
471 )
472 step(
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"
475 )
476
477 dut = "r2"
478 protocol = "bgp"
479 result = verify_bgp_rib(tgen, addr_type, dut, input_dict)
480 assert result is True, "Testcase {} : Failed \n Error: {}".format(
481 tc_name, result
482 )
483
484 result = verify_rib(tgen, addr_type, dut, input_dict, protocol=protocol)
485 assert result is True, "Testcase {} : Failed \n Error: {}".format(
486 tc_name, result
487 )
488
489 step("Configure prefix list P1 on R2 to permit route coming from R1")
490 # Create ip prefix list
491 input_dict_2 = {
492 "r2": {
493 "prefix_lists": {
494 addr_type: {
495 "pf_list_1_{}".format(addr_type): [
496 {"seqid": 10, "network": "any", "action": "permit"}
497 ]
498 }
499 }
500 }
501 }
502 result = create_prefix_lists(tgen, input_dict_2)
503 assert result is True, "Testcase {} : Failed \n Error: {}".format(
504 tc_name, result
505 )
506
507 # Create route map
508 input_dict_3 = {
509 "r2": {
510 "route_maps": {
511 "rmap_match_pf_1_{}".format(addr_type): [
512 {
513 "action": "permit",
514 "seq_id": "5",
515 "match": {
516 addr_type: {"prefix_lists": "pf_list_1_" + addr_type}
517 },
518 "set": {"community": {"num": "no-advertise"}},
519 }
520 ]
521 }
522 }
523 }
524 result = create_route_maps(tgen, input_dict_3)
525 assert result is True, "Testcase {} : Failed \n Error: {}".format(
526 tc_name, result
527 )
528 step(
529 "Apply route-map RM1 on R2, R2 to R3 BGP neighbor with no"
530 " advertise community"
531 )
532
533 # Configure neighbor for route map
534 input_dict_4 = {
535 "r2": {
536 "bgp": {
537 "address_family": {
538 addr_type: {
539 "unicast": {
540 "neighbor": {
541 "r1": {
542 "dest_link": {
543 "r2": {
544 "route_maps": [
545 {
546 "name": "rmap_match_pf_1_"
547 + addr_type,
548 "direction": "in",
549 }
550 ]
551 }
552 }
553 }
554 }
555 }
556 }
557 }
558 }
559 }
560 }
561 result = create_router_bgp(tgen, topo, input_dict_4)
562 assert result is True, "Testcase {} : Failed \n Error: {}".format(
563 tc_name, result
564 )
565
566 step(
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"
570 )
571
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)
575
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)
579
580 step("Remove and Add no advertise community")
581 # Configure neighbor for route map
582 input_dict_4 = {
583 "r2": {
584 "bgp": {
585 "address_family": {
586 addr_type: {
587 "unicast": {
588 "neighbor": {
589 "r1": {
590 "dest_link": {
591 "r2": {
592 "route_maps": [
593 {
594 "name": "rmap_match_pf_1_"
595 + addr_type,
596 "direction": "in",
597 "delete": True,
598 }
599 ]
600 }
601 }
602 }
603 }
604 }
605 }
606 }
607 }
608 }
609 }
610 result = create_router_bgp(tgen, topo, input_dict_4)
611 assert result is True, "Testcase {} : Failed \n Error: {}".format(
612 tc_name, result
613 )
614
615 step(
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 "
619 " show ip route"
620 )
621
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)
625
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)
629
630 write_test_footer(tc_name)
631
632
633 if __name__ == "__main__":
634 args = ["-s"] + sys.argv[1:]
635 sys.exit(pytest.main(args))