"""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"]