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.
This commit is contained in:
1 parent
e2970831f8
commit
059d97f6de
3 files changed
+47
-1
No files matched your search
@@ -2,6 +2,9 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import inspect
|
||||
|
||||
import napalm_device_types
|
||||
from napalm_device_types.updates import PackageIndexMissing
|
||||
|
||||
@@ -18,3 +21,29 @@ def test_a_missing_index_is_its_own_failure():
|
||||
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")
|
||||
Reference in new issue
Block a user