]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_suppress_fib/test_bgp_suppress_fib.py
Merge pull request #10183 from idryzhov/rework-vrf-rename
[mirror_frr.git] / tests / topotests / bgp_suppress_fib / test_bgp_suppress_fib.py
1 #!/usr/bin/env python
2
3 #
4 # test_bgp_suppress_fib.py
5 #
6 # Copyright (c) 2019 by
7 #
8 # Permission to use, copy, modify, and/or distribute this software
9 # for any purpose with or without fee is hereby granted, provided
10 # that the above copyright notice and this permission notice appear
11 # in all copies.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES
14 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR
16 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
17 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 # OF THIS SOFTWARE.
21 #
22
23 """
24 """
25
26 import os
27 import sys
28 import json
29 import pytest
30 from functools import partial
31 from time import sleep
32 from lib.topolog import logger
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
41 pytestmark = [pytest.mark.bgpd]
42
43
44 def build_topo(tgen):
45 for routern in range(1, 4):
46 tgen.add_router("r{}".format(routern))
47
48 switch = tgen.add_switch("s1")
49 switch.add_link(tgen.gears["r1"])
50 switch.add_link(tgen.gears["r2"])
51
52 switch = tgen.add_switch("s2")
53 switch.add_link(tgen.gears["r2"])
54 switch.add_link(tgen.gears["r3"])
55
56
57 def setup_module(mod):
58 tgen = Topogen(build_topo, mod.__name__)
59 tgen.start_topology()
60
61 router_list = tgen.routers()
62
63 for i, (rname, router) in enumerate(router_list.items(), 1):
64 router.load_config(
65 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
66 )
67 router.load_config(
68 TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname))
69 )
70
71 tgen.start_router()
72
73
74 def teardown_module(mod):
75 tgen = get_topogen()
76 tgen.stop_topology()
77
78
79 def test_bgp_route():
80 tgen = get_topogen()
81
82 if tgen.routers_have_failure():
83 pytest.skip(tgen.errors)
84
85 r3 = tgen.gears["r3"]
86
87 sleep(5)
88
89 json_file = "{}/r3/v4_route.json".format(CWD)
90 expected = json.loads(open(json_file).read())
91
92 test_func = partial(
93 topotest.router_json_cmp,
94 r3,
95 "show ip route 40.0.0.0 json",
96 expected,
97 )
98 _, result = topotest.run_and_expect(test_func, None, count=2, wait=0.5)
99 assertmsg = '"r3" JSON output mismatches'
100 assert result is None, assertmsg
101
102 json_file = "{}/r3/v4_route2.json".format(CWD)
103 expected = json.loads(open(json_file).read())
104
105 test_func = partial(
106 topotest.router_json_cmp,
107 r3,
108 "show ip route 50.0.0.0 json",
109 expected,
110 )
111 _, result = topotest.run_and_expect(test_func, None, count=3, wait=0.5)
112 assertmsg = '"r3" JSON output mismatches'
113 assert result is None, assertmsg
114
115
116 def test_bgp_better_admin_won():
117 "A better Admin distance protocol may come along and knock us out"
118
119 tgen = get_topogen()
120
121 if tgen.routers_have_failure():
122 pytest.skip(tgen.errors)
123
124 r2 = tgen.gears["r2"]
125 r2.vtysh_cmd("conf\nip route 40.0.0.0/8 10.0.0.10")
126
127 json_file = "{}/r2/v4_override.json".format(CWD)
128 expected = json.loads(open(json_file).read())
129
130 logger.info(expected)
131 test_func = partial(
132 topotest.router_json_cmp, r2, "show ip route 40.0.0.0 json", expected
133 )
134
135 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
136 assertmsg = '"r2" static route did not take over'
137 assert result is None, assertmsg
138
139 r3 = tgen.gears["r3"]
140
141 json_file = "{}/r3/v4_override.json".format(CWD)
142 expected = json.loads(open(json_file).read())
143
144 test_func = partial(
145 topotest.router_json_cmp, r3, "show ip route 40.0.0.0 json", expected
146 )
147
148 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
149 assertmsg = '"r3" route to 40.0.0.0 should have been lost'
150 assert result is None, assertmsg
151
152 r2.vtysh_cmd("conf\nno ip route 40.0.0.0/8 10.0.0.10")
153
154 json_file = "{}/r3/v4_route.json".format(CWD)
155 expected = json.loads(open(json_file).read())
156
157 test_func = partial(
158 topotest.router_json_cmp,
159 r3,
160 "show ip route 40.0.0.0 json",
161 expected,
162 )
163 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
164 assertmsg = '"r3" route to 40.0.0.0 did not come back'
165 assert result is None, assertmsg
166
167
168 def test_bgp_allow_as_in():
169 tgen = get_topogen()
170
171 if tgen.routers_have_failure():
172 pytest.skip(tgen.errors)
173
174 r2 = tgen.gears["r2"]
175
176 config_file = "{}/r2/bgpd.allowas_in.conf".format(CWD)
177 r2.run("vtysh -f {}".format(config_file))
178
179 json_file = "{}/r2/bgp_ipv4_allowas.json".format(CWD)
180 expected = json.loads(open(json_file).read())
181
182 test_func = partial(
183 topotest.router_json_cmp,
184 r2,
185 "show bgp ipv4 uni 192.168.1.1/32 json",
186 expected,
187 )
188 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
189 assertmsg = '"r2" static redistribution failed into bgp'
190 assert result is None, assertmsg
191
192 r1 = tgen.gears["r1"]
193
194 json_file = "{}/r1/bgp_ipv4_allowas.json".format(CWD)
195 expected = json.loads(open(json_file).read())
196
197 test_func = partial(
198 topotest.router_json_cmp,
199 r1,
200 "show bgp ipv4 uni 192.168.1.1/32 json",
201 expected,
202 )
203
204 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
205 assertmsg = '"r1" 192.168.1.1/32 route should have arrived'
206 assert result is None, assertmsg
207
208 r2.vtysh_cmd("conf\nno ip route 192.168.1.1/32 10.0.0.10")
209
210 json_file = "{}/r2/no_bgp_ipv4_allowas.json".format(CWD)
211 expected = json.loads(open(json_file).read())
212
213 test_func = partial(
214 topotest.router_json_cmp,
215 r2,
216 "show bgp ipv4 uni 192.168.1.1/32 json",
217 expected,
218 )
219
220 _, result = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
221 assertmsg = '"r2" 192.168.1.1/32 route should be gone'
222 assert result is None, assertmsg
223
224
225 if __name__ == "__main__":
226 args = ["-s"] + sys.argv[1:]
227 sys.exit(pytest.main(args))