]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_features/test_bgp_features.py
2 # SPDX-License-Identifier: ISC
6 # Part of NetDEF Topology Tests
8 # Copyright (c) 2020 by
9 # Network Device Education Foundation, Inc. ("NetDEF")
13 test_bgp_features.py: Test various BGP features.
24 # Save the Current Working Directory to find configuration files.
25 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
26 sys
.path
.append(os
.path
.join(CWD
, "../"))
28 # pylint: disable=C0413
29 # Import topogen and topotest helpers
30 from lib
import topotest
31 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
32 from lib
.topolog
import logger
34 # Required to instantiate the topology builder class.
36 pytestmark
= [pytest
.mark
.bgpd
, pytest
.mark
.ospfd
]
38 #####################################################
40 # Network Topology Definition
42 #####################################################
46 for rtrNum
in range(1, 6):
47 tgen
.add_router("r{}".format(rtrNum
))
50 for peer_num
in range(1, 5):
52 "peer{}".format(peer_num
),
53 ip
="192.168.101.{}".format(peer_num
+ 2),
54 defaultRoute
="via 192.168.101.1",
57 # Setup Switches and connections
58 for swNum
in range(1, 11):
59 tgen
.add_switch("sw{}".format(swNum
))
61 # Add connections to stub switches
62 tgen
.gears
["r1"].add_link(tgen
.gears
["sw6"])
63 tgen
.gears
["r2"].add_link(tgen
.gears
["sw7"])
64 tgen
.gears
["r3"].add_link(tgen
.gears
["sw8"])
65 tgen
.gears
["r4"].add_link(tgen
.gears
["sw9"])
66 tgen
.gears
["r5"].add_link(tgen
.gears
["sw10"])
68 # Add connections to R1-R2-R3 core
69 tgen
.gears
["r1"].add_link(tgen
.gears
["sw1"])
70 tgen
.gears
["r1"].add_link(tgen
.gears
["sw3"])
71 tgen
.gears
["r2"].add_link(tgen
.gears
["sw1"])
72 tgen
.gears
["r2"].add_link(tgen
.gears
["sw2"])
73 tgen
.gears
["r3"].add_link(tgen
.gears
["sw2"])
74 tgen
.gears
["r3"].add_link(tgen
.gears
["sw3"])
76 # Add connections to external R4/R5 Routers
77 tgen
.gears
["r1"].add_link(tgen
.gears
["sw4"])
78 tgen
.gears
["r4"].add_link(tgen
.gears
["sw4"])
79 tgen
.gears
["r2"].add_link(tgen
.gears
["sw5"])
80 tgen
.gears
["r5"].add_link(tgen
.gears
["sw5"])
82 # Add ExaBGP peers to sw4
83 tgen
.gears
["peer1"].add_link(tgen
.gears
["sw4"])
84 tgen
.gears
["peer2"].add_link(tgen
.gears
["sw4"])
85 tgen
.gears
["peer3"].add_link(tgen
.gears
["sw4"])
86 tgen
.gears
["peer4"].add_link(tgen
.gears
["sw4"])
89 #####################################################
93 #####################################################
96 def setup_module(module
):
97 tgen
= Topogen(build_topo
, module
.__name
__)
101 router_list
= tgen
.routers()
102 for rname
, router
in router_list
.items():
104 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
106 if os
.path
.exists(os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))):
107 logger
.info("{} uses BGPd".format(rname
))
109 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
111 if os
.path
.exists(os
.path
.join(CWD
, "{}/ospfd.conf".format(rname
))):
112 logger
.info("{} uses OSPFd".format(rname
))
114 TopoRouter
.RD_OSPF
, os
.path
.join(CWD
, "{}/ospfd.conf".format(rname
))
116 if os
.path
.exists(os
.path
.join(CWD
, "{}/ospf6d.conf".format(rname
))):
117 logger
.info("{} uses OSPF6d".format(rname
))
119 TopoRouter
.RD_OSPF6
, os
.path
.join(CWD
, "{}/ospf6d.conf".format(rname
))
124 def teardown_module(module
):
129 def test_ospf_convergence():
130 "Test for OSPFv2 topology convergence"
133 # Skip if previous fatal error condition is raised
134 if tgen
.routers_have_failure():
135 pytest
.skip(tgen
.errors
)
137 # Check Router r1, r2 & r3 OSPF
138 for rtrNum
in range(1, 4):
139 logger
.info("Checking OSPFv2 convergence on router r{}".format(rtrNum
))
141 router
= tgen
.gears
["r{}".format(rtrNum
)]
142 reffile
= os
.path
.join(CWD
, "r{}/ospf_neighbor.json".format(rtrNum
))
143 expected
= json
.loads(open(reffile
).read())
145 test_func
= functools
.partial(
146 topotest
.router_json_cmp
, router
, "show ip ospf neighbor json", expected
148 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
149 assertmsg
= "OSPF router R{} did not converge".format(rtrNum
)
150 assert res
is None, assertmsg
153 def test_bgp_convergence():
154 "Test for BGP topology convergence"
157 # Skip if previous fatal error condition is raised
158 if tgen
.routers_have_failure():
159 pytest
.skip(tgen
.errors
)
161 # Check Router r1 & r2 BGP
162 for rtrNum
in [1, 2, 4, 5]:
163 logger
.info("Checking BGP IPv4 convergence on router r{}".format(rtrNum
))
165 router
= tgen
.gears
["r{}".format(rtrNum
)]
166 reffile
= os
.path
.join(CWD
, "r{}/bgp_summary.json".format(rtrNum
))
167 expected
= json
.loads(open(reffile
).read())
169 test_func
= functools
.partial(
170 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
172 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
173 assertmsg
= "BGP router R{} did not converge".format(rtrNum
)
174 assert res
is None, assertmsg
179 def get_shut_msg_count(tgen
):
181 for rtrNum
in [2, 4]:
182 shutmsg
= tgen
.net
["r{}".format(rtrNum
)].cmd_nostatus(
183 'grep -c "NOTIFICATION.*Cease/Administrative Shutdown" bgpd.log', warn
=False
186 shuts
[rtrNum
] = int(shutmsg
.strip())
192 def test_bgp_shutdown():
193 "Test BGP instance shutdown"
197 # Skip if previous fatal error condition is raised
198 if tgen
.routers_have_failure():
199 pytest
.skip(tgen
.errors
)
201 shuts_before
= get_shut_msg_count(tgen
)
204 'vtysh -c "conf t" -c "router bgp 65000" -c "bgp shutdown message ABCDabcd"'
207 # Check BGP Summary on local and remote routers
208 for rtrNum
in [1, 2, 4]:
210 "Checking BGP Summary after shutdown of R1 BGP on router r{}".format(rtrNum
)
213 router
= tgen
.gears
["r{}".format(rtrNum
)]
214 reffile
= os
.path
.join(CWD
, "r{}/bgp_shutdown_summary.json".format(rtrNum
))
215 expected
= json
.loads(open(reffile
).read())
217 test_func
= functools
.partial(
218 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
220 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
221 assertmsg
= "BGP sessions on router R{} are in incorrect state (not down as expected?)".format(
224 assert res
is None, assertmsg
226 shuts_after
= get_shut_msg_count(tgen
)
228 for k
in shuts_before
:
229 assert shuts_before
[k
] + 1 == shuts_after
[k
]
232 def test_bgp_shutdown_message():
233 "Test BGP Peer Shutdown Message"
237 # Skip if previous fatal error condition is raised
238 if tgen
.routers_have_failure():
239 pytest
.skip(tgen
.errors
)
241 for rtrNum
in [2, 4]:
242 logger
.info("Checking BGP shutdown received on router r{}".format(rtrNum
))
244 shut_message
= tgen
.net
["r{}".format(rtrNum
)].cmd(
245 'grep -e "NOTIFICATION.*Cease/Administrative Shutdown.*ABCDabcd" bgpd.log'
247 assertmsg
= "BGP shutdown message not received on router R{}".format(rtrNum
)
248 assert shut_message
!= "", assertmsg
251 def test_bgp_no_shutdown():
252 "Test BGP instance no shutdown"
256 # Skip if previous fatal error condition is raised
257 if tgen
.routers_have_failure():
258 pytest
.skip(tgen
.errors
)
260 tgen
.net
["r1"].cmd('vtysh -c "conf t" -c "router bgp 65000" -c "no bgp shutdown"')
262 # Check BGP Summary on local and remote routers
263 for rtrNum
in [1, 2, 4]:
265 "Checking BGP Summary after removing bgp shutdown on router r1 on router r{}".format(
270 router
= tgen
.gears
["r{}".format(rtrNum
)]
271 reffile
= os
.path
.join(CWD
, "r{}/bgp_summary.json".format(rtrNum
))
272 expected
= json
.loads(open(reffile
).read())
274 test_func
= functools
.partial(
275 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
277 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
278 assertmsg
= "BGP sessions on router R{} are in incorrect state (not down as expected?)".format(
281 assert res
is None, assertmsg
284 def test_bgp_metric_config():
285 "Test BGP Changing metric values in route-maps"
289 # Skip if previous fatal error condition is raised
290 if tgen
.routers_have_failure():
291 pytest
.skip(tgen
.errors
)
293 logger
.info("Configuring bgp route-maps on router r1 and r2 to update metric")
295 # # Adding the following configuration to r1:
297 # address-family ipv4 unicast
298 # neighbor 192.168.0.2 route-map addmetric-in in
299 # neighbor 192.168.0.2 route-map addmetric-out out
300 # neighbor 192.168.101.2 route-map setmetric-in in
301 # neighbor 192.168.101.2 route-map setmetric-out out
302 # exit-address-family
304 # ip prefix-list net1 seq 10 permit 192.168.101.0/24
305 # ip prefix-list net2 seq 20 permit 192.168.1.0/24
307 # route-map setmetric-in permit 10
308 # match ip address prefix-list net1
311 # route-map setmetric-in permit 20
313 # route-map setmetric-out permit 10
314 # match ip address prefix-list net2
317 # route-map setmetric-out permit 20
319 # route-map addmetric-in permit 10
322 # route-map addmetric-out permit 10
327 'vtysh -c "conf t" -c "router bgp 65000" '
328 + '-c "address-family ipv4 unicast" '
329 + '-c "neighbor 192.168.0.2 route-map addmetric-in in" '
330 + '-c "neighbor 192.168.0.2 route-map addmetric-out out" '
331 + '-c "neighbor 192.168.101.2 route-map setmetric-in in" '
332 + '-c "neighbor 192.168.101.2 route-map setmetric-out out" '
336 + '-c "ip prefix-list net1 seq 10 permit 192.168.101.0/24" '
337 + '-c "ip prefix-list net2 seq 20 permit 192.168.1.0/24"'
341 + '-c "route-map setmetric-in permit 10" '
342 + '-c "match ip address prefix-list net1" '
343 + '-c "set metric 111" '
344 + '-c "route-map setmetric-in permit 20"'
348 + '-c "route-map setmetric-out permit 10" '
349 + '-c "match ip address prefix-list net2" '
350 + '-c "set metric 1011" '
351 + '-c "route-map setmetric-out permit 20"'
355 + '-c "route-map addmetric-in permit 10" '
356 + '-c "set metric +11"'
360 + '-c "route-map addmetric-out permit 10" '
361 + '-c "set metric +12"'
364 # # Adding the following configuration to r2:
366 # address-family ipv4 unicast
367 # neighbor 192.168.0.1 route-map subtractmetric-in in
368 # neighbor 192.168.0.1 route-map subtractmetric-out out
369 # neighbor 192.168.201.2 route-map setmetric-in in
370 # neighbor 192.168.201.2 route-map setmetric-out out
371 # exit-address-family
373 # ip prefix-list net1 seq 10 permit 192.168.201.0/24
374 # ip prefix-list net2 seq 20 permit 192.168.2.0/24
376 # route-map setmetric-in permit 10
377 # match ip address prefix-list net1
380 # route-map setmetric-in permit 20
382 # route-map setmetric-out permit 10
383 # match ip address prefix-list net2
386 # route-map setmetric-out permit 20
388 # route-map subtractmetric-in permit 10
391 # route-map subtractmetric-out permit 10
396 'vtysh -c "conf t" -c "router bgp 65000" '
397 + '-c "address-family ipv4 unicast" '
398 + '-c "neighbor 192.168.0.1 route-map subtractmetric-in in" '
399 + '-c "neighbor 192.168.0.1 route-map subtractmetric-out out" '
400 + '-c "neighbor 192.168.201.2 route-map setmetric-in in" '
401 + '-c "neighbor 192.168.201.2 route-map setmetric-out out" '
405 + '-c "ip prefix-list net1 seq 10 permit 192.168.201.0/24" '
406 + '-c "ip prefix-list net2 seq 20 permit 192.168.2.0/24" '
410 + '-c "route-map setmetric-in permit 10" '
411 + '-c "match ip address prefix-list net1" '
412 + '-c "set metric 222" '
413 + '-c "route-map setmetric-in permit 20"'
417 + '-c "route-map setmetric-out permit 10" '
418 + '-c "match ip address prefix-list net2" '
419 + '-c "set metric 2022" '
420 + '-c "route-map setmetric-out permit 20"'
424 + '-c "route-map subtractmetric-in permit 10" '
425 + '-c "set metric -22"'
429 + '-c "route-map subtractmetric-out permit 10" '
430 + '-c "set metric -23"'
433 # Clear IN the bgp neighbors to make sure the route-maps are applied
435 'vtysh -c "clear ip bgp 192.168.0.2 in" ' + '-c "clear ip bgp 192.168.101.2 in"'
438 'vtysh -c "clear ip bgp 192.168.0.1 in" ' + '-c "clear ip bgp 192.168.201.2 in"'
443 # Checking BGP config - should show the bgp metric settings in the route-maps
444 logger
.info("Checking BGP configuration for correct 'set metric' values")
447 tgen
.net
["r1"].cmd('vtysh -c "show running" | grep "^ set metric 111"').rstrip()
450 "'set metric 111' configuration applied to R1, but not visible in configuration"
452 assert setmetric111
== " set metric 111", assertmsg
455 tgen
.net
["r2"].cmd('vtysh -c "show running" | grep "^ set metric 222"').rstrip()
458 "'set metric 222' configuration applied to R2, but not visible in configuration"
460 assert setmetric222
== " set metric 222", assertmsg
463 def test_bgp_metric_add_config():
464 "Test BGP Changing metric values in route-maps"
468 # Skip if previous fatal error condition is raised
469 if tgen
.routers_have_failure():
470 pytest
.skip(tgen
.errors
)
472 logger
.info("Checking BGP configuration for correct 'set metric' ADD value")
475 tgen
.net
["r1"].cmd('vtysh -c "show running" | grep "^ set metric +11"').rstrip()
478 "'set metric +11' configuration applied to R1, but not visible in configuration"
480 assert setmetricP11
== " set metric +11", assertmsg
483 def test_bgp_metric_subtract_config():
484 "Test BGP Changing metric values in route-maps"
488 # Skip if previous fatal error condition is raised
489 if tgen
.routers_have_failure():
490 pytest
.skip(tgen
.errors
)
492 logger
.info("Checking BGP configuration for correct 'set metric' SUBTRACT value")
495 tgen
.net
["r2"].cmd('vtysh -c "show running" | grep "^ set metric -22"').rstrip()
498 "'set metric -22' configuration applied to R2, but not visible in configuration"
500 assert setmetricM22
== " set metric -22", assertmsg
503 def test_bgp_set_metric():
504 "Test setting metrics"
508 # Skip if previous fatal error condition is raised
509 if tgen
.routers_have_failure():
510 pytest
.skip(tgen
.errors
)
512 logger
.info("Test absolute metric")
514 # Check BGP Summary on local and remote routers
515 for rtrNum
in [1, 2, 4, 5]:
516 logger
.info("Checking metrics of BGP router on r{}".format(rtrNum
))
518 router
= tgen
.gears
["r{}".format(rtrNum
)]
519 reffile
= os
.path
.join(CWD
, "r{}/show_bgp_metric_test.json".format(rtrNum
))
520 expected
= json
.loads(open(reffile
).read())
522 test_func
= functools
.partial(
523 topotest
.router_json_cmp
, router
, "show ip bgp json", expected
525 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
526 assertmsg
= "BGP metrics on router r{} wrong".format(rtrNum
)
527 assert res
is None, assertmsg
530 def test_bgp_remove_metric_rmaps():
531 "Test removing route-maps with metric changes again"
535 # Skip if previous fatal error condition is raised
536 if tgen
.routers_have_failure():
537 pytest
.skip(tgen
.errors
)
539 logger
.info("Test absolute metric")
541 # Remove metric route-maps and relevant comfiguration
544 'vtysh -c "conf t" -c "router bgp 65000" '
545 + '-c "address-family ipv4 unicast" '
546 + '-c "no neighbor 192.168.0.2 route-map addmetric-in in" '
547 + '-c "no neighbor 192.168.0.2 route-map addmetric-out out" '
548 + '-c "no neighbor 192.168.101.2 route-map setmetric-in in" '
549 + '-c "no neighbor 192.168.101.2 route-map setmetric-out out" '
553 + '-c "no ip prefix-list net1" '
554 + '-c "no ip prefix-list net2"'
556 tgen
.net
["r1"].cmd('vtysh -c "conf t" ' + '-c "no route-map setmetric-in" ')
557 tgen
.net
["r1"].cmd('vtysh -c "conf t" ' + '-c "no route-map setmetric-out" ')
558 tgen
.net
["r1"].cmd('vtysh -c "conf t" ' + '-c "no route-map addmetric-in" ')
559 tgen
.net
["r1"].cmd('vtysh -c "conf t" ' + '-c "no route-map addmetric-out" ')
562 'vtysh -c "conf t" -c "router bgp 65000" '
563 + '-c "address-family ipv4 unicast" '
564 + '-c "no neighbor 192.168.0.1 route-map subtractmetric-in in" '
565 + '-c "no neighbor 192.168.0.1 route-map subtractmetric-out out" '
566 + '-c "no neighbor 192.168.201.2 route-map setmetric-in in" '
567 + '-c "no neighbor 192.168.201.2 route-map setmetric-out out" '
571 + '-c "no ip prefix-list net1" '
572 + '-c "no ip prefix-list net2" '
574 tgen
.net
["r2"].cmd('vtysh -c "conf t" ' + '-c "no route-map setmetric-in" ')
575 tgen
.net
["r2"].cmd('vtysh -c "conf t" ' + '-c "no route-map setmetric-out" ')
576 tgen
.net
["r2"].cmd('vtysh -c "conf t" ' + '-c "no route-map addmetric-in" ')
577 tgen
.net
["r2"].cmd('vtysh -c "conf t" ' + '-c "no route-map addmetric-out" ')
579 # Clear IN the bgp neighbors to make sure the route-maps are applied
581 'vtysh -c "clear ip bgp 192.168.0.2 in" ' + '-c "clear ip bgp 192.168.101.2 in"'
584 'vtysh -c "clear ip bgp 192.168.0.1 in" ' + '-c "clear ip bgp 192.168.201.2 in"'
589 # Check BGP Summary on local and remote routers
590 for rtrNum
in [1, 2]:
591 logger
.info("Checking metrics of BGP router on r{}".format(rtrNum
))
593 router
= tgen
.gears
["r{}".format(rtrNum
)]
594 reffile
= os
.path
.join(CWD
, "r{}/show_bgp.json".format(rtrNum
))
595 expected
= json
.loads(open(reffile
).read())
597 test_func
= functools
.partial(
598 topotest
.router_json_cmp
, router
, "show ip bgp json", expected
600 _
, res
= topotest
.run_and_expect(test_func
, None, count
=60, wait
=2)
601 assertmsg
= "BGP routes on router r{} are wrong after removing metric route-maps".format(
604 assert res
is None, assertmsg
607 def test_bgp_norib():
608 "Test BGP disable RIB (Zebra) Route Install"
612 # Skip if previous fatal error condition is raised
613 if tgen
.routers_have_failure():
614 pytest
.skip(tgen
.errors
)
616 logger
.info("Configuring 'bgp no-rib' on router r1")
618 tgen
.net
["r1"].cmd('vtysh -c "conf t" -c "bgp no-rib"')
620 # Checking BGP config - should show the "bgp no-rib" under the router bgp section
621 logger
.info("Checking BGP configuration for 'bgp no-rib'")
624 tgen
.net
["r1"].cmd('vtysh -c "show running bgpd" | grep "^bgp no-rib"').rstrip()
627 assertmsg
= "'bgp no-rib' configuration applied, but not visible in configuration"
628 assert norib_cfg
== "bgp no-rib", assertmsg
631 def test_bgp_norib_routes():
632 "Test Routes in Zebra and BGP with the 'bgp-norib' configuration"
636 # Skip if previous fatal error condition is raised
637 if tgen
.routers_have_failure():
638 pytest
.skip(tgen
.errors
)
640 # Checking local BGP routes - they need to be gone from Zebra
641 logger
.info("Checking Zebra routes after removing bgp shutdown on router r1")
643 router
= tgen
.gears
["r1"]
644 reffile
= os
.path
.join(CWD
, "r1/ip_route_norib.json")
645 expected
= json
.loads(open(reffile
).read())
647 test_func
= functools
.partial(
648 topotest
.router_json_cmp
, router
, "show ip route json", expected
650 _
, res
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=2)
651 assertmsg
= "Zebra IPv4 Routes after configuring 'bgp no-rib' (There should be no BGP routes in Zebra anymore)"
652 assert res
is None, assertmsg
654 # Check BGP Summary on local and remote routers
655 for rtrNum
in [1, 2, 4]:
657 "Checking BGP Summary after 'bgp no-rib' on router r1 on router r{}".format(
662 router
= tgen
.gears
["r{}".format(rtrNum
)]
663 reffile
= os
.path
.join(CWD
, "r{}/bgp_summary.json".format(rtrNum
))
664 expected
= json
.loads(open(reffile
).read())
666 test_func
= functools
.partial(
667 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
669 _
, res
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=2)
670 assertmsg
= "BGP sessions on router R{} has incorrect routes after adding 'bgp no-rib on r1'".format(
673 assert res
is None, assertmsg
678 def test_bgp_disable_norib():
679 "Test BGP disabling the no-RIB (Zebra) Route Install"
683 # Skip if previous fatal error condition is raised
684 if tgen
.routers_have_failure():
685 pytest
.skip(tgen
.errors
)
687 logger
.info("Configuring 'no bgp no-rib' on router r1")
689 tgen
.net
["r1"].cmd('vtysh -c "conf t" -c "no bgp no-rib"')
691 # Checking BGP config - should show the "bgp no-rib" under the router bgp section
692 logger
.info("Checking BGP configuration for 'bgp no-rib'")
696 .cmd('vtysh -c "show running bgpd" | grep "^ bgp no-rib"')
701 "'no bgp no-rib'configuration applied, but still visible in configuration"
703 assert norib_cfg
== "", assertmsg
706 def test_bgp_disable_norib_routes():
707 "Test Routes in Zebra and BGP with the 'bgp-norib' configuration"
711 # Skip if previous fatal error condition is raised
712 if tgen
.routers_have_failure():
713 pytest
.skip(tgen
.errors
)
715 # Checking local BGP routes - they need to be gone from Zebra
716 logger
.info("Checking Zebra routes after removing bgp shutdown on router r1")
718 router
= tgen
.gears
["r1"]
719 reffile
= os
.path
.join(CWD
, "r1/ip_route.json")
720 expected
= json
.loads(open(reffile
).read())
722 test_func
= functools
.partial(
723 topotest
.router_json_cmp
, router
, "show ip route json", expected
725 _
, res
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=2)
726 assertmsg
= "Zebra IPv4 Routes wrong after removing the 'bgp no-rib'"
727 assert res
is None, assertmsg
729 # Check BGP Summary on local and remote routers
730 for rtrNum
in [1, 2, 4]:
732 "Checking BGP Summary after removing the 'bgp no-rib' on router r1 on router r{}".format(
737 router
= tgen
.gears
["r{}".format(rtrNum
)]
738 reffile
= os
.path
.join(CWD
, "r{}/bgp_summary.json".format(rtrNum
))
739 expected
= json
.loads(open(reffile
).read())
741 test_func
= functools
.partial(
742 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
744 _
, res
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=2)
745 assertmsg
= "BGP sessions on router R{} has incorrect routes after removing 'bgp no-rib on r1'".format(
748 assert res
is None, assertmsg
753 def test_bgp_delayopen_without():
754 "Optional test of BGP functionality and behaviour without DelayOpenTimer enabled to establish a reference for following tests"
756 if tgen
.routers_have_failure():
757 pytest
.skip(tgen
.errors
)
759 # part 1: no delay r1 <=> no delay r4
761 "Starting optional test of BGP functionality without DelayOpenTimer enabled to establish a reference for following tests"
764 # 1.1 enable peering shutdown
765 logger
.info("Enable shutdown of peering between r1 and r4")
767 'vtysh -c "conf t" -c "router bgp 65000" -c "neighbor 192.168.101.2 shutdown"'
770 'vtysh -c "conf t" -c "router bgp 65100" -c "neighbor 192.168.101.1 shutdown"'
773 # 1.2 wait for peers to shut down (poll output)
774 for router_num
in [1, 4]:
776 "Checking BGP summary after enabling shutdown of peering on r{}".format(
780 router
= tgen
.gears
["r{}".format(router_num
)]
781 reffile
= os
.path
.join(
782 CWD
, "r{}/bgp_delayopen_summary_shutdown.json".format(router_num
)
784 expected
= json
.loads(open(reffile
).read())
785 test_func
= functools
.partial(
786 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
788 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
789 assertmsg
= "BGP session on r{} did not shut down peer".format(router_num
)
790 assert res
is None, assertmsg
792 # 1.3 disable peering shutdown
793 logger
.info("Disable shutdown of peering between r1 and r4")
795 'vtysh -c "conf t" -c "router bgp 65000" -c "no neighbor 192.168.101.2 shutdown"'
798 'vtysh -c "conf t" -c "router bgp 65100" -c "no neighbor 192.168.101.1 shutdown"'
801 # 1.4 wait for peers to establish connection (poll output)
802 for router_num
in [1, 4]:
804 "Checking BGP summary after disabling shutdown of peering on r{}".format(
808 router
= tgen
.gears
["r{}".format(router_num
)]
809 reffile
= os
.path
.join(
810 CWD
, "r{}/bgp_delayopen_summary_established.json".format(router_num
)
812 expected
= json
.loads(open(reffile
).read())
813 test_func
= functools
.partial(
814 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
816 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
818 "BGP session on r{} did not establish a connection with peer".format(
822 assert res
is None, assertmsg
826 # end test_bgp_delayopen_without
829 def test_bgp_delayopen_singular():
830 "Test of BGP functionality and behaviour with DelayOpenTimer enabled on one side of the peering"
833 if tgen
.routers_have_failure():
834 pytest
.skip(tgen
.errors
)
836 # part 2: delay 240s r1 <=> no delay r4
838 "Starting test of BGP functionality and behaviour with DelayOpenTimer enabled on one side of the peering"
841 # 2.1 enable peering shutdown
842 logger
.info("Enable shutdown of peering between r1 and r4")
844 'vtysh -c "conf t" -c "router bgp 65000" -c "neighbor 192.168.101.2 shutdown"'
847 'vtysh -c "conf t" -c "router bgp 65100" -c "neighbor 192.168.101.1 shutdown"'
850 # 2.2 wait for peers to shut down (poll output)
851 for router_num
in [1, 4]:
853 "Checking BGP summary after disabling shutdown of peering on r{}".format(
857 router
= tgen
.gears
["r{}".format(router_num
)]
858 reffile
= os
.path
.join(
859 CWD
, "r{}/bgp_delayopen_summary_shutdown.json".format(router_num
)
861 expected
= json
.loads(open(reffile
).read())
862 test_func
= functools
.partial(
863 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
865 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
866 assertmsg
= "BGP session on r{} did not shut down peer".format(router_num
)
867 assert res
is None, assertmsg
869 # 2.3 set delayopen on R1 to 240
870 logger
.info("Setting DelayOpenTime for neighbor r4 to 240 seconds on r1")
872 'vtysh -c "conf t" -c "router bgp 65000" -c "neighbor 192.168.101.2 timers delayopen 240"'
875 # 2.4 check config (poll output)
876 logger
.info("Checking BGP neighbor configuration after setting DelayOpenTime on r1")
877 router
= tgen
.gears
["r1"]
878 reffile
= os
.path
.join(CWD
, "r1/bgp_delayopen_neighbor.json")
879 expected
= json
.loads(open(reffile
).read())
880 test_func
= functools
.partial(
881 topotest
.router_json_cmp
, router
, "show bgp neighbors json", expected
883 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
884 assertmsg
= "BGP session on r1 failed to set DelayOpenTime for r4"
885 assert res
is None, assertmsg
887 # 2.5 disable peering shutdown
888 logger
.info("Disable shutdown of peering between r1 and r4")
890 'vtysh -c "conf t" -c "router bgp 65000" -c "no neighbor 192.168.101.2 shutdown"'
893 'vtysh -c "conf t" -c "router bgp 65100" -c "no neighbor 192.168.101.1 shutdown"'
896 # 2.6 wait for peers to establish connection (poll output)
897 for router_num
in [1, 4]:
899 "Checking BGP summary after disabling shutdown of peering on r{}".format(
903 router
= tgen
.gears
["r{}".format(router_num
)]
904 reffile
= os
.path
.join(
905 CWD
, "r{}/bgp_delayopen_summary_established.json".format(router_num
)
907 expected
= json
.loads(open(reffile
).read())
908 test_func
= functools
.partial(
909 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
911 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
913 "BGP session on r{} did not establish a connection with peer".format(
917 assert res
is None, assertmsg
919 # 2.7 unset delayopen on R1
920 logger
.info("Disabling DelayOpenTimer for neighbor r4 on r1")
922 'vtysh -c "conf t" -c "router bgp 65000" -c "no neighbor 192.168.101.2 timers delayopen"'
925 # 2.8 check config (poll output)
927 "Checking BGP neighbor configuration after disabling DelayOpenTimer on r1"
931 .cmd('vtysh -c "show bgp neighbors json" | grep "DelayOpenTimeMsecs"')
934 assertmsg
= "BGP session on r1 failed disable DelayOpenTimer for peer r4"
935 assert delayopen_cfg
== "", assertmsg
939 # end test_bgp_delayopen_singular
942 def test_bgp_delayopen_dual():
943 "Test of BGP functionality and behaviour with DelayOpenTimer enabled on both sides of the peering with different timer intervals"
945 if tgen
.routers_have_failure():
946 pytest
.skip(tgen
.errors
)
948 # part 3: delay 60s R2 <=> delay 30s R5
950 "Starting test of BGP functionality and behaviour with DelayOpenTimer enabled on both sides of the peering with different timer intervals"
953 # 3.1 enable peering shutdown
954 logger
.info("Enable shutdown of peering between r2 and r5")
956 'vtysh -c "conf t" -c "router bgp 65000" -c "neighbor 192.168.201.2 shutdown"'
959 'vtysh -c "conf t" -c "router bgp 65200" -c "neighbor 192.168.201.1 shutdown"'
962 # 3.2 wait for peers to shut down (pool output)
963 for router_num
in [2, 5]:
965 "Checking BGP summary after disabling shutdown of peering on r{}".format(
969 router
= tgen
.gears
["r{}".format(router_num
)]
970 reffile
= os
.path
.join(
971 CWD
, "r{}/bgp_delayopen_summary_shutdown.json".format(router_num
)
973 expected
= json
.loads(open(reffile
).read())
974 test_func
= functools
.partial(
975 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
977 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
978 assertmsg
= "BGP session on r{} did not shut down peer".format(router_num
)
979 assert res
is None, assertmsg
981 # 3.3 set delayopen on R2 to 60s and on R5 to 30s
982 logger
.info("Setting DelayOpenTime for neighbor r5 to 60 seconds on r2")
984 'vtysh -c "conf t" -c "router bgp 65000" -c "neighbor 192.168.201.2 timers delayopen 60"'
986 logger
.info("Setting DelayOpenTime for neighbor r2 to 30 seconds on r5")
988 'vtysh -c "conf t" -c "router bgp 65200" -c "neighbor 192.168.201.1 timers delayopen 30"'
991 # 3.4 check config (poll output)
992 for router_num
in [2, 5]:
994 "Checking BGP neighbor configuration after setting DelayOpenTime on r{}i".format(
998 router
= tgen
.gears
["r{}".format(router_num
)]
999 reffile
= os
.path
.join(
1000 CWD
, "r{}/bgp_delayopen_neighbor.json".format(router_num
)
1002 expected
= json
.loads(open(reffile
).read())
1003 test_func
= functools
.partial(
1004 topotest
.router_json_cmp
, router
, "show bgp neighbors json", expected
1006 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
1007 assertmsg
= "BGP session on r{} failed to set DelayOpenTime".format(router_num
)
1008 assert res
is None, assertmsg
1010 ## 3.5 disable peering shutdown
1011 logger
.info("Disable shutdown of peering between r2 and r5")
1013 'vtysh -c "conf t" -c "router bgp 65000" -c "no neighbor 192.168.201.2 shutdown"'
1016 'vtysh -c "conf t" -c "router bgp 65200" -c "no neighbor 192.168.201.1 shutdown"'
1019 ## 3.6 wait for peers to reach connect or active state (poll output)
1020 delay_start
= int(time
.time())
1021 for router_num
in [2, 5]:
1023 "Checking BGP summary after disabling shutdown of peering on r{}".format(
1027 router
= tgen
.gears
["r{}".format(router_num
)]
1028 reffile
= os
.path
.join(
1029 CWD
, "r{}/bgp_delayopen_summary_connect.json".format(router_num
)
1031 expected
= json
.loads(open(reffile
).read())
1032 test_func
= functools
.partial(
1033 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
1035 _
, res
= topotest
.run_and_expect(test_func
, None, count
=5, wait
=1)
1036 assertmsg
= "BGP session on r{} did not enter Connect state with peer".format(
1039 assert res
is None, assertmsg
1041 ## 3.7 wait for peers to establish connection (poll output)
1042 for router_num
in [2, 5]:
1044 "Checking BGP summary after disabling shutdown of peering on r{}".format(
1048 router
= tgen
.gears
["r{}".format(router_num
)]
1049 reffile
= os
.path
.join(
1050 CWD
, "r{}/bgp_delayopen_summary_established.json".format(router_num
)
1052 expected
= json
.loads(open(reffile
).read())
1053 test_func
= functools
.partial(
1054 topotest
.router_json_cmp
, router
, "show ip bgp summary json", expected
1056 _
, res
= topotest
.run_and_expect(test_func
, None, count
=35, wait
=1)
1058 "BGP session on r{} did not establish a connection with peer".format(
1062 assert res
is None, assertmsg
1064 delay_stop
= int(time
.time())
1065 assertmsg
= "BGP peering between r2 and r5 was established before DelayOpenTimer (30sec) on r2 could expire"
1066 assert (delay_stop
- delay_start
) > 30, assertmsg
1068 # 3.8 unset delayopen on R2 and R5
1069 logger
.info("Disabling DelayOpenTimer for neighbor r5 on r2")
1071 'vtysh -c "conf t" -c "router bgp 65000" -c "no neighbor 192.168.201.2 timers delayopen"'
1073 logger
.info("Disabling DelayOpenTimer for neighbor r2 on r5")
1075 'vtysh -c "conf t" -c "router bgp 65200" -c "no neighbor 192.168.201.1 timers delayopen"'
1078 # 3.9 check config (poll output)
1079 for router_num
in [2, 5]:
1081 "Checking BGP neighbor configuration after disabling DelayOpenTimer on r{}".format(
1086 tgen
.net
["r{}".format(router_num
)]
1087 .cmd('vtysh -c "show bgp neighbors json" | grep "DelayOpenTimeMsecs"')
1090 assertmsg
= "BGP session on r{} failed disable DelayOpenTimer".format(
1093 assert delayopen_cfg
== "", assertmsg
1095 # tgen.mininet_cli()
1097 # end test_bgp_delayopen_dual
1100 if __name__
== "__main__":
1101 args
= ["-s"] + sys
.argv
[1:]
1102 sys
.exit(pytest
.main(args
))