2 # SPDX-License-Identifier: ISC
5 # Copyright (c) 2020 by Niral Networks, Inc. ("Niral Networks")
6 # Used Copyright (c) 2018 by Network Device Education Foundation,
7 # Inc. ("NetDEF") in this file.
11 test_isis_topo1_vrf.py: Test ISIS vrf topology.
21 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
22 sys
.path
.append(os
.path
.join(CWD
, "../"))
24 # pylint: disable=C0413
25 from lib
import topotest
26 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
27 from lib
.topolog
import logger
28 from lib
.topotest
import iproute2_is_vrf_capable
29 from lib
.common_config
import required_linux_kernel_version
32 pytestmark
= [pytest
.mark
.isisd
]
60 for routern
in range(1, 6):
61 tgen
.add_router("r{}".format(routern
))
64 sw
= tgen
.add_switch("sw1")
65 sw
.add_link(tgen
.gears
["r1"])
66 sw
.add_link(tgen
.gears
["r3"])
69 sw
= tgen
.add_switch("sw2")
70 sw
.add_link(tgen
.gears
["r2"])
71 sw
.add_link(tgen
.gears
["r4"])
74 sw
= tgen
.add_switch("sw3")
75 sw
.add_link(tgen
.gears
["r3"])
76 sw
.add_link(tgen
.gears
["r5"])
79 sw
= tgen
.add_switch("sw4")
80 sw
.add_link(tgen
.gears
["r4"])
81 sw
.add_link(tgen
.gears
["r5"])
84 def setup_module(mod
):
85 "Sets up the pytest environment"
86 tgen
= Topogen(build_topo
, mod
.__name
__)
89 logger
.info("Testing with VRF Lite support")
92 "ip link add {0}-cust1 type vrf table 1001",
93 "ip link add loop1 type dummy",
94 "ip link set {0}-eth0 master {0}-cust1",
97 eth1_cmds
= ["ip link set {0}-eth1 master {0}-cust1"]
99 # For all registered routers, load the zebra configuration file
100 for rname
, router
in tgen
.routers().items():
101 # create VRF rx-cust1 and link rx-eth0 to rx-cust1
103 output
= tgen
.net
[rname
].cmd(cmd
.format(rname
))
105 # If router has an rX-eth1, link that to vrf also
106 if "{}-eth1".format(rname
) in router
.links
.keys():
107 for cmd
in eth1_cmds
:
108 output
= output
+ tgen
.net
[rname
].cmd(cmd
.format(rname
))
110 for rname
, router
in tgen
.routers().items():
112 TopoRouter
.RD_ZEBRA
, os
.path
.join(CWD
, "{}/zebra.conf".format(rname
))
115 TopoRouter
.RD_ISIS
, os
.path
.join(CWD
, "{}/isisd.conf".format(rname
))
117 # After loading the configurations, this function loads configured daemons.
121 def teardown_module(mod
):
122 "Teardown the pytest environment"
124 # move back rx-eth0 to default VRF
129 def test_isis_convergence():
130 "Wait for the protocol to converge before starting to test"
132 # Don't run this test if we have any failure.
133 if tgen
.routers_have_failure():
134 pytest
.skip(tgen
.errors
)
136 logger
.info("waiting for ISIS protocol to converge")
138 for rname
, router
in tgen
.routers().items():
139 filename
= "{0}/{1}/{1}_topology.json".format(CWD
, rname
)
140 expected
= json
.loads(open(filename
).read())
142 def compare_isis_topology(router
, expected
):
143 "Helper function to test ISIS vrf topology convergence."
144 actual
= show_isis_topology(router
)
146 return topotest
.json_cmp(actual
, expected
)
148 test_func
= functools
.partial(compare_isis_topology
, router
, expected
)
149 (result
, diff
) = topotest
.run_and_expect(test_func
, None, wait
=0.5, count
=120)
150 assert result
, "ISIS did not converge on {}:\n{}".format(rname
, diff
)
153 def test_isis_route_installation():
154 "Check whether all expected routes are present"
156 # Don't run this test if we have any failure.
157 if tgen
.routers_have_failure():
158 pytest
.skip(tgen
.errors
)
160 logger
.info("Checking routers for installed ISIS vrf routes")
161 # Check for routes in 'show ip route vrf {}-cust1 json'
162 for rname
, router
in tgen
.routers().items():
163 filename
= "{0}/{1}/{1}_route.json".format(CWD
, rname
)
164 expected
= json
.loads(open(filename
, "r").read())
166 def compare_routing_table(router
, expected
):
167 "Helper function to ensure zebra rib convergence"
169 actual
= router
.vtysh_cmd(
170 "show ip route vrf {0}-cust1 json".format(rname
), isjson
=True
172 return topotest
.json_cmp(actual
, expected
)
174 test_func
= functools
.partial(compare_routing_table
, router
, expected
)
175 (result
, diff
) = topotest
.run_and_expect(test_func
, None, count
=20, wait
=1)
176 assertmsg
= "Router '{}' routes mismatch diff: {}".format(rname
, diff
)
177 assert result
, assertmsg
180 def test_isis_linux_route_installation():
181 "Check whether all expected routes are present and installed in the OS"
183 # Don't run this test if we have any failure.
184 if tgen
.routers_have_failure():
185 pytest
.skip(tgen
.errors
)
187 # Required linux kernel version for this suite to run.
188 result
= required_linux_kernel_version("4.15")
189 if result
is not True:
190 pytest
.skip("Kernel requirements are not met")
192 # iproute2 needs to support VRFs for this suite to run.
193 if not iproute2_is_vrf_capable():
194 pytest
.skip("Installed iproute2 version does not support VRFs")
196 logger
.info("Checking routers for installed ISIS vrf routes in OS")
197 # Check for routes in `ip route show vrf {}-cust1`
198 for rname
, router
in tgen
.routers().items():
199 filename
= "{0}/{1}/{1}_route_linux.json".format(CWD
, rname
)
200 expected
= json
.loads(open(filename
, "r").read())
201 actual
= topotest
.ip4_vrf_route(router
)
202 assertmsg
= "Router '{}' OS routes mismatch".format(rname
)
203 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
206 def test_isis_route6_installation():
207 "Check whether all expected routes are present"
209 # Don't run this test if we have any failure.
210 if tgen
.routers_have_failure():
211 pytest
.skip(tgen
.errors
)
213 logger
.info("Checking routers for installed ISIS vrf IPv6 routes")
214 # Check for routes in 'show ipv6 route vrf {}-cust1 json'
215 for rname
, router
in tgen
.routers().items():
216 filename
= "{0}/{1}/{1}_route6.json".format(CWD
, rname
)
217 expected
= json
.loads(open(filename
, "r").read())
219 def compare_routing_table(router
, expected
):
220 "Helper function to ensure zebra rib convergence"
221 actual
= router
.vtysh_cmd(
222 "show ipv6 route vrf {}-cust1 json".format(rname
), isjson
=True
224 return topotest
.json_cmp(actual
, expected
)
226 test_func
= functools
.partial(compare_routing_table
, router
, expected
)
227 (result
, diff
) = topotest
.run_and_expect(test_func
, None, count
=20, wait
=1)
228 assertmsg
= "Router '{}' routes mismatch diff: ".format(rname
, diff
)
229 assert result
, assertmsg
232 def test_isis_linux_route6_installation():
233 "Check whether all expected routes are present and installed in the OS"
235 # Don't run this test if we have any failure.
236 if tgen
.routers_have_failure():
237 pytest
.skip(tgen
.errors
)
239 # Required linux kernel version for this suite to run.
240 result
= required_linux_kernel_version("4.15")
241 if result
is not True:
242 pytest
.skip("Kernel requirements are not met")
244 # iproute2 needs to support VRFs for this suite to run.
245 if not iproute2_is_vrf_capable():
246 pytest
.skip("Installed iproute2 version does not support VRFs")
248 logger
.info("Checking routers for installed ISIS vrf IPv6 routes in OS")
249 # Check for routes in `ip -6 route show vrf {}-cust1`
250 for rname
, router
in tgen
.routers().items():
251 filename
= "{0}/{1}/{1}_route6_linux.json".format(CWD
, rname
)
252 expected
= json
.loads(open(filename
, "r").read())
253 actual
= topotest
.ip6_vrf_route(router
)
254 assertmsg
= "Router '{}' OS routes mismatch".format(rname
)
255 assert topotest
.json_cmp(actual
, expected
) is None, assertmsg
258 def test_memory_leak():
259 "Run the memory leak test and report results."
261 if not tgen
.is_memleak_enabled():
262 pytest
.skip("Memory leak test/report is disabled")
264 tgen
.report_memory_leaks()
267 if __name__
== "__main__":
268 args
= ["-s"] + sys
.argv
[1:]
269 sys
.exit(pytest
.main(args
))
273 # Auxiliary functions
277 def dict_merge(dct
, merge_dct
):
279 Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
280 updating only top-level keys, dict_merge recurses down into dicts nested
281 to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
283 :param dct: dict onto which the merge is executed
284 :param merge_dct: dct merged into dct
288 https://gist.github.com/angstwad/bf22d1822c38a92ec0a9
290 for k
, v
in merge_dct
.items():
291 if k
in dct
and isinstance(dct
[k
], dict) and topotest
.is_mapping(merge_dct
[k
]):
292 dict_merge(dct
[k
], merge_dct
[k
])
294 dct
[k
] = merge_dct
[k
]
297 def parse_topology(lines
, level
):
299 Parse the output of 'show isis topology level-X' into a Python dict.
304 vertex_type_regex
= "|".join(VERTEX_TYPE_LIST
)
307 area_match
= re
.match(r
"Area (.+):", line
)
309 area
= area_match
.group(1)
310 if area
not in areas
:
311 areas
[area
] = {level
: {"ipv4": [], "ipv6": []}}
317 if re
.match(r
"IS\-IS paths to level-. routers that speak IPv6", line
):
320 if re
.match(r
"IS\-IS paths to level-. routers that speak IP", line
):
324 item_match
= re
.match(
325 r
"([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)", line
328 item_match
is not None
329 and item_match
.group(1) == "Vertex"
330 and item_match
.group(2) == "Type"
331 and item_match
.group(3) == "Metric"
332 and item_match
.group(4) == "Next-Hop"
333 and item_match
.group(5) == "Interface"
334 and item_match
.group(6) == "Parent"
339 item_match
= re
.match(
340 r
"([^\s]+) ({}) ([0]|([1-9][0-9]*)) ([^\s]+) ([^\s]+) ([^\s]+)".format(
345 if item_match
is not None:
346 areas
[area
][level
][ipv
].append(
348 "vertex": item_match
.group(1),
349 "type": item_match
.group(2),
350 "metric": item_match
.group(3),
351 "next-hop": item_match
.group(5),
352 "interface": item_match
.group(6),
353 "parent": item_match
.group(7),
358 item_match
= re
.match(
359 r
"([^\s]+) ({}) ([0]|([1-9][0-9]*)) ([^\s]+)".format(vertex_type_regex
),
363 if item_match
is not None:
364 areas
[area
][level
][ipv
].append(
366 "vertex": item_match
.group(1),
367 "type": item_match
.group(2),
368 "metric": item_match
.group(3),
369 "parent": item_match
.group(5),
374 item_match
= re
.match(r
"([^\s]+)", line
)
375 if item_match
is not None:
376 areas
[area
][level
][ipv
].append({"vertex": item_match
.group(1)})
382 def show_isis_topology(router
):
384 Get the ISIS vrf topology in a dictionary format.
396 'vertex': '10.0.0.1/24',
406 "interface": "rX-ethY",
417 l1out
= topotest
.normalize_text(
418 router
.vtysh_cmd("show isis vrf {}-cust1 topology level-1".format(router
.name
))
420 l2out
= topotest
.normalize_text(
421 router
.vtysh_cmd("show isis vrf {}-cust1 topology level-2".format(router
.name
))
424 l1
= parse_topology(l1out
, "level-1")
425 l2
= parse_topology(l2out
, "level-2")