2 # SPDX-License-Identifier: ISC
6 # Part of NetDEF Topology Tests
8 # Copyright (c) 2019 by
9 # Network Device Education Foundation, Inc. ("NetDEF")
13 test_nhrp_topo.py: Test the FRR/Quagga NHRP daemon
19 from functools
import partial
22 # Save the Current Working Directory to find configuration files.
23 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
24 sys
.path
.append(os
.path
.join(CWD
, "../"))
26 # pylint: disable=C0413
27 # Import topogen and topotest helpers
28 from lib
import topotest
29 from lib
.topogen
import Topogen
, TopoRouter
, get_topogen
30 from lib
.topolog
import logger
31 from lib
.common_config
import required_linux_kernel_version
33 # Required to instantiate the topology builder class.
35 pytestmark
= [pytest
.mark
.nhrpd
]
42 for routern
in range(1, 4):
43 tgen
.add_router("r{}".format(routern
))
45 switch
= tgen
.add_switch("s1")
46 switch
.add_link(tgen
.gears
["r1"])
47 switch
.add_link(tgen
.gears
["r3"])
48 switch
= tgen
.add_switch("s2")
49 switch
.add_link(tgen
.gears
["r2"])
50 switch
.add_link(tgen
.gears
["r3"])
51 switch
= tgen
.add_switch("s3")
52 switch
.add_link(tgen
.gears
["r2"])
53 switch
= tgen
.add_switch("s4")
54 switch
.add_link(tgen
.gears
["r1"])
57 def _populate_iface():
60 "ip tunnel add {0}-gre0 mode gre ttl 64 key 42 dev {0}-eth0 local 10.2.1.{1} remote 0.0.0.0",
61 "ip link set dev {0}-gre0 up",
62 "echo 0 > /proc/sys/net/ipv4/ip_forward_use_pmtu",
63 "echo 1 > /proc/sys/net/ipv6/conf/{0}-eth0/disable_ipv6",
64 "echo 1 > /proc/sys/net/ipv6/conf/{0}-gre0/disable_ipv6",
68 "ip tunnel add {0}-gre0 mode gre ttl 64 key 42 dev {0}-eth0 local 10.1.1.{1} remote 0.0.0.0",
69 "ip link set dev {0}-gre0 up",
70 "echo 0 > /proc/sys/net/ipv4/ip_forward_use_pmtu",
71 "echo 1 > /proc/sys/net/ipv6/conf/{0}-eth0/disable_ipv6",
72 "echo 1 > /proc/sys/net/ipv6/conf/{0}-gre0/disable_ipv6",
75 for cmd
in cmds_tot_hub
:
76 input = cmd
.format("r2", "2")
77 logger
.info("input: " + input)
78 output
= tgen
.net
["r2"].cmd(input)
79 logger
.info("output: " + output
)
82 input = cmd
.format("r1", "1")
83 logger
.info("input: " + input)
84 output
= tgen
.net
["r1"].cmd(input)
85 logger
.info("output: " + output
)
88 def setup_module(mod
):
89 "Sets up the pytest environment"
91 result
= required_linux_kernel_version("5.0")
92 if result
is not True:
93 pytest
.skip("Kernel requirements are not met")
95 tgen
= Topogen(build_topo
, mod
.__name
__)
98 router_list
= tgen
.routers()
101 for rname
, router
in router_list
.items():
104 os
.path
.join(CWD
, "{}/zebra.conf".format(rname
)),
106 if rname
in ("r1", "r2"):
108 TopoRouter
.RD_NHRP
, os
.path
.join(CWD
, "{}/nhrpd.conf".format(rname
))
111 # Initialize all routers.
112 logger
.info("Launching NHRP")
113 for name
in router_list
:
114 router
= tgen
.gears
[name
]
118 def teardown_module(_mod
):
119 "Teardown the pytest environment"
124 def test_protocols_convergence():
126 Assert that all protocols have converged before checking for the NHRP
127 statuses as they depend on it.
130 if tgen
.routers_have_failure():
131 pytest
.skip(tgen
.errors
)
133 # Check IPv4 routing tables.
134 logger
.info("Checking NHRP cache and IPv4 routes for convergence")
135 router_list
= tgen
.routers()
137 for rname
, router
in router_list
.items():
141 json_file
= "{}/{}/nhrp4_cache.json".format(CWD
, router
.name
)
142 if not os
.path
.isfile(json_file
):
143 logger
.info("skipping file {}".format(json_file
))
146 expected
= json
.loads(open(json_file
).read())
148 topotest
.router_json_cmp
, router
, "show ip nhrp cache json", expected
150 _
, result
= topotest
.run_and_expect(test_func
, None, count
=40, wait
=0.5)
152 output
= router
.vtysh_cmd("show ip nhrp cache")
155 assertmsg
= '"{}" JSON output mismatches'.format(router
.name
)
156 assert result
is None, assertmsg
158 for rname
, router
in router_list
.items():
162 json_file
= "{}/{}/nhrp_route4.json".format(CWD
, router
.name
)
163 if not os
.path
.isfile(json_file
):
164 logger
.info("skipping file {}".format(json_file
))
167 expected
= json
.loads(open(json_file
).read())
169 topotest
.router_json_cmp
, router
, "show ip route nhrp json", expected
171 _
, result
= topotest
.run_and_expect(test_func
, None, count
=40, wait
=0.5)
173 output
= router
.vtysh_cmd("show ip route nhrp")
176 assertmsg
= '"{}" JSON output mismatches'.format(router
.name
)
177 assert result
is None, assertmsg
179 for rname
, router
in router_list
.items():
182 logger
.info("Dump neighbor information on {}-gre0".format(rname
))
183 output
= router
.run("ip neigh show")
187 def test_nhrp_connection():
188 "Assert that the NHRP peers can find themselves."
190 if tgen
.routers_have_failure():
191 pytest
.skip(tgen
.errors
)
193 pingrouter
= tgen
.gears
["r1"]
194 logger
.info("Check Ping IPv4 from R1 to R2 = 10.255.255.2)")
195 output
= pingrouter
.run("ping 10.255.255.2 -f -c 1000")
197 if "1000 packets transmitted, 1000 received" not in output
:
198 assertmsg
= "expected ping IPv4 from R1 to R2 should be ok"
201 logger
.info("Check Ping IPv4 from R1 to R2 OK")
204 def test_memory_leak():
205 "Run the memory leak test and report results."
207 if not tgen
.is_memleak_enabled():
208 pytest
.skip("Memory leak test/report is disabled")
210 tgen
.report_memory_leaks()
213 if __name__
== "__main__":
214 args
= ["-s"] + sys
.argv
[1:]
215 sys
.exit(pytest
.main(args
))