2 # SPDX-License-Identifier: ISC
5 # Copyright (c) 2022 by
6 # Donatas Abraitis <donatas@opensourcerouting.org>
10 Conditionally advertise 172.16.255.2/32 to r1, only if 172.16.255.3/32
13 Also, withdraw if 172.16.255.3/32 disappears.
22 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
23 sys
.path
.append(os
.path
.join(CWD
, "../"))
25 # pylint: disable=C0413
26 from lib
import topotest
27 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
28 from lib
.common_config
import (
32 pytestmark
= [pytest
.mark
.bgpd
]
36 for routern
in range(1, 5):
37 tgen
.add_router("r{}".format(routern
))
39 switch
= tgen
.add_switch("s1")
40 switch
.add_link(tgen
.gears
["r1"])
41 switch
.add_link(tgen
.gears
["r2"])
43 switch
= tgen
.add_switch("s2")
44 switch
.add_link(tgen
.gears
["r2"])
45 switch
.add_link(tgen
.gears
["r3"])
48 def setup_module(mod
):
49 tgen
= Topogen(build_topo
, mod
.__name
__)
52 router_list
= tgen
.routers()
54 for i
, (rname
, router
) in enumerate(router_list
.items(), 1):
56 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
59 TopoRouter
.RD_STATIC
, os
.path
.join(CWD
, "{}/staticd.conf".format(rname
))
62 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
68 def teardown_module(mod
):
73 def test_bgp_conditional_advertisement_track_peer():
80 if tgen
.routers_have_failure():
81 pytest
.skip(tgen
.errors
)
86 "show bgp ipv4 unicast neighbors 192.168.1.1 advertised-routes json"
90 "advertisedRoutes": {"172.16.255.2/32": None},
91 "totalPrefixCounter": 0,
92 "filteredPrefixCounter": 0,
94 return topotest
.json_cmp(output
, expected
)
96 # Verify if R2 does not send any routes to R1
97 test_func
= functools
.partial(_bgp_converge
)
98 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
99 assert result
is None, "R2 SHOULD not send any routes to R1"
101 step("Enable session between R2 and R3")
106 no neighbor 192.168.2.2 shutdown
110 def _bgp_check_conditional_static_routes_from_r2():
111 output
= json
.loads(r1
.vtysh_cmd("show bgp ipv4 unicast json"))
114 "172.16.255.2/32": [{"valid": True, "nexthops": [{"hostname": "r2"}]}]
117 return topotest
.json_cmp(output
, expected
)
119 # Verify if R1 received 172.16.255.2/32 from R2
120 test_func
= functools
.partial(_bgp_check_conditional_static_routes_from_r2
)
121 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
122 assert result
is None, "R1 SHOULD receive 172.16.255.2/32 from R2"
124 step("Disable session between R2 and R3 again")
129 neighbor 192.168.2.2 shutdown
133 # Verify if R2 is not sending any routes to R1 again
134 test_func
= functools
.partial(_bgp_converge
)
135 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
136 assert result
is None, "R2 SHOULD not send any routes to R1"
139 if __name__
== "__main__":
140 args
= ["-s"] + sys
.argv
[1:]
141 sys
.exit(pytest
.main(args
))