]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_aggregate_address_matching_med/test_bgp_aggregate_address_matching_med.py
Merge pull request #12366 from manojvn/ospfv2-flood-reduction
[mirror_frr.git] / tests / topotests / bgp_aggregate_address_matching_med / test_bgp_aggregate_address_matching_med.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # Copyright (c) 2022 by
6 # Donatas Abraitis <donatas@opensourcerouting.org>
7 #
8
9 """
10 Test if aggregate-address command works fine when suppressing summary-only
11 and using matching-MED-only together.
12 """
13
14 import os
15 import sys
16 import json
17 import pytest
18 import functools
19 from lib.common_config import (
20 step,
21 )
22
23 # pylint: disable=C0413
24 from lib import topotest
25 from lib.topogen import Topogen, TopoRouter, get_topogen
26
27 pytestmark = [pytest.mark.bgpd]
28
29 CWD = os.path.dirname(os.path.realpath(__file__))
30 sys.path.append(os.path.join(CWD, "../"))
31
32
33 def build_topo(tgen):
34 for routern in range(1, 5):
35 tgen.add_router("r{}".format(routern))
36
37 switch = tgen.add_switch("s1")
38 switch.add_link(tgen.gears["r1"])
39 switch.add_link(tgen.gears["r2"])
40
41 switch = tgen.add_switch("s2")
42 switch.add_link(tgen.gears["r2"])
43 switch.add_link(tgen.gears["r3"])
44
45
46 def setup_module(mod):
47 tgen = Topogen(build_topo, mod.__name__)
48 tgen.start_topology()
49
50 router_list = tgen.routers()
51
52 for i, (rname, router) in enumerate(router_list.items(), 1):
53 router.load_config(
54 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
55 )
56 router.load_config(
57 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
58 )
59
60 tgen.start_router()
61
62
63 def teardown_module(mod):
64 tgen = get_topogen()
65 tgen.stop_topology()
66
67
68 def test_aggregate_address_matching_med():
69 tgen = get_topogen()
70
71 if tgen.routers_have_failure():
72 pytest.skip(tgen.errors)
73
74 r1 = tgen.gears["r1"]
75 r3 = tgen.gears["r3"]
76
77 def _bgp_converge():
78 output = json.loads(r3.vtysh_cmd("show bgp ipv4 unicast json"))
79 expected = {
80 "routes": {
81 "172.16.255.0/24": None,
82 "172.16.255.1/32": [{"path": "65002 65001"}],
83 "172.16.255.2/32": [{"path": "65002 65001"}],
84 "172.16.255.3/32": [{"path": "65002 65001"}],
85 }
86 }
87 return topotest.json_cmp(output, expected)
88
89 test_func = functools.partial(_bgp_converge)
90 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
91 assert result is None, "Failed to see unsuppressed routes from R2"
92
93 step("Change MED for 172.16.255.3/32 from 400 to 300")
94 r1.vtysh_cmd(
95 """
96 configure terminal
97 route-map r2 permit 20
98 set metric 300
99 """
100 )
101
102 step("Check if 172.16.255.0/24 aggregated route was created and others suppressed")
103
104 def _bgp_aggregated_summary_only_med_match():
105 output = json.loads(r3.vtysh_cmd("show bgp ipv4 unicast json"))
106 expected = {
107 "routes": {
108 "172.16.255.0/24": [{"path": "65002"}],
109 "172.16.255.1/32": None,
110 "172.16.255.2/32": None,
111 "172.16.255.3/32": None,
112 }
113 }
114 return topotest.json_cmp(output, expected)
115
116 test_func = functools.partial(_bgp_aggregated_summary_only_med_match)
117 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
118 assert result is None, "Failed to see unsuppressed routes from R2"
119
120 step("Change MED for 172.16.255.3/32 back to 400 from 300")
121 r1.vtysh_cmd(
122 """
123 configure terminal
124 route-map r2 permit 20
125 set metric 400
126 """
127 )
128 test_func = functools.partial(_bgp_converge)
129 _, result = topotest.run_and_expect(test_func, None, count=30, wait=0.5)
130 assert result is None, "Failed to see unsuppressed routes from R2"
131
132
133 if __name__ == "__main__":
134 args = ["-s"] + sys.argv[1:]
135 sys.exit(pytest.main(args))