Files
napalm-device-types/tests/test_updates.py
T
christianmanivong 059d97f6de feat(updates): apply_updates takes an optional log callback (NetOrk/netork#1112)
UpdateMixin.apply_updates declares a keyword-only log, as
harden_ssh_server does: it receives each line of the package manager's
output as the host prints it. A driver that cannot stream does not call
it and returns the output at the end, as before; the declaration stays
compatible with every driver that implements it without the keyword.
2026-11-29 20:18:31 +01:00

50 lines
1.9 KiB
Python

"""The contract of :class:`~napalm_device_types.updates.UpdateMixin`."""
from __future__ import annotations
import ast
import inspect
import napalm_device_types
from napalm_device_types.updates import PackageIndexMissing
def test_a_missing_index_is_its_own_failure():
"""A reader can tell "no index to read from" from any other failure, and a
caller that only knows failures still catches it as one."""
exc = PackageIndexMissing("The package index is not loaded (run apk update first)")
assert isinstance(exc, RuntimeError)
assert str(exc) == "The package index is not loaded (run apk update first)"
def test_it_is_exported_from_the_package():
assert napalm_device_types.PackageIndexMissing is PackageIndexMissing
assert "PackageIndexMissing" in napalm_device_types.__all__
def _declared_apply_updates():
"""``UpdateMixin.apply_updates`` as declared (under ``TYPE_CHECKING``, so read from source)."""
from napalm_device_types import updates
for node in ast.walk(ast.parse(inspect.getsource(updates))):
if isinstance(node, ast.FunctionDef) and node.name == "apply_updates":
return node
raise AssertionError("apply_updates is not declared")
def test_apply_updates_takes_a_log_callback_by_keyword_only():
"""A caller can follow the host's output as it comes (NetOrk/netork#1112); the
positional signature stays as it was, so existing drivers keep conforming."""
node = _declared_apply_updates()
assert [a.arg for a in node.args.args] == ["self", "packages"]
assert [a.arg for a in node.args.kwonlyargs] == ["log"]
(default,) = node.args.kw_defaults
assert isinstance(default, ast.Constant) and default.value is None
assert ast.unparse(node.args.kwonlyargs[0].annotation) == "Callable[[str], None] | None"
def test_apply_updates_still_hides_at_runtime():
assert not hasattr(napalm_device_types.UpdateMixin, "apply_updates")