"""The SMBIOS identity on Windows: CIM classes instead of the DMI files (NetOrk/netork#2888). One script reads ``Win32_ComputerSystemProduct``, ``Win32_BaseBoard`` and ``Win32_BIOS``; a fake transport answers it from tests/fixtures/synthetic/system_identity.json, a Dell server as the classes describe it. The placeholders are napalm-device-types' own (``clean_system_identity``). """ 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 system_identity as mod FIXTURE = Path(__file__).parent / "fixtures" / "synthetic" / "system_identity.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 _driver(**changes: Any) -> WindowsDriver: d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport(json.dumps({**json.loads(FIXTURE.read_text()), **changes})) return d def test_reads_the_identity_and_the_firmware(): assert _driver().get_system_identity() == { "system_uuid": "4c4c4544-0042-3510-8051-b4c04f4d3732", "product_serial": "4B5QM72", "board_serial": ".4B5QM72.CNCMS0012345AB.", "bios_vendor": "Dell Inc.", "bios_version": "2.19.0", "bios_date": "2023-08-15", } def test_one_script_reads_the_three_classes(): d = _driver() d.get_system_identity() (script,) = d._transport.sent # type: ignore[union-attr] assert script == mod._PS_SYSTEM_IDENTITY for cls in ("Win32_ComputerSystemProduct", "Win32_BaseBoard", "Win32_BIOS"): assert cls in script @pytest.mark.parametrize( ("field", "value"), [ ("product_serial", "To be filled by O.E.M."), ("board_serial", "Default string"), ("uuid", "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"), ("uuid", "03000200-0400-0500-0006-000700080009"), ("bios_vendor", "To be filled by O.E.M."), ], ) def test_oem_placeholders_read_as_empty(field, value): identity = _driver(**{field: value}).get_system_identity() key = {"uuid": "system_uuid"}.get(field, field) assert identity[key] == "" # type: ignore[literal-required] @pytest.mark.parametrize("field", ["uuid", "product_serial", "board_serial", "bios_date"]) def test_a_value_the_host_did_not_give_reads_as_empty(field): # A VM without a base board, a firmware without a release date: null in the JSON. identity = _driver(**{field: None}).get_system_identity() key = {"uuid": "system_uuid"}.get(field, field) assert identity[key] == "" # type: ignore[literal-required] def test_the_release_date_is_the_utc_day(): # CIM gives the SMBIOS date as midnight UTC; the script sends it as Unix time. assert _driver(bios_date=1692057600 + 86399).get_system_identity()["bios_date"] == "2023-08-15" def test_an_empty_answer_is_no_reading(): d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport("") with pytest.raises(ValueError): d.get_system_identity()