]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/isis_topo1/test_isis_topo1.py
5 # Part of NetDEF Topology Tests
7 # Copyright (c) 2017 by
8 # Network Device Education Foundation, Inc. ("NetDEF")
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
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
26 test_isis_topo1.py: Test ISIS topology.
36 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
37 sys
.path
.append(os
.path
.join(CWD
, "../"))
39 # pylint: disable=C0413
40 from lib
import topotest
41 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
42 from lib
.topolog
import logger
45 pytestmark
= [pytest
.mark
.isisd
]
73 for routern
in range(1, 6):
74 tgen
.add_router("r{}".format(routern
))
77 sw
= tgen
.add_switch("sw1")
78 sw
.add_link(tgen
.gears
["r1"])
79 sw
.add_link(tgen
.gears
["r3"])
82 sw
= tgen
.add_switch("sw2")
83 sw
.add_link(tgen
.gears
["r2"])
84 sw
.add_link(tgen
.gears
["r4"])
87 sw
= tgen
.add_switch("sw3")
88 sw
.add_link(tgen
.gears
["r3"])
89 sw
.add_link(tgen
.gears
["r5"])
92 sw
= tgen
.add_switch("sw4")
93 sw
.add_link(tgen
.gears
["r4"])
94 sw
.add_link(tgen
.gears
["r5"])
97 def setup_module(mod
):
98 "Sets up the pytest environment"
99 tgen
= Topogen(build_topo
, mod
.__name
__)
100 tgen
.start_topology()
102 # For all registered routers, load the zebra configuration file
103 for rname
, router
in tgen
.routers().items():
105 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
108 TopoRouter
.RD_ISIS
, os
.path
.join(CWD
, "{}/isisd.conf".format(rname
))
111 # After loading the configurations, this function loads configured daemons.
115 def teardown_module(mod
):
116 "Teardown the pytest environment"
119 # This function tears down the whole topology.
123 def test_isis_convergence():
124 "Wait for the protocol to converge before starting to test"
126 # Don't run this test if we have any failure.
127 if tgen
.routers_have_failure():
128 pytest
.skip(tgen
.errors
)
130 logger
.info("waiting for ISIS protocol to converge")
131 # Code to generate the json files.
132 # for rname, router in tgen.routers().items():
133 # open('/tmp/{}_topology.json'.format(rname), 'w').write(
134 # json.dumps(show_isis_topology(router), indent=2, sort_keys=True)
137 for rname
, router
in tgen
.routers().items():
138 filename
= "{0}/{1}/{1}_topology.json".format(CWD
, rname
)
139 expected
= json
.loads(open(filename
).read())
141 def compare_isis_topology(router
, expected
):
142 "Helper function to test ISIS topology convergence."
143 actual
= show_isis_topology(router
)
144 return topotest
.json_cmp(actual
, expected
)
146 test_func
= functools
.partial(compare_isis_topology
, router
, expected
)
147 (result
, diff
) = topotest
.run_and_expect(test_func
, None, wait
=0.5, count
=120)
148 assert result
, "ISIS did not converge on {}:\n{}".format(rname
, diff
)
151 def test_isis_route_installation():
152 "Check whether all expected routes are present"
154 # Don't run this test if we have any failure.
155 if tgen
.routers_have_failure():
156 pytest
.skip(tgen
.errors
)
158 logger
.info("Checking routers for installed ISIS routes")
160 # Check for routes in 'show ip route json'
161 for rname
, router
in tgen
.routers().items():
162 filename
= "{0}/{1}/{1}_route.json".format(CWD
, rname
)
163 expected
= json
.loads(open(filename
, "r").read())
164 actual
= router
.vtysh_cmd("show ip route json", isjson
=True)
165 assertmsg
= "Router '{}' routes mismatch".format(rname
)
166 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
169 def test_isis_linux_route_installation():
170 "Check whether all expected routes are present and installed in the OS"
172 # Don't run this test if we have any failure.
173 if tgen
.routers_have_failure():
174 pytest
.skip(tgen
.errors
)
176 logger
.info("Checking routers for installed ISIS routes in OS")
178 # Check for routes in `ip route`
179 for rname
, router
in tgen
.routers().items():
180 filename
= "{0}/{1}/{1}_route_linux.json".format(CWD
, rname
)
181 expected
= json
.loads(open(filename
, "r").read())
182 actual
= topotest
.ip4_route(router
)
183 assertmsg
= "Router '{}' OS routes mismatch".format(rname
)
184 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
187 def test_isis_route6_installation():
188 "Check whether all expected routes are present"
190 # Don't run this test if we have any failure.
191 if tgen
.routers_have_failure():
192 pytest
.skip(tgen
.errors
)
194 logger
.info("Checking routers for installed ISIS IPv6 routes")
196 # Check for routes in 'show ip route json'
197 for rname
, router
in tgen
.routers().items():
198 filename
= "{0}/{1}/{1}_route6.json".format(CWD
, rname
)
199 expected
= json
.loads(open(filename
, "r").read())
200 actual
= router
.vtysh_cmd("show ipv6 route json", isjson
=True)
201 assertmsg
= "Router '{}' routes mismatch".format(rname
)
202 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
205 def test_isis_linux_route6_installation():
206 "Check whether all expected routes are present and installed in the OS"
208 # Don't run this test if we have any failure.
209 if tgen
.routers_have_failure():
210 pytest
.skip(tgen
.errors
)
212 logger
.info("Checking routers for installed ISIS IPv6 routes in OS")
214 # Check for routes in `ip route`
215 for rname
, router
in tgen
.routers().items():
216 filename
= "{0}/{1}/{1}_route6_linux.json".format(CWD
, rname
)
217 expected
= json
.loads(open(filename
, "r").read())
218 actual
= topotest
.ip6_route(router
)
219 assertmsg
= "Router '{}' OS routes mismatch".format(rname
)
220 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
223 def test_memory_leak():
224 "Run the memory leak test and report results."
226 if not tgen
.is_memleak_enabled():
227 pytest
.skip("Memory leak test/report is disabled")
229 tgen
.report_memory_leaks()
232 if __name__
== "__main__":
233 args
= ["-s"] + sys
.argv
[1:]
234 sys
.exit(pytest
.main(args
))
238 # Auxiliary functions
242 def dict_merge(dct
, merge_dct
):
244 Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
245 updating only top-level keys, dict_merge recurses down into dicts nested
246 to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
248 :param dct: dict onto which the merge is executed
249 :param merge_dct: dct merged into dct
253 https://gist.github.com/angstwad/bf22d1822c38a92ec0a9
255 for k
, v
in merge_dct
.items():
256 if k
in dct
and isinstance(dct
[k
], dict) and topotest
.is_mapping(merge_dct
[k
]):
257 dict_merge(dct
[k
], merge_dct
[k
])
259 dct
[k
] = merge_dct
[k
]
262 def parse_topology(lines
, level
):
264 Parse the output of 'show isis topology level-X' into a Python dict.
269 vertex_type_regex
= "|".join(VERTEX_TYPE_LIST
)
272 area_match
= re
.match(r
"Area (.+):", line
)
274 area
= area_match
.group(1)
275 if area
not in areas
:
276 areas
[area
] = {level
: {"ipv4": [], "ipv6": []}}
282 if re
.match(r
"IS\-IS paths to level-. routers that speak IPv6", line
):
285 if re
.match(r
"IS\-IS paths to level-. routers that speak IP", line
):
289 item_match
= re
.match(
290 r
"([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)", line
293 item_match
is not None
294 and item_match
.group(1) == "Vertex"
295 and item_match
.group(2) == "Type"
296 and item_match
.group(3) == "Metric"
297 and item_match
.group(4) == "Next-Hop"
298 and item_match
.group(5) == "Interface"
299 and item_match
.group(6) == "Parent"
304 item_match
= re
.match(
305 r
"([^\s]+) ({}) ([0]|([1-9][0-9]*)) ([^\s]+) ([^\s]+) ([^\s]+)".format(
310 if item_match
is not None:
311 areas
[area
][level
][ipv
].append(
313 "vertex": item_match
.group(1),
314 "type": item_match
.group(2),
315 "metric": item_match
.group(3),
316 "next-hop": item_match
.group(5),
317 "interface": item_match
.group(6),
318 "parent": item_match
.group(7),
323 item_match
= re
.match(
324 r
"([^\s]+) ({}) ([0]|([1-9][0-9]*)) ([^\s]+)".format(vertex_type_regex
),
328 if item_match
is not None:
329 areas
[area
][level
][ipv
].append(
331 "vertex": item_match
.group(1),
332 "type": item_match
.group(2),
333 "metric": item_match
.group(3),
334 "parent": item_match
.group(5),
339 item_match
= re
.match(r
"([^\s]+)", line
)
340 if item_match
is not None:
341 areas
[area
][level
][ipv
].append({"vertex": item_match
.group(1)})
347 def show_isis_topology(router
):
349 Get the ISIS topology in a dictionary format.
361 'vertex': '10.0.0.1/24',
371 "interface": "rX-ethY",
382 l1out
= topotest
.normalize_text(
383 router
.vtysh_cmd("show isis topology level-1")
385 l2out
= topotest
.normalize_text(
386 router
.vtysh_cmd("show isis topology level-2")
389 l1
= parse_topology(l1out
, "level-1")
390 l2
= parse_topology(l2out
, "level-2")