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