feat(host-repairs): a host's checks and repairs as a fixed catalog of runs (NetOrk/netork#2899)

HostRepairMixin declares one method per run -- verify_system_files,
repair_system_files, check_component_store, repair_component_store,
update_antimalware_signatures and scan_antimalware -- so the method list is the
guard, and a repair is never offered with its check. Each streams the tool's
lines to log and returns a HostRepairResultDict; tool_outcome builds it from the
lines and the exit code, failing a non-zero code with the tool's last lines.
offered_host_repairs lists the runs a driver defines.
This commit is contained in:
christianmanivong committed 2026-12-01 12:44:45 +01:00
1 parent 2df46741f2
commit 54bda2cb79
5 files changed
+349

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
"""HostRepairMixin: a host's own checks and repairs of its system, as fixed runs (NetOrk/netork#2899).
One method per run, declared and not implemented, so ``hasattr`` tells an application
which runs a driver offers. ``tool_outcome`` is the shared part of every run: a tool's
exit code decides, a failure names the tool's last lines, and the output is bounded.
"""
from __future__ import annotations
import inspect
import pytest
from napalm_device_types import (
DeviceTypeDriver,
HostRepairMixin,
HostRepairResultDict,
tool_outcome,
)
from napalm_device_types.host_repairs import (
FAILURE_TAIL_LINES,
HOST_REPAIR_METHODS,
OUTPUT_LIMIT,
LineLog,
offered_host_repairs,
)
_RUNS = (
"verify_system_files",
"repair_system_files",
"check_component_store",
"repair_component_store",
"update_antimalware_signatures",
"scan_antimalware",
)
class TestContract:
@pytest.mark.parametrize("name", _RUNS)
def test_declared_not_implemented(self, name):
assert not hasattr(HostRepairMixin, name)
@pytest.mark.parametrize("name", _RUNS)
def test_each_run_is_documented(self, name):
source = inspect.getsource(HostRepairMixin)
assert f"def {name}(" in source
assert "log: LineLog | None = None" in source
def test_not_on_any_role_base(self):
# A driver mixes it in where its host has these tools.
assert not issubclass(DeviceTypeDriver, HostRepairMixin)
def test_the_catalog_names_every_run(self):
assert set(HOST_REPAIR_METHODS) == set(_RUNS)
def test_a_line_log_takes_one_line(self):
lines: list[str] = []
log: LineLog = lines.append
log("x")
assert lines == ["x"]
class TestOffered:
def test_only_the_runs_a_driver_defines(self):
class _Host(HostRepairMixin):
def verify_system_files(self, *, log=None): # noqa: ARG002
return tool_outcome(["ok"], 0, tool="sfc")
def update_antimalware_signatures(self, *, log=None): # noqa: ARG002
return tool_outcome([], 0, tool="Update-MpSignature")
assert offered_host_repairs(_Host) == [
"verify_system_files",
"update_antimalware_signatures",
]
def test_a_run_set_to_none_is_not_offered(self):
class _Host(HostRepairMixin):
scan_antimalware = None
assert offered_host_repairs(_Host) == []
def test_a_driver_without_the_mixin_offers_none(self):
assert offered_host_repairs(object) == []
class TestToolOutcome:
def test_exit_code_zero_succeeds_with_the_whole_output(self):
result: HostRepairResultDict = tool_outcome(["one", "two"], 0, tool="sfc")
assert result == {"success": True, "output": "one\ntwo", "exit_code": 0}
def test_a_non_zero_exit_code_fails_with_the_last_lines(self):
lines = [f"line {n}" for n in range(30)]
result = tool_outcome(lines, 5, tool="DISM")
assert result["success"] is False
assert result["exit_code"] == 5
assert result["error"].startswith("DISM ended with exit code 5:")
tail = result["error"].split(":", 1)[1].strip().splitlines()
assert tail == lines[-FAILURE_TAIL_LINES:]
def test_a_failure_without_output_names_the_code_only(self):
result = tool_outcome([], 2, tool="sfc")
assert result["error"] == "sfc ended with exit code 2"
def test_exit_codes_are_shown_as_hex_when_they_are_hresults(self):
result = tool_outcome(["Error: 0x800f081f"], 0x800F081F, tool="DISM")
assert result["error"].startswith("DISM ended with exit code 0x800F081F:")
def test_a_negative_exit_code_is_read_as_unsigned(self):
# .NET's Process.ExitCode is an Int32: 0x800F081F arrives as -2146498529.
result = tool_outcome([], -2146498529, tool="DISM")
assert result["exit_code"] == 0x800F081F
assert "0x800F081F" in result["error"]
def test_no_exit_code_is_a_failure(self):
result = tool_outcome(["started"], None, tool="sfc")
assert result["success"] is False
assert result["exit_code"] is None
assert result["error"] == "sfc ended without an exit code: started"
def test_blank_lines_are_not_the_tail(self):
result = tool_outcome(["the reason", "", " "], 1, tool="sfc")
assert result["error"] == "sfc ended with exit code 1: the reason"
def test_the_output_keeps_its_end_within_the_limit(self):
lines = ["x" * 1000] * (OUTPUT_LIMIT // 1000 + 50) + ["the end"]
result = tool_outcome(lines, 0, tool="sfc")
assert len(result["output"]) <= OUTPUT_LIMIT
assert result["output"].endswith("the end")