Files
napalm-openwrt/tests/unit/test_packages.py
T
Christian Manivong 58ee774250
driver / standard (pull_request) Successful in 26s
driver / lint (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 59s
driver / lowest (pull_request) Successful in 28s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m21s
CI / driver (pull_request) Successful in 3m14s
driver / publish (pull_request) Skipped
fix(packages): decide install/uninstall success by exit status
install_package and uninstall_package searched the output for keywords.
opkg's dependency failure ("Collected errors:") contains none of them and
read as success; a successful install naming a "failed" file read as a
failure. Append the exit-status marker and let the status alone decide.

Closes #28
2026-11-02 08:34:29 +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.168.1.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=$?"