212 lines
8.0 KiB
Python
212 lines
8.0 KiB
Python
"""Whether a Windows host needs a restart, and whether it updates itself (NetOrk/netork#2891).
|
|
|
|
One script reads Windows Update's own answer (``Microsoft.Update.SystemInfo``), the
|
|
registry's restart markers, the Windows Update policy and history, and the system
|
|
drive; a fake transport answers it from tests/fixtures/synthetic/host_status.json: a
|
|
member server whose policy installs updates automatically, with file renames pending
|
|
from an installer and nothing else.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from napalm_device_types.models import HostStatusDict
|
|
|
|
from napalm_windows import WindowsDriver
|
|
from napalm_windows import host_status as mod
|
|
|
|
FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "host_status.json"
|
|
MONTH = 31 * 86400
|
|
|
|
|
|
class FakeTransport:
|
|
def __init__(self, answer: str) -> None:
|
|
self.answer = answer
|
|
self.sent: list[str] = []
|
|
self.is_open = True
|
|
|
|
def run(self, script: str) -> str:
|
|
self.sent.append(script)
|
|
return self.answer
|
|
|
|
|
|
def _status(**changes: Any) -> HostStatusDict:
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
reading = {**json.loads(FIXTURE.read_text()), **changes}
|
|
d._transport = FakeTransport(json.dumps(reading))
|
|
return d.get_host_status()
|
|
|
|
|
|
def test_reads_the_host_status():
|
|
assert _status() == {
|
|
"reboot_required": False,
|
|
"reboot_reason": "files wait to be replaced at the next restart "
|
|
"(PendingFileRenameOperations)",
|
|
"auto_updates": True,
|
|
"auto_updates_last_success": 1763337600,
|
|
"auto_updates_interval": MONTH,
|
|
"filesystems": [
|
|
{
|
|
"mountpoint": "C:\\",
|
|
"total": 136363114496,
|
|
"used": 136363114496 - 52613349376,
|
|
"available": 52613349376,
|
|
}
|
|
],
|
|
}
|
|
|
|
|
|
# ── restart ───────────────────────────────────────────────────────────────────
|
|
|
|
|
|
def test_pending_file_renames_alone_need_no_restart():
|
|
# Antivirus and installers set them routinely; as a trigger they would keep the flag up.
|
|
status = _status()
|
|
assert status["reboot_required"] is False
|
|
assert "PendingFileRenameOperations" in (status["reboot_reason"] or "")
|
|
|
|
|
|
def test_windows_update_saying_so_needs_a_restart():
|
|
status = _status(wu_reboot_required=True, file_renames=False)
|
|
assert status["reboot_required"] is True
|
|
assert status["reboot_reason"] == "Windows Update needs a restart to finish installing updates"
|
|
|
|
|
|
def test_component_servicing_pending_needs_a_restart():
|
|
status = _status(cbs_reboot_pending=True, file_renames=False)
|
|
assert status["reboot_required"] is True
|
|
assert status["reboot_reason"] == "component-based servicing has a restart pending"
|
|
|
|
|
|
def test_windows_updates_registry_marker_stands_in_where_its_com_object_did_not_answer():
|
|
status = _status(wu_reboot_required=None, wu_reboot_key=True, file_renames=False)
|
|
assert status["reboot_required"] is True
|
|
|
|
|
|
def test_nothing_pending_needs_no_restart_and_gives_no_reason():
|
|
status = _status(file_renames=False)
|
|
assert status["reboot_required"] is False
|
|
assert status["reboot_reason"] is None
|
|
|
|
|
|
def test_every_marker_found_is_named():
|
|
status = _status(
|
|
wu_reboot_required=True,
|
|
cbs_reboot_pending=True,
|
|
update_exe="1",
|
|
computer_name="WIN02",
|
|
)
|
|
assert status["reboot_reason"] == "; ".join(
|
|
[
|
|
"Windows Update needs a restart to finish installing updates",
|
|
"component-based servicing has a restart pending",
|
|
"files wait to be replaced at the next restart (PendingFileRenameOperations)",
|
|
"an update waits for a restart (UpdateExeVolatile)",
|
|
"the computer takes its new name WIN02 at the next restart",
|
|
]
|
|
)
|
|
|
|
|
|
def test_a_rename_alone_needs_no_restart_for_updates():
|
|
status = _status(file_renames=False, computer_name="WIN02")
|
|
assert status["reboot_required"] is False
|
|
assert status["reboot_reason"] == "the computer takes its new name WIN02 at the next restart"
|
|
|
|
|
|
# ── automatic updates ─────────────────────────────────────────────────────────
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("au_options", "expected"),
|
|
[("2", False), ("3", False), ("4", True), ("7", False)],
|
|
ids=["notify", "download-only", "install", "server-notify-install-and-restart"],
|
|
)
|
|
def test_the_policys_au_options(au_options, expected):
|
|
assert _status(au_options=au_options)["auto_updates"] is expected
|
|
|
|
|
|
def test_a_policy_that_turns_automatic_updates_off():
|
|
assert _status(no_auto_update="1")["auto_updates"] is False
|
|
|
|
|
|
def test_a_policy_that_leaves_it_to_the_local_administrator():
|
|
assert _status(au_options="5", local_au_options="4")["auto_updates"] is True
|
|
assert _status(au_options="5", local_au_options="3")["auto_updates"] is False
|
|
|
|
|
|
def test_a_client_without_a_policy_installs_updates_itself():
|
|
assert _status(au_options="", product_type=1)["auto_updates"] is True
|
|
|
|
|
|
def test_a_client_ignores_a_leftover_local_setting():
|
|
# Windows 10 and 11 honour only the policy; an old AUOptions left from an upgrade stays.
|
|
assert _status(au_options="", local_au_options="2", product_type=1)["auto_updates"] is True
|
|
|
|
|
|
def test_a_server_follows_its_local_setting_without_a_policy():
|
|
assert _status(au_options="", local_au_options="4")["auto_updates"] is True
|
|
assert _status(au_options="", local_au_options="3")["auto_updates"] is False
|
|
|
|
|
|
def test_a_server_without_any_setting_cannot_tell():
|
|
status = _status(au_options="", local_au_options="")
|
|
assert status["auto_updates"] is None
|
|
assert status["auto_updates_interval"] is None
|
|
|
|
|
|
def test_without_automatic_updates_there_is_no_interval():
|
|
status = _status(au_options="3")
|
|
assert status["auto_updates_interval"] is None
|
|
assert status["auto_updates_last_success"] == 1763337600
|
|
|
|
|
|
def test_the_last_success_is_the_latest_any_source_knows():
|
|
status = _status(installs=[], au_last_install=None, legacy_last_install="2025-11-20 03:12:44")
|
|
assert status["auto_updates_last_success"] == 1763608364
|
|
status = _status(installs=[1760659200], au_last_install=1762732800)
|
|
assert status["auto_updates_last_success"] == 1762732800
|
|
|
|
|
|
def test_no_installation_known_has_no_last_success():
|
|
status = _status(installs=None, au_last_install=None, legacy_last_install="")
|
|
assert status["auto_updates_last_success"] is None
|
|
|
|
|
|
def test_an_unset_com_date_is_no_installation():
|
|
# WUA's DATE zero is 1899-12-30, which travels as a negative Unix time.
|
|
status = _status(installs=[], au_last_install=-2209161600, legacy_last_install="garbage")
|
|
assert status["auto_updates_last_success"] is None
|
|
|
|
|
|
# ── the system drive ──────────────────────────────────────────────────────────
|
|
|
|
|
|
def test_an_unread_system_drive_is_no_filesystem_reading():
|
|
assert _status(system_drive=None)["filesystems"] is None
|
|
|
|
|
|
def test_one_script_reads_it_all_each_part_on_its_own():
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeTransport(FIXTURE.read_text())
|
|
d.get_host_status()
|
|
(script,) = d._transport.sent # type: ignore[union-attr]
|
|
assert script == mod._PS_HOST_STATUS
|
|
for part in (
|
|
"Microsoft.Update.SystemInfo",
|
|
r"Component Based Servicing\RebootPending",
|
|
"PendingFileRenameOperations",
|
|
r"Policies\Microsoft\Windows\WindowsUpdate\AU",
|
|
"QueryHistory",
|
|
"Win32_LogicalDisk",
|
|
):
|
|
assert part in script
|
|
# Each COM object is asked inside its own try, so one that does not answer over
|
|
# WinRM leaves the rest of the reading standing.
|
|
assert script.count("try {") >= 4
|
|
# No update search in the poll: that is the update reading's, and slow.
|
|
assert "Search(" not in script
|