Files
napalm-openwrt/tests/unit/test_lldp_agent.py
T
Christian Manivong 5cce38b2bb
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Failing after 1m1s
driver / test (3.12) (pull_request) Failing after 59s
driver / test (3.13) (pull_request) Failing after 49s
driver / lowest (pull_request) Failing after 25s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 1m1s
CI / driver (pull_request) Failing after 2m28s
driver / publish (pull_request) Skipped
feat(lldp): configure_lldp_agent sets up lldpd on the given interfaces
The LLDP getters started lldpd and rewrote its interface list on every
poll; with read-only getters that setup needs an action. The driver
mixes in napalm-device-types' LldpAgentMixin and writes the step the
template leaves to it: lldpd.config.interface in UCI. A VLAN device is
written as the device under it, as the old helper did, since lldpd
sends on the L2 device. Which interfaces is the caller's choice.

Refs #8
2026-11-01 22:13:56 +01:00

116 lines
3.5 KiB
Python

"""configure_lldp_agent: lldpd on the interfaces the caller names (#8).
The LLDP getters used to start lldpd and rewrite its interface list on every
poll. That is an action now, napalm-device-types' ``LldpAgentMixin``: the
template installs and starts lldpd, the driver writes the list into UCI.
"""
from __future__ import annotations
import pytest
from napalm_openwrt import OpenWrtDriver
class _Shell:
"""Collects issued commands; *fail* makes a command containing it exit 1."""
def __init__(self, fail: str = ""):
self.fail = fail
self.issued: list[str] = []
def __call__(self, cmd, **kw):
self.issued.append(cmd)
if "__OWRT_RC" in cmd:
if self.fail and self.fail in cmd:
return "uci: Invalid argument\n__OWRT_RC=1"
return "__OWRT_RC=0"
return ""
@pytest.fixture
def shell():
return _Shell()
@pytest.fixture
def owrt(shell):
d = OpenWrtDriver("ap", "root", "pw")
d._send_command = shell
d.get_packages = lambda: [{"name": "lldpd", "version": "1.0.17-1", "installed": True}]
return d
def _uci(shell) -> str:
return next(c for c in shell.issued if "uci commit lldpd" in c)
def test_install_configure_enable_and_restart(owrt, shell):
result = owrt.configure_lldp_agent(["br-lan"])
assert result["success"] is True, result["output"]
assert [s["step"] for s in result["steps"]] == ["install", "configure", "start"]
uci = _uci(shell)
assert "uci -q delete lldpd.config.interface" in uci
assert "uci add_list lldpd.config.interface='br-lan'" in uci
lldpd = [c.split(" 2>")[0] for c in shell.issued if c.startswith("/etc/init.d/lldpd")]
assert lldpd == ["/etc/init.d/lldpd enable", "/etc/init.d/lldpd restart"]
def test_the_list_is_replaced_and_a_missing_section_is_made(owrt, shell):
owrt.configure_lldp_agent(["br-lan", "eth1"])
uci = _uci(shell)
assert "uci set lldpd.config=lldpd" in uci
assert uci.index("delete lldpd.config.interface") < uci.index("add_list")
assert "interface='br-lan'" in uci
assert "interface='eth1'" in uci
def test_a_vlan_device_is_written_as_the_device_under_it(owrt, shell):
# lldpd sends on the L2 device: on br-ap, not on its VLAN br-ap.10.
owrt.configure_lldp_agent(["br-ap.10", "br-ap"])
uci = _uci(shell)
assert "interface='br-ap'" in uci
assert "br-ap.10" not in uci
assert uci.count("add_list") == 1
def test_a_name_that_is_not_an_interface_name_writes_nothing(owrt, shell):
result = owrt.configure_lldp_agent(["br-lan'; reboot; '"])
assert result["success"] is False
assert result["steps"][-1]["step"] == "configure"
assert not any("uci" in c or "lldpd restart" in c for c in shell.issued)
def test_a_refused_uci_write_leaves_lldpd_alone():
shell = _Shell(fail="uci commit lldpd")
d = OpenWrtDriver("ap", "root", "pw")
d._send_command = shell
d.get_packages = lambda: [{"name": "lldpd", "installed": True}]
result = d.configure_lldp_agent(["br-lan"])
assert result["success"] is False
assert "Invalid argument" in result["output"]
assert not any("/etc/init.d/lldpd" in c for c in shell.issued)
def test_lldpd_is_installed_where_it_is_missing(owrt, shell):
owrt.get_packages = lambda: []
asked: list[str] = []
owrt.install_package = lambda name, version="": (
asked.append(name)
or {
"success": True,
"output": f"installed {name}",
}
)
result = owrt.configure_lldp_agent(["br-lan"])
assert result["success"] is True
assert asked == ["lldpd"]