]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_aggregate_address_topo1/test_bgp_aggregate_address_topo1.py
tests: cleanup - remove unused imports
[mirror_frr.git] / tests / topotests / bgp_aggregate_address_topo1 / test_bgp_aggregate_address_topo1.py
1 #!/usr/bin/env python
2
3 #
4 # test_bgp_aggregate_address_topo1.py
5 # Part of NetDEF Topology Tests
6 #
7 # Copyright (c) 2020 by
8 # Network Device Education Foundation, Inc. ("NetDEF")
9 #
10 # Permission to use, copy, modify, and/or distribute this software
11 # for any purpose with or without fee is hereby granted, provided
12 # that the above copyright notice and this permission notice appear
13 # in all copies.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
16 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
18 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
19 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
21 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 # OF THIS SOFTWARE.
23 #
24
25 """
26 Test BGP aggregate address features.
27 """
28
29 import os
30 import sys
31 import pytest
32 import functools
33
34 CWD = os.path.dirname(os.path.realpath(__file__))
35 sys.path.append(os.path.join(CWD, "../"))
36
37 # pylint: disable=C0413
38 from lib import topotest
39 from lib.topogen import Topogen, TopoRouter, get_topogen
40 from lib.topolog import logger
41
42 pytestmark = [pytest.mark.bgpd]
43
44
45
46 def build_topo(tgen):
47 r1 = tgen.add_router("r1")
48 r2 = tgen.add_router("r2")
49 peer1 = tgen.add_exabgp_peer(
50 "peer1", ip="10.0.0.2", defaultRoute="via 10.0.0.1"
51 )
52
53 switch = tgen.add_switch("s1")
54 switch.add_link(r1)
55 switch.add_link(peer1)
56
57 switch = tgen.add_switch("s2")
58 switch.add_link(r1)
59 switch.add_link(r2)
60
61
62 def setup_module(mod):
63 tgen = Topogen(build_topo, mod.__name__)
64 tgen.start_topology()
65
66 router = tgen.gears["r1"]
67 router.load_config(TopoRouter.RD_ZEBRA, os.path.join(CWD, "r1/zebra.conf"))
68 router.load_config(TopoRouter.RD_BGP, os.path.join(CWD, "r1/bgpd.conf"))
69 router.start()
70
71 router = tgen.gears["r2"]
72 router.load_config(TopoRouter.RD_ZEBRA, os.path.join(CWD, "r2/zebra.conf"))
73 router.load_config(TopoRouter.RD_BGP, os.path.join(CWD, "r2/bgpd.conf"))
74 router.start()
75
76 peer = tgen.gears["peer1"]
77 peer.start(os.path.join(CWD, "peer1"), os.path.join(CWD, "exabgp.env"))
78
79
80 def teardown_module(mod):
81 tgen = get_topogen()
82 tgen.stop_topology()
83
84
85 def expect_route(router_name, routes_expected):
86 "Helper function to avoid repeated code."
87 tgen = get_topogen()
88 test_func = functools.partial(
89 topotest.router_json_cmp,
90 tgen.gears[router_name],
91 "show ip route json",
92 routes_expected,
93 )
94 _, result = topotest.run_and_expect(test_func, None, count=120, wait=1)
95 assertmsg = '"{}" BGP convergence failure'.format(router_name)
96 assert result is None, assertmsg
97
98
99 def test_expect_convergence():
100 "Test that BGP protocol converged."
101
102 tgen = get_topogen()
103 if tgen.routers_have_failure():
104 pytest.skip(tgen.errors)
105
106 logger.info("waiting for protocols to converge")
107
108 def expect_loopback_route(router, iptype, route, proto):
109 "Wait until route is present on RIB for protocol."
110 logger.info("waiting route {} in {}".format(route, router))
111 test_func = functools.partial(
112 topotest.router_json_cmp,
113 tgen.gears[router],
114 "show {} route json".format(iptype),
115 {route: [{"protocol": proto}]},
116 )
117 _, result = topotest.run_and_expect(test_func, None, count=130, wait=1)
118 assertmsg = '"{}" BGP convergence failure'.format(router)
119 assert result is None, assertmsg
120
121 expect_loopback_route("r2", "ip", "10.254.254.1/32", "bgp")
122 expect_loopback_route("r2", "ip", "10.254.254.3/32", "bgp")
123
124
125 def test_bgp_aggregate_address_matching_med_only():
126 "Test that the command matching-MED-only works."
127
128 tgen = get_topogen()
129 if tgen.routers_have_failure():
130 pytest.skip(tgen.errors)
131
132 routes_expected = {
133 # All MED matches, aggregation must exist.
134 "192.168.0.0/24": [{"protocol": "bgp", "metric": 0}],
135 "192.168.0.1/32": [{"protocol": "bgp", "metric": 10}],
136 "192.168.0.2/32": [{"protocol": "bgp", "metric": 10}],
137 "192.168.0.3/32": [{"protocol": "bgp", "metric": 10}],
138 # Non matching MED: aggregation must not exist.
139 "192.168.1.0/24": None,
140 "192.168.1.1/32": [{"protocol": "bgp", "metric": 10}],
141 "192.168.1.2/32": [{"protocol": "bgp", "metric": 10}],
142 "192.168.1.3/32": [{"protocol": "bgp", "metric": 20}],
143 }
144
145 test_func = functools.partial(
146 topotest.router_json_cmp,
147 tgen.gears["r2"],
148 "show ip route json",
149 routes_expected,
150 )
151 _, result = topotest.run_and_expect(test_func, None, count=20, wait=1)
152 assertmsg = '"r2" BGP convergence failure'
153 assert result is None, assertmsg
154
155
156 def test_bgp_aggregate_address_match_and_suppress():
157 "Test that the command matching-MED-only with suppression works."
158
159 tgen = get_topogen()
160 if tgen.routers_have_failure():
161 pytest.skip(tgen.errors)
162
163 tgen.gears["r1"].vtysh_multicmd(
164 """
165 configure terminal
166 router bgp 65000
167 address-family ipv4 unicast
168 no aggregate-address 192.168.0.0/24 matching-MED-only
169 no aggregate-address 192.168.1.0/24 matching-MED-only
170 aggregate-address 192.168.0.0/24 matching-MED-only summary-only
171 aggregate-address 192.168.1.0/24 matching-MED-only summary-only
172 """
173 )
174
175 routes_expected = {
176 # All MED matches, aggregation must exist.
177 "192.168.0.0/24": [{"protocol": "bgp", "metric": 0}],
178 "192.168.0.1/32": None,
179 "192.168.0.2/32": None,
180 "192.168.0.3/32": None,
181 # Non matching MED: aggregation must not exist.
182 "192.168.1.0/24": None,
183 "192.168.1.1/32": [{"protocol": "bgp", "metric": 10}],
184 "192.168.1.2/32": [{"protocol": "bgp", "metric": 10}],
185 "192.168.1.3/32": [{"protocol": "bgp", "metric": 20}],
186 }
187
188 test_func = functools.partial(
189 topotest.router_json_cmp,
190 tgen.gears["r2"],
191 "show ip route json",
192 routes_expected,
193 )
194 _, result = topotest.run_and_expect(test_func, None, count=120, wait=1)
195 assertmsg = '"r2" BGP convergence failure'
196 assert result is None, assertmsg
197
198
199 def test_bgp_aggregate_address_suppress_map():
200 "Test that the command suppress-map works."
201
202 tgen = get_topogen()
203 if tgen.routers_have_failure():
204 pytest.skip(tgen.errors)
205
206 expect_route(
207 "r2",
208 {
209 "192.168.2.0/24": [{"protocol": "bgp"}],
210 "192.168.2.1/32": None,
211 "192.168.2.2/32": [{"protocol": "bgp"}],
212 "192.168.2.3/32": [{"protocol": "bgp"}],
213 },
214 )
215
216 # Change route map and test again.
217 tgen.gears["r1"].vtysh_multicmd(
218 """
219 configure terminal
220 router bgp 65000
221 address-family ipv4 unicast
222 no aggregate-address 192.168.2.0/24 suppress-map rm-sup-one
223 aggregate-address 192.168.2.0/24 suppress-map rm-sup-two
224 """
225 )
226
227 expect_route(
228 "r2",
229 {
230 "192.168.2.0/24": [{"protocol": "bgp"}],
231 "192.168.2.1/32": [{"protocol": "bgp"}],
232 "192.168.2.2/32": None,
233 "192.168.2.3/32": [{"protocol": "bgp"}],
234 },
235 )
236
237
238 def test_bgp_aggregate_address_suppress_map_update_route_map():
239 "Test that the suppress-map late route map creation works."
240 tgen = get_topogen()
241 if tgen.routers_have_failure():
242 pytest.skip(tgen.errors)
243
244 tgen.gears["r1"].vtysh_multicmd(
245 """
246 configure terminal
247 router bgp 65000
248 address-family ipv4 unicast
249 no aggregate-address 192.168.2.0/24 suppress-map rm-sup-two
250 aggregate-address 192.168.2.0/24 suppress-map rm-sup-three
251 """
252 )
253
254 expect_route(
255 "r2",
256 {
257 "192.168.2.0/24": [{"protocol": "bgp"}],
258 "192.168.2.1/32": [{"protocol": "bgp"}],
259 "192.168.2.2/32": [{"protocol": "bgp"}],
260 "192.168.2.3/32": [{"protocol": "bgp"}],
261 },
262 )
263
264 # Create missing route map and test again.
265 tgen.gears["r1"].vtysh_multicmd(
266 """
267 configure terminal
268 route-map rm-sup-three permit 10
269 match ip address acl-sup-three
270 """
271 )
272
273 expect_route(
274 "r2",
275 {
276 "192.168.2.0/24": [{"protocol": "bgp"}],
277 "192.168.2.1/32": [{"protocol": "bgp"}],
278 "192.168.2.2/32": [{"protocol": "bgp"}],
279 "192.168.2.3/32": None,
280 },
281 )
282
283
284 def test_memory_leak():
285 "Run the memory leak test and report results."
286 tgen = get_topogen()
287 if not tgen.is_memleak_enabled():
288 pytest.skip("Memory leak test/report is disabled")
289
290 tgen.report_memory_leaks()
291
292
293 if __name__ == "__main__":
294 args = ["-s"] + sys.argv[1:]
295 sys.exit(pytest.main(args))