WindowsStorageMixin implements napalm-device-types' HostStorageMixin, so the host reports its storage and stays a host: - get_disks: Get-PhysicalDisk with medium, bus, Windows' health and the Storage Spaces pool a disk is in; a Storage Spaces virtual disk is no physical disk. - get_disk_smart: Get-StorageReliabilityCounter per disk (temperature, wear, power-on hours, uncorrected read and write errors). A counter that cannot be read is no counter, not an error; disks that report nothing are left out. - get_disk_pools: Storage Spaces' pools. - get_volumes: fixed volumes mounted at a drive letter or in a folder, with file system and health. A disk whose medium Windows cannot tell and whose counters are empty (a virtual disk, a RAID controller's logical disk) reads temperature -1 and health "unknown". Every script probes its cmdlets first. The fixtures are synthetic; the scripts are in harvest.py for the first real host.
318 lines
12 KiB
Python
318 lines
12 KiB
Python
"""Disks, pools, volumes and drive health on Windows, without smartctl (NetOrk/netork#2896).
|
|
|
|
Each reading is one script whose JSON a fake host answers from
|
|
tests/fixtures/synthetic/: ``disks_server.json`` (an NVMe SSD, two SATA disks in a
|
|
Storage Spaces pool, a RAID controller's logical disk and a Storage Spaces virtual
|
|
disk), ``disks_vm.json`` (a Hyper-V guest's virtual disk with empty reliability
|
|
counters), ``storage_pools.json`` and ``volumes.json``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import pytest
|
|
from napalm_device_types import HostStorageMixin, OSDriver
|
|
from napalm_device_types.roles import primary_role_of, role_keys_of
|
|
|
|
from napalm_windows import WindowsDriver
|
|
from napalm_windows import storage as mod
|
|
|
|
FIXTURES = Path(__file__).parent / "fixtures" / "synthetic"
|
|
|
|
|
|
def _load(name: str) -> Any:
|
|
return json.loads((FIXTURES / name).read_text())
|
|
|
|
|
|
class FakeHost:
|
|
"""Answers each storage script with *answers*' entry for it."""
|
|
|
|
def __init__(self, answers: dict[str, Any]) -> None:
|
|
self.answers = answers
|
|
self.sent: list[str] = []
|
|
self.is_open = True
|
|
|
|
def run(self, script: str) -> str:
|
|
self.sent.append(script)
|
|
answer = self.answers[script]
|
|
return answer if isinstance(answer, str) else json.dumps(answer)
|
|
|
|
def close(self) -> None:
|
|
self.is_open = False
|
|
|
|
|
|
def _driver(**answers: Any) -> WindowsDriver:
|
|
scripts = {
|
|
"disks": mod._PS_DISKS,
|
|
"pools": mod._PS_STORAGE_POOLS,
|
|
"volumes": mod._PS_VOLUMES,
|
|
}
|
|
d = WindowsDriver("win01", "admin", "secret")
|
|
d._transport = FakeHost({scripts[k]: v for k, v in answers.items()}) # type: ignore[assignment]
|
|
return d
|
|
|
|
|
|
def _server() -> WindowsDriver:
|
|
return _driver(
|
|
disks=_load("disks_server.json"),
|
|
pools=_load("storage_pools.json"),
|
|
volumes=_load("volumes.json"),
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The contract
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestContract:
|
|
def test_a_host_with_storage_stays_a_host(self):
|
|
assert issubclass(WindowsDriver, HostStorageMixin)
|
|
assert "storage" not in role_keys_of(WindowsDriver)
|
|
assert primary_role_of(WindowsDriver) == primary_role_of(OSDriver)
|
|
|
|
def test_every_getter_is_there(self):
|
|
for name in ("get_disks", "get_disk_pools", "get_volumes", "get_disk_smart"):
|
|
assert callable(getattr(WindowsDriver, name)), name
|
|
|
|
@pytest.mark.parametrize("script", ["_PS_DISKS", "_PS_STORAGE_POOLS", "_PS_VOLUMES"])
|
|
def test_scripts_follow_the_driver_conventions(self, script):
|
|
text = getattr(mod, script)
|
|
assert "$ErrorActionPreference = 'Stop'" in text
|
|
assert "ConvertTo-Json" in text
|
|
assert "Get-Command" in text # a missing cmdlet is "not supported", not an error
|
|
|
|
def test_counters_are_read_per_disk_and_may_fail(self):
|
|
"""Get-StorageReliabilityCounter needs an administrator and is missing on some
|
|
virtual disks: one disk's failure is that disk's missing counter."""
|
|
assert "try { $counter = $disk | Get-StorageReliabilityCounter }" in mod._PS_DISKS
|
|
assert "catch { $counter = $null }" in mod._PS_DISKS
|
|
|
|
def test_no_smartctl(self):
|
|
for script in (mod._PS_DISKS, mod._PS_STORAGE_POOLS, mod._PS_VOLUMES):
|
|
assert "smartctl" not in script
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get_disks
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestDisks:
|
|
def test_an_nvme_ssd(self):
|
|
disk = _server().get_disks()[0]
|
|
assert disk == {
|
|
"slot": "PhysicalDisk0",
|
|
"model": "Samsung SSD 980 PRO 1TB",
|
|
"serial": "0025_385A_11B2_2C33.",
|
|
"vendor": "",
|
|
"type": "nvme",
|
|
"size": 1000204886016,
|
|
"rpm": 0,
|
|
"temperature": 41,
|
|
"health": "healthy",
|
|
"pool": "",
|
|
}
|
|
|
|
def test_a_pool_member_with_a_warning(self):
|
|
disk = _server().get_disks()[1]
|
|
assert disk["slot"] == "PhysicalDisk1"
|
|
assert disk["serial"] == "WD-WCC7K1ABCDEF" # ATA serials come padded
|
|
assert (disk["type"], disk["rpm"], disk["temperature"]) == ("hdd", 5400, 36)
|
|
assert (disk["health"], disk["pool"]) == ("warning", "Pool1")
|
|
|
|
def test_a_zero_temperature_and_an_unknown_speed_are_unknown(self):
|
|
disk = _server().get_disks()[2]
|
|
assert (disk["temperature"], disk["rpm"]) == (-1, 0)
|
|
assert disk["health"] == "healthy" # a real disk with thin counters keeps its verdict
|
|
|
|
def test_a_raid_logical_disk_has_no_drive_health(self):
|
|
"""Windows calls the controller's logical disk Healthy; that is no drive's verdict."""
|
|
disk = _server().get_disks()[3]
|
|
assert (disk["type"], disk["temperature"], disk["health"]) == ("unknown", -1, "unknown")
|
|
|
|
def test_a_storage_spaces_virtual_disk_is_no_physical_disk(self):
|
|
slots = [d["slot"] for d in _server().get_disks()]
|
|
assert slots == ["PhysicalDisk0", "PhysicalDisk1", "PhysicalDisk3", "PhysicalDisk4"]
|
|
|
|
def test_a_virtual_disk_with_empty_counters(self):
|
|
"""Temperature -1 and health "unknown", not zeros and not a "healthy" nobody
|
|
measured."""
|
|
[disk] = _driver(disks=_load("disks_vm.json")).get_disks()
|
|
assert disk["temperature"] == -1
|
|
assert disk["health"] == "unknown"
|
|
assert disk["type"] == "unknown"
|
|
|
|
def test_bad_news_is_never_unknown(self):
|
|
raw = _load("disks_vm.json")
|
|
raw["disks"][0]["health"] = "Unhealthy"
|
|
assert _driver(disks=raw).get_disks()[0]["health"] == "failed"
|
|
|
|
def test_a_disk_without_a_number_is_named_by_its_id(self):
|
|
raw = _load("disks_vm.json")
|
|
raw["disks"][0]["device_id"] = ""
|
|
slot = _driver(disks=raw).get_disks()[0]["slot"]
|
|
assert slot == "60022480A1B2C3D4E5F60718293A4B5C"
|
|
|
|
def test_without_the_storage_module_there_are_none(self):
|
|
assert _driver(disks={"supported": False}).get_disks() == []
|
|
|
|
def test_an_empty_answer_could_not_look(self):
|
|
with pytest.raises(ValueError, match="disk reading came back empty"):
|
|
_driver(disks="").get_disks()
|
|
|
|
def test_one_disk_arrives_bare(self):
|
|
"""ConvertTo-Json unwraps a one-element array; the parse wraps it again."""
|
|
raw = _load("disks_vm.json")
|
|
raw["disks"] = raw["disks"][0]
|
|
assert len(_driver(disks=raw).get_disks()) == 1
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get_disk_smart
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestDiskSmart:
|
|
def test_the_reliability_counters_of_an_nvme_ssd(self):
|
|
smart = _server().get_disk_smart()
|
|
assert smart["PhysicalDisk0"] == {
|
|
"model": "Samsung SSD 980 PRO 1TB",
|
|
"serial": "0025_385A_11B2_2C33.",
|
|
"type": "nvme",
|
|
"size": 1000204886016,
|
|
"health": "ok",
|
|
"wearout": None,
|
|
"temperature": 41,
|
|
"percentage_used": 3,
|
|
"available_spare": None,
|
|
"reallocated_sectors": None,
|
|
"power_on_hours": 5120,
|
|
"temperature_max": 68,
|
|
"read_errors_uncorrected": 0,
|
|
"write_errors_uncorrected": 0,
|
|
}
|
|
|
|
def test_a_disk_with_errors_and_a_warning(self):
|
|
entry = _server().get_disk_smart()["PhysicalDisk1"]
|
|
assert entry["health"] == "warning"
|
|
assert entry["read_errors_uncorrected"] == 12
|
|
assert entry["percentage_used"] is None # a spinning disk does not wear out by writes
|
|
|
|
def test_thin_counters_are_none_where_empty(self):
|
|
entry = _server().get_disk_smart()["PhysicalDisk3"]
|
|
assert entry["temperature"] is None
|
|
assert entry["power_on_hours"] == 30990
|
|
assert entry["read_errors_uncorrected"] is None
|
|
|
|
def test_disks_that_report_nothing_are_left_out(self):
|
|
assert set(_server().get_disk_smart()) == {
|
|
"PhysicalDisk0",
|
|
"PhysicalDisk1",
|
|
"PhysicalDisk3",
|
|
}
|
|
|
|
def test_a_virtual_host_has_no_drive_health(self):
|
|
assert _driver(disks=_load("disks_vm.json")).get_disk_smart() == {}
|
|
|
|
def test_without_the_counter_cmdlet_there_is_none(self):
|
|
raw = _load("disks_server.json")
|
|
raw["counters"] = False
|
|
for disk in raw["disks"]:
|
|
disk["counter"] = None
|
|
assert _driver(disks=raw).get_disk_smart() == {}
|
|
|
|
def test_without_the_storage_module_there_is_none(self):
|
|
assert _driver(disks={"supported": False}).get_disk_smart() == {}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get_disk_pools
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestPools:
|
|
def test_a_degraded_mirror(self):
|
|
assert _server().get_disk_pools() == {
|
|
"Pool1": {
|
|
"name": "Pool1",
|
|
"type": "storage-spaces",
|
|
"level": "mirror",
|
|
"status": "degraded",
|
|
"total": 8001574060032,
|
|
"used": 2199023255552,
|
|
"available": 5802550804480,
|
|
"disks": ["PhysicalDisk1", "PhysicalDisk3"],
|
|
"auto_expand": False,
|
|
"dedup": False,
|
|
"compression": "off",
|
|
}
|
|
}
|
|
|
|
@pytest.mark.parametrize(
|
|
("health", "resiliency", "status", "level"),
|
|
[
|
|
("Healthy", "Simple", "online", "stripe"),
|
|
("Unhealthy", "Parity", "faulted", "parity"),
|
|
("Unknown", "", "unknown", "unknown"),
|
|
],
|
|
)
|
|
def test_status_and_level(self, health, resiliency, status, level):
|
|
raw = _load("storage_pools.json")
|
|
raw["pools"][0].update(health=health, resiliency=resiliency)
|
|
pool = _driver(pools=raw).get_disk_pools()["Pool1"]
|
|
assert (pool["status"], pool["level"]) == (status, level)
|
|
|
|
def test_no_pools(self):
|
|
assert _driver(pools={"supported": True, "pools": []}).get_disk_pools() == {}
|
|
|
|
def test_without_storage_spaces_there_are_none(self):
|
|
assert _driver(pools={"supported": False}).get_disk_pools() == {}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# get_volumes
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
class TestVolumes:
|
|
def test_the_system_drive(self):
|
|
assert _server().get_volumes()["C:"] == {
|
|
"name": "C:",
|
|
"pool": "",
|
|
"type": "filesystem",
|
|
"total": 999483936768,
|
|
"used": 287138238464,
|
|
"available": 712345698304,
|
|
"mountpoint": "C:\\",
|
|
"compression": "",
|
|
"dedup": False,
|
|
"readonly": False,
|
|
"snapshots": 0,
|
|
"file_system": "NTFS",
|
|
"health": "healthy",
|
|
}
|
|
|
|
def test_a_labelled_volume_with_a_warning(self):
|
|
volume = _server().get_volumes()["D:"]
|
|
assert (volume["name"], volume["file_system"]) == ("Data", "ReFS")
|
|
assert volume.get("health") == "warning"
|
|
|
|
def test_a_volume_mounted_in_a_folder(self):
|
|
volume = _server().get_volumes()["C:\\Logs"]
|
|
assert (volume["name"], volume["mountpoint"]) == ("Logs", "C:\\Logs\\")
|
|
|
|
def test_volumes_mounted_nowhere_are_left_out(self):
|
|
"""The EFI system partition and the recovery partition hold no one's data."""
|
|
assert list(_server().get_volumes()) == ["C:", "D:", "C:\\Logs"]
|
|
|
|
def test_without_the_storage_module_there_are_none(self):
|
|
assert _driver(volumes={"supported": False}).get_volumes() == {}
|
|
|
|
def test_an_empty_answer_could_not_look(self):
|
|
with pytest.raises(ValueError, match="volume reading came back empty"):
|
|
_driver(volumes="").get_volumes()
|