Files
napalm-openmediavault/tests/test_disks.py
T
Christian Manivong c8f4a0b1f7 test(disks): use recorded smartctl output
The get_disks tests ran against hand-written smartctl sections. They now
read smartctl -iHA as recorded on real hosts: an ATA SATA DOM and an
NVMe SSD (PASSED), a SAS SSD (OK) and a QEMU disk without SMART, with
serial numbers, WWNs and LU ids replaced. The failing verdicts replace
the verdict line of a recording, as no failing disk was at hand. A
recorded /proc/mdstat without arrays is added too.
2026-11-01 22:14:40 +01:00

226 lines
7.6 KiB
Python

"""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 -<flags>`` 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)
@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: <none>
"""
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