fix(tests): repair the fixture and doubles that made seven tests fail

Closes netork#110.

The seven failures had two causes, neither of them in the driver.

The `driver` fixture builds a LinuxDriver with `__new__`, bypassing `__init__`,
and never set `_sudo_password`. Every call through `_sudo()` therefore raised
AttributeError, which the callers' broad `except Exception` reported as
`{"success": False}` — so six apply_updates tests failed for a reason unrelated
to what they were asserting.

`test_apply_updates_apt_all_packages` had a second one: its `capture_send(cmd)`
double accepted no keyword arguments, while `_sudo()` passes `read_timeout`.

The seventh, the apt update listing test, supplied `side_effect=["",
APT_UPGRADABLE]` — two values for a cache refresh that never existed.
`_get_updates_apt` has made exactly one `_send` call since it was written in
2712389, so the empty first value was consumed and parsed as the package list.
Checked out that commit and ran it: the test failed there too. It was committed
red and never passed.

That settles the open question in netork#110: the implementation was not changed,
the tests were written against one that never existed. `get_available_updates`
reads the local apt cache deliberately — refreshing it needs sudo and would cost
a round trip on every poll — so there is no stale-cache bug behind the
`updates_available` warning.
This commit is contained in:
Christian Manivong
2026-08-21 12:57:11 +07:00
parent 27027eec56
commit ce40299033
+10 -2
View File
@@ -22,6 +22,11 @@ def driver():
d._secret = "pass" # noqa: S105 d._secret = "pass" # noqa: S105
d._forced_pkg_manager = None d._forced_pkg_manager = None
d._pkg_manager = "apt" d._pkg_manager = "apt"
# Set by __init__, which this fixture bypasses via __new__. Without it every
# call through _sudo() raises AttributeError, which the callers' broad
# `except Exception` turns into a plain {"success": False} -- so the tests
# failed for a reason that had nothing to do with what they were testing.
d._sudo_password = None
d.netmiko_optional_args = {} d.netmiko_optional_args = {}
d._device = MagicMock() d._device = MagicMock()
return d return d
@@ -179,7 +184,7 @@ APT_UPGRADABLE = (
def test_get_available_updates_apt(driver): def test_get_available_updates_apt(driver):
driver._pkg_manager = "apt" driver._pkg_manager = "apt"
with patch.object(driver, "_send", side_effect=["", APT_UPGRADABLE]): with patch.object(driver, "_send", return_value=APT_UPGRADABLE):
updates = driver.get_available_updates() updates = driver.get_available_updates()
assert len(updates) == 2 assert len(updates) == 2
assert updates[0]["name"] == "openssh-server" assert updates[0]["name"] == "openssh-server"
@@ -312,7 +317,10 @@ def test_apply_updates_apt_all_packages(driver):
driver._pkg_manager = "apt" driver._pkg_manager = "apt"
sent_commands = [] sent_commands = []
def capture_send(cmd): def capture_send(cmd, **kwargs):
# _sudo() passes read_timeout as a keyword; without **kwargs this raises
# TypeError, which the caller's `except Exception` reports as a failed
# upgrade rather than a broken test double.
sent_commands.append(cmd) sent_commands.append(cmd)
return APT_UPGRADE_SUCCESS return APT_UPGRADE_SUCCESS