diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f72609..c3fdda7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,20 @@ All notable changes to this driver. The format follows - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver on 3.12, so 3.10 and 3.11 are no longer tested (NAPALM/driver-standard#6). +### Fixed +- `get_disks()` reports each disk's SMART verdict: `healthy` when smartctl's overall + self-assessment passed (SCSI/SAS: health status `OK`), `failed` when it failed or a + SCSI disk predicts a failure, `unknown` only when the disk gives none. The driver + asked smartctl without `-H`, which never prints the verdict: a disk read `unknown`, + and an ATA disk with an attribute table read `failed` from that table's + `WHEN_FAILED` column, whatever its state. +- `get_disks()` puts a disk in the md array only when the disk itself or one of its + partitions is a member: `sda` no longer joins the array of `sdaa1`, nor `nvme0n1` + that of `nvme0n11`. `/proc/mdstat` is read once per call instead of once per disk. +- `get_disks()` reports a SCSI/SAS disk's temperature (`Current Drive Temperature`); + it read `-1` for every SAS disk. A disk that prints `0 C` there, as QEMU's virtual + disks do, still reads `-1`. + ## [0.3.0] - 2026-10-23 The first release on PyPI. diff --git a/napalm_openmediavault/openmediavault.py b/napalm_openmediavault/openmediavault.py index e77871e..dc4b0d4 100644 --- a/napalm_openmediavault/openmediavault.py +++ b/napalm_openmediavault/openmediavault.py @@ -50,6 +50,71 @@ _HOSTNAME_MODULES = frozenset( _DIRTY_MODULES = "/var/lib/openmediavault/dirtymodules.json" +#: Identity, overall health verdict and attributes; ``-H`` alone prints the verdict. +_SMARTCTL = "smartctl -iHA /dev/{name} 2>/dev/null" +#: The overall verdict of an ATA or NVMe disk: ``PASSED`` or ``FAILED!``. +_SMART_VERDICT_RE = re.compile(r"^SMART overall-health self-assessment test result:\s*(\S+)", re.M) +#: A SCSI/SAS disk's: ``OK``, or the failure it predicts. +_SMART_SCSI_STATUS_RE = re.compile(r"^SMART Health Status:\s*(.+?)\s*$", re.M) +_SMART_VERDICTS = {"PASSED": "healthy", "FAILED": "failed"} +#: An ATA disk's temperature attribute, an NVMe disk's ``Temperature:`` line. +_SMART_TEMP_RE = re.compile( + r"(?:Temperature_Celsius|Current Temperature|Temperature:)[^\n]*?(\d+)\s*(?:Celsius|\(|$)" +) +#: A SCSI/SAS disk's; a QEMU disk without a sensor prints ``0 C``. +_SMART_SCSI_TEMP_RE = re.compile(r"^Current Drive Temperature:\s+(\d+) C\s*$", re.M) +#: An md array's line in /proc/mdstat, and its members: ``sda1[0]``, ``sdc1[1](F)``. +_MDSTAT_ARRAY_RE = re.compile(r"^(md\d+)\s*:(.*)$", re.M) +_MDSTAT_MEMBER_RE = re.compile(r"(\S+?)\[\d+\]") + + +def smart_health(output: str) -> str: + """The overall SMART verdict in ``smartctl -H`` output. + + ``"healthy"`` or ``"failed"``; ``"unknown"`` when smartctl printed none (no + SMART support, a USB bridge without pass-through). + """ + verdict = _SMART_VERDICT_RE.search(output) + if verdict: + return _SMART_VERDICTS.get(verdict.group(1).rstrip("!"), "unknown") + status = _SMART_SCSI_STATUS_RE.search(output) + if status: + return "healthy" if status.group(1) == "OK" else "failed" + return "unknown" + + +def smart_temperature(output: str) -> int: + """A disk's temperature in Celsius from ``smartctl -A`` output; ``-1`` if none.""" + found = _SMART_TEMP_RE.search(output) + if found: + return int(found.group(1)) + scsi = _SMART_SCSI_TEMP_RE.search(output) + if scsi and int(scsi.group(1)) > 0: + return int(scsi.group(1)) + return -1 + + +def md_members(mdstat: str) -> dict[str, str]: + """Each member device in ``/proc/mdstat``, mapped to its array: ``{"sda1": "md1"}``.""" + return { + member: array + for array, rest in _MDSTAT_ARRAY_RE.findall(mdstat) + for member in _MDSTAT_MEMBER_RE.findall(rest) + } + + +def _pool_of(disk: str, members: dict[str, str]) -> str: + """The array *disk* or one of its partitions is a member of, or ``""``. + + Linux names a partition by appending its number, after a ``p`` when the disk's + name ends in a digit: ``sda`` owns ``sda1``, ``nvme0n1`` owns ``nvme0n1p1``; + ``sdaa1`` is not ``sda``'s, nor ``nvme0n11`` (another namespace) ``nvme0n1``'s. + """ + partition = r"p\d+" if disk[-1:].isdigit() else r"\d+" + own = re.compile(rf"{re.escape(disk)}(?:{partition})?") + return next((array for member, array in members.items() if own.fullmatch(member)), "") + + class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): """NAPALM driver for OpenMediaVault NAS systems. @@ -157,8 +222,9 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): def get_disks(self) -> list[PhysicalDiskDict]: """Return physical disk inventory. - Uses ``lsblk -J`` for device enumeration and ``smartctl -iA`` - (via sudo) for health, temperature, and RPM data. + Uses ``lsblk -J`` for device enumeration, ``smartctl -iHA`` (via sudo) + for health, temperature, and RPM data, and ``/proc/mdstat``, read once, + for array membership. Each entry contains: @@ -200,6 +266,7 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): except Exception: return result + members = self._md_members() for dev in lsblk.get("blockdevices", []): if dev.get("type") != "disk": continue @@ -221,29 +288,7 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): else: disk_type = "hdd" - temperature = -1 - health = "unknown" - rpm = 0 - - try: - smart = self._sudo(f"smartctl -iA /dev/{name} 2>/dev/null", read_timeout=15) - if "PASSED" in smart: - health = "healthy" - elif "FAILED" in smart: - health = "failed" - temp_m = re.search( - r"(?:Temperature_Celsius|Current Temperature|Temperature:)" - r"[^\n]*?(\d+)\s*(?:Celsius|\(|$)", - smart, - ) - if temp_m: - temperature = int(temp_m.group(1)) - if disk_type == "hdd": - rpm_m = re.search(r"Rotation Rate:\s+(\d+)\s+rpm", smart) - if rpm_m: - rpm = int(rpm_m.group(1)) - except Exception: - logger.debug("%s: smartctl for %s: ignored", self.hostname, name, exc_info=True) + health, temperature, rpm = self._smart(name, disk_type) result.append( PhysicalDiskDict( @@ -256,22 +301,34 @@ class OpenMediaVaultDriver(OmvLocalAccountsMixin, StorageDriver, LinuxDriver): rpm=rpm, temperature=temperature, health=health, - pool=self._disk_to_pool(name), + pool=_pool_of(name, members), ) ) return result - def _disk_to_pool(self, disk_name: str) -> str: - """Return the md array a disk belongs to, or empty string.""" + def _smart(self, name: str, disk_type: str) -> tuple[str, int, int]: + """A disk's SMART health, temperature (``-1`` if unknown) and rpm (``0``).""" + health, temperature, rpm = "unknown", -1, 0 try: - mdstat = self._send("cat /proc/mdstat 2>/dev/null") - for line in mdstat.splitlines(): - if disk_name in line and re.match(r"^md\d+", line): - return line.split(":")[0].strip() + smart = self._sudo(_SMARTCTL.format(name=name), read_timeout=15) + health = smart_health(smart) + temperature = smart_temperature(smart) + if disk_type == "hdd": + rpm_m = re.search(r"Rotation Rate:\s+(\d+)\s+rpm", smart) + if rpm_m: + rpm = int(rpm_m.group(1)) + except Exception: + logger.debug("%s: smartctl for %s: ignored", self.hostname, name, exc_info=True) + return health, temperature, rpm + + def _md_members(self) -> dict[str, str]: + """The md array of each member device, from one read of ``/proc/mdstat``.""" + try: + return md_members(self._send("cat /proc/mdstat 2>/dev/null")) except Exception: logger.debug("%s: /proc/mdstat not read: ignored", self.hostname, exc_info=True) - return "" + return {} # ── md RAID pools ────────────────────────────────────────────────────────── diff --git a/tests/fixtures/mdstat-omv-no-arrays.txt b/tests/fixtures/mdstat-omv-no-arrays.txt new file mode 100644 index 0000000..aa58307 --- /dev/null +++ b/tests/fixtures/mdstat-omv-no-arrays.txt @@ -0,0 +1,2 @@ +Personalities : [raid0] [raid1] [raid4] [raid5] [raid6] [raid10] +unused devices: diff --git a/tests/fixtures/smartctl-7.4-scsi-qemu-harddisk-unavailable.txt b/tests/fixtures/smartctl-7.4-scsi-qemu-harddisk-unavailable.txt new file mode 100644 index 0000000..01f1762 --- /dev/null +++ b/tests/fixtures/smartctl-7.4-scsi-qemu-harddisk-unavailable.txt @@ -0,0 +1,20 @@ +smartctl 7.4 2023-08-01 r5530 [x86_64-linux-7.2.11+deb13-amd64] (local build) +Copyright (C) 2002-23, Bruce Allen, Christian Franke, www.smartmontools.org + +=== START OF INFORMATION SECTION === +Vendor: QEMU +Product: QEMU HARDDISK +Revision: 2.5+ +Compliance: SPC-3 +User Capacity: 34,359,738,368 bytes [34.3 GB] +Logical block size: 512 bytes +LU is thin provisioned, LBPRZ=0 +Device type: disk +Local Time is: Sun Nov 1 21:08:12 2026 UTC +SMART support is: Unavailable - device lacks SMART capability. + +=== START OF READ SMART DATA SECTION === +Current Drive Temperature: 0 C +Drive Trip Temperature: 0 C + + diff --git a/tests/fixtures/smartctl-7.5-ata-supermicro-superdom-SOB20R-passed.txt b/tests/fixtures/smartctl-7.5-ata-supermicro-superdom-SOB20R-passed.txt new file mode 100644 index 0000000..3117c11 --- /dev/null +++ b/tests/fixtures/smartctl-7.5-ata-supermicro-superdom-SOB20R-passed.txt @@ -0,0 +1,55 @@ +smartctl 7.5 2025-04-30 r5714 [x86_64-linux-7.0.14-26-pve] (local build) +Copyright (C) 2002-25, Bruce Allen, Christian Franke, www.smartmontools.org + +=== START OF INFORMATION SECTION === +Model Family: Supermicro SATA DOM (SuperDOM) +Device Model: SuperMicro SSD +Serial Number: FAKESERIAL0000000001 +LU WWN Device Id: 5 000000 000000001 +Firmware Version: SOB20R +User Capacity: 15,837,691,904 bytes [15.8 GB] +Sector Size: 512 bytes logical/physical +Rotation Rate: Solid State Device +TRIM Command: Available, deterministic, zeroed +Device is: In smartctl database 7.5/5706 +ATA Version is: ACS-2 (minor revision not indicated) +SATA Version is: SATA 3.1, 6.0 Gb/s (current: 6.0 Gb/s) +Local Time is: Sun Nov 1 22:09:52 2026 CET +SMART support is: Available - device has SMART capability. +SMART support is: Enabled + +=== START OF READ SMART DATA SECTION === +SMART overall-health self-assessment test result: PASSED + +SMART Attributes Data Structure revision number: 1 +Vendor Specific SMART Attributes with Thresholds: +ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE + 1 Raw_Read_Error_Rate 0x0000 100 100 000 Old_age Offline - 0 + 5 Reallocated_Sector_Ct 0x0000 100 100 000 Old_age Offline - 0 + 9 Power_On_Hours 0x0000 100 100 000 Old_age Offline - 18373 + 12 Power_Cycle_Count 0x0000 100 100 000 Old_age Offline - 54 + 15 User_Cpcty_Sector_Cnt 0x0000 100 100 000 Old_age Offline - 30932992 +160 Not_In_Use 0x0000 100 100 000 Old_age Offline - 0 +161 Not_In_Use 0x0000 100 100 000 Old_age Offline - 160 +163 Not_In_Use 0x0000 100 100 000 Old_age Offline - 6 +164 Not_In_Use 0x0000 100 100 000 Old_age Offline - 661737 +165 Not_In_Use 0x0000 100 100 000 Old_age Offline - 363 +166 Minimum_PE_Cycles_TLC 0x0000 100 100 000 Old_age Offline - 262 +167 Not_In_Use 0x0000 100 100 000 Old_age Offline - 316 +168 Maximum_PE_Cycles_TLC 0x0000 100 100 000 Old_age Offline - 3000 +172 Unknown_Attribute 0x0000 100 100 000 Old_age Offline - 0 +175 Program_Fail_Count_Chip 0x0000 100 100 000 Old_age Offline - 6 +177 Wear_Leveling_Count 0x0000 100 100 000 Old_age Offline - 3855 +181 Program_Fail_Cnt_Total 0x0000 100 100 000 Old_age Offline - 0 +187 Reported_Uncorrect 0x0000 100 100 000 Old_age Offline - 0 +192 Power-Off_Retract_Count 0x0000 100 100 000 Old_age Offline - 12 +194 Temperature_Celsius 0x0000 100 100 000 Old_age Offline - 34 (Min/Max 1/58) +195 Hardware_ECC_Recovered 0x0000 100 100 000 Old_age Offline - 75 +197 Current_Pending_Sector 0x0000 100 100 000 Old_age Offline - 0 +198 Offline_Uncorrectable 0x0000 100 100 000 Old_age Offline - 0 +199 UDMA_CRC_Error_Count 0x0000 100 100 000 Old_age Offline - 0 +231 SSD_Life_Left 0x0000 100 100 000 Old_age Offline - 100 +232 Available_Reservd_Space 0x0000 100 100 000 Old_age Offline - 90 +233 NAND_Writes_1GiB 0x0000 100 100 000 Old_age Offline - 5169 +241 Lifetime_Writes_GiB 0x0000 100 100 000 Old_age Offline - 2361 +242 Lifetime_Reads_GiB 0x0000 100 100 000 Old_age Offline - 2579 diff --git a/tests/fixtures/smartctl-7.5-nvme-crucial-CT500P5PSSD8-P7CR402-passed.txt b/tests/fixtures/smartctl-7.5-nvme-crucial-CT500P5PSSD8-P7CR402-passed.txt new file mode 100644 index 0000000..a6adbf1 --- /dev/null +++ b/tests/fixtures/smartctl-7.5-nvme-crucial-CT500P5PSSD8-P7CR402-passed.txt @@ -0,0 +1,43 @@ +smartctl 7.5 2025-04-30 r5714 [x86_64-linux-7.0.14-26-pve] (local build) +Copyright (C) 2002-25, Bruce Allen, Christian Franke, www.smartmontools.org + +=== START OF INFORMATION SECTION === +Model Number: CT500P5PSSD8 +Serial Number: FAKESERIAL03 +Firmware Version: P7CR402 +PCI Vendor/Subsystem ID: 0xc0a9 +IEEE OUI Identifier: 0x00a075 +Total NVM Capacity: 500,107,862,016 [500 GB] +Unallocated NVM Capacity: 0 +Controller ID: 0 +NVMe Version: 1.4 +Number of Namespaces: 1 +Namespace 1 Size/Capacity: 500,107,862,016 [500 GB] +Namespace 1 Formatted LBA Size: 512 +Namespace 1 IEEE EUI-64: 00a075 0000000003 +Local Time is: Sun Nov 1 22:10:17 2026 CET + +=== START OF SMART DATA SECTION === +SMART overall-health self-assessment test result: PASSED + +SMART/Health Information (NVMe Log 0x02, NSID 0x1) +Critical Warning: 0x00 +Temperature: 40 Celsius +Available Spare: 100% +Available Spare Threshold: 5% +Percentage Used: 5% +Data Units Read: 5,484,072 [2.80 TB] +Data Units Written: 32,786,179 [16.7 TB] +Host Read Commands: 129,533,249 +Host Write Commands: 609,881,286 +Controller Busy Time: 960 +Power Cycles: 101 +Power On Hours: 998 +Unsafe Shutdowns: 22 +Media and Data Integrity Errors: 0 +Error Information Log Entries: 85 +Warning Comp. Temperature Time: 0 +Critical Comp. Temperature Time: 0 +Temperature Sensor 1: 40 Celsius +Thermal Temp. 1 Transition Count: 1 +Thermal Temp. 1 Total Time: 69 diff --git a/tests/fixtures/smartctl-7.5-sas-hgst-HSCAC2DA4SUN400G-A29A-ok.txt b/tests/fixtures/smartctl-7.5-sas-hgst-HSCAC2DA4SUN400G-A29A-ok.txt new file mode 100644 index 0000000..2bb3faf --- /dev/null +++ b/tests/fixtures/smartctl-7.5-sas-hgst-HSCAC2DA4SUN400G-A29A-ok.txt @@ -0,0 +1,39 @@ +smartctl 7.5 2025-04-30 r5714 [x86_64-linux-7.0.14-26-pve] (local build) +Copyright (C) 2002-25, Bruce Allen, Christian Franke, www.smartmontools.org + +=== START OF INFORMATION SECTION === +Vendor: HGST +Product: HSCAC2DA4SUN400G +Revision: A29A +Compliance: SPC-4 +User Capacity: 399,421,472,768 bytes [399 GB] +Logical block size: 512 bytes +LU is resource provisioned, LBPRZ=1 +Rotation Rate: Solid State Device +Form Factor: 2.5 inches +Logical Unit id: 0x5000000000000002 +Serial number: FAKESERIAL02 FAKESN02 +Device type: disk +Transport protocol: SAS (SPL-4) +Local Time is: Sun Nov 1 22:09:52 2026 CET +SMART support is: Available - device has SMART capability. +SMART support is: Enabled +Temperature Warning: Enabled + +=== START OF READ SMART DATA SECTION === +SMART Health Status: OK + +Percentage used endurance indicator: 3% +Current Drive Temperature: 28 C +Drive Trip Temperature: 70 C + +Accumulated power on time, hours:minutes 54545:19 +Manufactured in week 09 of year 2015 +Specified cycle count over device lifetime: 0 +Accumulated start-stop cycles: 0 +Specified load-unload count over device lifetime: 0 +Accumulated load-unload cycles: 0 +Elements in grown defect list: 0 + +Vendor (Seagate Cache) information + Blocks sent to initiator = 674671535507636224 diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 9f2e60f..6af16f8 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -1,11 +1,11 @@ { "coverage_floor": 15.0, "function_complexity": { - "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 18 + "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 12 }, "function_lines": { "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disk_pools": 81, - "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 107, + "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_disks": 87, "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_storage_services": 58, "napalm_openmediavault/openmediavault.py::OpenMediaVaultDriver.get_volumes": 90 }, diff --git a/tests/test_disks.py b/tests/test_disks.py new file mode 100644 index 0000000..2c43b40 --- /dev/null +++ b/tests/test_disks.py @@ -0,0 +1,242 @@ +"""get_disks: the overall SMART verdict, and the md array each disk belongs to. + +smartctl prints the overall verdict only for ``-H``; ``-i`` (identity) and ``-A`` +(attributes) never do. The fake host below answers smartctl with a recorded +``smartctl -iHA`` output and drops the verdict line when the command does not ask +for ``-H``. + +The recordings in ``fixtures/`` are smartmontools 7.5 on Proxmox VE (an ATA SATA DOM, +a SAS SSD, an NVMe SSD) and 7.4 on an OMV VM (a QEMU disk without SMART), with serial +numbers, WWNs and LU ids replaced. +""" + +from __future__ import annotations + +import json +import re +from pathlib import Path + +import pytest + +from napalm_openmediavault.openmediavault import OpenMediaVaultDriver, smart_health + +FIXTURES = Path(__file__).parent / "fixtures" + + +def _recorded(name: str) -> str: + return (FIXTURES / name).read_text(encoding="utf-8") + + +def _replaced(recording: str, line: str, by: str) -> str: + """The recording with one of its lines replaced; the line must be there.""" + assert recording.count(line) == 1, line + return recording.replace(line, by) + + +ATA_PASSED = _recorded("smartctl-7.5-ata-supermicro-superdom-SOB20R-passed.txt") +NVME_PASSED = _recorded("smartctl-7.5-nvme-crucial-CT500P5PSSD8-P7CR402-passed.txt") +SAS_OK = _recorded("smartctl-7.5-sas-hgst-HSCAC2DA4SUN400G-A29A-ok.txt") +QEMU_NO_SMART = _recorded("smartctl-7.4-scsi-qemu-harddisk-unavailable.txt") + +_ATA_VERDICT = "SMART overall-health self-assessment test result: PASSED\n" +# No failing disk was at hand: the failing verdicts are smartmontools' wording put in +# place of a recorded verdict line. +ATA_FAILED = _replaced( + ATA_PASSED, + _ATA_VERDICT, + "SMART overall-health self-assessment test result: FAILED!\n" + "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n", +) +SAS_PREDICTED = _replaced( + SAS_OK, + "SMART Health Status: OK\n", + "SMART Health Status: FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS [asc=5d, ascq=64]\n", +) +#: What ``smartctl -iA`` prints: no verdict, but the attribute table's WHEN_FAILED header. +ATA_WITHOUT_VERDICT = _replaced(ATA_PASSED, _ATA_VERDICT, "") + +_VERDICT_LINE_RE = re.compile( + r"^SMART (?:overall-health self-assessment test result|Health Status):.*\n", re.M +) + + +def smartctl(flags: str, recording: str) -> str: + """What ``smartctl -`` prints: the recording, its verdict only for ``-H``.""" + return recording if "H" in flags else _VERDICT_LINE_RE.sub("", recording) + + +class Nas: + """The NAS as ``_send`` and ``_sudo`` see it: lsblk, /proc/mdstat and smartctl.""" + + def __init__(self, disks: dict[str, tuple[str, str]], mdstat: str = "") -> None: + #: name -> (tran, recorded ``smartctl -iHA`` output) + self.disks = disks + self.mdstat = mdstat + self.commands: list[str] = [] + + def send(self, command: str, read_timeout: float = 100) -> str: + self.commands.append(command) + if command.startswith("lsblk "): + return json.dumps( + { + "blockdevices": [ + { + "name": name, + "size": 6001175126016, + "type": "disk", + "rota": tran != "nvme" and "Solid State Device" not in recording, + "vendor": "", + "model": "m", + "serial": "s", + "tran": tran, + } + for name, (tran, recording) in self.disks.items() + ] + } + ) + if command.startswith("cat /proc/mdstat"): + return self.mdstat + raise AssertionError(command) + + def sudo(self, command: str, read_timeout: float = 100) -> str: + self.commands.append(command) + m = re.match(r"smartctl -(\w+) /dev/(\w+)", command) + assert m, command + _tran, recording = self.disks[m.group(2)] + return smartctl(m.group(1), recording) + + +def _driver(nas: Nas) -> OpenMediaVaultDriver: + driver = OpenMediaVaultDriver("nas", "admin", "p") + driver._send = nas.send # type: ignore[method-assign] + driver._sudo = nas.sudo # type: ignore[method-assign] + return driver + + +def _by_slot(nas: Nas) -> dict[str, dict]: + return {d["slot"]: dict(d) for d in _driver(nas).get_disks()} + + +# ── the overall verdict ───────────────────────────────────────────────────────── + + +def test_a_passing_ata_disk_is_healthy(): + nas = Nas({"sda": ("sata", ATA_PASSED)}) + + disk = _by_slot(nas)["/dev/sda"] + + assert disk["health"] == "healthy" + # A solid-state disk has no rotation rate. + assert (disk["temperature"], disk["rpm"]) == (34, 0) + + +def test_a_failing_ata_disk_is_failed(): + nas = Nas({"sda": ("sata", ATA_FAILED)}) + + assert _by_slot(nas)["/dev/sda"]["health"] == "failed" + + +def test_a_passing_nvme_disk_is_healthy(): + nas = Nas({"nvme0n1": ("nvme", NVME_PASSED)}) + + disk = _by_slot(nas)["/dev/nvme0n1"] + + assert (disk["health"], disk["temperature"]) == ("healthy", 40) + + +def test_a_sas_disk_reports_its_drive_temperature(): + nas = Nas({"sdb": ("", SAS_OK)}) + + disk = _by_slot(nas)["/dev/sdb"] + + assert (disk["health"], disk["temperature"]) == ("healthy", 28) + + +def test_a_disk_without_smart_has_no_temperature(): + # QEMU prints "Current Drive Temperature: 0 C" for a disk that has no sensor. + nas = Nas({"sda": ("", QEMU_NO_SMART)}) + + disk = _by_slot(nas)["/dev/sda"] + + assert (disk["health"], disk["temperature"]) == ("unknown", -1) + + +@pytest.mark.parametrize( + ("output", "health"), + [ + (ATA_PASSED, "healthy"), + (ATA_FAILED, "failed"), + (NVME_PASSED, "healthy"), + (SAS_OK, "healthy"), + (SAS_PREDICTED, "failed"), + (QEMU_NO_SMART, "unknown"), + ("", "unknown"), + # The attribute table's WHEN_FAILED column is no verdict. + (ATA_WITHOUT_VERDICT, "unknown"), + ], + ids=[ + "ata-passed", + "ata-failed", + "nvme-passed", + "sas-ok", + "sas-predicted", + "no-smart", + "empty", + "when-failed-column", + ], +) +def test_smart_health_reads_the_verdict_line_only(output, health): + assert smart_health(output) == health + + +# ── md membership ─────────────────────────────────────────────────────────────── + +# The recorded hosts have no md array (MDSTAT_NO_ARRAYS), so the arrays below are +# written out by hand to cover look-alike names and a failed member. +MDSTAT = """\ +Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10] +md0 : active raid1 sdab1[1] sdaa1[0] + 976630464 blocks super 1.2 [2/2] [UU] + +md1 : active raid1 sdb1[1] sdc1[0](F) + 5860389888 blocks super 1.2 [2/1] [U_] + +md2 : active raid1 nvme0n1p1[0] nvme1n1[1] + 976630464 blocks super 1.2 [2/2] [UU] + +md3 : active raid1 nvme2n11[0] nvme2n12[1] + 976630464 blocks super 1.2 [2/2] [UU] + +unused devices: +""" +MDSTAT_NO_ARRAYS = _recorded("mdstat-omv-no-arrays.txt") + + +def test_a_disk_is_in_the_array_its_partition_or_itself_is_in(): + names = ["sda", "sdaa", "sdc", "nvme0n1", "nvme1n1", "nvme2n1"] + nas = Nas({n: ("sata", ATA_PASSED) for n in names}, MDSTAT) + + pools = {slot: d["pool"] for slot, d in _by_slot(nas).items()} + + assert pools == { + "/dev/sda": "", # sdaa1 and sdab1 are not sda's + "/dev/sdaa": "md0", + "/dev/sdc": "md1", # a failed member is still a member + "/dev/nvme0n1": "md2", + "/dev/nvme1n1": "md2", + "/dev/nvme2n1": "", # nvme2n11 is another namespace, not a partition + } + + +def test_no_disk_is_in_an_array_when_there_is_none(): + nas = Nas({n: ("", QEMU_NO_SMART) for n in ("sda", "sdb")}, MDSTAT_NO_ARRAYS) + + assert {d["pool"] for d in _by_slot(nas).values()} == {""} + + +def test_mdstat_is_read_once_for_all_disks(): + nas = Nas({n: ("sata", ATA_PASSED) for n in ("sda", "sdb")}, MDSTAT) + + _driver(nas).get_disks() + + assert sum(c.startswith("cat /proc/mdstat") for c in nas.commands) == 1