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