Merge remote-tracking branch 'origin/main' into feat/lldp-agent-setup
This commit is contained in:
commit
1e988c87f4
7 files changed
+383
-78
No files matched your search
@@ -336,6 +336,91 @@ class TestGetLldpNeighbors:
|
||||
assert result["eth0"][0]["port"] == "ether1"
|
||||
|
||||
|
||||
# Two neighbours on one port, laid out the way lldpd's keyvalue writer prints them
|
||||
# (src/client/display.c, kv_writer.c): every neighbour opens with an optional
|
||||
# ``alias`` and then ``via``, and the same key may repeat inside one neighbour
|
||||
# (one ``chassis.mgmt-ip`` per address). Not recorded from a device.
|
||||
LLDPCTL_KV_TWO_ON_ONE_PORT = """\
|
||||
lldp.lan1.via=LLDP
|
||||
lldp.lan1.rid=1
|
||||
lldp.lan1.age=0 day, 00:10:00
|
||||
lldp.lan1.chassis.mac=00:11:22:33:44:01
|
||||
lldp.lan1.chassis.name=desk-phone
|
||||
lldp.lan1.chassis.mgmt-ip=10.0.0.21
|
||||
lldp.lan1.chassis.mgmt-ip=fe80::211:22ff:fe33:4401
|
||||
lldp.lan1.port.ifname=lan
|
||||
lldp.lan1.port.descr=phone uplink
|
||||
lldp.lan1.alias=desk
|
||||
lldp.lan1.via=LLDP
|
||||
lldp.lan1.rid=2
|
||||
lldp.lan1.age=0 day, 00:09:00
|
||||
lldp.lan1.chassis.mac=00:11:22:33:44:02
|
||||
lldp.lan1.chassis.name=desk-pc
|
||||
lldp.lan1.port.ifname=eno1
|
||||
lldp.eth0.via=LLDP
|
||||
lldp.eth0.rid=3
|
||||
lldp.eth0.chassis.mac=00:aa:bb:cc:dd:ee
|
||||
lldp.eth0.chassis.name=core-router
|
||||
lldp.eth0.port.ifname=ether1
|
||||
"""
|
||||
|
||||
|
||||
class TestLldpGettersOnlyRead:
|
||||
"""Reading the neighbours neither starts lldpd nor rewrites its configuration (#8)."""
|
||||
|
||||
def _answer(self, driver) -> list:
|
||||
sent: list = []
|
||||
|
||||
def send(cmd, **kw):
|
||||
sent.append(cmd)
|
||||
# A default route on a VLAN of another bridge: the old code rewrote
|
||||
# lldpd's interface list for it and restarted the daemon.
|
||||
if "ip route" in cmd:
|
||||
return "br-ap"
|
||||
if "uci get lldpd" in cmd:
|
||||
return "br-lan"
|
||||
return LLDPCTL_KV
|
||||
|
||||
driver._send_command = send
|
||||
return sent
|
||||
|
||||
@pytest.mark.parametrize("getter", ["get_lldp_neighbors", "get_lldp_neighbors_detail"])
|
||||
def test_only_lldpctl_is_asked(self, driver, getter):
|
||||
sent = self._answer(driver)
|
||||
|
||||
getattr(driver, getter)()
|
||||
|
||||
assert sent == ["lldpctl -f keyvalue"]
|
||||
|
||||
|
||||
class TestLldpSeveralNeighboursOnOnePort:
|
||||
"""Each neighbour on a port is its own entry; none is lost (#9)."""
|
||||
|
||||
def test_both_neighbours_on_the_port_are_reported(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT
|
||||
|
||||
result = driver.get_lldp_neighbors()
|
||||
|
||||
assert result["lan1"] == [
|
||||
{"hostname": "desk-phone", "port": "lan"},
|
||||
{"hostname": "desk-pc", "port": "eno1"},
|
||||
]
|
||||
assert result["eth0"] == [{"hostname": "core-router", "port": "ether1"}]
|
||||
|
||||
def test_a_neighbour_keeps_only_its_own_fields(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT
|
||||
|
||||
pc = driver.get_lldp_neighbors_detail("lan1")["lan1"][1]
|
||||
|
||||
assert pc["remote_chassis_id"] == "00:11:22:33:44:02"
|
||||
assert pc["remote_port_description"] == ""
|
||||
|
||||
def test_a_repeated_key_inside_one_neighbour_is_no_new_neighbour(self, driver):
|
||||
driver._send_command = lambda cmd, **kw: LLDPCTL_KV_TWO_ON_ONE_PORT
|
||||
|
||||
assert len(driver.get_lldp_neighbors_detail("lan1")["lan1"]) == 2
|
||||
|
||||
|
||||
BRIDGE_VLAN_SHOW = """\
|
||||
port vlan-id
|
||||
eth0 1 PVID Egress Untagged
|
||||
@@ -1940,3 +2025,126 @@ class TestGetSystemConfigBridgeStp:
|
||||
# Nothing to have an opinion about — distinct from "STP is off".
|
||||
driver._send_command = _system_send(network_out="network.lan=interface\n")
|
||||
assert driver.get_system_config()["bridge_stp"] is None
|
||||
|
||||
|
||||
class TestOptionalArgs:
|
||||
def test_the_callers_optional_args_are_left_as_they_were(self):
|
||||
optional_args = {"port": 2222, "conn_timeout": 15}
|
||||
|
||||
drv = OpenWrtDriver("192.168.1.1", "root", "", optional_args=optional_args)
|
||||
|
||||
assert drv.port == 2222
|
||||
assert optional_args == {"port": 2222, "conn_timeout": 15}
|
||||
|
||||
|
||||
class TestManageService:
|
||||
"""Only the init script's exit status decides whether an action succeeded (#3)."""
|
||||
|
||||
def _answer(self, driver, reply: str) -> list:
|
||||
sent: list = []
|
||||
|
||||
def send(cmd, **kw):
|
||||
sent.append(cmd)
|
||||
return reply
|
||||
|
||||
driver._send_command = send
|
||||
return sent
|
||||
|
||||
def test_a_clean_exit_is_a_success(self, driver):
|
||||
sent = self._answer(driver, "__OWRT_RC=0\n")
|
||||
|
||||
assert driver.manage_service("dnsmasq", "restart") == {"success": True, "output": ""}
|
||||
assert sent[0].startswith("/etc/init.d/dnsmasq restart 2>&1")
|
||||
|
||||
def test_a_failed_action_is_no_success(self, driver):
|
||||
self._answer(driver, "Command failed: Not found\n__OWRT_RC=1\n")
|
||||
|
||||
result = driver.manage_service("dnsmasq", "start")
|
||||
|
||||
assert result == {"success": False, "output": "Command failed: Not found"}
|
||||
|
||||
def test_a_missing_init_script_is_no_success(self, driver):
|
||||
self._answer(driver, "ash: /etc/init.d/nope: not found\n__OWRT_RC=127\n")
|
||||
|
||||
result = driver.manage_service("nope", "start")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "not found" in result["output"]
|
||||
|
||||
def test_a_silent_failure_names_the_status(self, driver):
|
||||
self._answer(driver, "__OWRT_RC=1\n")
|
||||
|
||||
result = driver.manage_service("lldpd", "enable")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "status 1" in result["output"]
|
||||
|
||||
def test_output_without_a_status_is_no_success(self, driver):
|
||||
self._answer(driver, "")
|
||||
|
||||
assert driver.manage_service("dnsmasq", "stop")["success"] is False
|
||||
|
||||
def test_an_invalid_name_is_refused_before_anything_is_sent(self, driver):
|
||||
sent = self._answer(driver, "__OWRT_RC=0\n")
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
driver.manage_service("x; reboot", "start")
|
||||
assert sent == []
|
||||
|
||||
|
||||
# Busy 15 % on average since boot, 90 % in the second between the two reads.
|
||||
PROC_STAT_CPU_TWICE = """\
|
||||
cpu 1000 0 500 8500 0 0 0 0 0 0
|
||||
cpu 1090 0 500 8510 0 0 0 0 0 0
|
||||
"""
|
||||
|
||||
MEMINFO = """\
|
||||
MemTotal: 124608 kB
|
||||
MemFree: 43204 kB
|
||||
MemAvailable: 62000 kB
|
||||
"""
|
||||
|
||||
|
||||
class TestGetEnvironmentCpu:
|
||||
"""CPU usage is the second between two reads, not the average since boot (#10)."""
|
||||
|
||||
def _answer(self, driver, cpu: str) -> list:
|
||||
sent: list = []
|
||||
|
||||
def send(cmd, **kw):
|
||||
sent.append(cmd)
|
||||
return MEMINFO if "meminfo" in cmd else cpu
|
||||
|
||||
driver._send_command = send
|
||||
return sent
|
||||
|
||||
def test_usage_is_the_difference_of_two_reads(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
assert driver.get_environment()["cpu"] == {0: {"%usage": 90.0}}
|
||||
|
||||
def test_the_two_reads_are_a_second_apart(self, driver):
|
||||
sent = self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
driver.get_environment()
|
||||
|
||||
command = next(cmd for cmd in sent if "/proc/stat" in cmd)
|
||||
assert command.count("/proc/stat") == 2
|
||||
assert "sleep 1" in command
|
||||
|
||||
def test_counters_that_did_not_move_are_zero(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\ncpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_a_single_read_is_not_taken_for_usage(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_memory_is_still_read(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
memory = driver.get_environment()["memory"]
|
||||
|
||||
assert memory == {"available_ram": 62000 * 1024, "used_ram": (124608 - 62000) * 1024}
|
||||
@@ -111,3 +111,54 @@ class TestRefresh:
|
||||
result = driver.refresh_available_updates()
|
||||
|
||||
assert result == {"success": False, "output": "ERROR: network unreachable"}
|
||||
|
||||
|
||||
class TestApplyUpdates:
|
||||
"""Only the package manager's exit status decides whether an upgrade succeeded (#11)."""
|
||||
|
||||
def test_apk_upgrade_that_worked(self, driver):
|
||||
sent = _answer(
|
||||
driver, "apk", "(1/1) Upgrading dnsmasq (2.90-r4 -> 2.90-r5)\nOK\n__OWRT_RC=0\n"
|
||||
)
|
||||
|
||||
result = driver.apply_updates(["dnsmasq"])
|
||||
|
||||
assert result == {
|
||||
"success": True,
|
||||
"output": "(1/1) Upgrading dnsmasq (2.90-r4 -> 2.90-r5)\nOK",
|
||||
}
|
||||
assert sent[-1].startswith("apk upgrade dnsmasq 2>&1")
|
||||
|
||||
def test_apk_upgrade_that_failed_is_no_success(self, driver):
|
||||
_answer(
|
||||
driver,
|
||||
"apk",
|
||||
"ERROR: unable to select packages:\n nope (no such package)\n__OWRT_RC=1\n",
|
||||
)
|
||||
|
||||
result = driver.apply_updates(["nope"])
|
||||
|
||||
assert result["success"] is False
|
||||
assert "unable to select packages" in result["output"]
|
||||
assert "status 1" in result["error"]
|
||||
|
||||
def test_opkg_upgrade_that_failed_is_no_success(self, driver):
|
||||
sent = _answer(
|
||||
driver,
|
||||
"opkg",
|
||||
" * opkg_download: Failed to download\nCollected errors:\n__OWRT_RC=255\n",
|
||||
)
|
||||
|
||||
result = driver.apply_updates(["dnsmasq"])
|
||||
|
||||
assert result["success"] is False
|
||||
assert "status 255" in result["error"]
|
||||
assert sent[-1].startswith("opkg upgrade dnsmasq 2>&1")
|
||||
|
||||
def test_output_without_a_status_is_no_success(self, driver):
|
||||
_answer(driver, "opkg", "")
|
||||
|
||||
result = driver.apply_updates(["dnsmasq"])
|
||||
|
||||
assert result["success"] is False
|
||||
assert result["error"]
|
||||
Reference in new issue
Block a user