mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-30 06:46:31 +00:00
svc-bkp-nfs-2-local snapshots the export base (/srv/nfs), so a
generation's files/ root already holds infinito-state/. Without an
explicit target, `recover nfs` and `recover full` restored it onto
/srv/nfs/infinito-state: the tree came out as
infinito-state/infinito-state/<volume>, and `rsync --delete` wiped every
live volume directory and the backup root under the old target.
- paths.NFS_EXPORT_BASE = get_export_base(), the function behind
lookup('nfs_export_base') that also feeds the backup unit's source,
replaces NFS_EXPORT_STATE. NfsRecoverer defaults to it; an explicit
`snapshot:/abs` target still wins, a remote target is still prefixed
with its host. `recover full` inherits the default.
- recover_drill.sh seeds its marker at files/infinito-state/<volume>/
and asserts it under the export base, so the drill can no longer pass
on a nested layout.
- The help examples, schema.yml and the CLI README show the export base;
the documented subtree example would have nested too and now names the
matching snapshot subtree.
Reproduced on a temp tree with the role's real script.sh and recover.py
and the CLI's argv: the old default loses every live volume and the
backup root, the new one restores in place. The new tests fail 6 of 6
against the old default.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
202 lines
6.3 KiB
Python
202 lines
6.3 KiB
Python
"""Unit tests for cli.administration.recover.recoverers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import unittest
|
|
from pathlib import PurePosixPath
|
|
|
|
from cli.administration.recover import paths, recoverers
|
|
from utils.storage.nfs import STATE_SUBDIR, get_export_base, state_path
|
|
|
|
|
|
def _args(name: str, source: str, **kw) -> list[str]:
|
|
return recoverers.RECOVERERS[name].command(source, **kw)[2:]
|
|
|
|
|
|
class NfsTest(unittest.TestCase):
|
|
def test_default_target(self):
|
|
self.assertEqual(_args("nfs", "/snap"), ["/snap", get_export_base()])
|
|
|
|
def test_snapshot_state_dir_lands_on_the_live_state_path(self):
|
|
target = PurePosixPath(_args("nfs", "/snap")[1])
|
|
self.assertEqual(
|
|
str(target / STATE_SUBDIR), state_path(get_export_base(), STATE_SUBDIR)
|
|
)
|
|
|
|
def test_absolute_colon_target_overrides(self):
|
|
self.assertEqual(
|
|
_args("nfs", "/snap:/srv/nfs/infinito-state/matomo_data"),
|
|
["/snap", "/srv/nfs/infinito-state/matomo_data"],
|
|
)
|
|
|
|
def test_relative_colon_target_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
_args("nfs", "/snap:relative")
|
|
|
|
def test_no_safety_backup(self):
|
|
self.assertEqual(
|
|
_args("nfs", "/snap", service_backup=False)[-1], "--no-safety-backup"
|
|
)
|
|
|
|
|
|
class VolumeTest(unittest.TestCase):
|
|
def test_name_from_source(self):
|
|
self.assertEqual(
|
|
_args("volume", "/g/pg_data/files"), ["/g/pg_data/files", "pg_data"]
|
|
)
|
|
|
|
|
|
class SecretsTest(unittest.TestCase):
|
|
def test_source_only(self):
|
|
self.assertEqual(_args("secrets", "/gen/files"), ["/gen/files"])
|
|
|
|
|
|
class DeviceTest(unittest.TestCase):
|
|
def test_bare_device_defaults(self):
|
|
self.assertEqual(
|
|
_args("device", "/dev/sdb1"),
|
|
["/dev/sdb1", paths.RECOVER_MOUNT, paths.BACKUP_ROOT],
|
|
)
|
|
|
|
def test_relative_subpath(self):
|
|
self.assertEqual(
|
|
_args("device", "/dev/sdb1:usb/gen"),
|
|
[
|
|
"/dev/sdb1",
|
|
paths.RECOVER_MOUNT,
|
|
paths.BACKUP_ROOT,
|
|
"--device-target",
|
|
"usb/gen",
|
|
],
|
|
)
|
|
|
|
def test_subpath_and_absolute_target(self):
|
|
self.assertEqual(
|
|
_args("device", "/img:usb/gen:/tmp/restore", passphrase_stdin=True),
|
|
[
|
|
"/img",
|
|
paths.RECOVER_MOUNT,
|
|
"/tmp/restore",
|
|
"--device-target",
|
|
"usb/gen",
|
|
"--passphrase-stdin",
|
|
],
|
|
)
|
|
|
|
def test_absolute_target_only_empty_subpath(self):
|
|
self.assertEqual(
|
|
_args("device", "/img::/tmp/restore"),
|
|
["/img", paths.RECOVER_MOUNT, "/tmp/restore"],
|
|
)
|
|
|
|
def test_digit_segment_is_snapshot(self):
|
|
self.assertEqual(
|
|
_args("device", "/img:usb:20260710153000:/tmp/restore"),
|
|
[
|
|
"/img",
|
|
paths.RECOVER_MOUNT,
|
|
"/tmp/restore",
|
|
"--device-target",
|
|
"usb",
|
|
"--snapshot",
|
|
"20260710153000",
|
|
],
|
|
)
|
|
|
|
def test_no_safety_backup_never_for_device(self):
|
|
self.assertNotIn(
|
|
"--no-safety-backup", _args("device", "/dev/sdb1", service_backup=False)
|
|
)
|
|
|
|
|
|
class RemoteTest(unittest.TestCase):
|
|
def test_is_remote(self):
|
|
self.assertFalse(recoverers.is_remote("/local/path"))
|
|
self.assertTrue(recoverers.is_remote("user@host:/remote/path"))
|
|
self.assertTrue(recoverers.is_remote("host:/remote/path"))
|
|
|
|
def test_nfs_remote_source_passthrough_default_target(self):
|
|
self.assertEqual(
|
|
_args("nfs", "user@host:/remote/snap"),
|
|
["user@host:/remote/snap", get_export_base()],
|
|
)
|
|
|
|
def test_volume_remote_source(self):
|
|
self.assertEqual(
|
|
_args("volume", "host:/b/gen/pg_data/files"),
|
|
["host:/b/gen/pg_data/files", "pg_data"],
|
|
)
|
|
|
|
def test_device_rejects_remote(self):
|
|
with self.assertRaises(ValueError):
|
|
_args("device", "host:/dev/sdb1")
|
|
|
|
def test_secrets_rejects_remote(self):
|
|
with self.assertRaises(ValueError):
|
|
_args("secrets", "host:/gen/files")
|
|
|
|
|
|
class RemoteTargetTest(unittest.TestCase):
|
|
def test_nfs_remote_target_prefixes_host(self):
|
|
self.assertEqual(
|
|
_args("nfs", "/snap", target="user@host"),
|
|
["/snap", f"user@host:{get_export_base()}"],
|
|
)
|
|
|
|
def test_volume_remote_target_docker_host(self):
|
|
self.assertEqual(
|
|
_args("volume", "/g/pg_data/files", target="user@host"),
|
|
["/g/pg_data/files", "pg_data", "--docker-host", "ssh://user@host"],
|
|
)
|
|
|
|
def test_secrets_remote_target_flag(self):
|
|
self.assertEqual(
|
|
_args("secrets", "/gen/files", target="user@host"),
|
|
["/gen/files", "--target-host", "user@host"],
|
|
)
|
|
|
|
def test_localhost_stays_local(self):
|
|
self.assertEqual(
|
|
_args("nfs", "/snap", target="localhost"), ["/snap", get_export_base()]
|
|
)
|
|
|
|
|
|
class RegistryTest(unittest.TestCase):
|
|
def test_order(self):
|
|
self.assertEqual(
|
|
recoverers.ORDER, ("device", "nfs", "volume", "database", "secrets")
|
|
)
|
|
|
|
def test_database_follows_volume(self):
|
|
self.assertLess(
|
|
recoverers.ORDER.index("volume"), recoverers.ORDER.index("database")
|
|
)
|
|
|
|
def test_database_args_are_the_generation(self):
|
|
self.assertEqual(
|
|
recoverers.RECOVERERS["database"].args("/gen", "localhost"),
|
|
["--databases", "/gen"],
|
|
)
|
|
|
|
def test_database_refuses_a_remote_target(self):
|
|
with self.assertRaises(ValueError):
|
|
recoverers.RECOVERERS["database"].args("/gen", "node-1")
|
|
|
|
def test_database_runs_the_volume_role_script(self):
|
|
cmd = recoverers.RECOVERERS["database"].command("/gen")
|
|
self.assertTrue(
|
|
cmd[1].endswith("svc-bkp-volume-2-local/files/python/recover.py")
|
|
)
|
|
|
|
def test_command_starts_with_interpreter_and_script(self):
|
|
cmd = recoverers.RECOVERERS["secrets"].command("/gen/files")
|
|
self.assertEqual(cmd[0], sys.executable)
|
|
self.assertTrue(
|
|
cmd[1].endswith("svc-bkp-secrets-2-local/files/python/recover.py")
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|