Files
napalm-device-types/tests/test_updates.py
T
christianmanivong 216d60d464 feat(updates): an update can name its title, advisories, CVEs and vendor rating (NetOrk/netork#2892)
UpdateDict takes optional title, advisories, cves and vendor_severity, for a
source that says more of an update than a package name: Windows Update's
title, KBArticleIDs, CveIDs and MsrcSeverity. UpdateHistoryPackageDict takes
an optional title. Additive: every reader that leaves them out conforms.
2026-12-01 12:47:06 +01:00

76 lines
2.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")
def test_a_pending_update_can_name_what_it_fixes():
"""A source that says which advisories and CVEs an update closes, and how its
vendor rates it (Windows Update's ``KBArticleIDs``, ``CveIDs``, ``MsrcSeverity``,
NetOrk/netork#2892), can pass that on. Optional: every reader that says nothing
of it still conforms."""
from typing import get_type_hints
from napalm_device_types.models import UpdateDict
hints = get_type_hints(UpdateDict)
assert {"title", "advisories", "cves", "vendor_severity"} <= set(hints)
assert UpdateDict.__required_keys__ == {"name", "current_version", "new_version"}
def test_a_past_update_can_carry_its_title():
"""Windows names an update by its title, not by a package (NetOrk/netork#2892)."""
from typing import get_type_hints
from napalm_device_types.updates import UpdateHistoryPackageDict
assert "title" in get_type_hints(UpdateHistoryPackageDict, include_extras=True)
assert "NotRequired" in str(
get_type_hints(UpdateHistoryPackageDict, include_extras=True)["title"]
)