]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/integration/test_basic.py
9dd34803be1b992f4cff41ae51ab87e76ac96977
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"
17 assert "lldp.eth0.chassis.Router.enabled" in out
18 assert "lldp.eth0.chassis.Station.enabled" in out
19 del out
["lldp.eth0.age"]
20 del out
["lldp.eth0.chassis.descr"]
21 del out
["lldp.eth0.chassis.Router.enabled"]
22 del out
["lldp.eth0.chassis.Station.enabled"]
24 "lldp.eth0.via": "LLDP",
26 "lldp.eth0.chassis.mac": "00:00:00:00:00:02",
27 "lldp.eth0.chassis.name": "ns-2.example.com",
28 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:2",
29 "lldp.eth0.chassis.mgmt-iface": "2",
30 "lldp.eth0.chassis.Bridge.enabled": "off",
31 "lldp.eth0.chassis.Wlan.enabled": "off",
32 "lldp.eth0.port.mac": "00:00:00:00:00:02",
33 "lldp.eth0.port.descr": "eth1",
34 "lldp.eth0.port.ttl": "120",
38 @pytest.mark
.parametrize("neighbors", (5, 10, 20))
39 def test_several_neighbors(lldpd
, lldpcli
, links
, namespaces
, neighbors
):
40 for i
in range(2, neighbors
+ 1):
41 links(namespaces(1), namespaces(i
))
42 for i
in range(1, neighbors
+ 1):
44 lldpd(sleep
=(i
== 1 and 2 or 0), silent
=True)
47 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
48 for i
in range(2, neighbors
+ 1):
50 "lldp.eth{}.chassis.name".format((i
- 2) * 2)
51 ] == "ns-{}.example.com".format(i
)
54 def test_one_interface(lldpd1
, lldpd
, lldpcli
, namespaces
):
58 out
= lldpcli("-f", "keyvalue", "show", "interfaces")
59 assert out
["lldp.eth0.chassis.descr"].startswith(
60 "Spectacular GNU/Linux 2016 Linux"
62 assert "lldp.eth0.chassis.Router.enabled" in out
63 assert "lldp.eth0.chassis.Station.enabled" in out
64 del out
["lldp.eth0.chassis.descr"]
65 del out
["lldp.eth0.chassis.Router.enabled"]
66 del out
["lldp.eth0.chassis.Station.enabled"]
68 "lldp.eth0.status": "RX and TX",
69 "lldp.eth0.chassis.mac": "00:00:00:00:00:01",
70 "lldp.eth0.chassis.name": "ns-1.example.com",
71 "lldp.eth0.chassis.mgmt-ip": "fe80::200:ff:fe00:1",
72 "lldp.eth0.chassis.mgmt-iface": "3",
73 "lldp.eth0.chassis.Bridge.enabled": "off",
74 "lldp.eth0.chassis.Wlan.enabled": "off",
75 "lldp.eth0.port.mac": "00:00:00:00:00:01",
76 "lldp.eth0.port.descr": "eth0",
77 "lldp.eth0.ttl.ttl": "120",
81 @pytest.mark
.parametrize("interfaces", (5, 10, 20))
82 def test_several_interfaces(lldpd
, lldpcli
, links
, namespaces
, interfaces
):
83 for i
in range(2, interfaces
+ 1):
84 links(namespaces(1), namespaces(i
))
85 for i
in range(1, interfaces
+ 1):
89 out
= lldpcli("-f", "keyvalue", "show", "interfaces")
90 for i
in range(2, interfaces
+ 1):
92 out
["lldp.eth{}.chassis.mac".format((i
- 2) * 2)] == "00:00:00:00:00:01"
95 "lldp.eth{}.port.mac".format((i
- 2) * 2)
96 ] == "00:00:00:00:00:{num:02x}".format(num
=(i
- 2) * 2 + 1)
99 def test_different_mtu(lldpd
, lldpcli
, links
, namespaces
):
100 links(namespaces(1), namespaces(2), mtu
=1500)
101 links(namespaces(1), namespaces(2), mtu
=9000)
107 out
= lldpcli("-f", "keyvalue", "show", "interfaces")
108 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:01"
109 assert out
["lldp.eth2.chassis.mac"] == "00:00:00:00:00:01"
112 def test_overrided_description(lldpd1
, lldpd
, lldpcli
, namespaces
):
114 lldpd("-S", "Modified description")
116 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
117 assert out
["lldp.eth0.chassis.descr"] == "Modified description"
120 def test_overrided_description2(lldpd1
, lldpd
, lldpcli
, namespaces
):
123 lldpcli("configure", "system", "description", "Modified description")
127 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
128 assert out
["lldp.eth0.chassis.descr"] == "Modified description"
131 def test_overrided_chassisid(lldpd1
, lldpd
, lldpcli
, namespaces
):
134 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
138 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
139 assert out
["lldp.eth0.chassis.local"] == "Modified chassis ID"
142 def test_overrided_chassisid_kept(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
145 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
153 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
154 assert out
["lldp.eth0.chassis.local"] == "Modified chassis ID"
157 def test_overrided_chassisid_reverse(lldpd1
, lldpd
, lldpcli
, namespaces
):
160 lldpcli("configure", "system", "chassisid", "Modified chassis ID")
161 lldpcli("unconfigure", "system", "chassisid")
165 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
166 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
169 def test_hide_kernel(lldpd1
, lldpd
, lldpcli
, namespaces
):
173 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
174 assert out
["lldp.eth0.chassis.descr"] == "Spectacular GNU/Linux 2016"
177 def test_listen_only(lldpd1
, lldpd
, lldpcli
, namespaces
):
181 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
185 def test_forced_unknown_management_address(lldpd1
, lldpd
, lldpcli
, namespaces
):
187 lldpd("-m", "2001:db8::47")
189 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
190 assert out
["lldp.eth0.chassis.mgmt-ip"] == "2001:db8::47"
191 assert "lldp.eth0.chassis.mgmt-iface" not in out
194 def test_forced_known_management_address(lldpd1
, lldpd
, lldpcli
, namespaces
):
196 with pyroute2
.IPRoute() as ipr
:
197 idx
= ipr
.link_lookup(ifname
="eth1")[0]
198 ipr
.addr("add", index
=idx
, address
="192.168.14.2", prefixlen
=24)
199 lldpd("-m", "192.168.14.2")
201 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
202 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.2"
203 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
206 def test_management_address(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
208 with pyroute2
.IPRoute() as ipr
:
209 idx
= ipr
.link_lookup(ifname
="eth1")[0]
210 ipr
.addr("add", index
=idx
, address
="192.168.14.2", prefixlen
=24)
211 ipr
.addr("add", index
=idx
, address
="172.25.21.47", prefixlen
=24)
212 lldpd("-m", "172.25.*")
214 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
215 assert out
["lldp.eth0.chassis.mgmt-ip"] == "172.25.21.47"
216 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
219 def test_management_interface(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
220 links(namespaces(1), namespaces(2), 4)
222 with pyroute2
.IPRoute() as ipr
:
223 idx
= ipr
.link_lookup(ifname
="eth1")[0]
224 ipr
.addr("add", index
=idx
, address
="192.168.14.2", prefixlen
=24)
225 idx
= ipr
.link_lookup(ifname
="eth3")[0]
226 ipr
.addr("add", index
=idx
, address
="172.25.21.47", prefixlen
=24)
229 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
230 assert out
["lldp.eth0.chassis.mgmt-ip"] == [
232 "fe80::200:ff:fe00:4",
234 assert out
["lldp.eth0.chassis.mgmt-iface"] == ["4", "4"]
237 def test_change_management_address(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
239 with pyroute2
.IPRoute() as ipr
:
240 idx
= ipr
.link_lookup(ifname
="eth1")[0]
241 ipr
.addr("add", index
=idx
, address
="192.168.14.2", prefixlen
=24)
242 lldpd("-m", "192.168.*")
243 # We need a short TX interval as updating the IP address
244 # doesn't trigger a resend.
245 lldpcli("configure", "lldp", "tx-interval", "2")
247 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
248 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.2"
249 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
251 with pyroute2
.IPRoute() as ipr
:
252 ipr
.addr("del", index
=idx
, address
="192.168.14.2", prefixlen
=24)
253 ipr
.addr("add", index
=idx
, address
="192.168.14.5", prefixlen
=24)
256 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
257 assert out
["lldp.eth0.chassis.mgmt-ip"] == "192.168.14.5"
258 assert out
["lldp.eth0.chassis.mgmt-iface"] == "2"
261 def test_portid_subtype_ifname(lldpd1
, lldpd
, lldpcli
, namespaces
):
264 lldpcli("configure", "lldp", "portidsubtype", "ifname")
267 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
268 assert out
["lldp.eth0.port.ifname"] == "eth1"
269 assert out
["lldp.eth0.port.descr"] == "eth1"
272 def test_portid_subtype_with_alias(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
274 with pyroute2
.IPRoute() as ipr
:
275 idx
= ipr
.link_lookup(ifname
="eth1")[0]
276 ipr
.link("set", index
=idx
, ifalias
="alias of eth1")
279 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
280 assert out
["lldp.eth0.port.ifname"] == "eth1"
281 assert out
["lldp.eth0.port.descr"] == "alias of eth1"
284 def test_portid_subtype_macaddress(lldpd1
, lldpd
, lldpcli
, links
, namespaces
):
286 with pyroute2
.IPRoute() as ipr
:
287 idx
= ipr
.link_lookup(ifname
="eth1")[0]
288 ipr
.link("set", index
=idx
, ifalias
="alias of eth1")
290 lldpcli("configure", "lldp", "portidsubtype", "macaddress")
293 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
294 assert out
["lldp.eth0.port.mac"] == "00:00:00:00:00:02"
295 assert out
["lldp.eth0.port.descr"] == "eth1"
298 def test_portid_subtype_local(lldpd1
, lldpd
, lldpcli
, namespaces
):
301 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
304 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
305 assert out
["lldp.eth0.port.local"] == "localname"
306 assert out
["lldp.eth0.port.descr"] == "eth1"
309 def test_portid_subtype_local_with_description(lldpd1
, lldpd
, lldpcli
, namespaces
):
323 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
324 assert out
["lldp.eth0.port.local"] == "localname"
325 assert out
["lldp.eth0.port.descr"] == "localdescription"
328 def test_portdescription(lldpd1
, lldpd
, lldpcli
, namespaces
):
331 lldpcli("configure", "lldp", "portdescription", "localdescription")
334 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
335 assert out
["lldp.eth0.port.descr"] == "localdescription"
338 def test_portid_subtype_local_with_alias(lldpd1
, lldpd
, lldpcli
, namespaces
):
340 with pyroute2
.IPRoute() as ipr
:
341 idx
= ipr
.link_lookup(ifname
="eth1")[0]
342 ipr
.link("set", index
=idx
, ifalias
="alias of eth1")
344 lldpcli("configure", "lldp", "portidsubtype", "local", "localname")
347 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
348 assert out
["lldp.eth0.port.local"] == "localname"
349 assert out
["lldp.eth0.port.descr"] == "alias of eth1"
352 def test_port_status_txonly(lldpd
, lldpcli
, namespaces
, links
):
353 links(namespaces(1), namespaces(2))
356 lldpcli("configure", "lldp", "status", "tx-only")
360 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
364 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
365 assert out
["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
368 def test_port_status_rxonly(lldpd
, lldpcli
, namespaces
, links
):
369 links(namespaces(1), namespaces(2))
372 lldpcli("configure", "lldp", "status", "rx-only")
376 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
377 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
380 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
384 def test_port_status_rxandtx(lldpd
, lldpcli
, namespaces
, links
):
385 links(namespaces(1), namespaces(2))
388 lldpcli("configure", "lldp", "status", "rx-and-tx") # noop
392 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
393 assert out
["lldp.eth0.chassis.mac"] == "00:00:00:00:00:02"
396 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
397 assert out
["lldp.eth1.chassis.mac"] == "00:00:00:00:00:01"
400 def test_port_status_disabled(lldpd
, lldpcli
, namespaces
, links
):
401 links(namespaces(1), namespaces(2))
404 lldpcli("configure", "lldp", "status", "disabled")
408 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
412 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
416 def test_port_vlan_tx(lldpd1
, lldpd
, lldpcli
, namespaces
):
431 out
= lldpcli("-f", "keyvalue", "show", "interfaces", "ports", "eth0")
432 assert out
["lldp.eth0.port.vlanTX.id"] == "100"
433 assert out
["lldp.eth0.port.vlanTX.prio"] == "5"
434 assert out
["lldp.eth0.port.vlanTX.dei"] == "1"
435 # unconfigure VLAN TX
436 lldpcli("unconfigure", "ports", "eth0", "lldp", "vlan-tx")
437 out
= lldpcli("-f", "keyvalue", "show", "interfaces", "ports", "eth0")
438 assert "lldp.eth0.port.vlanTX.id" not in out
439 assert "lldp.eth0.port.vlanTX.prio" not in out
440 assert "lldp.eth0.port.vlanTX.dei" not in out
443 def test_set_interface_alias(lldpd1
, lldpd
, lldpcli
, namespaces
):
445 lldpcli("configure", "system", "interface", "description")
449 with pyroute2
.IPRoute() as ipr
:
450 link
= ipr
.link("get", ifname
="eth0")[0]
452 link
.get_attr("IFLA_IFALIAS") == "lldpd: connected to ns-2.example.com"
456 def test_lldpdu_shutdown(lldpd
, lldpcli
, namespaces
, links
):
457 links(namespaces(1), namespaces(2))
458 links(namespaces(1), namespaces(2))
461 # From https://github.com/lldpd/lldpd/issues/348
464 src
="04:fe:7f:00:00:01", dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
466 / scapy
.contrib
.lldp
.LLDPDUChassisID(
467 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
468 id=b
"\x04\xfe\x7f\x00\x00\x00",
470 / scapy
.contrib
.lldp
.LLDPDUPortID(
471 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
, id="Fa0/1"
473 / scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=65535)
474 / scapy
.contrib
.lldp
.LLDPDUSystemName(
475 system_name
="this info should not disappear"
477 / scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
479 frm_fa01
= frm_fa01
.build()
480 frm_fa01
= scapy
.all
.Ether(frm_fa01
)
484 src
="04:fe:7f:00:00:02", dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
486 / scapy
.contrib
.lldp
.LLDPDUChassisID(
487 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
488 id=b
"\x04\xfe\x7f\x00\x00\x00",
490 / scapy
.contrib
.lldp
.LLDPDUPortID(
491 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
, id="Fa0/2"
493 / scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=65535)
494 / scapy
.contrib
.lldp
.LLDPDUSystemName(
495 system_name
="this info should not disappear"
497 / scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
499 frm_fa02
= frm_fa02
.build()
500 frm_fa02
= scapy
.all
.Ether(frm_fa02
)
504 src
="04:fe:7f:00:00:01", dst
=scapy
.contrib
.lldp
.LLDP_NEAREST_BRIDGE_MAC
506 / scapy
.contrib
.lldp
.LLDPDUChassisID(
507 subtype
=scapy
.contrib
.lldp
.LLDPDUChassisID
.SUBTYPE_MAC_ADDRESS
,
508 id=b
"\x04\xfe\x7f\x00\x00\x00",
510 / scapy
.contrib
.lldp
.LLDPDUPortID(
511 subtype
=scapy
.contrib
.lldp
.LLDPDUPortID
.SUBTYPE_INTERFACE_NAME
, id="Fa0/1"
513 / scapy
.contrib
.lldp
.LLDPDUTimeToLive(ttl
=0)
514 / scapy
.contrib
.lldp
.LLDPDUEndOfLLDPDU()
516 frm_shut_fa01
= frm_shut_fa01
.build()
517 frm_shut_fa01
= scapy
.all
.Ether(frm_shut_fa01
)
520 scapy
.all
.sendp(frm_fa01
, iface
="eth1")
521 scapy
.all
.sendp(frm_fa02
, iface
="eth3")
524 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
525 del out
["lldp.eth0.age"]
526 del out
["lldp.eth2.age"]
528 "lldp.eth0.via": "LLDP",
529 "lldp.eth0.rid": "1",
530 "lldp.eth0.chassis.mac": "04:fe:7f:00:00:00",
531 "lldp.eth0.chassis.name": "this info should not disappear",
532 "lldp.eth0.port.ifname": "Fa0/1",
533 "lldp.eth0.port.ttl": "65535",
534 "lldp.eth2.via": "LLDP",
535 "lldp.eth2.rid": "1",
536 "lldp.eth2.chassis.mac": "04:fe:7f:00:00:00",
537 "lldp.eth2.chassis.name": "this info should not disappear",
538 "lldp.eth2.port.ifname": "Fa0/2",
539 "lldp.eth2.port.ttl": "65535",
542 scapy
.all
.sendp(frm_shut_fa01
, iface
="eth1")
545 out
= lldpcli("-f", "keyvalue", "show", "neighbors")
546 del out
["lldp.eth2.age"]
548 "lldp.eth2.via": "LLDP",
549 "lldp.eth2.rid": "1",
550 "lldp.eth2.chassis.mac": "04:fe:7f:00:00:00",
551 "lldp.eth2.chassis.name": "this info should not disappear",
552 "lldp.eth2.port.ifname": "Fa0/2",
553 "lldp.eth2.port.ttl": "65535",