"""Regression tests for evidence and safety gates; no host actions.""" import importlib.util import json from pathlib import Path import subprocess import tempfile import unittest from unittest import mock ROOT = Path(__file__).resolve().parents[2] SPEC = importlib.util.spec_from_file_location("doctor_k3s_host", ROOT / "scripts/doctor-k3s-storage-host.py") host = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(host) host.DATA_MOUNT = "/data" host.RUNTIME_PATH = "/data/jeannie/k3s-runtime" host.EXPECTED_CA = "a" * 64 host.STOP_SCRIPT = "stop-only" UUID = "19972338-9757-4f4b-89d7-c202df14a9aa" class StorageDoctorTests(unittest.TestCase): def test_stale_mount_is_distinguished_from_reappeared_partition(self): stale = {"target": "/data", "source": "/dev/sda1", "maj:min": "8:1", "options": "ro,relatime"} runtime = {"target": "/var/lib/rancher/k3s", "maj:min": "8:1", "options": "ro,relatime"} devices = [{"name": "sdb", "type": "disk", "maj:min": "8:16", "tran": "usb", "children": [ {"name": "sdb1", "type": "part", "maj:min": "8:17", "fstype": "ext4", "uuid": "uuid", "mountpoint": None}]}] with mock.patch.object(host, "mount", side_effect=[stale, runtime]), \ mock.patch.object(host, "disks", return_value=devices), \ mock.patch.object(host, "run", side_effect=["8:1\n8:2", "failed", "EXT4-fs error on sda1"]): result = host.inspect() self.assertTrue(result["stale_readonly_mount"]) self.assertEqual(result["reappeared_usb_ext4"][0]["maj:min"], "8:17") def test_candidate_mounted_elsewhere_is_not_selected(self): stale = {"maj:min": "8:1", "options": "ro,relatime"} devices = [{"name": "sdb", "type": "disk", "maj:min": "8:16", "tran": "usb", "children": [ {"name": "sdb1", "type": "part", "maj:min": "8:17", "fstype": "ext4", "uuid": "uuid", "mountpoint": None}]}] with mock.patch.object(host, "mount", side_effect=[stale, stale]), \ mock.patch.object(host, "disks", return_value=devices), \ mock.patch.object(host, "run", side_effect=["8:1\n8:17", "failed", ""]): result = host.inspect() self.assertEqual(result["reappeared_usb_ext4"], []) def test_ambiguous_or_running_service_never_changes_mounts(self): for evidence in ({"stale_readonly_mount": True, "service": "active", "reappeared_usb_ext4": [{"uuid": UUID}]}, {"stale_readonly_mount": True, "service": "failed", "reappeared_usb_ext4": [{"uuid": UUID}, {"uuid": "y"}]}, {"stale_readonly_mount": False, "service": "failed", "reappeared_usb_ext4": [{"uuid": UUID}]}): with self.subTest(evidence=evidence), mock.patch.object(host, "inspect", return_value=evidence), \ mock.patch.object(host, "run") as run: with self.assertRaises(RuntimeError): host.repair(UUID) run.assert_not_called() def test_wrong_ca_is_unmounted_before_any_service_stop(self): evidence = {"stale_readonly_mount": True, "service": "failed", "reappeared_usb_ext4": [{"uuid": UUID}]} with mock.patch.object(host, "inspect", return_value=evidence), \ mock.patch.object(host, "runtime_ca", return_value="b" * 64), \ mock.patch.object(host, "run", return_value="") as run: with self.assertRaisesRegex(RuntimeError, "pinned K3s CA"): host.repair(UUID) commands = [call.args[0] for call in run.call_args_list] self.assertEqual(commands[0][:3], ["mount", "-o", "ro,noload"]) self.assertEqual(commands[1][0], "umount") self.assertFalse(any(command[0] in ("bash", "systemctl") for command in commands)) def test_busy_data_mount_never_mounts_or_starts_k3s(self): evidence = {"stale_readonly_mount": True, "service": "failed", "reappeared_usb_ext4": [{"uuid": UUID}]} def run(command, **kwargs): if command == ["umount", "/data"]: raise RuntimeError("mount is busy") return "" with mock.patch.object(host, "inspect", return_value=evidence), \ mock.patch.object(host, "runtime_ca", return_value=host.EXPECTED_CA), \ mock.patch.object(host, "run", side_effect=run) as called: with self.assertRaisesRegex(RuntimeError, "busy"): host.repair(UUID) commands = [call.args[0] for call in called.call_args_list] self.assertFalse(any(command[:2] == ["systemctl", "start"] for command in commands)) self.assertFalse(any(command[0] == "mount" and command[-1] == "/data" for command in commands)) def test_doctor_narrative_explains_stale_mount_and_repair_command(self): with tempfile.TemporaryDirectory() as directory: rows = Path(directory) / "rows" rows.write_text("warn\tOrange Pi diagnostics\tK3s storage evidence\tStale read-only K3s mount; the mounted device is no longer detected\tdevice evidence\t\t\t\n") result = subprocess.run(["python3", str(ROOT / "scripts/doctor-narrative.py"), str(rows), "Doctor"], capture_output=True, text=True, check=True) self.assertIn("candidate, not yet verified", result.stdout) self.assertIn("doctor opi5 --repair-k3s", result.stdout) if __name__ == "__main__": unittest.main()