Files
computer-playbook/tests/unit/python/cli/administration/recover/test_full.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

117 lines
4.2 KiB
Python

"""Unit tests for cli.administration.recover.full discovery + pull."""
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from cli.administration.recover import full, paths
class PlanTest(unittest.TestCase):
def _tree(self, root: Path) -> None:
base = root / "HASH"
(base / "backup-nfs-to-local" / "20260101000000" / "files").mkdir(parents=True)
(
base / "backup-docker-to-local" / "20260101000000" / "pg_data" / "files"
).mkdir(parents=True)
(base / "backup-docker-to-local" / "20260101000000" / "redis" / "files").mkdir(
parents=True
)
(base / "backup-secrets-to-local" / "20260101000000" / "files").mkdir(
parents=True
)
def test_plan_order_and_volume_expansion(self):
with tempfile.TemporaryDirectory() as td:
root = Path(td)
self._tree(root)
steps, _unreadable = full.plan(root)
types = [t for t, _ in steps]
self.assertEqual(types, ["nfs", "volume", "volume", "secrets"])
self.assertTrue(
steps[0][1].endswith("backup-nfs-to-local/20260101000000/files")
)
self.assertTrue(any(s.endswith("pg_data/files") for _, s in steps))
self.assertTrue(any(s.endswith("redis/files") for _, s in steps))
def test_plan_picks_newest_generation(self):
with tempfile.TemporaryDirectory() as td:
root = Path(td)
repo = root / "HASH" / "backup-nfs-to-local"
(repo / "20260101000000" / "files").mkdir(parents=True)
(repo / "20260709120000" / "files").mkdir(parents=True)
steps, _unreadable = full.plan(root)
self.assertEqual(len(steps), 1)
self.assertIn("20260709120000", steps[0][1])
def test_plan_empty_root(self):
with tempfile.TemporaryDirectory() as td:
self.assertEqual(full.plan(Path(td)), ([], []))
class DeviceDetectTest(unittest.TestCase):
def test_file_is_device(self):
with tempfile.NamedTemporaryFile() as tf:
self.assertTrue(full._is_device(tf.name))
self.assertTrue(full._is_device(f"{tf.name}:usb:/tmp/r"))
def test_directory_is_not_device(self):
with tempfile.TemporaryDirectory() as td:
self.assertFalse(full._is_device(td))
def test_restore_root_from_absolute_segment(self):
self.assertEqual(full._device_restore_root("/dev/sdb1:usb:/tmp/r"), "/tmp/r")
def test_restore_root_defaults_to_backup_root(self):
self.assertEqual(full._device_restore_root("/dev/sdb1:usb"), paths.BACKUP_ROOT)
class PullCmdTest(unittest.TestCase):
def test_bare_host_uses_standard_root(self):
self.assertEqual(
full._pull_cmd("user@srchost"),
[
"rsync",
"-a",
f"user@srchost:{paths.BACKUP_ROOT}/",
f"{full.PULL_STAGE}/",
],
)
def test_host_with_explicit_path(self):
self.assertEqual(
full._pull_cmd("host:/custom/root"),
["rsync", "-a", "host:/custom/root/", f"{full.PULL_STAGE}/"],
)
if __name__ == "__main__":
unittest.main()
class TestUnreadableManifest(unittest.TestCase):
"""One corrupt manifest must not block the repos that are intact."""
def _root(self) -> Path:
root = Path(tempfile.mkdtemp())
bad = root / "hash" / "backup-nfs-to-local" / "20260101000000"
bad.mkdir(parents=True)
(bad / "manifest.json").write_text(
'{"schema": 99, "layout": {}, "volumes": {}}', encoding="utf-8"
)
(
root / "hash" / "backup-docker-to-local" / "20260101000000" / "v" / "files"
).mkdir(parents=True)
return root
def test_the_intact_repo_is_still_planned(self) -> None:
steps, _unreadable = full.plan(self._root())
self.assertEqual([rtype for rtype, _src in steps], ["volume"])
def test_the_skipped_repo_is_named(self) -> None:
_steps, unreadable = full.plan(self._root())
self.assertEqual(len(unreadable), 1)
self.assertIn("backup-nfs-to-local", unreadable[0])