]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_local_asn/test_bgp_local_asn_topo2.py
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tests / topotests / bgp_local_asn / test_bgp_local_asn_topo2.py
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2022 by VMware, Inc. ("VMware")
4 # Used Copyright (c) 2018 by Network Device Education Foundation,
5 # Inc. ("NetDEF") in this file.
6 #
7 # Permission to use, copy, modify, and/or distribute this software
8 # for any purpose with or without fee is hereby granted, provided
9 # that the above copyright notice and this permission notice appear
10 # in all copies.
11 #
12 # THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
19 # OF THIS SOFTWARE.
20 #
21 ##########################################################################################################
22 #
23 # Testcases
24 #
25 ##########################################################################################################
26 ##########################################################################################################
27 #
28 # 1.10.1.2. Verify the BGP Local AS functionality by configuring 4 Byte AS in between eBGP Peers.
29 #
30 # 1.10.1.4. Verify the BGP Local AS functionality by configuring Old AS(local as) in 2 bytes and New AS in 4 bytes in between eBGP Peers.
31 #
32 ###############################################################################################################
33
34 import os
35 import sys
36 import time
37 import pytest
38
39 # Save the Current Working Directory to find configuration files.
40 CWD = os.path.dirname(os.path.realpath(__file__))
41 sys.path.append(os.path.join(CWD, "../"))
42 sys.path.append(os.path.join(CWD, "../lib/"))
43
44 # pylint: disable=C0413
45 # Import topogen and topotest helpers
46 from lib.topogen import Topogen, get_topogen
47 from lib.topotest import version_cmp
48
49 from lib.common_config import (
50 start_topology,
51 write_test_header,
52 create_static_routes,
53 write_test_footer,
54 reset_config_on_routers,
55 verify_rib,
56 step,
57 check_address_types,
58 check_router_status,
59 create_static_routes,
60 )
61
62 from lib.topolog import logger
63 from lib.bgp import (
64 verify_bgp_convergence,
65 verify_bgp_rib,
66 create_router_bgp,
67 verify_bgp_advertised_routes_from_neighbor,
68 )
69 from lib.topojson import build_config_from_json
70
71 pytestmark = [pytest.mark.bgpd, pytest.mark.staticd]
72
73 # Global variables
74 BGP_CONVERGENCE = False
75 ADDR_TYPES = check_address_types()
76 NETWORK = {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
77 NEXT_HOP_IP = {"ipv4": "Null0", "ipv6": "Null0"}
78
79
80 def setup_module(mod):
81 """
82 Sets up the pytest environment
83
84 * `mod`: module name
85 """
86
87 testsuite_run_time = time.asctime(time.localtime(time.time()))
88 logger.info("Testsuite start time: {}".format(testsuite_run_time))
89 logger.info("=" * 40)
90
91 logger.info("Running setup_module to create topology")
92
93 # This function initiates the topology build with Topogen...
94 json_file = "{}/bgp_local_asn_topo2.json".format(CWD)
95 tgen = Topogen(json_file, mod.__name__)
96 global topo
97 topo = tgen.json_topo
98 # ... and here it calls Mininet initialization functions.
99
100 # Starting topology, create tmp files which are loaded to routers
101 # to start daemons and then start routers
102 start_topology(tgen)
103
104 # Creating configuration from JSON
105 build_config_from_json(tgen, topo)
106
107 global BGP_CONVERGENCE
108 global ADDR_TYPES
109 ADDR_TYPES = check_address_types()
110
111 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
112 assert BGP_CONVERGENCE is True, "setup_module : Failed \n Error: {}".format(
113 BGP_CONVERGENCE
114 )
115
116 logger.info("Running setup_module() done")
117
118
119 def teardown_module():
120 """Teardown the pytest environment"""
121
122 logger.info("Running teardown_module to delete topology")
123
124 tgen = get_topogen()
125
126 # Stop toplogy and Remove tmp files
127 tgen.stop_topology()
128
129 logger.info(
130 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
131 )
132 logger.info("=" * 40)
133
134
135 ##########################################################################################################
136 #
137 # Testcases
138 #
139 ##########################################################################################################
140
141
142 def test_verify_bgp_local_as_in_4_Byte_AS_EBGP_p0(request):
143 """
144 Verify the BGP Local AS functionality by configuring 4 Byte AS in between eBGP Peers.
145 """
146
147 tgen = get_topogen()
148 global BGP_CONVERGENCE
149
150 if BGP_CONVERGENCE != True:
151 pytest.skip("skipped because of BGP Convergence failure")
152
153 # test case name
154 tc_name = request.node.name
155 write_test_header(tc_name)
156 if tgen.routers_have_failure():
157 check_router_status(tgen)
158 reset_config_on_routers(tgen)
159
160 step("Base config is done as part of JSON")
161 step("Configure local-as at R3 towards R4.")
162 for addr_type in ADDR_TYPES:
163 for neighbor in ["r2", "r4"]:
164 input_dict_r3 = {
165 "r3": {
166 "bgp": {
167 "local_as": "12000300",
168 "address_family": {
169 addr_type: {
170 "unicast": {
171 "neighbor": {
172 neighbor: {
173 "dest_link": {
174 "r3": {
175 "local_asn": {
176 "local_as": "12000110"
177 }
178 }
179 }
180 }
181 }
182 }
183 }
184 },
185 }
186 }
187 }
188 result = create_router_bgp(tgen, topo, input_dict_r3)
189 assert result is True, "Testcase {} :Failed \n Error: {}".format(
190 tc_name, result
191 )
192
193 for addr_type in ADDR_TYPES:
194 for dut, asn, neighbor in zip(
195 ["r2", "r4"], ["12000200", "12000400"], ["r3", "r3"]
196 ):
197 input_dict_r2_r4 = {
198 dut: {
199 "bgp": {
200 "local_as": asn,
201 "address_family": {
202 addr_type: {
203 "unicast": {
204 "neighbor": {
205 neighbor: {
206 "dest_link": {
207 dut: {
208 "local_asn": {
209 "remote_as": "12000110"
210 }
211 }
212 }
213 }
214 }
215 }
216 }
217 },
218 }
219 }
220 }
221 result = create_router_bgp(tgen, topo, input_dict_r2_r4)
222 assert result is True, "Testcase {} :Failed \n Error: {}".format(
223 tc_name, result
224 )
225
226 step("BGP neighborship is verified by following commands in R3 routers")
227 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
228 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
229 BGP_CONVERGENCE
230 )
231
232 # configure static routes
233 step(
234 "Done in base config: Advertise prefix 10.1.1.0/32 from Router-1(AS-12000100)."
235 )
236 step(
237 "Done in base config: Advertise an ipv6 prefix 10:1::1:0/128 from Router-1(AS-12000100)."
238 )
239 step("Verify that Static routes are redistributed in BGP process")
240
241 dut = "r1"
242 protocol = "bgp"
243 for addr_type in ADDR_TYPES:
244 # Enable static routes
245 input_static_r1 = {
246 "r1": {
247 "static_routes": [
248 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
249 ]
250 }
251 }
252
253 logger.info("Configure static routes")
254 result = create_static_routes(tgen, input_static_r1)
255 assert result is True, "Testcase {} : Failed \n Error: {}".format(
256 tc_name, result
257 )
258
259 step("configure redistribute static in Router BGP in R1")
260
261 input_static_redist_r1 = {
262 "r1": {
263 "bgp": {
264 "address_family": {
265 addr_type: {
266 "unicast": {"redistribute": [{"redist_type": "static"}]}
267 }
268 }
269 }
270 }
271 }
272 result = create_router_bgp(tgen, topo, input_static_redist_r1)
273 assert result is True, "Testcase {} : Failed \n Error: {}".format(
274 tc_name, result
275 )
276
277 step("Verify that Static routes are redistributed in BGP process")
278 for addr_type in ADDR_TYPES:
279 input_static_verify_r1 = {
280 "r1": {"static_routes": [{"network": NETWORK[addr_type]}]}
281 }
282
283 result = verify_rib(tgen, addr_type, "r1", input_static_verify_r1)
284 assert result is True, "Testcase {}: Failed \n Error: {}".format(
285 tc_name, result
286 )
287
288 for dut in ["r2", "r3", "r4"]:
289 result = verify_rib(tgen, addr_type, dut, input_static_r1)
290 assert result is True, "Testcase {}: Failed \n Error: {}".format(
291 tc_name, result
292 )
293
294 step(
295 "Verify that AS-12000110 is got added in the AS list 12000110 12000200 12000100 by following"
296 "commands at R3 router."
297 )
298 dut = "r3"
299 aspath = "12000110 12000200 12000100"
300 for addr_type in ADDR_TYPES:
301 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
302 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
303 assert result is True, "Testcase {} : Failed \n Error: {}".format(
304 tc_name, result
305 )
306
307 step("Configure local-as with no-prepend at R3 towards R4 & R2.")
308 for addr_type in ADDR_TYPES:
309 for neighbor in ["r2", "r4"]:
310 input_dict_r3 = {
311 "r3": {
312 "bgp": {
313 "local_as": "12000300",
314 "address_family": {
315 addr_type: {
316 "unicast": {
317 "neighbor": {
318 neighbor: {
319 "dest_link": {
320 "r3": {
321 "local_asn": {
322 "local_as": "12000110",
323 "no_prepend": True,
324 }
325 }
326 }
327 }
328 }
329 }
330 }
331 },
332 }
333 }
334 }
335 result = create_router_bgp(tgen, topo, input_dict_r3)
336 assert result is True, "Testcase {} :Failed \n Error: {}".format(
337 tc_name, result
338 )
339
340 step("BGP neighborship is verified by following commands in R3 routers")
341 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
342 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
343 BGP_CONVERGENCE
344 )
345
346 step("Verify advertised routes to R4 at R3")
347 expected_routes = {
348 "ipv4": [
349 {"network": "10.1.1.0/32", "nexthop": ""},
350 ],
351 "ipv6": [
352 {"network": "10:1::1:0/128", "nexthop": ""},
353 ],
354 }
355 result = verify_bgp_advertised_routes_from_neighbor(
356 tgen, topo, dut="r3", peer="r4", expected_routes=expected_routes
357 )
358 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
359
360 dut = "r3"
361 aspath = "12000200 12000100"
362 for addr_type in ADDR_TYPES:
363 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
364 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
365 assert result is True, "Testcase {} : Failed \n Error: {}".format(
366 tc_name, result
367 )
368
369 step("Configure local-as with no-prepend and replace-as at R3 towards R4 & R2.")
370 for addr_type in ADDR_TYPES:
371 for neighbor in ["r2", "r4"]:
372 input_dict_r3 = {
373 "r3": {
374 "bgp": {
375 "local_as": "12000300",
376 "address_family": {
377 addr_type: {
378 "unicast": {
379 "neighbor": {
380 neighbor: {
381 "dest_link": {
382 "r3": {
383 "local_asn": {
384 "local_as": "12000110",
385 "no_prepend": True,
386 "replace_as": True,
387 }
388 }
389 }
390 }
391 }
392 }
393 }
394 },
395 }
396 }
397 }
398 result = create_router_bgp(tgen, topo, input_dict_r3)
399 assert result is True, "Testcase {} :Failed \n Error: {}".format(
400 tc_name, result
401 )
402
403 step("BGP neighborship is verified by following commands in R3 routers")
404 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
405 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
406 BGP_CONVERGENCE
407 )
408
409 dut = "r4"
410 aspath = "12000110 12000200 12000100"
411 for addr_type in ADDR_TYPES:
412 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
413 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
414 assert result is True, "Testcase {} : Failed \n Error: {}".format(
415 tc_name, result
416 )
417
418 write_test_footer(tc_name)
419
420
421 def test_verify_bgp_local_as_in_old_AS2_new_AS4_EBGP_p0(request):
422 """
423 Verify the BGP Local AS functionality by configuring Old AS(local as) in
424 2 bytes and New AS in 4 bytes in between eBGP Peers.
425 """
426 tgen = get_topogen()
427 global BGP_CONVERGENCE
428
429 if BGP_CONVERGENCE != True:
430 pytest.skip("skipped because of BGP Convergence failure")
431 # test case name
432 tc_name = request.node.name
433 write_test_header(tc_name)
434 if tgen.routers_have_failure():
435 check_router_status(tgen)
436 reset_config_on_routers(tgen)
437
438 step("Base config is done as part of JSON")
439 step("Configure local-as at R3 towards R4.")
440 for addr_type in ADDR_TYPES:
441 for neighbor in ["r2", "r4"]:
442 input_dict_r3 = {
443 "r3": {
444 "bgp": {
445 "local_as": "12000300",
446 "address_family": {
447 addr_type: {
448 "unicast": {
449 "neighbor": {
450 neighbor: {
451 "dest_link": {
452 "r3": {"local_asn": {"local_as": "110"}}
453 }
454 }
455 }
456 }
457 }
458 },
459 }
460 }
461 }
462 result = create_router_bgp(tgen, topo, input_dict_r3)
463 assert result is True, "Testcase {} :Failed \n Error: {}".format(
464 tc_name, result
465 )
466
467 for addr_type in ADDR_TYPES:
468 for dut, asn, neighbor in zip(
469 ["r2", "r4"], ["12000200", "12000400"], ["r3", "r3"]
470 ):
471 input_dict_r2_r4 = {
472 dut: {
473 "bgp": {
474 "local_as": asn,
475 "address_family": {
476 addr_type: {
477 "unicast": {
478 "neighbor": {
479 neighbor: {
480 "dest_link": {
481 dut: {"local_asn": {"remote_as": "110"}}
482 }
483 }
484 }
485 }
486 }
487 },
488 }
489 }
490 }
491 result = create_router_bgp(tgen, topo, input_dict_r2_r4)
492 assert result is True, "Testcase {} :Failed \n Error: {}".format(
493 tc_name, result
494 )
495
496 step("BGP neighborship is verified by following commands in R3 routers")
497 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
498 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
499 BGP_CONVERGENCE
500 )
501
502 # configure static routes
503 step(
504 "Done in base config: Advertise prefix 10.1.1.0/32 from Router-1(AS-12000100)."
505 )
506 step(
507 "Done in base config: Advertise an ipv6 prefix 10:1::1:0/128 from Router-1(AS-12000100)."
508 )
509 step("Verify that Static routes are redistributed in BGP process")
510
511 dut = "r1"
512 protocol = "bgp"
513 for addr_type in ADDR_TYPES:
514 # Enable static routes
515 input_static_r1 = {
516 "r1": {
517 "static_routes": [
518 {"network": NETWORK[addr_type], "next_hop": NEXT_HOP_IP[addr_type]}
519 ]
520 }
521 }
522
523 logger.info("Configure static routes")
524 result = create_static_routes(tgen, input_static_r1)
525 assert result is True, "Testcase {} : Failed \n Error: {}".format(
526 tc_name, result
527 )
528
529 step("configure redistribute static in Router BGP in R1")
530
531 input_static_redist_r1 = {
532 "r1": {
533 "bgp": {
534 "address_family": {
535 addr_type: {
536 "unicast": {"redistribute": [{"redist_type": "static"}]}
537 }
538 }
539 }
540 }
541 }
542 result = create_router_bgp(tgen, topo, input_static_redist_r1)
543 assert result is True, "Testcase {} : Failed \n Error: {}".format(
544 tc_name, result
545 )
546
547 step("Verify that Static routes are redistributed in BGP process")
548 for addr_type in ADDR_TYPES:
549 input_static_verify_r1 = {
550 "r1": {"static_routes": [{"network": NETWORK[addr_type]}]}
551 }
552
553 result = verify_rib(tgen, addr_type, "r1", input_static_verify_r1)
554 assert result is True, "Testcase {}: Failed \n Error: {}".format(
555 tc_name, result
556 )
557
558 for dut in ["r3", "r4"]:
559 result = verify_rib(tgen, addr_type, dut, input_static_r1)
560 assert result is True, "Testcase {}: Failed \n Error: {}".format(
561 tc_name, result
562 )
563
564 for dut, input_routes in zip(["r1"], [input_static_r1]):
565 result = verify_rib(tgen, addr_type, dut, input_routes)
566 assert result is True, "Testcase {}: Failed \n Error: {}".format(
567 tc_name, result
568 )
569
570 step(
571 "Verify that AS-110 is got added in the AS list 110 12000200 12000100 by following"
572 "commands at R3 router."
573 )
574 dut = "r3"
575 aspath = "110 12000200 12000100"
576 for addr_type in ADDR_TYPES:
577 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
578 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
579 assert result is True, "Testcase {} : Failed \n Error: {}".format(
580 tc_name, result
581 )
582
583 step("Configure local-as with no-prepend at R3 towards R4 & R2.")
584 for addr_type in ADDR_TYPES:
585 for neighbor in ["r2", "r4"]:
586 input_dict_r3 = {
587 "r3": {
588 "bgp": {
589 "local_as": "12000300",
590 "address_family": {
591 addr_type: {
592 "unicast": {
593 "neighbor": {
594 neighbor: {
595 "dest_link": {
596 "r3": {
597 "local_asn": {
598 "local_as": "110",
599 "no_prepend": True,
600 }
601 }
602 }
603 }
604 }
605 }
606 }
607 },
608 }
609 }
610 }
611 result = create_router_bgp(tgen, topo, input_dict_r3)
612 assert result is True, "Testcase {} :Failed \n Error: {}".format(
613 tc_name, result
614 )
615
616 step("BGP neighborship is verified by following commands in R3 routers")
617 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
618 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
619 BGP_CONVERGENCE
620 )
621
622 step("Verify advertised routes to R4 at R3")
623 expected_routes = {
624 "ipv4": [
625 {"network": "10.1.1.0/32", "nexthop": ""},
626 ],
627 "ipv6": [
628 {"network": "10:1::1:0/128", "nexthop": ""},
629 ],
630 }
631 result = verify_bgp_advertised_routes_from_neighbor(
632 tgen, topo, dut="r3", peer="r4", expected_routes=expected_routes
633 )
634 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
635
636 dut = "r3"
637 aspath = "12000200 12000100"
638 for addr_type in ADDR_TYPES:
639 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
640 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
641 assert result is True, "Testcase {} : Failed \n Error: {}".format(
642 tc_name, result
643 )
644
645 step("Configure local-as with no-prepend and replace-as at R3 towards R4 & R2.")
646 for addr_type in ADDR_TYPES:
647 for neighbor in ["r2", "r4"]:
648 input_dict_r3 = {
649 "r3": {
650 "bgp": {
651 "local_as": "12000300",
652 "address_family": {
653 addr_type: {
654 "unicast": {
655 "neighbor": {
656 neighbor: {
657 "dest_link": {
658 "r3": {
659 "local_asn": {
660 "local_as": "110",
661 "no_prepend": True,
662 "replace_as": True,
663 }
664 }
665 }
666 }
667 }
668 }
669 }
670 },
671 }
672 }
673 }
674 result = create_router_bgp(tgen, topo, input_dict_r3)
675 assert result is True, "Testcase {} :Failed \n Error: {}".format(
676 tc_name, result
677 )
678
679 step("BGP neighborship is verified by following commands in R3 routers")
680 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
681 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
682 BGP_CONVERGENCE
683 )
684
685 dut = "r4"
686 aspath = "110 12000200 12000100"
687 for addr_type in ADDR_TYPES:
688 input_static_r1 = {"r1": {"static_routes": [{"network": NETWORK[addr_type]}]}}
689 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r1, aspath=aspath)
690 assert result is True, "Testcase {} : Failed \n Error: {}".format(
691 tc_name, result
692 )
693
694 write_test_footer(tc_name)
695
696
697 if __name__ == "__main__":
698 args = ["-s"] + sys.argv[1:]
699 sys.exit(pytest.main(args))