feat(repairs): a caller's limit tightens a run's bound (NetOrk/netork#2899)

This commit is contained in:
Christian Manivong committed 2026-12-01 12:53:17 +01:00
1 parent 0bbeebb7a7
commit 0df672fb7c
4 files changed
+45 -13

No files matched your search

+2 -1
View File
@@ -11,7 +11,8 @@ All notable changes to this driver. The format follows
system files (`sfc /verifyonly`) and the component store (`DISM /ScanHealth`), updating
Microsoft Defender's signatures and running a quick or full Defender scan. Each streams
the tool's output line by line as the host prints it and ends with the tool's exit
code; a failure names the tool's last lines. A host without Defender says so instead of
code; a failure names the tool's last lines. Each run has its own bound in time,
which a caller's `limit` can tighten. A host without Defender says so instead of
failing with an error. Repairing the system files or the component store is not offered
yet: both wait for a test on a real host. `PsrpTransport.run_streaming(script,
on_output, limit=...)` runs a long script, passing each output line on as it arrives and
+1 -1
View File
@@ -106,7 +106,7 @@ See netork#300 for the plan.
napalm-device-types' `HostRepairMixin` (NetOrk/netork#2899): a fixed catalog of runs, one
method each, so an application can start these and nothing else.
| Run | Tool | Limit |
| Run | Tool | Bound (a caller's `limit` can tighten it) |
|---|---|---|
| `verify_system_files` | `sfc /verifyonly` (changes nothing) | 1 h |
| `check_component_store` | `DISM /Online /Cleanup-Image /ScanHealth /English` (changes nothing) | 1 h |
+21 -11
View File
@@ -54,7 +54,7 @@ from napalm_windows.transport import PowerShellError
if TYPE_CHECKING: # pragma: no cover - for type checkers only
from napalm_windows.transport import PsrpTransport
#: The longest each run may take, in seconds.
#: The longest each run may take, in seconds; a caller's ``limit`` can only tighten it.
SFC_LIMIT = 3600
DISM_LIMIT = 3600
SIGNATURE_LIMIT = 900
@@ -224,30 +224,40 @@ class WindowsRepairsMixin(HostRepairMixin):
if TYPE_CHECKING: # pragma: no cover - declared for type checkers only
_transport: PsrpTransport | None
def verify_system_files(self, *, log: LineLog | None = None) -> HostRepairResultDict:
def verify_system_files(
self, *, log: LineLog | None = None, limit: float | None = None
) -> HostRepairResultDict:
"""``sfc /verifyonly``: checks the protected system files and changes nothing."""
return self._tool_run(_PS_SFC_VERIFY, "sfc /verifyonly", SFC_LIMIT, log)
return self._tool_run(_PS_SFC_VERIFY, "sfc /verifyonly", SFC_LIMIT, log, limit)
def check_component_store(self, *, log: LineLog | None = None) -> HostRepairResultDict:
def check_component_store(
self, *, log: LineLog | None = None, limit: float | None = None
) -> HostRepairResultDict:
"""``DISM /Online /Cleanup-Image /ScanHealth``: scans the component store and
changes nothing; up to half an hour."""
return self._tool_run(_PS_DISM_SCAN, "DISM /ScanHealth", DISM_LIMIT, log)
return self._tool_run(_PS_DISM_SCAN, "DISM /ScanHealth", DISM_LIMIT, log, limit)
def update_antimalware_signatures(self, *, log: LineLog | None = None) -> HostRepairResultDict:
def update_antimalware_signatures(
self, *, log: LineLog | None = None, limit: float | None = None
) -> HostRepairResultDict:
"""``Update-MpSignature``, with the signature version before and after."""
return self._tool_run(_PS_SIGNATURE_UPDATE, "Update-MpSignature", SIGNATURE_LIMIT, log)
return self._tool_run(
_PS_SIGNATURE_UPDATE, "Update-MpSignature", SIGNATURE_LIMIT, log, limit
)
def scan_antimalware(
self, quick: bool = True, *, log: LineLog | None = None
self, quick: bool = True, *, log: LineLog | None = None, limit: float | None = None
) -> HostRepairResultDict:
"""``Start-MpScan``, quick or full, with what it detected."""
if quick:
return self._tool_run(_PS_QUICK_SCAN, "Start-MpScan", QUICK_SCAN_LIMIT, log)
return self._tool_run(_PS_FULL_SCAN, "Start-MpScan", FULL_SCAN_LIMIT, log)
return self._tool_run(_PS_QUICK_SCAN, "Start-MpScan", QUICK_SCAN_LIMIT, log, limit)
return self._tool_run(_PS_FULL_SCAN, "Start-MpScan", FULL_SCAN_LIMIT, log, limit)
def _tool_run(
self, script: str, tool: str, limit: float, log: LineLog | None
self, script: str, tool: str, bound: float, log: LineLog | None, limit: float | None
) -> HostRepairResultDict:
"""Run *script*; within *bound*, or the caller's *limit* where that is tighter."""
limit = bound if limit is None else min(limit, bound)
if self._transport is None:
raise ConnectionClosedException("WinRM connection is not open")
run = _ToolRun(log)
+21
View File
@@ -253,6 +253,27 @@ class TestScanAntimalware:
assert transport.limits == [mod.FULL_SCAN_LIMIT]
class TestLimit:
@pytest.mark.parametrize(
("run", "bound"),
[
(lambda d, **kw: d.verify_system_files(**kw), mod.SFC_LIMIT),
(lambda d, **kw: d.check_component_store(**kw), mod.DISM_LIMIT),
(lambda d, **kw: d.update_antimalware_signatures(**kw), mod.SIGNATURE_LIMIT),
(lambda d, **kw: d.scan_antimalware(**kw), mod.QUICK_SCAN_LIMIT),
(lambda d, **kw: d.scan_antimalware(False, **kw), mod.FULL_SCAN_LIMIT),
],
)
def test_a_callers_limit_tightens_the_bound_never_widens_it(self, run, bound):
driver, transport = _driver(["napalm-exit-code:0"])
run(driver, limit=600)
run(driver, limit=bound * 2)
run(driver)
assert transport.limits == [600, bound, bound]
class TestCutOff:
def test_a_run_past_its_limit_fails_with_what_it_printed(self):
driver, _ = _driver(