]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/nhrp_topo/test_nhrp_topo.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / nhrp_topo / test_nhrp_topo.py
1 #!/usr/bin/env python
2 # SPDX-License-Identifier: ISC
3
4 #
5 # test_nhrp_topo.py
6 # Part of NetDEF Topology Tests
7 #
8 # Copyright (c) 2019 by
9 # Network Device Education Foundation, Inc. ("NetDEF")
10 #
11
12 """
13 test_nhrp_topo.py: Test the FRR/Quagga NHRP daemon
14 """
15
16 import os
17 import sys
18 import json
19 from functools import partial
20 import pytest
21
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, "../"))
25
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
32
33 # Required to instantiate the topology builder class.
34
35 pytestmark = [pytest.mark.nhrpd]
36
37
38 def build_topo(tgen):
39 "Build function"
40
41 # Create 3 routers.
42 for routern in range(1, 4):
43 tgen.add_router("r{}".format(routern))
44
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"])
55
56
57 def _populate_iface():
58 tgen = get_topogen()
59 cmds_tot_hub = [
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",
65 ]
66
67 cmds_tot = [
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",
73 ]
74
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)
80
81 for cmd in cmds_tot:
82 input = cmd.format("r1", "1")
83 logger.info("input: " + input)
84 output = tgen.net["r1"].cmd(input)
85 logger.info("output: " + output)
86
87
88 def setup_module(mod):
89 "Sets up the pytest environment"
90
91 result = required_linux_kernel_version("5.0")
92 if result is not True:
93 pytest.skip("Kernel requirements are not met")
94
95 tgen = Topogen(build_topo, mod.__name__)
96 tgen.start_topology()
97
98 router_list = tgen.routers()
99 _populate_iface()
100
101 for rname, router in router_list.items():
102 router.load_config(
103 TopoRouter.RD_ZEBRA,
104 os.path.join(CWD, "{}/zebra.conf".format(rname)),
105 )
106 if rname in ("r1", "r2"):
107 router.load_config(
108 TopoRouter.RD_NHRP, os.path.join(CWD, "{}/nhrpd.conf".format(rname))
109 )
110
111 # Initialize all routers.
112 logger.info("Launching NHRP")
113 for name in router_list:
114 router = tgen.gears[name]
115 router.start()
116
117
118 def teardown_module(_mod):
119 "Teardown the pytest environment"
120 tgen = get_topogen()
121 tgen.stop_topology()
122
123
124 def test_protocols_convergence():
125 """
126 Assert that all protocols have converged before checking for the NHRP
127 statuses as they depend on it.
128 """
129 tgen = get_topogen()
130 if tgen.routers_have_failure():
131 pytest.skip(tgen.errors)
132
133 # Check IPv4 routing tables.
134 logger.info("Checking NHRP cache and IPv4 routes for convergence")
135 router_list = tgen.routers()
136
137 for rname, router in router_list.items():
138 if rname == "r3":
139 continue
140
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))
144 continue
145
146 expected = json.loads(open(json_file).read())
147 test_func = partial(
148 topotest.router_json_cmp, router, "show ip nhrp cache json", expected
149 )
150 _, result = topotest.run_and_expect(test_func, None, count=40, wait=0.5)
151
152 output = router.vtysh_cmd("show ip nhrp cache")
153 logger.info(output)
154
155 assertmsg = '"{}" JSON output mismatches'.format(router.name)
156 assert result is None, assertmsg
157
158 for rname, router in router_list.items():
159 if rname == "r3":
160 continue
161
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))
165 continue
166
167 expected = json.loads(open(json_file).read())
168 test_func = partial(
169 topotest.router_json_cmp, router, "show ip route nhrp json", expected
170 )
171 _, result = topotest.run_and_expect(test_func, None, count=40, wait=0.5)
172
173 output = router.vtysh_cmd("show ip route nhrp")
174 logger.info(output)
175
176 assertmsg = '"{}" JSON output mismatches'.format(router.name)
177 assert result is None, assertmsg
178
179 for rname, router in router_list.items():
180 if rname == "r3":
181 continue
182 logger.info("Dump neighbor information on {}-gre0".format(rname))
183 output = router.run("ip neigh show")
184 logger.info(output)
185
186
187 def test_nhrp_connection():
188 "Assert that the NHRP peers can find themselves."
189 tgen = get_topogen()
190 if tgen.routers_have_failure():
191 pytest.skip(tgen.errors)
192
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")
196 logger.info(output)
197 if "1000 packets transmitted, 1000 received" not in output:
198 assertmsg = "expected ping IPv4 from R1 to R2 should be ok"
199 assert 0, assertmsg
200 else:
201 logger.info("Check Ping IPv4 from R1 to R2 OK")
202
203
204 def test_memory_leak():
205 "Run the memory leak test and report results."
206 tgen = get_topogen()
207 if not tgen.is_memleak_enabled():
208 pytest.skip("Memory leak test/report is disabled")
209
210 tgen.report_memory_leaks()
211
212
213 if __name__ == "__main__":
214 args = ["-s"] + sys.argv[1:]
215 sys.exit(pytest.main(args))