]> git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/integration/test_basic.py
1ff7f4b728f77ea5b6f75cd8c2e88e958216d189
[thirdparty/lldpd.git] / tests / integration / test_basic.py
1 import time
2 import pytest
3 import pyroute2
4
5
6 def test_one_neighbor(lldpd1, lldpd, lldpcli, namespaces):
7 with namespaces(2):
8 lldpd()
9 with namespaces(1):
10 out = lldpcli("-f", "keyvalue", "show", "neighbors")
11 assert out['lldp.eth0.age'].startswith('0 day, 00:00:')
12 assert out['lldp.eth0.chassis.descr'].startswith(
13 "Spectacular GNU/Linux 2016 Linux")
14 assert 'lldp.eth0.chassis.Router.enabled' in out
15 assert 'lldp.eth0.chassis.Station.enabled' in out
16 del out['lldp.eth0.age']
17 del out['lldp.eth0.chassis.descr']
18 del out['lldp.eth0.chassis.Router.enabled']
19 del out['lldp.eth0.chassis.Station.enabled']
20 assert out == {"lldp.eth0.via": "LLDP",
21 "lldp.eth0.rid": "1",
22 "lldp.eth0.chassis.mac": "00:00:00:00:00:02",
23 "lldp.eth0.chassis.name": "ns-2.example.com",
24 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:2",
25 "lldp.eth0.chassis.Bridge.enabled": "off",
26 "lldp.eth0.chassis.Wlan.enabled": "off",
27 "lldp.eth0.port.mac": "00:00:00:00:00:02",
28 "lldp.eth0.port.descr": "eth1",
29 "lldp.eth0.port.ttl": "120"}
30
31
32 @pytest.mark.parametrize("neighbors", (5, 10, 20))
33 def test_several_neighbors(lldpd, lldpcli, links, namespaces, neighbors):
34 for i in range(2, neighbors + 1):
35 links(namespaces(1), namespaces(i))
36 for i in range(1, neighbors + 1):
37 with namespaces(i):
38 lldpd(sleep=(i == 1 and 2 or 0),
39 silent=True)
40 time.sleep(10)
41 with namespaces(1):
42 out = lldpcli("-f", "keyvalue", "show", "neighbors")
43 for i in range(2, neighbors + 1):
44 assert out['lldp.eth{}.chassis.name'.format((i - 2)*2)] == \
45 'ns-{}.example.com'.format(i)
46
47
48 def test_one_interface(lldpd1, lldpd, lldpcli, namespaces):
49 with namespaces(2):
50 lldpd()
51 with namespaces(1):
52 out = lldpcli("-f", "keyvalue", "show", "interfaces")
53 assert out['lldp.eth0.chassis.descr'].startswith(
54 "Spectacular GNU/Linux 2016 Linux")
55 assert 'lldp.eth0.age' in out
56 assert 'lldp.eth0.chassis.Router.enabled' in out
57 assert 'lldp.eth0.chassis.Station.enabled' in out
58 del out['lldp.eth0.age']
59 del out['lldp.eth0.chassis.descr']
60 del out['lldp.eth0.chassis.Router.enabled']
61 del out['lldp.eth0.chassis.Station.enabled']
62 assert out == {"lldp.eth0.via": "unknown",
63 "lldp.eth0.chassis.mac": "00:00:00:00:00:01",
64 "lldp.eth0.chassis.name": "ns-1.example.com",
65 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:1",
66 "lldp.eth0.chassis.Bridge.enabled": "off",
67 "lldp.eth0.chassis.Wlan.enabled": "off",
68 "lldp.eth0.port.mac": "00:00:00:00:00:01",
69 "lldp.eth0.port.descr": "eth0",
70 "lldp.eth0.ttl.ttl": "120"}
71
72 @pytest.mark.parametrize("interfaces", (5, 10, 20))
73 def test_several_interfaces(lldpd, lldpcli, links, namespaces, interfaces):
74 for i in range(2, interfaces + 1):
75 links(namespaces(1), namespaces(i))
76 for i in range(1, interfaces + 1):
77 with namespaces(i):
78 lldpd()
79 with namespaces(1):
80 out = lldpcli("-f", "keyvalue", "show", "interfaces")
81 for i in range(2, interfaces + 1):
82 assert out['lldp.eth{}.chassis.mac'.format((i - 2)*2)] == \
83 '00:00:00:00:00:01'
84 assert out['lldp.eth{}.port.mac'.format((i - 2)*2)] == \
85 '00:00:00:00:00:{num:02x}'.format(num=(i - 2)*2 + 1)
86
87
88 def test_overrided_description(lldpd1, lldpd, lldpcli, namespaces):
89 with namespaces(2):
90 lldpd("-S", "Modified description")
91 with namespaces(1):
92 out = lldpcli("-f", "keyvalue", "show", "neighbors")
93 assert out['lldp.eth0.chassis.descr'] == "Modified description"
94
95
96 def test_overrided_description2(lldpd1, lldpd, lldpcli, namespaces):
97 with namespaces(2):
98 lldpd()
99 lldpcli("configure", "system", "description", "Modified description")
100 lldpcli("update")
101 time.sleep(1)
102 with namespaces(1):
103 out = lldpcli("-f", "keyvalue", "show", "neighbors")
104 assert out['lldp.eth0.chassis.descr'] == "Modified description"
105
106
107 def test_overrided_chassisid(lldpd1, lldpd, lldpcli, namespaces):
108 with namespaces(2):
109 lldpd()
110 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
111 lldpcli("update")
112 time.sleep(1)
113 with namespaces(1):
114 out = lldpcli("-f", "keyvalue", "show", "neighbors")
115 assert out['lldp.eth0.chassis.local'] == "Modified chassis ID"
116
117
118 def test_overrided_chassisid_reverse(lldpd1, lldpd, lldpcli, namespaces):
119 with namespaces(2):
120 lldpd()
121 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
122 lldpcli("unconfigure", "system", "chassisid")
123 lldpcli("update")
124 time.sleep(1)
125 with namespaces(1):
126 out = lldpcli("-f", "keyvalue", "show", "neighbors")
127 assert out['lldp.eth0.chassis.mac'] == "00:00:00:00:00:02"
128
129
130 def test_hide_kernel(lldpd1, lldpd, lldpcli, namespaces):
131 with namespaces(2):
132 lldpd("-k")
133 with namespaces(1):
134 out = lldpcli("-f", "keyvalue", "show", "neighbors")
135 assert out["lldp.eth0.chassis.descr"] == \
136 "Spectacular GNU/Linux 2016"
137
138
139 def test_listen_only(lldpd1, lldpd, lldpcli, namespaces):
140 with namespaces(2):
141 lldpd("-r")
142 with namespaces(1):
143 out = lldpcli("-f", "keyvalue", "show", "neighbors")
144 assert out == {}
145
146
147 def test_forced_management_address(lldpd1, lldpd, lldpcli, namespaces):
148 with namespaces(2):
149 lldpd("-m", "2001:db8::47")
150 with namespaces(1):
151 out = lldpcli("-f", "keyvalue", "show", "neighbors")
152 assert out["lldp.eth0.chassis.mgmt-ip"] == "2001:db8::47"
153
154
155 def test_management_address(lldpd1, lldpd, lldpcli, links, namespaces):
156 with namespaces(2):
157 ipr = pyroute2.IPRoute()
158 idx = ipr.link_lookup(ifname="eth1")[0]
159 ipr.addr('add', index=idx, address="192.168.14.2", mask=24)
160 ipr.addr('add', index=idx, address="172.25.21.47", mask=24)
161 lldpd("-m", "172.25.*")
162 with namespaces(1):
163 out = lldpcli("-f", "keyvalue", "show", "neighbors")
164 assert out["lldp.eth0.chassis.mgmt-ip"] == "172.25.21.47"
165
166
167 def test_portid_subtype_ifname(lldpd1, lldpd, lldpcli, namespaces):
168 with namespaces(2):
169 lldpd()
170 lldpcli("configure", "lldp", "portidsubtype", "ifname")
171 time.sleep(3)
172 with namespaces(1):
173 out = lldpcli("-f", "keyvalue", "show", "neighbors")
174 assert out["lldp.eth0.port.ifname"] == "eth1"
175 assert out["lldp.eth0.port.descr"] == "eth1"
176
177
178 def test_portid_subtype_with_alias(lldpd1, lldpd, lldpcli, links, namespaces):
179 with namespaces(2):
180 ipr = pyroute2.IPRoute()
181 idx = ipr.link_lookup(ifname="eth1")[0]
182 ipr.link('set', index=idx, ifalias="alias of eth1")
183 lldpd()
184 with namespaces(1):
185 out = lldpcli("-f", "keyvalue", "show", "neighbors")
186 assert out["lldp.eth0.port.ifname"] == "eth1"
187 assert out["lldp.eth0.port.descr"] == "alias of eth1"
188
189
190 def test_portid_subtype_macaddress(lldpd1, lldpd, lldpcli, links, namespaces):
191 with namespaces(2):
192 ipr = pyroute2.IPRoute()
193 idx = ipr.link_lookup(ifname="eth1")[0]
194 ipr.link('set', index=idx, ifalias="alias of eth1")
195 lldpd()
196 lldpcli("configure", "lldp", "portidsubtype", "macaddress")
197 time.sleep(3)
198 with namespaces(1):
199 out = lldpcli("-f", "keyvalue", "show", "neighbors")
200 assert out["lldp.eth0.port.mac"] == "00:00:00:00:00:02"
201 assert out["lldp.eth0.port.descr"] == "eth1"
202
203
204 def test_portid_subtype_local(lldpd1, lldpd, lldpcli, namespaces):
205 with namespaces(2):
206 lldpd()
207 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
208 time.sleep(3)
209 with namespaces(1):
210 out = lldpcli("-f", "keyvalue", "show", "neighbors")
211 assert out["lldp.eth0.port.local"] == "localname"
212 assert out["lldp.eth0.port.descr"] == "eth1"
213
214
215 def test_portid_subtype_local_with_description(lldpd1, lldpd, lldpcli, namespaces):
216 with namespaces(2):
217 lldpd()
218 lldpcli("configure", "lldp", "portidsubtype", "local", "localname", "description", "localdescription")
219 time.sleep(3)
220 with namespaces(1):
221 out = lldpcli("-f", "keyvalue", "show", "neighbors")
222 assert out["lldp.eth0.port.local"] == "localname"
223 assert out["lldp.eth0.port.descr"] == "localdescription"
224
225
226 def test_portid_subtype_local_with_alias(lldpd1, lldpd, lldpcli, namespaces):
227 with namespaces(2):
228 ipr = pyroute2.IPRoute()
229 idx = ipr.link_lookup(ifname="eth1")[0]
230 ipr.link('set', index=idx, ifalias="alias of eth1")
231 lldpd()
232 lldpd()
233 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
234 time.sleep(3)
235 with namespaces(1):
236 out = lldpcli("-f", "keyvalue", "show", "neighbors")
237 assert out["lldp.eth0.port.local"] == "localname"
238 assert out["lldp.eth0.port.descr"] == "alias of eth1"
239
240
241 def test_port_status_txonly(lldpd, lldpcli, namespaces, links):
242 links(namespaces(1), namespaces(2))
243 with namespaces(1):
244 lldpd()
245 lldpcli("configure", "lldp", "status", "tx-only")
246 with namespaces(2):
247 lldpd()
248 with namespaces(1):
249 out = lldpcli("-f", "keyvalue", "show", "neighbors")
250 assert out == {}
251 lldpcli("update")
252 with namespaces(2):
253 out = lldpcli("-f", "keyvalue", "show", "neighbors")
254 assert out["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
255
256
257 def test_port_status_rxonly(lldpd, lldpcli, namespaces, links):
258 links(namespaces(1), namespaces(2))
259 with namespaces(1):
260 lldpd()
261 lldpcli("configure", "lldp", "status", "rx-only")
262 with namespaces(2):
263 lldpd()
264 with namespaces(1):
265 out = lldpcli("-f", "keyvalue", "show", "neighbors")
266 assert out["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
267 lldpcli("update")
268 with namespaces(2):
269 out = lldpcli("-f", "keyvalue", "show", "neighbors")
270 assert out == {}
271
272
273 def test_port_status_rxandtx(lldpd, lldpcli, namespaces, links):
274 links(namespaces(1), namespaces(2))
275 with namespaces(1):
276 lldpd()
277 lldpcli("configure", "lldp", "status", "rx-and-tx") # noop
278 with namespaces(2):
279 lldpd()
280 with namespaces(1):
281 out = lldpcli("-f", "keyvalue", "show", "neighbors")
282 assert out["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
283 lldpcli("update")
284 with namespaces(2):
285 out = lldpcli("-f", "keyvalue", "show", "neighbors")
286 assert out["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
287
288
289 def test_port_status_disabled(lldpd, lldpcli, namespaces, links):
290 links(namespaces(1), namespaces(2))
291 with namespaces(1):
292 lldpd()
293 lldpcli("configure", "lldp", "status", "disabled")
294 with namespaces(2):
295 lldpd()
296 with namespaces(1):
297 out = lldpcli("-f", "keyvalue", "show", "neighbors")
298 assert out == {}
299 lldpcli("update")
300 with namespaces(2):
301 out = lldpcli("-f", "keyvalue", "show", "neighbors")
302 assert out == {}
303
304