Files
napalm-openwrt/tests/unit/test_packages.py
T
Christian Manivong e4ae42fea0
driver / types (pull_request) Successful in 1m0s
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 15s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 44s
driver / security (pull_request) Successful in 1m19s
CI / driver (pull_request) Successful in 2m56s
driver / publish (pull_request) Skipped
test: documentation names and addresses in fixtures
Fixtures, docstring examples and the README use neutral host names
(ap-01, ap-02) and addresses from the documentation ranges 192.0.2.0/24,
198.51.100.0/24 and 203.0.113.0/24, mapped one to one so every test
keeps its meaning: the management network test still has its address
inside that network, a route still covers its prefix. Loopback, the
wildcard address and netmasks are unchanged.
2026-11-12 19:21:54 +01:00

121 lines
3.9 KiB
Python

"""install_package / uninstall_package: only the exit status decides (#28).
Searching the output for words like "error:" or "failed" misreads both ways:
opkg's dependency failure says "Collected errors:" (not "error:") and a
successful install may name a file containing "failed".
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import pytest
from napalm_openwrt.openwrt import OpenWrtDriver
OPKG_DEPENDS_FAILED = (
"Installing lldpd (1.0.17-1) to root...\n"
"Collected errors:\n"
" * satisfy_dependencies_for: Cannot satisfy the following dependencies for lldpd:\n"
" * \tlibevent2-7 (>= 2.1.12)\n"
" * opkg_install_cmd: Cannot install package lldpd.\n"
"__OWRT_RC=255\n"
)
APK_ADD_OK_NAMING_FAILED = (
"(1/1) Installing failed-login-notify (1.0-r1)\nOK: 12 MiB in 140 packages\n__OWRT_RC=0\n"
)
@pytest.fixture
def driver():
with patch("napalm_openwrt.openwrt.ConnectHandler"):
drv = OpenWrtDriver(hostname="192.0.2.1", username="root", password="")
drv.device = MagicMock()
yield drv
def _answer(driver, pm: str, reply: str) -> list[str]:
"""Answer the package-manager probe with *pm* and everything else with *reply*."""
sent: list[str] = []
def send(cmd, **kwargs):
sent.append(cmd)
if cmd.startswith("command -v apk"):
return "/usr/bin/apk" if pm == "apk" else "__no_apk__"
return reply
driver._send_command = send
return sent
class TestInstallPackage:
def test_opkg_dependency_failure_is_no_success(self, driver):
sent = _answer(driver, "opkg", OPKG_DEPENDS_FAILED)
result = driver.install_package("lldpd")
assert result["success"] is False
assert "Cannot install package lldpd" in result["output"]
assert "__OWRT_RC" not in result["output"]
assert sent[-1] == "opkg install lldpd 2>&1; echo __OWRT_RC=$?"
def test_success_whose_output_says_failed_is_success(self, driver):
sent = _answer(driver, "apk", APK_ADD_OK_NAMING_FAILED)
result = driver.install_package("failed-login-notify")
assert result == {
"success": True,
"output": "(1/1) Installing failed-login-notify (1.0-r1)\nOK: 12 MiB in 140 packages",
}
assert sent[-1] == "apk add failed-login-notify 2>&1; echo __OWRT_RC=$?"
def test_marker_behind_a_progress_bar_is_still_read(self, driver):
_answer(driver, "apk", "(1/1) Installing lldpd (1.0.17-r1)\n 100% ####\r__OWRT_RC=0\n")
assert driver.install_package("lldpd")["success"] is True
def test_output_without_a_status_is_no_success(self, driver):
_answer(driver, "apk", "")
result = driver.install_package("lldpd")
assert result["success"] is False
assert result["output"]
def test_failure_without_output_names_the_status(self, driver):
_answer(driver, "apk", "__OWRT_RC=99\n")
result = driver.install_package("lldpd")
assert result["success"] is False
assert "status 99" in result["output"]
class TestUninstallPackage:
def test_opkg_remove_that_failed_is_no_success(self, driver):
sent = _answer(
driver,
"opkg",
"Collected errors:\n * print_dependents_warning: Package lldpd is depended upon by:\n"
"__OWRT_RC=255\n",
)
result = driver.uninstall_package("lldpd")
assert result["success"] is False
assert "depended upon" in result["output"]
assert sent[-1] == "opkg remove lldpd 2>&1; echo __OWRT_RC=$?"
def test_apk_del_that_worked(self, driver):
sent = _answer(
driver,
"apk",
"(1/1) Purging failed-login-notify (1.0-r1)\nOK: 11 MiB in 139 packages\n__OWRT_RC=0\n",
)
result = driver.uninstall_package("failed-login-notify")
assert result["success"] is True
assert sent[-1] == "apk del failed-login-notify 2>&1; echo __OWRT_RC=$?"