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/test_disks.py b/tests/test_disks.py index 924c656..2df6971 100644 --- a/tests/test_disks.py +++ b/tests/test_disks.py @@ -1,92 +1,75 @@ """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 the way it builds its -output: one section per flag asked for, so a command without ``-H`` gets no verdict. +(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 sections are in the form smartmontools 7.3 prints them (ATA, NVMe and SCSI), -cut to the lines that matter here; they are not recorded from a device. +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 -_BANNER = ( - "smartctl 7.3 2022-02-28 r5338 [x86_64-linux-6.1.0-26-amd64] (local build)\n" - "Copyright (C) 2002-22, Bruce Allen, Christian Franke, www.smartmontools.org\n" -) +FIXTURES = Path(__file__).parent / "fixtures" -ATA_INFO = """\ -=== START OF INFORMATION SECTION === -Model Family: HGST Ultrastar 7K6 -Device Model: HGST HUS726T6TALE6L4 -Serial Number: K1AAAAAA -Rotation Rate: 7200 rpm -SMART support is: Available - device has SMART capability. -SMART support is: Enabled -""" -ATA_PASSED = "SMART overall-health self-assessment test result: PASSED\n" -ATA_FAILED = ( + +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" + "Drive failure expected in less than 24 hours. SAVE ALL DATA.\n", ) -ATA_ATTRIBUTES = """\ -SMART Attributes Data Structure revision number: 16 -Vendor Specific SMART Attributes with Thresholds: -ID# ATTRIBUTE_NAME FLAG VALUE WORST THRESH TYPE UPDATED WHEN_FAILED RAW_VALUE - 1 Raw_Read_Error_Rate 0x000b 100 100 016 Pre-fail Always - 0 -194 Temperature_Celsius 0x0002 176 176 000 Old_age Always - 34 (Min/Max 20/45) -""" - -NVME_INFO = """\ -=== START OF INFORMATION SECTION === -Model Number: Samsung SSD 980 PRO 1TB -Serial Number: S5AAAAAAAAAAAAA -""" -NVME_PASSED = "SMART overall-health self-assessment test result: PASSED\n" -NVME_ATTRIBUTES = """\ -SMART/Health Information (NVMe Log 0x02) -Critical Warning: 0x00 -Temperature: 41 Celsius -""" - -SCSI_OK = "SMART Health Status: OK\n" -SCSI_PREDICTED = ( - "SMART Health Status: FIRMWARE IMPENDING FAILURE TOO MANY BLOCK REASSIGNS [asc=5d, ascq=64]\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, "") -USB_NO_SMART = ( - "SMART support is: Unavailable - device lacks SMART capability.\n" - "\n" - "A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n" +_VERDICT_LINE_RE = re.compile( + r"^SMART (?:overall-health self-assessment test result|Health Status):.*\n", re.M ) -def smartctl(flags: str, info: str, health: str, attributes: str) -> str: - """What ``smartctl -`` prints: the banner, then one section per flag.""" - out = _BANNER + "\n" - if "i" in flags: - out += info + "\n" - if "H" in flags or "A" in flags: - out += "=== START OF READ SMART DATA SECTION ===\n" - if "H" in flags: - out += health + "\n" - if "A" in flags: - out += attributes - return out +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, str, str]], mdstat: str = "") -> None: - #: name -> (tran, info, health, attributes) + 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] = [] @@ -101,13 +84,13 @@ class Nas: "name": name, "size": 6001175126016, "type": "disk", - "rota": tran != "nvme", + "rota": tran != "nvme" and "Solid State Device" not in recording, "vendor": "", "model": "m", "serial": "s", "tran": tran, } - for name, (tran, *_rest) in self.disks.items() + for name, (tran, recording) in self.disks.items() ] } ) @@ -119,8 +102,8 @@ class Nas: self.commands.append(command) m = re.match(r"smartctl -(\w+) /dev/(\w+)", command) assert m, command - _tran, info, health, attributes = self.disks[m.group(2)] - return smartctl(m.group(1), info, health, attributes) + _tran, recording = self.disks[m.group(2)] + return smartctl(m.group(1), recording) def _driver(nas: Nas) -> OpenMediaVaultDriver: @@ -138,26 +121,27 @@ def _by_slot(nas: Nas) -> dict[str, dict]: def test_a_passing_ata_disk_is_healthy(): - nas = Nas({"sda": ("sata", ATA_INFO, ATA_PASSED, ATA_ATTRIBUTES)}) + nas = Nas({"sda": ("sata", ATA_PASSED)}) disk = _by_slot(nas)["/dev/sda"] assert disk["health"] == "healthy" - assert (disk["temperature"], disk["rpm"]) == (34, 7200) + # 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_INFO, ATA_FAILED, ATA_ATTRIBUTES)}) + 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_INFO, NVME_PASSED, NVME_ATTRIBUTES)}) + nas = Nas({"nvme0n1": ("nvme", NVME_PASSED)}) disk = _by_slot(nas)["/dev/nvme0n1"] - assert (disk["health"], disk["temperature"]) == ("healthy", 41) + assert (disk["health"], disk["temperature"]) == ("healthy", 40) @pytest.mark.parametrize( @@ -166,30 +150,32 @@ def test_a_passing_nvme_disk_is_healthy(): (ATA_PASSED, "healthy"), (ATA_FAILED, "failed"), (NVME_PASSED, "healthy"), - (SCSI_OK, "healthy"), - (SCSI_PREDICTED, "failed"), - (USB_NO_SMART, "unknown"), + (SAS_OK, "healthy"), + (SAS_PREDICTED, "failed"), + (QEMU_NO_SMART, "unknown"), ("", "unknown"), # The attribute table's WHEN_FAILED column is no verdict. - (ATA_ATTRIBUTES, "unknown"), + (ATA_WITHOUT_VERDICT, "unknown"), ], ids=[ "ata-passed", "ata-failed", "nvme-passed", - "scsi-ok", - "scsi-predicted", + "sas-ok", + "sas-predicted", "no-smart", "empty", "when-failed-column", ], ) def test_smart_health_reads_the_verdict_line_only(output, health): - assert smart_health(_BANNER + 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] @@ -206,11 +192,12 @@ md3 : active raid1 nvme2n11[0] nvme2n12[1] 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_INFO, ATA_PASSED, ATA_ATTRIBUTES) for n in names}, MDSTAT) + nas = Nas({n: ("sata", ATA_PASSED) for n in names}, MDSTAT) pools = {slot: d["pool"] for slot, d in _by_slot(nas).items()} @@ -224,8 +211,14 @@ def test_a_disk_is_in_the_array_its_partition_or_itself_is_in(): } +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_INFO, ATA_PASSED, ATA_ATTRIBUTES) for n in ("sda", "sdb")}, MDSTAT) + nas = Nas({n: ("sata", ATA_PASSED) for n in ("sda", "sdb")}, MDSTAT) _driver(nas).get_disks()