my-homelab-configs/tests/jeannie/test_registry_storage_mount.py

69 lines
3.0 KiB
Python

"""Storage prerequisites. Run on the Debian deployment host."""
import importlib.util
import json
from pathlib import Path
import unittest
from unittest import mock
SPEC = importlib.util.spec_from_file_location(
"registry_storage_mount", Path(__file__).resolve().parents[2] / "scripts/registry-storage-reconcile.py"
)
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
class RegistryStorageMountTests(unittest.TestCase):
def mount(self, target, options="rw,relatime"):
return lambda command: json.dumps({"filesystems": [{"target": target, "source": "/dev/example", "options": options}]})
def test_missing_data_mount_cannot_fall_back_to_root(self):
with self.assertRaisesRegex(RuntimeError, "refusing root-filesystem fallback"):
MODULE.require_remote_data_mount(self.mount("/"), "/data")
def test_read_only_data_disk_is_refused(self):
with self.assertRaisesRegex(RuntimeError, "not writable"):
MODULE.require_remote_data_mount(self.mount("/data", "ro,relatime"), "/data")
def test_writable_declared_mount_is_accepted(self):
MODULE.require_remote_data_mount(self.mount("/data"), "/data")
def test_profile_without_separate_mount_requires_no_probe(self):
def unexpected_probe(command):
self.fail("A profile without a declared mount must not require /data to be a separate disk")
MODULE.require_remote_data_mount(unexpected_probe, "")
def test_stale_read_only_mount_is_safe_to_recreate(self):
stale = {"source": "/dev/sda1[/openebs/local/registry]", "options": "ro,relatime"}
current = {"source": "/dev/sdb1", "options": "rw,relatime"}
self.assertTrue(MODULE.mount_is_stale_readonly(stale, current))
def test_writable_or_same_source_mount_is_not_auto_recreated(self):
current = {"source": "/dev/sdb1", "options": "rw,relatime"}
self.assertFalse(MODULE.mount_is_stale_readonly(current, current))
self.assertFalse(MODULE.mount_is_stale_readonly(
{"source": "/dev/sda1", "options": "rw,relatime"}, current
))
def test_recovery_deletes_only_exact_stale_read_only_pod(self):
pod = {"metadata": {"name": "local-registry-abc", "uid": "100112e5-ab6d-497a-8b96-f31c36749870"}}
mounts = [
{"source": "/dev/sda1[/openebs/local/registry]", "options": "ro,relatime"},
{"source": "/dev/sdb1", "options": "rw,relatime"},
]
with mock.patch.object(MODULE, "find_mount", side_effect=mounts), \
mock.patch.object(MODULE, "run") as run:
recovered = MODULE.recover_stale_readonly_registry_mount(
lambda command: "yes", ["kubectl"], pod,
"registry-data-pimox-worker-01", "/data/openebs/local/registry"
)
self.assertTrue(recovered)
self.assertEqual(run.call_count, 2)
self.assertIn("delete", run.call_args_list[0].args[0])
self.assertIn("rollout", run.call_args_list[1].args[0])
if __name__ == "__main__":
unittest.main()