]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/integration/test_basic.py
5 import scapy
.contrib
.lldp
8 def test_one_neighbor(lldpd1
, lldpd
, lldpcli
, namespaces
):
12 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
13 assert out
['lldp.eth0.age'].startswith('0 day, 00:00:')
14 assert out
['lldp.eth0.chassis.descr'].startswith(
15 "Spectacular GNU/Linux 2016 Linux")
16 assert 'lldp.eth0.chassis.Router.enabled' in out
17 assert 'lldp.eth0.chassis.Station.enabled' in out
18 del out
['lldp.eth0.age']
19 del out
['lldp.eth0.chassis.descr']
20 del out
['lldp.eth0.chassis.Router.enabled']
21 del out
['lldp.eth0.chassis.Station.enabled']
22 assert out
== {"lldp.eth0.via": "LLDP",
24 "lldp.eth0.chassis.mac": "00:00:00:00:00:02",
25 "lldp.eth0.chassis.name": "ns-2.example.com",
26 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:2",
27 "lldp.eth0.chassis.mgmt-iface": "2",
28 "lldp.eth0.chassis.Bridge.enabled": "off",
29 "lldp.eth0.chassis.Wlan.enabled": "off",
30 "lldp.eth0.port.mac": "00:00:00:00:00:02",
31 "lldp.eth0.port.descr": "eth1",
32 "lldp.eth0.port.ttl": "120"}
35 @pytest.mark
.parametrize("neighbors", (5, 10, 20))
36 def test_several_neighbors(lldpd
, lldpcli
, links
, namespaces
, neighbors
):
37 for i
in range(2, neighbors
+ 1):
38 links(namespaces(1), namespaces(i
))
39 for i
in range(1, neighbors
+ 1):
41 lldpd(sleep
=(i
== 1 and 2 or 0),
45 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
46 for i
in range(2, neighbors
+ 1):
47 assert out
['lldp.eth{}.chassis.name'.format((i
- 2)*2)] == \
48 'ns-{}.example.com'.format(i
)
51 def test_one_interface(lldpd1
, lldpd
, lldpcli
, namespaces
):
55 out
= lldpcli("-f", "keyvalue", "show", "interfaces")
56 assert out
['lldp.eth0.chassis.descr'].startswith(
57 "Spectacular GNU/Linux 2016 Linux")
58 assert 'lldp.eth0.age' in out
59 assert 'lldp.eth0.chassis.Router.enabled' in out
60 assert 'lldp.eth0.chassis.Station.enabled' in out
61 del out
['lldp.eth0.age']
62 del out
['lldp.eth0.chassis.descr']
63 del out
['lldp.eth0.chassis.Router.enabled']
64 del out
['lldp.eth0.chassis.Station.enabled']
65 assert out
== {"lldp.eth0.via": "unknown",
66 "lldp.eth0.chassis.mac": "00:00:00:00:00:01",
67 "lldp.eth0.chassis.name": "ns-1.example.com",
68 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:1",
69 "lldp.eth0.chassis.mgmt-iface": "3",
70 "lldp.eth0.chassis.Bridge.enabled": "off",
71 "lldp.eth0.chassis.Wlan.enabled": "off",
72 "lldp.eth0.port.mac": "00:00:00:00:00:01",
73 "lldp.eth0.port.descr": "eth0",
74 "lldp.eth0.ttl.ttl": "120"}
76 @pytest.mark
.parametrize("interfaces", (5, 10, 20))
77 def test_several_interfaces(lldpd
, lldpcli
, links
, namespaces
, interfaces
):
78 for i
in range(2, interfaces
+ 1):
79 links(namespaces(1), namespaces(i
))
80 for i
in range(1, interfaces
+ 1):
84 out
= lldpcli("-f", "keyvalue", "show", "interfaces")
85 for i
in range(2, interfaces
+ 1):
86 assert out
['lldp.eth{}.chassis.mac'.format((i
- 2)*2)] == \
88 assert out
['lldp.eth{}.port.mac'.format((i
- 2)*2)] == \
89 '00:00:00:00:00:{num:02x}'.format(num
=(i
- 2)*2 + 1)
92 def test_overrided_description(lldpd1
, lldpd
, lldpcli
, namespaces
):
94 lldpd("-S", "Modified description")
96 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
97 assert out
['lldp.eth0.chassis.descr'] == "Modified description"
100 def test_overrided_description2(lldpd1
, lldpd
, lldpcli
, namespaces
):
103 lldpcli("configure", "system", "description", "Modified description")
107 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
108 assert out
['lldp.eth0.chassis.descr'] == "Modified description"
111 def test_overrided_chassisid(lldpd1
, lldpd
, lldpcli
, namespaces
):
114 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
118 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
119 assert out
['lldp.eth0.chassis.local'] == "Modified chassis ID"
122 def test_overrided_chassisid_reverse(lldpd1
, lldpd
, lldpcli
, namespaces
):
125 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
126 lldpcli("unconfigure", "system", "chassisid")
130 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
131 assert out
['lldp.eth0.chassis.mac'] == "00:00:00:00:00:02"
134 def test_hide_kernel(lldpd1
, lldpd
, lldpcli
, namespaces
):
138 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
139 assert out
["lldp.eth0.chassis.descr"] == \
140 "Spectacular GNU/Linux 2016"
143 def test_listen_only(lldpd1
, lldpd
, lldpcli
, namespaces
):
147 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
151 def test_forced_unknown_management_address(lldpd1
, lldpd
, lldpcli
, namespaces
):
153 lldpd("-m", "2001:db8::47")
155 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
156 assert out
["lldp.eth0.chassis.mgmt-ip"] == "2001:db8::47"
157 assert "lldp.eth0.chassis.mgmt-iface" not in out
160 def test_forced_known_management_address(lldpd1
, lldpd
, lldpcli
, namespaces
):
162 ipr
= pyroute2
.IPRoute()
163 idx
= ipr
.link_lookup(ifname
="eth1")[0]
164 ipr
.addr('add', index
=idx
, address
="192.168.14.2", mask
=24)
165 lldpd("-m", "192.168.14.2")
167 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
168 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.2"
169 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
172 def test_management_address(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
174 ipr
= pyroute2
.IPRoute()
175 idx
= ipr
.link_lookup(ifname
="eth1")[0]
176 ipr
.addr('add', index
=idx
, address
="192.168.14.2", mask
=24)
177 ipr
.addr('add', index
=idx
, address
="172.25.21.47", mask
=24)
178 lldpd("-m", "172.25.*")
180 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
181 assert out
["lldp.eth0.chassis.mgmt-ip"] == "172.25.21.47"
182 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
185 def test_management_interface(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
186 links(namespaces(1), namespaces(2), 4)
188 ipr
= pyroute2
.IPRoute()
189 idx
= ipr
.link_lookup(ifname
="eth1")[0]
190 ipr
.addr('add', index
=idx
, address
="192.168.14.2", mask
=24)
191 idx
= ipr
.link_lookup(ifname
="eth3")[0]
192 ipr
.addr('add', index
=idx
, address
="172.25.21.47", mask
=24)
195 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
196 assert out
["lldp.eth0.chassis.mgmt-ip"] == ["172.25.21.47",
197 "fe80::200:ff:fe00:4"]
198 assert out
["lldp.eth0.chassis.mgmt-iface"] == ["4", "4"]
201 def test_change_management_address(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
203 ipr
= pyroute2
.IPRoute()
204 idx
= ipr
.link_lookup(ifname
="eth1")[0]
205 ipr
.addr('add', index
=idx
, address
="192.168.14.2", mask
=24)
206 lldpd("-m", "192.168.*")
207 # We need a short TX interval as updating the IP address
208 # doesn't trigger a resend.
209 lldpcli("configure", "lldp", "tx-interval", "2")
211 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
212 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.2"
213 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
215 ipr
.addr('del', index
=idx
, address
="192.168.14.2", mask
=24)
216 ipr
.addr('add', index
=idx
, address
="192.168.14.5", mask
=24)
219 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
220 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.5"
221 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
224 def test_portid_subtype_ifname(lldpd1
, lldpd
, lldpcli
, namespaces
):
227 lldpcli("configure", "lldp", "portidsubtype", "ifname")
230 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
231 assert out
["lldp.eth0.port.ifname"] == "eth1"
232 assert out
["lldp.eth0.port.descr"] == "eth1"
235 def test_portid_subtype_with_alias(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
237 ipr
= pyroute2
.IPRoute()
238 idx
= ipr
.link_lookup(ifname
="eth1")[0]
239 ipr
.link('set', index
=idx
, ifalias
="alias of eth1")
242 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
243 assert out
["lldp.eth0.port.ifname"] == "eth1"
244 assert out
["lldp.eth0.port.descr"] == "alias of eth1"
247 def test_portid_subtype_macaddress(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
249 ipr
= pyroute2
.IPRoute()
250 idx
= ipr
.link_lookup(ifname
="eth1")[0]
251 ipr
.link('set', index
=idx
, ifalias
="alias of eth1")
253 lldpcli("configure", "lldp", "portidsubtype", "macaddress")
256 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
257 assert out
["lldp.eth0.port.mac"] == "00:00:00:00:00:02"
258 assert out
["lldp.eth0.port.descr"] == "eth1"
261 def test_portid_subtype_local(lldpd1
, lldpd
, lldpcli
, namespaces
):
264 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
267 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
268 assert out
["lldp.eth0.port.local"] == "localname"
269 assert out
["lldp.eth0.port.descr"] == "eth1"
272 def test_portid_subtype_local_with_description(lldpd1
, lldpd
, lldpcli
, namespaces
):
275 lldpcli("configure", "lldp", "portidsubtype", "local", "localname", "description", "localdescription")
278 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
279 assert out
["lldp.eth0.port.local"] == "localname"
280 assert out
["lldp.eth0.port.descr"] == "localdescription"
283 def test_portdescription(lldpd1
, lldpd
, lldpcli
, namespaces
):
286 lldpcli("configure", "lldp", "portdescription", "localdescription")
289 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
290 assert out
["lldp.eth0.port.descr"] == "localdescription"
293 def test_portid_subtype_local_with_alias(lldpd1
, lldpd
, lldpcli
, namespaces
):
295 ipr
= pyroute2
.IPRoute()
296 idx
= ipr
.link_lookup(ifname
="eth1")[0]
297 ipr
.link('set', index
=idx
, ifalias
="alias of eth1")
299 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
302 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
303 assert out
["lldp.eth0.port.local"] == "localname"
304 assert out
["lldp.eth0.port.descr"] == "alias of eth1"
307 def test_port_status_txonly(lldpd
, lldpcli
, namespaces
, links
):
308 links(namespaces(1), namespaces(2))
311 lldpcli("configure", "lldp", "status", "tx-only")
315 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
319 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
320 assert out
["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
323 def test_port_status_rxonly(lldpd
, lldpcli
, namespaces
, links
):
324 links(namespaces(1), namespaces(2))
327 lldpcli("configure", "lldp", "status", "rx-only")
331 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
332 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
335 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
339 def test_port_status_rxandtx(lldpd
, lldpcli
, namespaces
, links
):
340 links(namespaces(1), namespaces(2))
343 lldpcli("configure", "lldp", "status", "rx-and-tx") # noop
347 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
348 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
351 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
352 assert out
["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
355 def test_port_status_disabled(lldpd
, lldpcli
, namespaces
, links
):
356 links(namespaces(1), namespaces(2))
359 lldpcli("configure", "lldp", "status", "disabled")
363 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
367 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
371 def test_set_interface_alias(lldpd1
, lldpd
, lldpcli
, namespaces
):
373 lldpcli("configure", "system", "interface", "description")
377 ipr
= pyroute2
.IPRoute()
378 link
= ipr
.link('get', ifname
='eth0')[0]
379 assert link
.get_attr('IFLA_IFALIAS') == 'lldpd: connected to ns-2.example.com'
382 def test_lldpdu_shutdown(lldpd
, lldpcli
, namespaces
, links
):
383 links(namespaces(1), namespaces(2))
384 links(namespaces(1), namespaces(2))
387 # From https://github.com/vincentbernat/lldpd/issues/348
388 frm_fa01
= scapy
.all
.Ether(
389 src
='04:fe:7f:00:00:01',
390 dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
) / \
391 scapy
.contrib
.lldp
.LLDPDUChassisID(
392 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
393 id=b
'\x04\xfe\x7f\x00\x00\x00') / \
394 scapy
.contrib
.lldp
.LLDPDUPortID(
395 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
,
397 scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=65535) / \
398 scapy
.contrib
.lldp
.LLDPDUSystemName(
399 system_name
='this info should not disappear') / \
400 scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
401 frm_fa01
= frm_fa01
.build()
402 frm_fa01
= scapy
.all
.Ether(frm_fa01
)
404 frm_fa02
= scapy
.all
.Ether(
405 src
='04:fe:7f:00:00:02',
406 dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
) / \
407 scapy
.contrib
.lldp
.LLDPDUChassisID(
408 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
409 id=b
'\x04\xfe\x7f\x00\x00\x00') / \
410 scapy
.contrib
.lldp
.LLDPDUPortID(
411 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
,
413 scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=65535) / \
414 scapy
.contrib
.lldp
.LLDPDUSystemName(
415 system_name
='this info should not disappear') / \
416 scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
417 frm_fa02
= frm_fa02
.build()
418 frm_fa02
= scapy
.all
.Ether(frm_fa02
)
420 frm_shut_fa01
= scapy
.all
.Ether(
421 src
='04:fe:7f:00:00:01',
422 dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
) / \
423 scapy
.contrib
.lldp
.LLDPDUChassisID(
424 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
425 id=b
'\x04\xfe\x7f\x00\x00\x00') / \
426 scapy
.contrib
.lldp
.LLDPDUPortID(
427 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
,
429 scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=0) / \
430 scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
431 frm_shut_fa01
= frm_shut_fa01
.build()
432 frm_shut_fa01
= scapy
.all
.Ether(frm_shut_fa01
)
435 scapy
.all
.sendp(frm_fa01
, iface
='eth1')
436 scapy
.all
.sendp(frm_fa02
, iface
='eth3')
439 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
440 del out
['lldp.eth0.age']
441 del out
['lldp.eth2.age']
443 "lldp.eth0.via": "LLDP",
444 "lldp.eth0.rid": "1",
445 "lldp.eth0.chassis.mac": "04:fe:7f:00:00:00",
446 "lldp.eth0.chassis.name": "this info should not disappear",
447 "lldp.eth0.port.ifname": "Fa0/1",
448 "lldp.eth0.port.ttl": "65535",
449 "lldp.eth2.via": "LLDP",
450 "lldp.eth2.rid": "1",
451 "lldp.eth2.chassis.mac": "04:fe:7f:00:00:00",
452 "lldp.eth2.chassis.name": "this info should not disappear",
453 "lldp.eth2.port.ifname": "Fa0/2",
454 "lldp.eth2.port.ttl": "65535"}
456 scapy
.all
.sendp(frm_shut_fa01
, iface
='eth1')
459 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
460 del out
['lldp.eth2.age']
462 "lldp.eth2.via": "LLDP",
463 "lldp.eth2.rid": "1",
464 "lldp.eth2.chassis.mac": "04:fe:7f:00:00:00",
465 "lldp.eth2.chassis.name": "this info should not disappear",
466 "lldp.eth2.port.ifname": "Fa0/2",
467 "lldp.eth2.port.ttl": "65535"}