4 # Copyright (c) 2022 by
5 # Donatas Abraitis <donatas@opensourcerouting.org>
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
12 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF 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
23 Conditionally advertise 172.16.255.2/32 to r1, only if 172.16.255.3/32
26 Also, withdraw if 172.16.255.3/32 disappears.
35 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
36 sys
.path
.append(os
.path
.join(CWD
, "../"))
38 # pylint: disable=C0413
39 from lib
import topotest
40 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
41 from lib
.common_config
import (
45 pytestmark
= [pytest
.mark
.bgpd
]
49 for routern
in range(1, 5):
50 tgen
.add_router("r{}".format(routern
))
52 switch
= tgen
.add_switch("s1")
53 switch
.add_link(tgen
.gears
["r1"])
54 switch
.add_link(tgen
.gears
["r2"])
56 switch
= tgen
.add_switch("s2")
57 switch
.add_link(tgen
.gears
["r2"])
58 switch
.add_link(tgen
.gears
["r3"])
61 def setup_module(mod
):
62 tgen
= Topogen(build_topo
, mod
.__name
__)
65 router_list
= tgen
.routers()
67 for i
, (rname
, router
) in enumerate(router_list
.items(), 1):
69 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
72 TopoRouter
.RD_STATIC
, os
.path
.join(CWD
, "{}/staticd.conf".format(rname
))
75 TopoRouter
.RD_BGP
, os
.path
.join(CWD
, "{}/bgpd.conf".format(rname
))
81 def teardown_module(mod
):
86 def test_bgp_conditional_advertisement_track_peer():
93 if tgen
.routers_have_failure():
94 pytest
.skip(tgen
.errors
)
99 "show bgp ipv4 unicast neighbors 192.168.1.1 advertised-routes json"
103 "advertisedRoutes": {"172.16.255.2/32": None},
104 "totalPrefixCounter": 0,
105 "filteredPrefixCounter": 0,
107 return topotest
.json_cmp(output
, expected
)
109 # Verify if R2 does not send any routes to R1
110 test_func
= functools
.partial(_bgp_converge
)
111 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
112 assert result
is None, "R2 SHOULD not send any routes to R1"
114 step("Enable session between R2 and R3")
119 no neighbor 192.168.2.2 shutdown
123 def _bgp_check_conditional_static_routes_from_r2():
124 output
= json
.loads(r1
.vtysh_cmd("show bgp ipv4 unicast json"))
127 "172.16.255.2/32": [{"valid": True, "nexthops": [{"hostname": "r2"}]}]
130 return topotest
.json_cmp(output
, expected
)
132 # Verify if R1 received 172.16.255.2/32 from R2
133 test_func
= functools
.partial(_bgp_check_conditional_static_routes_from_r2
)
134 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
135 assert result
is None, "R1 SHOULD receive 172.16.255.2/32 from R2"
137 step("Disable session between R2 and R3 again")
142 neighbor 192.168.2.2 shutdown
146 # Verify if R2 is not sending any routes to R1 again
147 test_func
= functools
.partial(_bgp_converge
)
148 _
, result
= topotest
.run_and_expect(test_func
, None, count
=30, wait
=0.5)
149 assert result
is None, "R2 SHOULD not send any routes to R1"
152 if __name__
== "__main__":
153 args
= ["-s"] + sys
.argv
[1:]
154 sys
.exit(pytest
.main(args
))