]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_local_asn/test_bgp_local_asn_agg.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_agg.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 """
24 Following tests are covered to test BGP Multi-VRF Dynamic Route Leaking:
25 1. Verify the BGP Local AS functionality by aggregating routes in between eBGP Peers.
26 """
27
28 import os
29 import sys
30 import time
31 import pytest
32
33 # Save the Current Working Directory to find configuration files.
34 CWD = os.path.dirname(os.path.realpath(__file__))
35 sys.path.append(os.path.join(CWD, "../"))
36 sys.path.append(os.path.join(CWD, "../lib/"))
37
38 # pylint: disable=C0413
39 # Import topogen and topotest helpers
40 from lib.topogen import Topogen, get_topogen
41 from lib.topotest import version_cmp
42
43 from lib.common_config import (
44 start_topology,
45 write_test_header,
46 write_test_footer,
47 reset_config_on_routers,
48 verify_rib,
49 step,
50 check_address_types,
51 check_router_status
52 )
53
54 from lib.topolog import logger
55 from lib.bgp import (
56 verify_bgp_convergence,
57 verify_bgp_rib,
58 create_router_bgp,
59 )
60 from lib.topojson import build_config_from_json
61
62 pytestmark = [pytest.mark.bgpd, pytest.mark.staticd]
63
64 # Global variables
65 BGP_CONVERGENCE = False
66 ADDR_TYPES = check_address_types()
67 NETWORK_1_1 = {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
68 NETWORK_1_2 = {"ipv4": "10.1.2.0/32", "ipv6": "10:1::2:0/128"}
69 AGGREGATE_NW = {"ipv4": "10.1.0.0/16", "ipv6": "10:1::/96"}
70 NEXT_HOP_IP = {"ipv4": "Null0", "ipv6": "Null0"}
71
72
73 def setup_module(mod):
74 """
75 Sets up the pytest environment
76
77 * `mod`: module name
78 """
79
80 testsuite_run_time = time.asctime(time.localtime(time.time()))
81 logger.info("Testsuite start time: {}".format(testsuite_run_time))
82 logger.info("=" * 40)
83
84 logger.info("Running setup_module to create topology")
85
86 # This function initiates the topology build with Topogen...
87 json_file = "{}/bgp_local_asn_agg.json".format(CWD)
88 tgen = Topogen(json_file, mod.__name__)
89 global topo
90 topo = tgen.json_topo
91 # ... and here it calls Mininet initialization functions.
92
93 # Starting topology, create tmp files which are loaded to routers
94 # to start daemons and then start routers
95 start_topology(tgen)
96
97 # Creating configuration from JSON
98 build_config_from_json(tgen, topo)
99
100 global BGP_CONVERGENCE
101 global ADDR_TYPES
102 ADDR_TYPES = check_address_types()
103
104 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
105 assert BGP_CONVERGENCE is True, "setup_module : Failed \n Error: {}".format(
106 BGP_CONVERGENCE
107 )
108
109 logger.info("Running setup_module() done")
110
111
112 def teardown_module():
113 """Teardown the pytest environment"""
114
115 logger.info("Running teardown_module to delete topology")
116
117 tgen = get_topogen()
118
119 # Stop toplogy and Remove tmp files
120 tgen.stop_topology()
121
122 logger.info(
123 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
124 )
125 logger.info("=" * 40)
126
127
128 ####################################################################################################################
129 #
130 # Testcases
131 #
132 ####################################################################################################################
133
134
135 def test_verify_bgp_local_as_agg_in_EBGP_p0(request):
136 """
137 Verify the BGP Local AS functionality by aggregating routes in between eBGP Peers.
138 """
139 tgen = get_topogen()
140 global BGP_CONVERGENCE
141
142 if BGP_CONVERGENCE != True:
143 pytest.skip("skipped because of BGP Convergence failure")
144
145 # test case name
146 tc_name = request.node.name
147 write_test_header(tc_name)
148 if tgen.routers_have_failure():
149 check_router_status(tgen)
150 reset_config_on_routers(tgen)
151
152 step("Base config is done as part of JSON")
153 step("Configure local-as at R3 towards R4.")
154 for addr_type in ADDR_TYPES:
155 for neighbor in ["r2", "r4"]:
156 input_dict_r3 = {
157 "r3": {
158 "bgp": {
159 "local_as": "300",
160 "address_family": {
161 addr_type: {
162 "unicast": {
163 "neighbor": {
164 neighbor: {
165 "dest_link": {
166 "r3": {"local_asn": {"local_as": "110"}}
167 }
168 }
169 }
170 }
171 }
172 },
173 }
174 }
175 }
176 result = create_router_bgp(tgen, topo, input_dict_r3)
177 assert result is True, "Testcase {} :Failed \n Error: {}".format(
178 tc_name, result
179 )
180
181 for addr_type in ADDR_TYPES:
182 for dut, asn, neighbor in zip(["r2", "r4"], ["200", "400"], ["r3", "r3"]):
183 input_dict_r2_r4 = {
184 dut: {
185 "bgp": {
186 "local_as": asn,
187 "address_family": {
188 addr_type: {
189 "unicast": {
190 "neighbor": {
191 neighbor: {
192 "dest_link": {
193 dut: {"local_asn": {"remote_as": "110"}}
194 }
195 }
196 }
197 }
198 }
199 },
200 }
201 }
202 }
203 result = create_router_bgp(tgen, topo, input_dict_r2_r4)
204 assert result is True, "Testcase {} :Failed \n Error: {}".format(
205 tc_name, result
206 )
207
208 step("BGP neighborship is verified by following commands in R3 routers")
209 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
210 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
211 BGP_CONVERGENCE
212 )
213
214 step("Done in base config: Advertise prefix 10.1.1.0/24 from Router-1(AS-100).")
215 step(
216 "Done in base config: Advertise an ipv6 prefix 10:1::1:0/120 from Router-1(AS-100)."
217 )
218 step("Verify that Static routes are redistributed in BGP process")
219 for addr_type in ADDR_TYPES:
220 input_static_verify_r1 = {
221 "r1": {"static_routes": [{"network": NETWORK_1_1[addr_type]}]}
222 }
223
224 input_static_verify_r2 = {
225 "r2": {"static_routes": [{"network": NETWORK_1_2[addr_type]}]}
226 }
227 result = verify_rib(tgen, addr_type, "r1", input_static_verify_r1)
228 assert result is True, "Testcase {}: Failed \n Error: {}".format(
229 tc_name, result
230 )
231
232 result = verify_rib(tgen, addr_type, "r2", input_static_verify_r2)
233 assert result is True, "Testcase {}: Failed \n Error: {}".format(
234 tc_name, result
235 )
236
237 step("Configure aggregate-address to summarise all the advertised routes.")
238 for addr_type in ADDR_TYPES:
239 route_aggregate = {
240 "r3": {
241 "bgp": {
242 "address_family": {
243 addr_type: {
244 "unicast": {
245 "aggregate_address": [
246 {
247 "network": AGGREGATE_NW[addr_type],
248 "summary": True,
249 "as_set": True,
250 }
251 ]
252 }
253 }
254 }
255 }
256 }
257 }
258
259 result = create_router_bgp(tgen, topo, route_aggregate)
260 assert result is True, "Testcase {} :Failed \n Error: {}".format(
261 tc_name, result
262 )
263
264 step(
265 "Verify that we see a summarised route on advertising router R3 "
266 "and receiving router R4 for both AFIs"
267 )
268
269 for addr_type in ADDR_TYPES:
270 input_static_agg_r1 = {
271 "r1": {"static_routes": [{"network": AGGREGATE_NW[addr_type]}]}
272 }
273 input_static_r1 = {
274 "r1": {"static_routes": [{"network": [NETWORK_1_1[addr_type]]}]}
275 }
276
277 input_static_r2 = {
278 "r2": {"static_routes": [{"network": [NETWORK_1_2[addr_type]]}]}
279 }
280
281 for dut in ["r3", "r4"]:
282 result = verify_rib(tgen, addr_type, dut, input_static_agg_r1)
283 assert result is True, "Testcase {}: Failed \n Error: {}".format(
284 tc_name, result
285 )
286
287 for dut, input_routes in zip(["r1", "r2"], [input_static_r1, input_static_r2]):
288 result = verify_rib(tgen, addr_type, dut, input_routes)
289 assert result is True, "Testcase {}: Failed \n Error: {}".format(
290 tc_name, result
291 )
292
293 step(
294 "Verify that AS-110 is got added in the AS list 110 {100,110,200} by following "
295 "commands at R3 router."
296 )
297 dut = "r3"
298 aspath = "{100,110,200}"
299 for addr_type in ADDR_TYPES:
300 input_static_agg_r1 = {
301 "r1": {"static_routes": [{"network": AGGREGATE_NW[addr_type]}]}
302 }
303 result = verify_bgp_rib(
304 tgen, addr_type, dut, input_static_agg_r1, aspath=aspath
305 )
306 assert result is True, "Testcase {} : Failed \n Error: {}".format(
307 tc_name, result
308 )
309
310 step("Configure local-as with no-prepend at R3 towards R4 & R2.")
311 for addr_type in ADDR_TYPES:
312 for neighbor in ["r2", "r4"]:
313 input_dict_r3 = {
314 "r3": {
315 "bgp": {
316 "local_as": "300",
317 "address_family": {
318 addr_type: {
319 "unicast": {
320 "neighbor": {
321 neighbor: {
322 "dest_link": {
323 "r3": {
324 "local_asn": {
325 "local_as": "110",
326 "no_prepend": True,
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334 },
335 }
336 }
337 }
338 result = create_router_bgp(tgen, topo, input_dict_r3)
339 assert result is True, "Testcase {} :Failed \n Error: {}".format(
340 tc_name, result
341 )
342
343 step("BGP neighborship is verified by following commands in R3 routers")
344 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
345 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
346 BGP_CONVERGENCE
347 )
348
349 dut = "r3"
350 aspath = "{100,200}"
351 for addr_type in ADDR_TYPES:
352 input_static_agg_r1 = {
353 "r1": {"static_routes": [{"network": AGGREGATE_NW[addr_type]}]}
354 }
355 result = verify_bgp_rib(
356 tgen, addr_type, dut, input_static_agg_r1, aspath=aspath
357 )
358 assert result is True, "Testcase {} : Failed \n Error: {}".format(
359 tc_name, result
360 )
361
362 step("Configure local-as with no-prepend and replace-as at R3 towards R4 & R2.")
363 for addr_type in ADDR_TYPES:
364 for neighbor in ["r2", "r4"]:
365 input_dict_r3 = {
366 "r3": {
367 "bgp": {
368 "local_as": "300",
369 "address_family": {
370 addr_type: {
371 "unicast": {
372 "neighbor": {
373 neighbor: {
374 "dest_link": {
375 "r3": {
376 "local_asn": {
377 "local_as": "110",
378 "no_prepend": True,
379 "replace_as": True,
380 }
381 }
382 }
383 }
384 }
385 }
386 }
387 },
388 }
389 }
390 }
391 result = create_router_bgp(tgen, topo, input_dict_r3)
392 assert result is True, "Testcase {} :Failed \n Error: {}".format(
393 tc_name, result
394 )
395
396 step("BGP neighborship is verified by following commands in R3 routers")
397 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
398 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
399 BGP_CONVERGENCE
400 )
401
402 dut = "r4"
403 aspath = "110 {100,200}"
404 for addr_type in ADDR_TYPES:
405 input_static_agg_r1 = {
406 "r1": {"static_routes": [{"network": AGGREGATE_NW[addr_type]}]}
407 }
408 result = verify_bgp_rib(
409 tgen, addr_type, dut, input_static_agg_r1, aspath=aspath
410 )
411 assert result is True, "Testcase {} : Failed \n Error: {}".format(
412 tc_name, result
413 )
414
415 write_test_footer(tc_name)
416
417
418 if __name__ == "__main__":
419 args = ["-s"] + sys.argv[1:]
420 sys.exit(pytest.main(args))