]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_prefix_list_topo1/test_prefix_lists.py
Merge pull request #9546 from proelbtn/add-support-for-perfix-sid-type-5
[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 deamons 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 (
350 result is not True
351 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
352 write_test_footer(tc_name)
353
354
355 def test_ip_prefix_lists_in_deny_and_permit_any(request):
356 """
357 Create ip prefix list and test permit/deny prefixes IN direction
358 """
359
360 tgen = get_topogen()
361 if BGP_CONVERGENCE is not True:
362 pytest.skip("skipped because of BGP Convergence failure")
363
364 # test case name
365 tc_name = request.node.name
366 write_test_header(tc_name)
367
368 # Creating configuration from JSON
369 reset_config_on_routers(tgen)
370
371 # Create Static Routes
372 input_dict = {
373 "r1": {
374 "static_routes": [
375 {"network": "10.0.20.1/32", "no_of_ip": 1, "next_hop": "10.0.0.2"}
376 ]
377 }
378 }
379 result = create_static_routes(tgen, input_dict)
380 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
381
382 # Api call to redistribute static routes
383 # Create ip prefix list
384 input_dict_2 = {
385 "r1": {
386 "prefix_lists": {
387 "ipv4": {
388 "pf_list_1": [
389 {"seqid": "10", "network": "10.0.20.1/32", "action": "deny"},
390 {"seqid": "11", "network": "any", "action": "permit"},
391 ]
392 }
393 }
394 }
395 }
396 result = create_prefix_lists(tgen, input_dict_2)
397 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
398
399 # Configure bgp neighbor with prefix list
400 input_dict_3 = {
401 "r1": {
402 "bgp": {
403 "address_family": {
404 "ipv4": {
405 "unicast": {
406 "redistribute": [
407 {"redist_type": "static"},
408 {"redist_type": "connected"},
409 ]
410 }
411 }
412 }
413 }
414 },
415 "r3": {
416 "bgp": {
417 "address_family": {
418 "ipv4": {
419 "unicast": {
420 "neighbor": {
421 "r1": {
422 "dest_link": {
423 "r3": {
424 "prefix_lists": [
425 {"name": "pf_list_1", "direction": "in"}
426 ]
427 }
428 }
429 }
430 }
431 }
432 }
433 }
434 }
435 },
436 }
437 # Configure prefix list to bgp neighbor
438 result = create_router_bgp(tgen, topo, input_dict_3)
439 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
440
441 # Verifying RIB routes
442 dut = "r3"
443 protocol = "bgp"
444 result = verify_rib(
445 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
446 )
447 assert (
448 result is not True
449 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
450
451 write_test_footer(tc_name)
452
453
454 def test_delete_prefix_lists(request):
455 """
456 Delete ip prefix list
457 """
458
459 tgen = get_topogen()
460 if BGP_CONVERGENCE is not True:
461 pytest.skip("skipped because of BGP Convergence failure")
462
463 # test case name
464 tc_name = request.node.name
465 write_test_header(tc_name)
466
467 # Creating configuration from JSON
468 reset_config_on_routers(tgen)
469
470 # Create ip prefix list
471 input_dict_2 = {
472 "r1": {
473 "prefix_lists": {
474 "ipv4": {
475 "pf_list_1": [
476 {"seqid": "10", "network": "10.0.20.1/32", "action": "deny"}
477 ]
478 }
479 }
480 }
481 }
482 result = create_prefix_lists(tgen, input_dict_2)
483 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
484
485 result = verify_prefix_lists(tgen, input_dict_2)
486 assert result is not True, "Testcase {} : Failed \n Error: {}".format(
487 tc_name, result
488 )
489
490 # Delete prefix list
491 input_dict_2 = {
492 "r1": {
493 "prefix_lists": {
494 "ipv4": {
495 "pf_list_1": [
496 {
497 "seqid": "10",
498 "network": "10.0.20.1/32",
499 "action": "deny",
500 "delete": True,
501 }
502 ]
503 }
504 }
505 }
506 }
507 result = create_prefix_lists(tgen, input_dict_2)
508 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
509
510 result = verify_prefix_lists(tgen, input_dict_2)
511 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
512
513 write_test_footer(tc_name)
514
515
516 def test_ip_prefix_lists_out_deny_and_permit_any(request):
517 """
518 Create ip prefix list and test deny/permit any prefixes OUT direction
519 """
520
521 tgen = get_topogen()
522 if BGP_CONVERGENCE is not True:
523 pytest.skip("skipped because of BGP Convergence failure")
524
525 # test case name
526 tc_name = request.node.name
527 write_test_header(tc_name)
528
529 # Creating configuration from JSON
530 reset_config_on_routers(tgen)
531
532 # Create Static Routes
533 input_dict = {
534 "r1": {
535 "static_routes": [
536 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
537 ]
538 }
539 }
540 result = create_static_routes(tgen, input_dict)
541 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
542
543 # Create Static Routes
544 input_dict_1 = {
545 "r2": {
546 "static_routes": [
547 {"network": "20.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.1"}
548 ]
549 }
550 }
551 result = create_static_routes(tgen, input_dict_1)
552 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
553
554 # Api call to redistribute static routes
555
556 # Create ip prefix list
557 input_dict_3 = {
558 "r3": {
559 "prefix_lists": {
560 "ipv4": {
561 "pf_list_1": [
562 {
563 "seqid": "10",
564 "network": "10.0.0.0/8",
565 "le": "32",
566 "action": "deny",
567 },
568 {"seqid": "11", "network": "any", "action": "permit"},
569 ]
570 }
571 }
572 }
573 }
574 result = create_prefix_lists(tgen, input_dict_3)
575 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
576
577 # Configure prefix list to bgp neighbor
578 input_dict_4 = {
579 "r1": {
580 "bgp": {
581 "address_family": {
582 "ipv4": {
583 "unicast": {
584 "redistribute": [
585 {"redist_type": "static"},
586 {"redist_type": "connected"},
587 ]
588 }
589 }
590 }
591 }
592 },
593 "r2": {
594 "bgp": {
595 "address_family": {
596 "ipv4": {
597 "unicast": {
598 "redistribute": [
599 {"redist_type": "static"},
600 {"redist_type": "connected"},
601 ]
602 }
603 }
604 }
605 }
606 },
607 "r3": {
608 "bgp": {
609 "address_family": {
610 "ipv4": {
611 "unicast": {
612 "neighbor": {
613 "r4": {
614 "dest_link": {
615 "r3": {
616 "prefix_lists": [
617 {
618 "name": "pf_list_1",
619 "direction": "out",
620 }
621 ]
622 }
623 }
624 }
625 }
626 }
627 }
628 }
629 }
630 },
631 }
632 result = create_router_bgp(tgen, topo, input_dict_4)
633 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
634
635 # Verifying RIB routes
636 dut = "r4"
637 protocol = "bgp"
638 result = verify_rib(tgen, "ipv4", dut, input_dict_1, protocol=protocol)
639 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
640
641 # Verifying RIB routes
642 dut = "r4"
643 protocol = "bgp"
644 result = verify_rib(
645 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
646 )
647 assert (
648 result is not True
649 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
650 write_test_footer(tc_name)
651
652
653 def test_modify_prefix_lists_in_permit_to_deny(request):
654 """
655 Modify ip prefix list and test permit to deny prefixes IN direction
656 """
657
658 tgen = get_topogen()
659 if BGP_CONVERGENCE is not True:
660 pytest.skip("skipped because of BGP Convergence failure")
661
662 # test case name
663 tc_name = request.node.name
664 write_test_header(tc_name)
665
666 # Creating configuration from JSON
667 reset_config_on_routers(tgen)
668
669 # Create Static Routes
670 input_dict = {
671 "r1": {
672 "static_routes": [
673 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
674 ]
675 }
676 }
677 result = create_static_routes(tgen, input_dict)
678 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
679
680 # Api call to redistribute static routes
681
682 # Create ip prefix list
683 input_dict_2 = {
684 "r3": {
685 "prefix_lists": {
686 "ipv4": {
687 "pf_list_1": [
688 {
689 "seqid": "10",
690 "network": "10.0.0.0/8",
691 "le": "32",
692 "action": "permit",
693 }
694 ]
695 }
696 }
697 }
698 }
699 result = create_prefix_lists(tgen, input_dict_2)
700 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
701
702 # Configure prefix list to bgp neighbor
703 input_dict_3 = {
704 "r1": {
705 "bgp": {
706 "address_family": {
707 "ipv4": {
708 "unicast": {
709 "redistribute": [
710 {"redist_type": "static"},
711 {"redist_type": "connected"},
712 ]
713 }
714 }
715 }
716 }
717 },
718 "r3": {
719 "bgp": {
720 "address_family": {
721 "ipv4": {
722 "unicast": {
723 "neighbor": {
724 "r1": {
725 "dest_link": {
726 "r3": {
727 "prefix_lists": [
728 {"name": "pf_list_1", "direction": "in"}
729 ]
730 }
731 }
732 }
733 }
734 }
735 }
736 }
737 }
738 },
739 }
740 result = create_router_bgp(tgen, topo, input_dict_3)
741 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
742
743 # Verifying RIB routes
744 dut = "r3"
745 protocol = "bgp"
746 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
747 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
748
749 # Modify prefix list
750 input_dict_1 = {
751 "r3": {
752 "prefix_lists": {
753 "ipv4": {
754 "pf_list_1": [
755 {
756 "seqid": "10",
757 "network": "10.0.0.0/8",
758 "le": "32",
759 "action": "deny",
760 },
761 {"seqid": "11", "network": "any", "action": "permit"},
762 ]
763 }
764 }
765 }
766 }
767 result = create_prefix_lists(tgen, input_dict_1)
768 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
769
770 # Api call to clear bgp, so config changes would be reflected
771 dut = "r3"
772 result = clear_bgp_and_verify(tgen, topo, dut)
773 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
774
775 # Verifying RIB routes
776 dut = "r3"
777 protocol = "bgp"
778 result = verify_rib(
779 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
780 )
781 assert (
782 result is not True
783 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
784
785 write_test_footer(tc_name)
786
787
788 def test_modify_prefix_lists_in_deny_to_permit(request):
789 """
790 Modify ip prefix list and test deny to permit prefixes IN direction
791 """
792
793 tgen = get_topogen()
794 if BGP_CONVERGENCE is not True:
795 pytest.skip("skipped because of BGP Convergence failure")
796
797 # test case name
798 tc_name = request.node.name
799 write_test_header(tc_name)
800
801 # Creating configuration from JSON
802 reset_config_on_routers(tgen)
803
804 # Create Static Routes
805 input_dict = {
806 "r1": {
807 "static_routes": [
808 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
809 ]
810 }
811 }
812 result = create_static_routes(tgen, input_dict)
813 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
814
815 # Api call to redistribute static routes
816
817 # Create ip prefix list
818 input_dict_1 = {
819 "r3": {
820 "prefix_lists": {
821 "ipv4": {
822 "pf_list_1": [
823 {
824 "seqid": "10",
825 "network": "10.0.0.0/8",
826 "le": "32",
827 "action": "deny",
828 },
829 {"seqid": "11", "network": "any", "action": "permit"},
830 ]
831 }
832 }
833 }
834 }
835 result = create_prefix_lists(tgen, input_dict_1)
836 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
837
838 # Configure prefix list to bgp neighbor
839 input_dict_2 = {
840 "r1": {
841 "bgp": {
842 "address_family": {
843 "ipv4": {
844 "unicast": {
845 "redistribute": [
846 {"redist_type": "static"},
847 {"redist_type": "connected"},
848 ]
849 }
850 }
851 }
852 }
853 },
854 "r3": {
855 "bgp": {
856 "address_family": {
857 "ipv4": {
858 "unicast": {
859 "neighbor": {
860 "r1": {
861 "dest_link": {
862 "r3": {
863 "prefix_lists": [
864 {"name": "pf_list_1", "direction": "in"}
865 ]
866 }
867 }
868 }
869 }
870 }
871 }
872 }
873 }
874 },
875 }
876 result = create_router_bgp(tgen, topo, input_dict_2)
877 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
878
879 # Verifying RIB routes
880 dut = "r3"
881 protocol = "bgp"
882 result = verify_rib(
883 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
884 )
885 assert (
886 result is not True
887 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
888
889 # Modify ip prefix list
890 input_dict_1 = {
891 "r3": {
892 "prefix_lists": {
893 "ipv4": {
894 "pf_list_1": [
895 {
896 "seqid": "10",
897 "network": "10.0.0.0/8",
898 "le": "32",
899 "action": "permit",
900 }
901 ]
902 }
903 }
904 }
905 }
906 result = create_prefix_lists(tgen, input_dict_1)
907 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
908
909 # Api call to clear bgp, so config changes would be reflected
910 dut = "r3"
911 result = clear_bgp_and_verify(tgen, topo, dut)
912 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
913
914 # Verifying RIB routes
915 dut = "r3"
916 protocol = "bgp"
917 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
918 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
919
920 write_test_footer(tc_name)
921
922
923 def test_modify_prefix_lists_out_permit_to_deny(request):
924 """
925 Modify ip prefix list and test permit to deny prefixes OUT direction
926 """
927
928 tgen = get_topogen()
929 if BGP_CONVERGENCE is not True:
930 pytest.skip("skipped because of BGP Convergence failure")
931
932 # test case name
933 tc_name = request.node.name
934 write_test_header(tc_name)
935
936 # Creating configuration from JSON
937 reset_config_on_routers(tgen)
938
939 # Create Static Routes
940 input_dict = {
941 "r1": {
942 "static_routes": [
943 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
944 ]
945 }
946 }
947 result = create_static_routes(tgen, input_dict)
948 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
949
950 # Api call to redistribute static routes
951
952 # Create ip prefix list
953 input_dict_1 = {
954 "r3": {
955 "prefix_lists": {
956 "ipv4": {
957 "pf_list_1": [
958 {
959 "seqid": "10",
960 "network": "10.0.0.0/8",
961 "le": "32",
962 "action": "permit",
963 }
964 ]
965 }
966 }
967 }
968 }
969 result = create_prefix_lists(tgen, input_dict_1)
970 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
971
972 # Configure prefix list to bgp neighbor
973 input_dict_2 = {
974 "r1": {
975 "bgp": {
976 "address_family": {
977 "ipv4": {
978 "unicast": {
979 "redistribute": [
980 {"redist_type": "static"},
981 {"redist_type": "connected"},
982 ]
983 }
984 }
985 }
986 }
987 },
988 "r3": {
989 "bgp": {
990 "address_family": {
991 "ipv4": {
992 "unicast": {
993 "neighbor": {
994 "r4": {
995 "dest_link": {
996 "r3": {
997 "prefix_lists": [
998 {
999 "name": "pf_list_1",
1000 "direction": "out",
1001 }
1002 ]
1003 }
1004 }
1005 }
1006 }
1007 }
1008 }
1009 }
1010 }
1011 },
1012 }
1013 result = create_router_bgp(tgen, topo, input_dict_2)
1014 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1015
1016 # Verifying RIB routes
1017 dut = "r4"
1018 protocol = "bgp"
1019 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1020 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1021
1022 # Modify ip prefix list
1023 input_dict_1 = {
1024 "r3": {
1025 "prefix_lists": {
1026 "ipv4": {
1027 "pf_list_1": [
1028 {
1029 "seqid": "10",
1030 "network": "10.0.0.0/8",
1031 "le": "32",
1032 "action": "deny",
1033 },
1034 {"seqid": "11", "network": "any", "action": "permit"},
1035 ]
1036 }
1037 }
1038 }
1039 }
1040 result = create_prefix_lists(tgen, input_dict_1)
1041 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1042
1043 # Api call to clear bgp, so config changes would be reflected
1044 dut = "r3"
1045 result = clear_bgp_and_verify(tgen, topo, dut)
1046 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1047
1048 # Verifying RIB routes
1049 dut = "r4"
1050 protocol = "bgp"
1051 result = verify_rib(
1052 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
1053 )
1054 assert (
1055 result is not True
1056 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
1057
1058 write_test_footer(tc_name)
1059
1060
1061 def test_modify_prefix_lists_out_deny_to_permit(request):
1062 """
1063 Modify ip prefix list and test deny to permit prefixes OUT direction
1064 """
1065
1066 tgen = get_topogen()
1067 if BGP_CONVERGENCE is not True:
1068 pytest.skip("skipped because of BGP Convergence failure")
1069
1070 # test case name
1071 tc_name = request.node.name
1072 write_test_header(tc_name)
1073
1074 # Creating configuration from JSON
1075 reset_config_on_routers(tgen)
1076
1077 # Create Static Routes
1078 input_dict = {
1079 "r1": {
1080 "static_routes": [
1081 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
1082 ]
1083 }
1084 }
1085 result = create_static_routes(tgen, input_dict)
1086 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1087
1088 # Api call to redistribute static routes
1089 # Create ip prefix list
1090 input_dict_1 = {
1091 "r3": {
1092 "prefix_lists": {
1093 "ipv4": {
1094 "pf_list_1": [
1095 {
1096 "seqid": "10",
1097 "network": "10.0.0.0/8",
1098 "le": "32",
1099 "action": "deny",
1100 },
1101 {"seqid": "11", "network": "any", "action": "permit"},
1102 ]
1103 }
1104 }
1105 }
1106 }
1107 result = create_prefix_lists(tgen, input_dict_1)
1108 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1109
1110 # Configure prefix list to bgp neighbor
1111 input_dict_2 = {
1112 "r1": {
1113 "bgp": {
1114 "address_family": {
1115 "ipv4": {
1116 "unicast": {
1117 "redistribute": [
1118 {"redist_type": "static"},
1119 {"redist_type": "connected"},
1120 ]
1121 }
1122 }
1123 }
1124 }
1125 },
1126 "r3": {
1127 "bgp": {
1128 "address_family": {
1129 "ipv4": {
1130 "unicast": {
1131 "neighbor": {
1132 "r4": {
1133 "dest_link": {
1134 "r3": {
1135 "prefix_lists": [
1136 {
1137 "name": "pf_list_1",
1138 "direction": "out",
1139 }
1140 ]
1141 }
1142 }
1143 }
1144 }
1145 }
1146 }
1147 }
1148 }
1149 },
1150 }
1151 result = create_router_bgp(tgen, topo, input_dict_2)
1152 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1153
1154 # Verifying RIB routes
1155 dut = "r4"
1156 protocol = "bgp"
1157 result = verify_rib(
1158 tgen, "ipv4", dut, input_dict, protocol=protocol, expected=False
1159 )
1160 assert (
1161 result is not True
1162 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
1163
1164 # Modify ip prefix list
1165 input_dict_1 = {
1166 "r3": {
1167 "prefix_lists": {
1168 "ipv4": {
1169 "pf_list_1": [
1170 {
1171 "seqid": "10",
1172 "network": "10.0.0.0/8",
1173 "le": "32",
1174 "action": "permit",
1175 }
1176 ]
1177 }
1178 }
1179 }
1180 }
1181 result = create_prefix_lists(tgen, input_dict_1)
1182 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1183
1184 # Api call to clear bgp, so config changes would be reflected
1185 dut = "r3"
1186 result = clear_bgp_and_verify(tgen, topo, dut)
1187 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1188
1189 # Verifying RIB routes
1190 dut = "r4"
1191 protocol = "bgp"
1192 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1193 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1194
1195 write_test_footer(tc_name)
1196
1197
1198 def test_ip_prefix_lists_implicit_deny(request):
1199 """
1200 Create ip prefix list and test implicit deny
1201 """
1202
1203 tgen = get_topogen()
1204 if BGP_CONVERGENCE is not True:
1205 pytest.skip("skipped because of BGP Convergence failure")
1206
1207 # test case name
1208 tc_name = request.node.name
1209 write_test_header(tc_name)
1210
1211 # Creating configuration from JSON
1212 reset_config_on_routers(tgen)
1213
1214 # Create Static Routes
1215 input_dict = {
1216 "r1": {
1217 "static_routes": [
1218 {"network": "10.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.2"}
1219 ]
1220 }
1221 }
1222 result = create_static_routes(tgen, input_dict)
1223 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1224
1225 # Create Static Routes
1226 input_dict_1 = {
1227 "r2": {
1228 "static_routes": [
1229 {"network": "20.0.20.1/32", "no_of_ip": 9, "next_hop": "10.0.0.1"}
1230 ]
1231 }
1232 }
1233 result = create_static_routes(tgen, input_dict)
1234 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1235
1236 # Api call to redistribute static routes
1237 # Create ip prefix list
1238 input_dict_3 = {
1239 "r3": {
1240 "prefix_lists": {
1241 "ipv4": {
1242 "pf_list_1": [
1243 {
1244 "seqid": "10",
1245 "network": "10.0.0.0/8",
1246 "le": "32",
1247 "action": "permit",
1248 }
1249 ]
1250 }
1251 }
1252 }
1253 }
1254 result = create_prefix_lists(tgen, input_dict_3)
1255 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1256
1257 # Configure prefix list to bgp neighbor
1258 input_dict_4 = {
1259 "r1": {
1260 "bgp": {
1261 "address_family": {
1262 "ipv4": {
1263 "unicast": {
1264 "redistribute": [
1265 {"redist_type": "static"},
1266 {"redist_type": "connected"},
1267 ]
1268 }
1269 }
1270 }
1271 }
1272 },
1273 "r2": {
1274 "bgp": {
1275 "address_family": {
1276 "ipv4": {
1277 "unicast": {
1278 "redistribute": [
1279 {"redist_type": "static"},
1280 {"redist_type": "connected"},
1281 ]
1282 }
1283 }
1284 }
1285 }
1286 },
1287 "r3": {
1288 "bgp": {
1289 "address_family": {
1290 "ipv4": {
1291 "unicast": {
1292 "neighbor": {
1293 "r4": {
1294 "dest_link": {
1295 "r3": {
1296 "prefix_lists": [
1297 {
1298 "name": "pf_list_1",
1299 "direction": "out",
1300 }
1301 ]
1302 }
1303 }
1304 }
1305 }
1306 }
1307 }
1308 }
1309 }
1310 },
1311 }
1312 result = create_router_bgp(tgen, topo, input_dict_4)
1313 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1314
1315 # Verifying RIB routes
1316 dut = "r4"
1317 protocol = "bgp"
1318 result = verify_rib(tgen, "ipv4", dut, input_dict, protocol=protocol)
1319 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
1320
1321 # Verifying RIB routes
1322 dut = "r4"
1323 protocol = "bgp"
1324 result = verify_rib(
1325 tgen, "ipv4", dut, input_dict_1, protocol=protocol, expected=False
1326 )
1327 assert (
1328 result is not True
1329 ), "Testcase {} : Failed \n Error: Routes still" " present in RIB".format(tc_name)
1330
1331 write_test_footer(tc_name)
1332
1333
1334 if __name__ == "__main__":
1335 args = ["-s"] + sys.argv[1:]
1336 sys.exit(pytest.main(args))