]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_prefix_list_topo1/test_prefix_lists.py
5131a89ce832512903b57e124b0842887fc78f71
[mirror_frr.git] / tests / topotests / bgp_prefix_list_topo1 / test_prefix_lists.py
1 #!/usr/bin/python
2
3 #
4 # Copyright (c) 2019 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 prefix-list functionality:
25
26 Test steps
27 - Create topology (setup module)
28 Creating 4 routers topology, r1, r2, r3 are in IBGP and
29 r3, r4 are in EBGP
30 - Bring up topology
31 - Verify for bgp to converge
32
33 IP prefix-list tests
34 - Test ip prefix-lists IN permit
35 - Test ip prefix-lists OUT permit
36 - Test ip prefix-lists IN deny and permit any
37 - Test delete ip prefix-lists
38 - Test ip prefix-lists OUT deny and permit any
39 - Test modify ip prefix-lists IN permit to deny
40 - Test modify ip prefix-lists IN deny to permit
41 - Test modify ip prefix-lists OUT permit to deny
42 - Test modify prefix-lists OUT deny to permit
43 - Test ip prefix-lists implicit deny
44 """
45
46 import sys
47 import time
48 import os
49 import pytest
50
51 # Save the Current Working Directory to find configuration files.
52 CWD = os.path.dirname(os.path.realpath(__file__))
53 sys.path.append(os.path.join(CWD, "../"))
54
55 # pylint: disable=C0413
56 # Import topogen and topotest helpers
57 from lib.topogen import Topogen, get_topogen
58
59 # Import topoJson from lib, to create topology and initial configuration
60 from lib.common_config import (
61 start_topology,
62 write_test_header,
63 write_test_footer,
64 reset_config_on_routers,
65 verify_rib,
66 create_static_routes,
67 create_prefix_lists,
68 verify_prefix_lists,
69 )
70 from lib.topolog import logger
71 from lib.bgp import verify_bgp_convergence, create_router_bgp, clear_bgp_and_verify
72 from lib.topojson import build_config_from_json
73
74
75 pytestmark = [pytest.mark.bgpd]
76
77
78 # Global variables
79 bgp_convergence = False
80
81
82 def setup_module(mod):
83 """
84 Sets up the pytest environment
85
86 * `mod`: module name
87 """
88
89 testsuite_run_time = time.asctime(time.localtime(time.time()))
90 logger.info("Testsuite start time: {}".format(testsuite_run_time))
91 logger.info("=" * 40)
92
93 logger.info("Running setup_module to create topology")
94
95 # This function initiates the topology build with Topogen...
96 json_file = "{}/prefix_lists.json".format(CWD)
97 tgen = Topogen(json_file, mod.__name__)
98 global topo
99 topo = tgen.json_topo
100 # ... and here it calls Mininet initialization functions.
101
102 # Starting topology, create tmp files which are loaded to routers
103 # to start daemons and then start routers
104 start_topology(tgen)
105
106 # Creating configuration from JSON
107 build_config_from_json(tgen, topo)
108
109 # Checking BGP convergence
110 global BGP_CONVERGENCE
111
112 # Don't run this test if we have any failure.
113 if tgen.routers_have_failure():
114 pytest.skip(tgen.errors)
115
116 # Api call verify whether BGP is converged
117 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
118 assert BGP_CONVERGENCE is True, "setup_module :Failed \n Error:" " {}".format(
119 BGP_CONVERGENCE
120 )
121
122 logger.info("Running setup_module() done")
123
124
125 def teardown_module(mod):
126 """
127 Teardown the pytest environment
128
129 * `mod`: module name
130 """
131
132 logger.info("Running teardown_module to delete topology")
133
134 tgen = get_topogen()
135
136 # Stop toplogy and Remove tmp files
137 tgen.stop_topology()
138
139 logger.info(
140 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
141 )
142 logger.info("=" * 40)
143
144
145 #####################################################
146 #
147 # Tests starting
148 #
149 #####################################################
150
151
152 def test_ip_prefix_lists_in_permit(request):
153 """
154 Create ip prefix list and test permit prefixes IN direction
155 """
156
157 tgen = get_topogen()
158 if BGP_CONVERGENCE is not True:
159 pytest.skip("skipped because of BGP Convergence failure")
160
161 # test case name
162 tc_name = request.node.name
163 write_test_header(tc_name)
164
165 # Create Static routes
166 input_dict = {
167 "r1": {
168 "static_routes": [
169 {"network": "20.0.20.1/32", "no_of_ip": 1, "next_hop": "10.0.0.2"}
170 ]
171 }
172 }
173 result = create_static_routes(tgen, input_dict)
174 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
175
176 # Create ip prefix list
177 input_dict_2 = {
178 "r3": {
179 "prefix_lists": {
180 "ipv4": {
181 "pf_list_1": [{"seqid": 10, "network": "any", "action": "permit"}]
182 }
183 }
184 }
185 }
186 result = create_prefix_lists(tgen, input_dict_2)
187 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
188
189 # Configure bgp neighbor with prefix list
190 input_dict_3 = {
191 "r1": {
192 "bgp": {
193 "address_family": {
194 "ipv4": {
195 "unicast": {
196 "redistribute": [
197 {"redist_type": "static"},
198 {"redist_type": "connected"},
199 ]
200 }
201 }
202 }
203 }
204 },
205 "r3": {
206 "bgp": {
207 "address_family": {
208 "ipv4": {
209 "unicast": {
210 "neighbor": {
211 "r1": {
212 "dest_link": {
213 "r3": {
214 "prefix_lists": [
215 {"name": "pf_list_1", "direction": "in"}
216 ]
217 }
218 }
219 }
220 }
221 }
222 }
223 }
224 }
225 },
226 }
227 result = create_router_bgp(tgen, topo, input_dict_3)
228 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
229
230 # Verifying RIB routes
231 dut = "r3"
232 protocol = "bgp"
233 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
234 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
235
236 write_test_footer(tc_name)
237
238
239 def test_ip_prefix_lists_out_permit(request):
240 """
241 Create ip prefix list and test permit prefixes out direction
242 """
243
244 tgen = get_topogen()
245 if BGP_CONVERGENCE is not True:
246 pytest.skip("skipped because of BGP Convergence failure")
247
248 # test case name
249 tc_name = request.node.name
250 write_test_header(tc_name)
251
252 # Creating configuration from JSON
253 reset_config_on_routers(tgen)
254
255 # Create Static routes
256 input_dict = {
257 "r1": {
258 "static_routes": [
259 {"network": "10.0.20.1/32", "no_of_ip": 1, "next_hop": "10.0.0.2"}
260 ]
261 }
262 }
263 result = create_static_routes(tgen, input_dict)
264 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
265
266 # Create Static routes
267 input_dict_1 = {
268 "r1": {
269 "static_routes": [
270 {"network": "20.0.20.1/32", "no_of_ip": 1, "next_hop": "10.0.0.2"}
271 ]
272 }
273 }
274 result = create_static_routes(tgen, input_dict_1)
275 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
276
277 input_dict_5 = {
278 "r3": {
279 "static_routes": [
280 {"network": "10.0.0.2/30", "no_of_ip": 1, "next_hop": "10.0.0.9"}
281 ]
282 }
283 }
284 result = create_static_routes(tgen, input_dict_5)
285 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
286
287 # Api call to redistribute static routes
288
289 # Create ip prefix list
290 input_dict_2 = {
291 "r1": {
292 "prefix_lists": {
293 "ipv4": {
294 "pf_list_1": [
295 {"seqid": 10, "network": "20.0.20.1/32", "action": "permit"}
296 ]
297 }
298 }
299 }
300 }
301 result = create_prefix_lists(tgen, input_dict_2)
302 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
303
304 # Configure prefix list to bgp neighbor
305 # Configure bgp neighbor with prefix list
306 input_dict_3 = {
307 "r1": {
308 "bgp": {
309 "address_family": {
310 "ipv4": {
311 "unicast": {
312 "neighbor": {
313 "r3": {
314 "dest_link": {
315 "r1": {
316 "prefix_lists": [
317 {
318 "name": "pf_list_1",
319 "direction": "out",
320 }
321 ]
322 }
323 }
324 }
325 },
326 "redistribute": [
327 {"redist_type": "static"},
328 {"redist_type": "connected"},
329 ],
330 }
331 }
332 }
333 }
334 }
335 }
336
337 result = create_router_bgp(tgen, topo, input_dict_3)
338 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
339
340 # Verifying RIB routes
341 dut = "r3"
342 protocol = "bgp"
343 result = verify_rib(tgen, "ipv4", dut, input_dict_1, protocol=protocol)
344 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
345
346 result = verify_rib(
347 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
348 )
349 assert result is not True, (
350 "Testcase {} : Failed \n "
351 "Expected: Routes should not be present in {} FIB \n "
352 "Found: {}".format(tc_name, dut, result)
353 )
354
355 write_test_footer(tc_name)
356
357
358 def test_ip_prefix_lists_in_deny_and_permit_any(request):
359 """
360 Create ip prefix list and test permit/deny prefixes IN direction
361 """
362
363 tgen = get_topogen()
364 if BGP_CONVERGENCE is not True:
365 pytest.skip("skipped because of BGP Convergence failure")
366
367 # test case name
368 tc_name = request.node.name
369 write_test_header(tc_name)
370
371 # Creating configuration from JSON
372 reset_config_on_routers(tgen)
373
374 # Create Static Routes
375 input_dict = {
376 "r1": {
377 "static_routes": [
378 {"network": "10.0.20.1/32", "no_of_ip": 1, "next_hop": "10.0.0.2"}
379 ]
380 }
381 }
382 result = create_static_routes(tgen, input_dict)
383 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
384
385 # Api call to redistribute static routes
386 # Create ip prefix list
387 input_dict_2 = {
388 "r1": {
389 "prefix_lists": {
390 "ipv4": {
391 "pf_list_1": [
392 {"seqid": "10", "network": "10.0.20.1/32", "action": "deny"},
393 {"seqid": "11", "network": "any", "action": "permit"},
394 ]
395 }
396 }
397 }
398 }
399 result = create_prefix_lists(tgen, input_dict_2)
400 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
401
402 # Configure bgp neighbor with prefix list
403 input_dict_3 = {
404 "r1": {
405 "bgp": {
406 "address_family": {
407 "ipv4": {
408 "unicast": {
409 "redistribute": [
410 {"redist_type": "static"},
411 {"redist_type": "connected"},
412 ]
413 }
414 }
415 }
416 }
417 },
418 "r3": {
419 "bgp": {
420 "address_family": {
421 "ipv4": {
422 "unicast": {
423 "neighbor": {
424 "r1": {
425 "dest_link": {
426 "r3": {
427 "prefix_lists": [
428 {"name": "pf_list_1", "direction": "in"}
429 ]
430 }
431 }
432 }
433 }
434 }
435 }
436 }
437 }
438 },
439 }
440 # Configure prefix list to bgp neighbor
441 result = create_router_bgp(tgen, topo, input_dict_3)
442 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
443
444 # Verifying RIB routes
445 dut = "r3"
446 protocol = "bgp"
447 result = verify_rib(
448 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
449 )
450 assert result is not True, (
451 "Testcase {} : Failed \n "
452 "Expected: Routes should not be present in {} BGP RIB \n "
453 "Found: {}".format(tc_name, dut, result)
454 )
455
456 write_test_footer(tc_name)
457
458
459 def test_delete_prefix_lists(request):
460 """
461 Delete ip prefix list
462 """
463
464 tgen = get_topogen()
465 if BGP_CONVERGENCE is not True:
466 pytest.skip("skipped because of BGP Convergence failure")
467
468 # test case name
469 tc_name = request.node.name
470 write_test_header(tc_name)
471
472 # Creating configuration from JSON
473 reset_config_on_routers(tgen)
474
475 # Create ip prefix list
476 input_dict_2 = {
477 "r1": {
478 "prefix_lists": {
479 "ipv4": {
480 "pf_list_1": [
481 {"seqid": "10", "network": "10.0.20.1/32", "action": "deny"}
482 ]
483 }
484 }
485 }
486 }
487 result = create_prefix_lists(tgen, input_dict_2)
488 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
489
490 result = verify_prefix_lists(tgen, input_dict_2)
491 assert result is not True, "Testcase {} : Failed \n Error: {}".format(
492 tc_name, result
493 )
494
495 # Delete prefix list
496 input_dict_2 = {
497 "r1": {
498 "prefix_lists": {
499 "ipv4": {
500 "pf_list_1": [
501 {
502 "seqid": "10",
503 "network": "10.0.20.1/32",
504 "action": "deny",
505 "delete": True,
506 }
507 ]
508 }
509 }
510 }
511 }
512 result = create_prefix_lists(tgen, input_dict_2)
513 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
514
515 result = verify_prefix_lists(tgen, input_dict_2)
516 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
517
518 write_test_footer(tc_name)
519
520
521 def test_ip_prefix_lists_out_deny_and_permit_any(request):
522 """
523 Create ip prefix list and test deny/permit any prefixes OUT direction
524 """
525
526 tgen = get_topogen()
527 if BGP_CONVERGENCE is not True:
528 pytest.skip("skipped because of BGP Convergence failure")
529
530 # test case name
531 tc_name = request.node.name
532 write_test_header(tc_name)
533
534 # Creating configuration from JSON
535 reset_config_on_routers(tgen)
536
537 # Create Static Routes
538 input_dict = {
539 "r1": {
540 "static_routes": [
541 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
542 ]
543 }
544 }
545 result = create_static_routes(tgen, input_dict)
546 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
547
548 # Create Static Routes
549 input_dict_1 = {
550 "r2": {
551 "static_routes": [
552 {"network": "20.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.1"}
553 ]
554 }
555 }
556 result = create_static_routes(tgen, input_dict_1)
557 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
558
559 # Api call to redistribute static routes
560
561 # Create ip prefix list
562 input_dict_3 = {
563 "r3": {
564 "prefix_lists": {
565 "ipv4": {
566 "pf_list_1": [
567 {
568 "seqid": "10",
569 "network": "10.0.0.0/8",
570 "le": "32",
571 "action": "deny",
572 },
573 {"seqid": "11", "network": "any", "action": "permit"},
574 ]
575 }
576 }
577 }
578 }
579 result = create_prefix_lists(tgen, input_dict_3)
580 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
581
582 # Configure prefix list to bgp neighbor
583 input_dict_4 = {
584 "r1": {
585 "bgp": {
586 "address_family": {
587 "ipv4": {
588 "unicast": {
589 "redistribute": [
590 {"redist_type": "static"},
591 {"redist_type": "connected"},
592 ]
593 }
594 }
595 }
596 }
597 },
598 "r2": {
599 "bgp": {
600 "address_family": {
601 "ipv4": {
602 "unicast": {
603 "redistribute": [
604 {"redist_type": "static"},
605 {"redist_type": "connected"},
606 ]
607 }
608 }
609 }
610 }
611 },
612 "r3": {
613 "bgp": {
614 "address_family": {
615 "ipv4": {
616 "unicast": {
617 "neighbor": {
618 "r4": {
619 "dest_link": {
620 "r3": {
621 "prefix_lists": [
622 {
623 "name": "pf_list_1",
624 "direction": "out",
625 }
626 ]
627 }
628 }
629 }
630 }
631 }
632 }
633 }
634 }
635 },
636 }
637 result = create_router_bgp(tgen, topo, input_dict_4)
638 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
639
640 # Verifying RIB routes
641 dut = "r4"
642 protocol = "bgp"
643 result = verify_rib(tgen, "ipv4", dut, input_dict_1, protocol=protocol)
644 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
645
646 # Verifying RIB routes
647 dut = "r4"
648 protocol = "bgp"
649 result = verify_rib(
650 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
651 )
652 assert result is not True, (
653 "Testcase {} : Failed \n "
654 "Expected: Routes should not be present in {} BGP RIB \n "
655 "Found: {}".format(tc_name, dut, result)
656 )
657
658 write_test_footer(tc_name)
659
660
661 def test_modify_prefix_lists_in_permit_to_deny(request):
662 """
663 Modify ip prefix list and test permit to deny prefixes IN direction
664 """
665
666 tgen = get_topogen()
667 if BGP_CONVERGENCE is not True:
668 pytest.skip("skipped because of BGP Convergence failure")
669
670 # test case name
671 tc_name = request.node.name
672 write_test_header(tc_name)
673
674 # Creating configuration from JSON
675 reset_config_on_routers(tgen)
676
677 # Create Static Routes
678 input_dict = {
679 "r1": {
680 "static_routes": [
681 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
682 ]
683 }
684 }
685 result = create_static_routes(tgen, input_dict)
686 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
687
688 # Api call to redistribute static routes
689
690 # Create ip prefix list
691 input_dict_2 = {
692 "r3": {
693 "prefix_lists": {
694 "ipv4": {
695 "pf_list_1": [
696 {
697 "seqid": "10",
698 "network": "10.0.0.0/8",
699 "le": "32",
700 "action": "permit",
701 }
702 ]
703 }
704 }
705 }
706 }
707 result = create_prefix_lists(tgen, input_dict_2)
708 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
709
710 # Configure prefix list to bgp neighbor
711 input_dict_3 = {
712 "r1": {
713 "bgp": {
714 "address_family": {
715 "ipv4": {
716 "unicast": {
717 "redistribute": [
718 {"redist_type": "static"},
719 {"redist_type": "connected"},
720 ]
721 }
722 }
723 }
724 }
725 },
726 "r3": {
727 "bgp": {
728 "address_family": {
729 "ipv4": {
730 "unicast": {
731 "neighbor": {
732 "r1": {
733 "dest_link": {
734 "r3": {
735 "prefix_lists": [
736 {"name": "pf_list_1", "direction": "in"}
737 ]
738 }
739 }
740 }
741 }
742 }
743 }
744 }
745 }
746 },
747 }
748 result = create_router_bgp(tgen, topo, input_dict_3)
749 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
750
751 # Verifying RIB routes
752 dut = "r3"
753 protocol = "bgp"
754 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
755 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
756
757 # Modify prefix list
758 input_dict_1 = {
759 "r3": {
760 "prefix_lists": {
761 "ipv4": {
762 "pf_list_1": [
763 {
764 "seqid": "10",
765 "network": "10.0.0.0/8",
766 "le": "32",
767 "action": "deny",
768 },
769 {"seqid": "11", "network": "any", "action": "permit"},
770 ]
771 }
772 }
773 }
774 }
775 result = create_prefix_lists(tgen, input_dict_1)
776 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
777
778 # Api call to clear bgp, so config changes would be reflected
779 dut = "r3"
780 result = clear_bgp_and_verify(tgen, topo, dut)
781 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
782
783 # Verifying RIB routes
784 dut = "r3"
785 protocol = "bgp"
786 result = verify_rib(
787 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
788 )
789 assert result is not True, (
790 "Testcase {} : Failed \n "
791 "Expected: Routes should not be present in {} BGP RIB \n "
792 "Found: {}".format(tc_name, dut, result)
793 )
794
795 write_test_footer(tc_name)
796
797
798 def test_modify_prefix_lists_in_deny_to_permit(request):
799 """
800 Modify ip prefix list and test deny to permit prefixes IN direction
801 """
802
803 tgen = get_topogen()
804 if BGP_CONVERGENCE is not True:
805 pytest.skip("skipped because of BGP Convergence failure")
806
807 # test case name
808 tc_name = request.node.name
809 write_test_header(tc_name)
810
811 # Creating configuration from JSON
812 reset_config_on_routers(tgen)
813
814 # Create Static Routes
815 input_dict = {
816 "r1": {
817 "static_routes": [
818 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
819 ]
820 }
821 }
822 result = create_static_routes(tgen, input_dict)
823 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
824
825 # Api call to redistribute static routes
826
827 # Create ip prefix list
828 input_dict_1 = {
829 "r3": {
830 "prefix_lists": {
831 "ipv4": {
832 "pf_list_1": [
833 {
834 "seqid": "10",
835 "network": "10.0.0.0/8",
836 "le": "32",
837 "action": "deny",
838 },
839 {"seqid": "11", "network": "any", "action": "permit"},
840 ]
841 }
842 }
843 }
844 }
845 result = create_prefix_lists(tgen, input_dict_1)
846 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
847
848 # Configure prefix list to bgp neighbor
849 input_dict_2 = {
850 "r1": {
851 "bgp": {
852 "address_family": {
853 "ipv4": {
854 "unicast": {
855 "redistribute": [
856 {"redist_type": "static"},
857 {"redist_type": "connected"},
858 ]
859 }
860 }
861 }
862 }
863 },
864 "r3": {
865 "bgp": {
866 "address_family": {
867 "ipv4": {
868 "unicast": {
869 "neighbor": {
870 "r1": {
871 "dest_link": {
872 "r3": {
873 "prefix_lists": [
874 {"name": "pf_list_1", "direction": "in"}
875 ]
876 }
877 }
878 }
879 }
880 }
881 }
882 }
883 }
884 },
885 }
886 result = create_router_bgp(tgen, topo, input_dict_2)
887 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
888
889 # Verifying RIB routes
890 dut = "r3"
891 protocol = "bgp"
892 result = verify_rib(
893 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
894 )
895 assert result is not True, (
896 "Testcase {} : Failed \n "
897 "Expected: Routes should not be present in {} BGP RIB \n "
898 "Found: {}".format(tc_name, dut, result)
899 )
900
901 # Modify ip prefix list
902 input_dict_1 = {
903 "r3": {
904 "prefix_lists": {
905 "ipv4": {
906 "pf_list_1": [
907 {
908 "seqid": "10",
909 "network": "10.0.0.0/8",
910 "le": "32",
911 "action": "permit",
912 }
913 ]
914 }
915 }
916 }
917 }
918 result = create_prefix_lists(tgen, input_dict_1)
919 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
920
921 # Api call to clear bgp, so config changes would be reflected
922 dut = "r3"
923 result = clear_bgp_and_verify(tgen, topo, dut)
924 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
925
926 # Verifying RIB routes
927 dut = "r3"
928 protocol = "bgp"
929 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
930 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
931
932 write_test_footer(tc_name)
933
934
935 def test_modify_prefix_lists_out_permit_to_deny(request):
936 """
937 Modify ip prefix list and test permit to deny prefixes OUT direction
938 """
939
940 tgen = get_topogen()
941 if BGP_CONVERGENCE is not True:
942 pytest.skip("skipped because of BGP Convergence failure")
943
944 # test case name
945 tc_name = request.node.name
946 write_test_header(tc_name)
947
948 # Creating configuration from JSON
949 reset_config_on_routers(tgen)
950
951 # Create Static Routes
952 input_dict = {
953 "r1": {
954 "static_routes": [
955 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
956 ]
957 }
958 }
959 result = create_static_routes(tgen, input_dict)
960 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
961
962 # Api call to redistribute static routes
963
964 # Create ip prefix list
965 input_dict_1 = {
966 "r3": {
967 "prefix_lists": {
968 "ipv4": {
969 "pf_list_1": [
970 {
971 "seqid": "10",
972 "network": "10.0.0.0/8",
973 "le": "32",
974 "action": "permit",
975 }
976 ]
977 }
978 }
979 }
980 }
981 result = create_prefix_lists(tgen, input_dict_1)
982 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
983
984 # Configure prefix list to bgp neighbor
985 input_dict_2 = {
986 "r1": {
987 "bgp": {
988 "address_family": {
989 "ipv4": {
990 "unicast": {
991 "redistribute": [
992 {"redist_type": "static"},
993 {"redist_type": "connected"},
994 ]
995 }
996 }
997 }
998 }
999 },
1000 "r3": {
1001 "bgp": {
1002 "address_family": {
1003 "ipv4": {
1004 "unicast": {
1005 "neighbor": {
1006 "r4": {
1007 "dest_link": {
1008 "r3": {
1009 "prefix_lists": [
1010 {
1011 "name": "pf_list_1",
1012 "direction": "out",
1013 }
1014 ]
1015 }
1016 }
1017 }
1018 }
1019 }
1020 }
1021 }
1022 }
1023 },
1024 }
1025 result = create_router_bgp(tgen, topo, input_dict_2)
1026 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1027
1028 # Verifying RIB routes
1029 dut = "r4"
1030 protocol = "bgp"
1031 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1032 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1033
1034 # Modify ip prefix list
1035 input_dict_1 = {
1036 "r3": {
1037 "prefix_lists": {
1038 "ipv4": {
1039 "pf_list_1": [
1040 {
1041 "seqid": "10",
1042 "network": "10.0.0.0/8",
1043 "le": "32",
1044 "action": "deny",
1045 },
1046 {"seqid": "11", "network": "any", "action": "permit"},
1047 ]
1048 }
1049 }
1050 }
1051 }
1052 result = create_prefix_lists(tgen, input_dict_1)
1053 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1054
1055 # Api call to clear bgp, so config changes would be reflected
1056 dut = "r3"
1057 result = clear_bgp_and_verify(tgen, topo, dut)
1058 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1059
1060 # Verifying RIB routes
1061 dut = "r4"
1062 protocol = "bgp"
1063 result = verify_rib(
1064 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
1065 )
1066 assert result is not True, (
1067 "Testcase {} : Failed \n "
1068 "Expected: Routes should not be present in {} BGP RIB \n "
1069 "Found: {}".format(tc_name, dut, result)
1070 )
1071
1072 write_test_footer(tc_name)
1073
1074
1075 def test_modify_prefix_lists_out_deny_to_permit(request):
1076 """
1077 Modify ip prefix list and test deny to permit prefixes OUT direction
1078 """
1079
1080 tgen = get_topogen()
1081 if BGP_CONVERGENCE is not True:
1082 pytest.skip("skipped because of BGP Convergence failure")
1083
1084 # test case name
1085 tc_name = request.node.name
1086 write_test_header(tc_name)
1087
1088 # Creating configuration from JSON
1089 reset_config_on_routers(tgen)
1090
1091 # Create Static Routes
1092 input_dict = {
1093 "r1": {
1094 "static_routes": [
1095 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
1096 ]
1097 }
1098 }
1099 result = create_static_routes(tgen, input_dict)
1100 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1101
1102 # Api call to redistribute static routes
1103 # Create ip prefix list
1104 input_dict_1 = {
1105 "r3": {
1106 "prefix_lists": {
1107 "ipv4": {
1108 "pf_list_1": [
1109 {
1110 "seqid": "10",
1111 "network": "10.0.0.0/8",
1112 "le": "32",
1113 "action": "deny",
1114 },
1115 {"seqid": "11", "network": "any", "action": "permit"},
1116 ]
1117 }
1118 }
1119 }
1120 }
1121 result = create_prefix_lists(tgen, input_dict_1)
1122 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1123
1124 # Configure prefix list to bgp neighbor
1125 input_dict_2 = {
1126 "r1": {
1127 "bgp": {
1128 "address_family": {
1129 "ipv4": {
1130 "unicast": {
1131 "redistribute": [
1132 {"redist_type": "static"},
1133 {"redist_type": "connected"},
1134 ]
1135 }
1136 }
1137 }
1138 }
1139 },
1140 "r3": {
1141 "bgp": {
1142 "address_family": {
1143 "ipv4": {
1144 "unicast": {
1145 "neighbor": {
1146 "r4": {
1147 "dest_link": {
1148 "r3": {
1149 "prefix_lists": [
1150 {
1151 "name": "pf_list_1",
1152 "direction": "out",
1153 }
1154 ]
1155 }
1156 }
1157 }
1158 }
1159 }
1160 }
1161 }
1162 }
1163 },
1164 }
1165 result = create_router_bgp(tgen, topo, input_dict_2)
1166 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1167
1168 # Verifying RIB routes
1169 dut = "r4"
1170 protocol = "bgp"
1171 result = verify_rib(
1172 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
1173 )
1174 assert result is not True, (
1175 "Testcase {} : Failed \n "
1176 "Expected: Routes should not be present in {} BGP RIB \n "
1177 "Found: {}".format(tc_name, dut, result)
1178 )
1179
1180 # Modify ip prefix list
1181 input_dict_1 = {
1182 "r3": {
1183 "prefix_lists": {
1184 "ipv4": {
1185 "pf_list_1": [
1186 {
1187 "seqid": "10",
1188 "network": "10.0.0.0/8",
1189 "le": "32",
1190 "action": "permit",
1191 }
1192 ]
1193 }
1194 }
1195 }
1196 }
1197 result = create_prefix_lists(tgen, input_dict_1)
1198 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1199
1200 # Api call to clear bgp, so config changes would be reflected
1201 dut = "r3"
1202 result = clear_bgp_and_verify(tgen, topo, dut)
1203 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1204
1205 # Verifying RIB routes
1206 dut = "r4"
1207 protocol = "bgp"
1208 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1209 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1210
1211 write_test_footer(tc_name)
1212
1213
1214 def test_ip_prefix_lists_implicit_deny(request):
1215 """
1216 Create ip prefix list and test implicit deny
1217 """
1218
1219 tgen = get_topogen()
1220 if BGP_CONVERGENCE is not True:
1221 pytest.skip("skipped because of BGP Convergence failure")
1222
1223 # test case name
1224 tc_name = request.node.name
1225 write_test_header(tc_name)
1226
1227 # Creating configuration from JSON
1228 reset_config_on_routers(tgen)
1229
1230 # Create Static Routes
1231 input_dict = {
1232 "r1": {
1233 "static_routes": [
1234 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
1235 ]
1236 }
1237 }
1238 result = create_static_routes(tgen, input_dict)
1239 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1240
1241 # Create Static Routes
1242 input_dict_1 = {
1243 "r2": {
1244 "static_routes": [
1245 {"network": "20.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.1"}
1246 ]
1247 }
1248 }
1249 result = create_static_routes(tgen, input_dict)
1250 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1251
1252 # Api call to redistribute static routes
1253 # Create ip prefix list
1254 input_dict_3 = {
1255 "r3": {
1256 "prefix_lists": {
1257 "ipv4": {
1258 "pf_list_1": [
1259 {
1260 "seqid": "10",
1261 "network": "10.0.0.0/8",
1262 "le": "32",
1263 "action": "permit",
1264 }
1265 ]
1266 }
1267 }
1268 }
1269 }
1270 result = create_prefix_lists(tgen, input_dict_3)
1271 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1272
1273 # Configure prefix list to bgp neighbor
1274 input_dict_4 = {
1275 "r1": {
1276 "bgp": {
1277 "address_family": {
1278 "ipv4": {
1279 "unicast": {
1280 "redistribute": [
1281 {"redist_type": "static"},
1282 {"redist_type": "connected"},
1283 ]
1284 }
1285 }
1286 }
1287 }
1288 },
1289 "r2": {
1290 "bgp": {
1291 "address_family": {
1292 "ipv4": {
1293 "unicast": {
1294 "redistribute": [
1295 {"redist_type": "static"},
1296 {"redist_type": "connected"},
1297 ]
1298 }
1299 }
1300 }
1301 }
1302 },
1303 "r3": {
1304 "bgp": {
1305 "address_family": {
1306 "ipv4": {
1307 "unicast": {
1308 "neighbor": {
1309 "r4": {
1310 "dest_link": {
1311 "r3": {
1312 "prefix_lists": [
1313 {
1314 "name": "pf_list_1",
1315 "direction": "out",
1316 }
1317 ]
1318 }
1319 }
1320 }
1321 }
1322 }
1323 }
1324 }
1325 }
1326 },
1327 }
1328 result = create_router_bgp(tgen, topo, input_dict_4)
1329 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1330
1331 # Verifying RIB routes
1332 dut = "r4"
1333 protocol = "bgp"
1334 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1335 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1336
1337 # Verifying RIB routes
1338 dut = "r4"
1339 protocol = "bgp"
1340 result = verify_rib(
1341 tgen, "ipv4", dut, input_dict_1, protocol=protocol, expected=False
1342 )
1343 assert result is not True, (
1344 "Testcase {} : Failed \n "
1345 "Expected: Routes should not be present in {} BGP RIB \n "
1346 "Found: {}".format(tc_name, dut, result)
1347 )
1348
1349 write_test_footer(tc_name)
1350
1351
1352 if __name__ == "__main__":
1353 args = ["-s"] + sys.argv[1:]
1354 sys.exit(pytest.main(args))