fix(packages): read install_package's success from the exit status

install_package searched the output of `... || true` for words such as
"failed", the pattern uninstall_package left behind for netork#267: a
postinst that only mentions a failure read as a failed install, and a
failure phrased without those words read as success. It now runs through
_sudo_status like the removal and falls back to the keywords only when
the channel gave no exit status.

The ruff findings named in the issue are gone since the driver adopted
the standard; strict mypy for linux.py is held by the quality ratchet,
and the None-vs-dict package contract is NAPALM/napalm-device-types#36.

Closes #4
This commit is contained in:
Christian Manivong committed 2026-11-01 21:44:51 +01:00
1 parent a729b35d8b
commit eaa08ae7e3
4 files changed
+70 -26

No files matched your search

+1 -1
View File
@@ -22,7 +22,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2265
"napalm_linux/linux.py": 2260
},
"untyped_public": []
}
+45
View File
@@ -1276,6 +1276,51 @@ class TestUninstallExitStatus:
assert result["success"] is False
class TestInstallExitStatus:
"""``install_package`` decides by the exit status too (#4), as removal does
since netork#267: keywords in the output were guesswork in both directions."""
def test_a_non_zero_exit_is_a_failure_whatever_the_output_says(self, driver):
driver._pkg_manager = "dnf"
_mock_send(driver, _with_rc("Last metadata expiration check: 0:01:02 ago.", 1))
result = driver.install_package("curl")
assert result["success"] is False
def test_a_zero_exit_is_a_success_even_if_the_output_mentions_failure(self, driver):
"""A postinst that cannot start a unit in a container says "Failed" and
apt still installs the package."""
_mock_send(
driver,
_with_rc(
"Setting up nginx (1.26.3-3) ...\n"
"Failed to start nginx.service: System has not been booted with systemd.",
0,
),
)
result = driver.install_package("nginx")
assert result["success"] is True
def test_the_install_command_keeps_its_exit_status(self, driver):
_mock_send(driver, _with_rc("Setting up curl ...", 0))
result = driver.install_package("curl")
sent = driver._device.send_command.call_args[0][0]
assert "|| true" not in sent
assert result["output"] == "Setting up curl ..."
def test_without_an_exit_status_the_output_is_read(self, driver):
_mock_send(driver, _with_rc("E: Unable to locate package nosuchpkg", -1))
result = driver.install_package("nosuchpkg")
assert result["success"] is False
# ---------------------------------------------------------------------------
# get_kernel_facts -- the command and its parse live in napalm-device-types
# ---------------------------------------------------------------------------