Files
napalm-windows/tests/test_packages.py
T
Christian Manivong 9b9fd261bd
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 18s
driver / lowest (pull_request) Successful in 33s
driver / types (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m14s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 59s
CI / driver (pull_request) Successful in 1m50s
driver / publish (pull_request) Skipped
fix(packages): architecture is PackageDict's optional key, left out when unnamed (#14)
napalm-device-types 4.59.0 declares architecture as an optional key of
PackageDict. WindowsPackageDict redeclared it as required, which strict
mypy refuses for a subclass. It now inherits the key and leaves it out
of a program whose Uninstall key names none, instead of writing "".
The floor is napalm-device-types 4.59.0.
2026-12-05 06:52:34 +01:00

143 lines
4.7 KiB
Python

"""Installed software on Windows: the machine-wide Uninstall keys (NetOrk/netork#2890).
One script reads the native and the 32-bit (``WOW6432Node``) Uninstall key of HKLM; a
fake transport answers it from tests/fixtures/synthetic/packages.json. The fixture holds
the cases the issue names: 7-Zip installed both as a 64-bit and a 32-bit program, a
``SystemComponent`` entry, and an update listed under its product (``ParentKeyName``).
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
import pytest
from napalm_windows import WindowsDriver
from napalm_windows import packages as mod
FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "packages.json"
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 _reading() -> list[dict[str, Any]]:
return json.loads(FIXTURE.read_text())
def _driver(reading: Any = None) -> WindowsDriver:
d = WindowsDriver("win01", "admin", "secret")
d._transport = FakeTransport(json.dumps(_reading() if reading is None else reading))
return d
def _packages(reading: Any = None) -> list[Any]:
return list(_driver(reading).get_packages())
def test_a_program_reads_as_a_package():
git = next(p for p in _packages() if p["name"] == "Git")
assert git == {
"name": "Git",
"version": "2.43.0",
"installed": True,
"description": "",
"size": 309000 * 1024,
"source": "exe",
"publisher": "The Git Development Community",
"architecture": "x64",
}
def test_a_windows_installer_package_is_msi():
acrobat = next(p for p in _packages() if p["name"] == "Adobe Acrobat Reader DC")
assert acrobat["source"] == "msi"
assert acrobat["architecture"] == "x86"
def test_a_32_and_a_64_bit_install_of_one_program_stay_two_packages():
zips = [p for p in _packages() if p["name"].startswith("7-Zip")]
assert sorted(p["architecture"] for p in zips) == ["x64", "x86"]
def test_system_components_updates_and_unnamed_keys_are_no_packages():
names = [p["name"] for p in _packages()]
assert "Office 16 Click-to-Run Extensibility Component" not in names
assert "Security Update for Windows Server (KB5034441)" not in names
assert "" not in names
assert len(names) == 5
def test_packages_come_sorted_by_name():
names = [p["name"] for p in _packages()]
assert names == sorted(names, key=str.lower)
def test_a_missing_size_is_zero():
reading = _reading()
reading[5]["size"] = ""
assert next(p for p in _packages(reading) if p["name"] == "Git")["size"] == 0
def test_a_single_package_survives_powershells_unwrapping():
assert [p["name"] for p in _packages(_reading()[5])] == ["Git"]
def test_a_host_with_nothing_installed_has_no_packages():
d = WindowsDriver("win01", "admin", "secret")
d._transport = FakeTransport("[]")
assert d.get_packages() == []
def test_one_script_reads_both_machine_wide_keys_and_no_users_hive():
d = _driver()
d.get_packages()
(script,) = d._transport.sent # type: ignore[union-attr]
assert script == mod._PS_PACKAGES
assert r"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" in script
assert r"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall" in script
# The connecting account's own hive says nothing about the host.
assert "HKCU" not in script
assert "winget" not in script
def test_installing_and_removing_stay_not_offered():
assert not hasattr(WindowsDriver, "install_package")
assert not hasattr(WindowsDriver, "uninstall_package")
@pytest.mark.parametrize("answer", ["", "null"])
def test_an_empty_answer_has_no_packages(answer):
d = WindowsDriver("win01", "admin", "secret")
d._transport = FakeTransport(answer)
assert d.get_packages() == []
def test_architecture_is_the_contract_s_optional_key():
"""``PackageDict.architecture`` (napalm-device-types 4.59.0) is inherited, not
redeclared: a subclass may not make the contract's optional key required."""
assert "architecture" in mod.WindowsPackageDict.__optional_keys__
@pytest.mark.parametrize("arch", [None, ""])
def test_a_program_without_an_architecture_leaves_it_out(arch):
reading = _reading()
git = next(e for e in reading if e.get("name") == "Git")
if arch is None:
del git["arch"]
else:
git["arch"] = arch
packages = _packages(reading)
assert "architecture" not in next(p for p in packages if p["name"] == "Git")
assert [p["name"] for p in packages] == sorted((p["name"] for p in packages), key=str.lower)