"""``lldpctl -f keyvalue``: one row per LLDP neighbour, for every driver that reads lldpd. napalm-linux, napalm-openwrt and napalm-proxmox each parsed this output on their own, and each copy grouped the lines by interface, so two neighbours on one port merged (NAPALM/napalm-openwrt#9, NAPALM/napalm-linux#58), split ``eth0.1`` at its dot (NAPALM/napalm-openwrt#26) and looked for ``chassis.id.value``, a key lldpd never prints. The parse lives here once now. """ from __future__ import annotations import pytest from napalm_device_types import LLDPCTL_KEYVALUE_COMMAND, parse_lldpctl_keyvalue # The recording napalm-openwrt's tests have used since the driver was written # (tests/unit/test_driver.py, ``LLDPCTL_KV``): one neighbour on eth0. Its two # ``chassis.cap.*`` lines are not lldpd's format (lldpd prints one # ``chassis..enabled=on|off`` line per capability), so they yield none. OPENWRT_ONE_NEIGHBOUR = """\ lldp.eth0.via=LLDP lldp.eth0.rid=1 lldp.eth0.age=0 day, 01:23:45 lldp.eth0.chassis.mac=00:aa:bb:cc:dd:ee lldp.eth0.chassis.name=core-router lldp.eth0.chassis.descr=RouterOS 7.x lldp.eth0.chassis.mgmt-ip=10.0.0.1 lldp.eth0.chassis.cap.available=Router, Bridge lldp.eth0.chassis.cap.enabled=Router lldp.eth0.port.ifname=ether1 lldp.eth0.port.descr=uplink """ # Laid out the way lldpd 1.0's keyvalue writer prints ``show neighbors details`` # (src/client/display.c ``display_interface``, ``display_chassis``, # ``display_port``, ``display_vlans``, ``display_med``; src/client/kv_writer.c): # a neighbour opens with the local interface's optional ``alias`` and ``via``, # an id is printed under its type (``chassis.mac``, ``port.local``), a # capability as ``chassis..enabled``, a management address once per # address. Not recorded from a device. # # On ``eth0.1`` (a VLAN interface, its name holds a dot) a phone and a PC behind # an unmanaged switch; on ``eth1`` a switch that names its port by a local id and # itself by an address. TWO_ON_ONE_DOTTED_PORT = """\ lldp.eth0.1.alias=desk lldp.eth0.1.via=LLDP lldp.eth0.1.rid=1 lldp.eth0.1.age=0 day, 00:10:00 lldp.eth0.1.chassis.mac=00:11:22:33:44:01 lldp.eth0.1.chassis.name=desk-phone lldp.eth0.1.chassis.descr=SIP phone, firmware a=1 lldp.eth0.1.chassis.mgmt-ip=10.0.0.21 lldp.eth0.1.chassis.mgmt-iface=2 lldp.eth0.1.chassis.mgmt-ip=fe80::211:22ff:fe33:4401 lldp.eth0.1.chassis.mgmt-iface=2 lldp.eth0.1.chassis.Bridge.enabled=off lldp.eth0.1.chassis.Tel.enabled=on lldp.eth0.1.port.mac=00:11:22:33:44:01 lldp.eth0.1.port.descr=WAN lldp.eth0.1.port.ttl=120 lldp.eth0.1.port.auto-negotiation.supported=yes lldp.eth0.1.port.auto-negotiation.enabled=yes lldp.eth0.1.port.auto-negotiation.100Base-TX.hd=yes lldp.eth0.1.port.auto-negotiation.100Base-TX.fd=yes lldp.eth0.1.port.auto-negotiation.current=100BaseTXFD - 2 pair category 5 UTP, full duplex mode lldp.eth0.1.vlan.vlan-id=10 lldp.eth0.1.vlan.pvid=yes lldp.eth0.1.vlan=voice lldp.eth0.1.lldp-med.device-type=Communication Device Endpoint (Class III) lldp.eth0.1.lldp-med.Capabilities.available=yes lldp.eth0.1.lldp-med.inventory.software=2.1.4 lldp.eth0.1.alias=desk lldp.eth0.1.via=LLDP lldp.eth0.1.rid=2 lldp.eth0.1.age=0 day, 00:09:00 lldp.eth0.1.chassis.mac=00:11:22:33:44:02 lldp.eth0.1.chassis.name=desk-pc lldp.eth0.1.chassis.descr=Debian GNU/Linux 12 (bookworm) Linux 6.1.0-25-amd64 lldp.eth0.1.chassis.Bridge.enabled=off lldp.eth0.1.chassis.Router.enabled=off lldp.eth0.1.chassis.Wlan.enabled=off lldp.eth0.1.chassis.Station.enabled=on lldp.eth0.1.port.ifname=eno1 lldp.eth0.1.port.ttl=120 lldp.eth0.1.unknown-tlvs.unknown-tlv.oui=00,80,C2 lldp.eth0.1.unknown-tlvs.unknown-tlv.subtype=9 lldp.eth0.1.unknown-tlvs.unknown-tlv.len=1 lldp.eth0.1.unknown-tlvs.unknown-tlv=01 lldp.eth1.via=LLDP lldp.eth1.rid=3 lldp.eth1.age=1 day, 02:00:00 lldp.eth1.chassis.ip=192.0.2.7 lldp.eth1.chassis.name=access-sw lldp.eth1.chassis.Bridge.enabled=on lldp.eth1.chassis.Router.enabled=off lldp.eth1.chassis.CVLAN.enabled=on lldp.eth1.port.local=gi1/0/7 lldp.eth1.port.descr=GigabitEthernet1/0/7 lldp.eth1.ppvid.supported=no lldp.eth1.ppvid.enabled=no lldp.eth1.pi=88cc """ def _rows(output: str = TWO_ON_ONE_DOTTED_PORT) -> list[dict[str, object]]: return [dict(row) for row in parse_lldpctl_keyvalue(output)] class TestOneRowPerNeighbour: def test_the_openwrt_recording_is_one_neighbour_with_its_fields(self): assert _rows(OPENWRT_ONE_NEIGHBOUR) == [ { "local_port": "eth0", "chassis_id": "00:aa:bb:cc:dd:ee", "chassis_id_type": "mac", "port_id": "ether1", "port_id_type": "ifname", "port_description": "uplink", "system_name": "core-router", "system_description": "RouterOS 7.x", "management_addresses": ["10.0.0.1"], "capabilities": [], "enabled_capabilities": [], } ] def test_two_neighbours_on_one_port_stay_apart(self): rows = _rows() assert [(r["local_port"], r["system_name"]) for r in rows] == [ ("eth0.1", "desk-phone"), ("eth0.1", "desk-pc"), ("eth1", "access-sw"), ] def test_a_neighbour_keeps_only_its_own_fields(self): pc = _rows()[1] assert pc["chassis_id"] == "00:11:22:33:44:02" assert pc["port_description"] == "" assert pc["management_addresses"] == [] def test_neighbours_without_alias_are_split_at_via(self): output = OPENWRT_ONE_NEIGHBOUR + OPENWRT_ONE_NEIGHBOUR.replace("core-router", "edge") assert [r["system_name"] for r in _rows(output)] == ["core-router", "edge"] class TestInterfaceName: def test_a_dotted_interface_name_survives(self): assert {r["local_port"] for r in _rows()} == {"eth0.1", "eth1"} def test_an_interface_named_like_a_field_survives(self): output = ( "lldp.vlan.100.via=LLDP\n" "lldp.vlan.100.chassis.mac=00:aa:bb:cc:dd:01\n" "lldp.vlan.100.port.ifname=port1\n" ) (row,) = _rows(output) assert (row["local_port"], row["chassis_id"], row["port_id"]) == ( "vlan.100", "00:aa:bb:cc:dd:01", "port1", ) def test_without_via_the_name_ends_at_the_first_field(self): output = "lldp.br-lan.10.chassis.name=sw\nlldp.br-lan.10.port.ifname=1\n" (row,) = _rows(output) assert (row["local_port"], row["system_name"], row["port_id"]) == ("br-lan.10", "sw", "1") class TestIds: def test_a_port_given_by_its_mac_is_read(self): phone = _rows()[0] assert (phone["port_id"], phone["port_id_type"]) == ("00:11:22:33:44:01", "mac") def test_a_port_given_by_a_local_id_is_read(self): switch = _rows()[2] assert (switch["port_id"], switch["port_id_type"]) == ("gi1/0/7", "local") def test_a_chassis_given_by_its_address_is_read(self): switch = _rows()[2] assert (switch["chassis_id"], switch["chassis_id_type"]) == ("192.0.2.7", "ip") @pytest.mark.parametrize("kind", ["local", "ifname", "ifalias", "unhandled"]) def test_every_id_type_lldpd_names_is_read(self, kind): output = f"lldp.eth0.via=LLDP\nlldp.eth0.chassis.{kind}=c1\nlldp.eth0.port.{kind}=p1\n" (row,) = _rows(output) assert (row["chassis_id"], row["chassis_id_type"]) == ("c1", kind) assert (row["port_id"], row["port_id_type"]) == ("p1", kind) def test_a_neighbour_without_ids_has_empty_ones(self): (row,) = _rows("lldp.eth0.via=CDPv2\nlldp.eth0.chassis.name=sw\n") assert (row["chassis_id"], row["chassis_id_type"]) == ("", "") assert (row["port_id"], row["port_id_type"]) == ("", "") class TestOtherFields: def test_every_management_address_is_kept(self): phone = _rows()[0] assert phone["management_addresses"] == ["10.0.0.21", "fe80::211:22ff:fe33:4401"] def test_a_value_may_hold_an_equals_sign(self): assert _rows()[0]["system_description"] == "SIP phone, firmware a=1" def test_capabilities_in_napalm_names_and_which_are_enabled(self): phone, pc, switch = _rows() assert phone["capabilities"] == ["bridge", "telephone"] assert phone["enabled_capabilities"] == ["telephone"] assert pc["capabilities"] == ["bridge", "router", "wlan-access-point", "station"] assert pc["enabled_capabilities"] == ["station"] assert switch["capabilities"] == ["bridge", "router", "cvlan"] assert switch["enabled_capabilities"] == ["bridge", "cvlan"] def test_every_napalm_capability_name(self): symbols = ["Other", "Repeater", "Bridge", "Router", "Wlan", "Tel", "Docsis", "Station"] output = "lldp.eth0.via=LLDP\n" + "".join( f"lldp.eth0.chassis.{s}.enabled=on\n" for s in symbols ) (row,) = _rows(output) assert row["capabilities"] == [ "other", "repeater", "bridge", "router", "wlan-access-point", "telephone", "docsis-cable-device", "station", ] assert row["enabled_capabilities"] == row["capabilities"] def test_a_capability_line_needs_on_or_off(self): (row,) = _rows(OPENWRT_ONE_NEIGHBOUR) assert row["capabilities"] == [] class TestReadOrFailure: """A failed read raises; ``[]`` only when lldpctl ran and listed nobody.""" def test_no_output_is_no_neighbour(self): assert parse_lldpctl_keyvalue("") == [] def test_a_zero_exit_status_without_neighbours_is_no_neighbour(self): assert parse_lldpctl_keyvalue("__LLDPCTL_RC=0\n") == [] def test_a_missing_lldpctl_raises(self): with pytest.raises(ValueError, match="lldpctl: not found"): parse_lldpctl_keyvalue("sh: lldpctl: not found\n") def test_a_non_zero_exit_status_raises_with_what_lldpctl_said(self): output = ( "[WARN/control] unable to connect to socket /var/run/lldpd.socket: " "No such file or directory\n" "__LLDPCTL_RC=1\n" ) with pytest.raises(ValueError, match="unable to connect to socket"): parse_lldpctl_keyvalue(output) def test_a_non_zero_exit_status_without_a_message_names_the_status(self): with pytest.raises(ValueError, match="exit status 127"): parse_lldpctl_keyvalue("__LLDPCTL_RC=127\n") def test_with_status_zero_a_warning_beside_the_neighbours_is_ignored(self): output = ( "lldpctl -f keyvalue 2>&1; echo __LLDPCTL_RC=$?\n" "[WARN/lldpctl] something lldpd wants to say\n" + OPENWRT_ONE_NEIGHBOUR + "__LLDPCTL_RC=0\n" ) assert [r["system_name"] for r in _rows(output)] == ["core-router"] def test_a_line_outside_lldp_is_no_neighbour(self): output = "lldpd.config.tx-delay=30\n" + OPENWRT_ONE_NEIGHBOUR assert len(_rows(output)) == 1 def test_a_key_without_a_known_field_is_skipped(self): output = "lldp.summary=1\nlldp.eth9.future-field=x\n" + OPENWRT_ONE_NEIGHBOUR assert [r["local_port"] for r in _rows(output)] == ["eth0"] class TestCommand: def test_the_command_reads_the_neighbours_in_keyvalue_form(self): assert LLDPCTL_KEYVALUE_COMMAND.startswith("lldpctl -f keyvalue ") def test_the_command_brings_back_the_error_text_and_the_exit_status(self): assert "2>&1" in LLDPCTL_KEYVALUE_COMMAND assert LLDPCTL_KEYVALUE_COMMAND.endswith("echo __LLDPCTL_RC=$?")