mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-01 12:34:50 +00:00
feat(backup): capture volumes from a filesystem snapshot
Backing up a live volume with rsync copies a moving target: a database
written to mid-copy lands on disk in a state no engine ever committed.
Stopping the container avoids that at the cost of downtime.
A snapshot removes both. `--snapshot {btrfs,zfs}` with `--snapshot-subject`
freezes the docker root once per run, and every volume copy is then read
from that frozen tree while the containers keep serving. A restore of such
a copy is an ordinary crash recovery, which every supported engine performs
on its own at startup.
An unsupported filesystem or an unknown snapshot kind fails loudly rather
than degrading to a live copy, since a silent fallback would return exactly
the torn backup the mode exists to prevent. `--shutdown` is rejected
alongside `--snapshot` instead of being ignored: under a snapshot no
container is ever stopped, so accepting the flag would promise downtime
semantics the run does not deliver.
Copies out of a snapshot skip rsync's --checksum verification. The source
is immutable for the lifetime of the copy, so size-and-mtime cannot race,
and dropping the second full read roughly halves the I/O per volume.
backup/app.py grew past what one module could carry and is split into
layout, policy and dumps along the lines it already had internally.
Tests: unit coverage for the new snapshot, layout, policy, volume and cli
units; e2e cases drive real btrfs, zfs and ext4 filesystems on loop devices
in a privileged container, including a MariaDB that is written to across
the snapshot and must recover from the restored copy without losing a
committed row. CI installs zfs and sets E2E_REQUIRE_FILESYSTEMS so a
missing kernel module fails the build instead of silently skipping a
filesystem.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
44
tests/unit/backup/compose_fixture.py
Normal file
44
tests/unit/backup/compose_fixture.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Builds the on-disk compose directories the compose tests discover."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def touch(p: Path) -> None:
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# ".env/env" leaves ".env" behind as a directory, which blocks a later ".env" file.
|
||||
if p.exists() and p.is_dir():
|
||||
shutil.rmtree(p)
|
||||
|
||||
p.write_text("x", encoding="utf-8")
|
||||
|
||||
|
||||
def setup_compose_dir(
|
||||
tmp_path: Path,
|
||||
name: str = "mailu",
|
||||
*,
|
||||
compose_name: str = "docker-compose.yml",
|
||||
with_override: bool = False,
|
||||
with_ca_override: bool = False,
|
||||
env_layout: str | None = None, # None | ".env" | ".env/env"
|
||||
) -> Path:
|
||||
d = tmp_path / name
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
touch(d / compose_name)
|
||||
|
||||
if with_override:
|
||||
touch(d / "docker-compose.override.yml")
|
||||
|
||||
if with_ca_override:
|
||||
touch(d / "docker-compose.ca.override.yml")
|
||||
|
||||
if env_layout == ".env":
|
||||
touch(d / ".env")
|
||||
elif env_layout == ".env/env":
|
||||
touch(d / ".env" / "env")
|
||||
|
||||
return d
|
||||
@@ -7,7 +7,7 @@ from contextlib import redirect_stderr
|
||||
import pandas as pd
|
||||
|
||||
# Adjust if your package name/import path differs.
|
||||
from baudolo.backup.app import _load_databases_df
|
||||
from baudolo.backup.dumps import load_databases_df
|
||||
|
||||
|
||||
EXPECTED_COLUMNS = ["instance", "database", "username", "password"]
|
||||
@@ -20,7 +20,7 @@ class TestLoadDatabasesDf(unittest.TestCase):
|
||||
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
df = _load_databases_df(missing_path)
|
||||
df = load_databases_df(missing_path)
|
||||
|
||||
stderr = buf.getvalue()
|
||||
self.assertIn("WARNING:", stderr)
|
||||
@@ -39,7 +39,7 @@ class TestLoadDatabasesDf(unittest.TestCase):
|
||||
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
df = _load_databases_df(empty_path)
|
||||
df = load_databases_df(empty_path)
|
||||
|
||||
stderr = buf.getvalue()
|
||||
self.assertIn("WARNING:", stderr)
|
||||
@@ -59,7 +59,7 @@ class TestLoadDatabasesDf(unittest.TestCase):
|
||||
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
df = _load_databases_df(csv_path)
|
||||
df = load_databases_df(csv_path)
|
||||
|
||||
stderr = buf.getvalue()
|
||||
self.assertEqual(stderr, "") # no warning expected
|
||||
|
||||
65
tests/unit/backup/test_cli.py
Normal file
65
tests/unit/backup/test_cli.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Contract of the backup CLI, in particular the snapshot flag pairing."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from baudolo.backup.cli import parse_args
|
||||
|
||||
REQUIRED = ["--compose-dir", "/compose", "--backups-dir", "/backups"]
|
||||
|
||||
|
||||
def parse(*extra: str):
|
||||
with mock.patch("sys.argv", ["baudolo", *REQUIRED, *extra]):
|
||||
return parse_args()
|
||||
|
||||
|
||||
class TestSnapshotFlags(unittest.TestCase):
|
||||
def test_no_snapshot_by_default(self) -> None:
|
||||
args = parse()
|
||||
self.assertIsNone(args.snapshot)
|
||||
self.assertIsNone(args.snapshot_subject)
|
||||
|
||||
def test_both_flags_together_are_accepted(self) -> None:
|
||||
args = parse("--snapshot", "btrfs", "--snapshot-subject", "/var/lib/docker")
|
||||
self.assertEqual(args.snapshot, "btrfs")
|
||||
self.assertEqual(args.snapshot_subject, "/var/lib/docker")
|
||||
|
||||
def test_the_kind_alone_is_rejected(self) -> None:
|
||||
with self.assertRaises(SystemExit):
|
||||
parse("--snapshot", "btrfs")
|
||||
|
||||
def test_the_subject_alone_is_rejected(self) -> None:
|
||||
with self.assertRaises(SystemExit):
|
||||
parse("--snapshot-subject", "/var/lib/docker")
|
||||
|
||||
def test_an_unsupported_kind_is_rejected(self) -> None:
|
||||
with self.assertRaises(SystemExit):
|
||||
parse("--snapshot", "ext4", "--snapshot-subject", "/var/lib/docker")
|
||||
|
||||
def test_zfs_is_accepted(self) -> None:
|
||||
self.assertEqual(parse("--snapshot", "zfs", "--snapshot-subject", "/d").snapshot, "zfs")
|
||||
|
||||
def test_shutdown_is_rejected_because_nothing_is_stopped(self) -> None:
|
||||
with self.assertRaises(SystemExit):
|
||||
parse("--snapshot", "btrfs", "--snapshot-subject", "/d", "--shutdown")
|
||||
|
||||
def test_shutdown_stays_available_without_a_snapshot(self) -> None:
|
||||
self.assertTrue(parse("--shutdown").shutdown)
|
||||
|
||||
|
||||
class TestRequiredFlags(unittest.TestCase):
|
||||
def test_backups_dir_is_required(self) -> None:
|
||||
with mock.patch("sys.argv", ["baudolo", "--compose-dir", "/compose"]):
|
||||
with self.assertRaises(SystemExit):
|
||||
parse_args()
|
||||
|
||||
def test_compose_dir_is_required(self) -> None:
|
||||
with mock.patch("sys.argv", ["baudolo", "--backups-dir", "/backups"]):
|
||||
with self.assertRaises(SystemExit):
|
||||
parse_args()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,50 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
def _touch(p: Path) -> None:
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# If the path already exists as a directory (e.g. ".env" created by ".env/env"),
|
||||
# remove it so we can create a file with the same name.
|
||||
if p.exists() and p.is_dir():
|
||||
shutil.rmtree(p)
|
||||
|
||||
p.write_text("x", encoding="utf-8")
|
||||
|
||||
|
||||
def _setup_compose_dir(
|
||||
tmp_path: Path,
|
||||
name: str = "mailu",
|
||||
*,
|
||||
compose_name: str = "docker-compose.yml",
|
||||
with_override: bool = False,
|
||||
with_ca_override: bool = False,
|
||||
env_layout: str | None = None, # None | ".env" | ".env/env"
|
||||
) -> Path:
|
||||
d = tmp_path / name
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
_touch(d / compose_name)
|
||||
|
||||
if with_override:
|
||||
_touch(d / "docker-compose.override.yml")
|
||||
|
||||
if with_ca_override:
|
||||
_touch(d / "docker-compose.ca.override.yml")
|
||||
|
||||
if env_layout == ".env":
|
||||
_touch(d / ".env")
|
||||
elif env_layout == ".env/env":
|
||||
_touch(d / ".env" / "env")
|
||||
|
||||
return d
|
||||
from .compose_fixture import setup_compose_dir as _setup_compose_dir
|
||||
|
||||
|
||||
class TestCompose(unittest.TestCase):
|
||||
@@ -249,63 +211,3 @@ class TestCompose(unittest.TestCase):
|
||||
),
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class HardRestartArgTests(unittest.TestCase):
|
||||
"""The hard-restart list defaults to empty (no compose down/up); callers
|
||||
opt in per dir, e.g. compose hosts pass 'mailu' while swarm hosts, where
|
||||
the dir is a stack whose overlay network collides with compose up, pass
|
||||
nothing."""
|
||||
|
||||
def _parse(self, extra: List[str]):
|
||||
import sys
|
||||
|
||||
from baudolo.backup import cli
|
||||
|
||||
argv = [
|
||||
"baudolo",
|
||||
"--compose-dir",
|
||||
"/tmp",
|
||||
"--backups-dir",
|
||||
"/tmp/backup",
|
||||
"--database-containers",
|
||||
"postgres",
|
||||
"--images-no-stop-required",
|
||||
"redis",
|
||||
*extra,
|
||||
]
|
||||
with patch.object(sys, "argv", argv):
|
||||
return cli.parse_args()
|
||||
|
||||
def test_default_is_empty(self) -> None:
|
||||
args = self._parse([])
|
||||
self.assertEqual(args.hard_restart_projects, [])
|
||||
|
||||
def test_empty_flag_stays_empty(self) -> None:
|
||||
args = self._parse(["--hard-restart-projects"])
|
||||
self.assertEqual(args.hard_restart_projects, [])
|
||||
|
||||
def test_explicit_names_preserved(self) -> None:
|
||||
args = self._parse(["--hard-restart-projects", "mailu", "foo"])
|
||||
self.assertEqual(args.hard_restart_projects, ["mailu", "foo"])
|
||||
|
||||
def test_backups_dir_is_required(self) -> None:
|
||||
import sys
|
||||
|
||||
from baudolo.backup import cli
|
||||
|
||||
argv = [
|
||||
"baudolo",
|
||||
"--compose-dir",
|
||||
"/tmp",
|
||||
"--database-containers",
|
||||
"postgres",
|
||||
"--images-no-stop-required",
|
||||
"redis",
|
||||
]
|
||||
with patch.object(sys, "argv", argv), self.assertRaises(SystemExit):
|
||||
cli.parse_args()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
|
||||
65
tests/unit/backup/test_compose_hard_restart.py
Normal file
65
tests/unit/backup/test_compose_hard_restart.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from typing import List
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
class HardRestartArgTests(unittest.TestCase):
|
||||
"""The hard-restart list defaults to empty (no compose down/up); callers
|
||||
opt in per dir, e.g. compose hosts pass 'mailu' while swarm hosts, where
|
||||
the dir is a stack whose overlay network collides with compose up, pass
|
||||
nothing."""
|
||||
|
||||
def _parse(self, extra: List[str]):
|
||||
import sys
|
||||
|
||||
from baudolo.backup import cli
|
||||
|
||||
argv = [
|
||||
"baudolo",
|
||||
"--compose-dir",
|
||||
"/tmp",
|
||||
"--backups-dir",
|
||||
"/tmp/backup",
|
||||
"--database-containers",
|
||||
"postgres",
|
||||
"--images-no-stop-required",
|
||||
"redis",
|
||||
*extra,
|
||||
]
|
||||
with patch.object(sys, "argv", argv):
|
||||
return cli.parse_args()
|
||||
|
||||
def test_default_is_empty(self) -> None:
|
||||
args = self._parse([])
|
||||
self.assertEqual(args.hard_restart_projects, [])
|
||||
|
||||
def test_empty_flag_stays_empty(self) -> None:
|
||||
args = self._parse(["--hard-restart-projects"])
|
||||
self.assertEqual(args.hard_restart_projects, [])
|
||||
|
||||
def test_explicit_names_preserved(self) -> None:
|
||||
args = self._parse(["--hard-restart-projects", "mailu", "foo"])
|
||||
self.assertEqual(args.hard_restart_projects, ["mailu", "foo"])
|
||||
|
||||
def test_backups_dir_is_required(self) -> None:
|
||||
import sys
|
||||
|
||||
from baudolo.backup import cli
|
||||
|
||||
argv = [
|
||||
"baudolo",
|
||||
"--compose-dir",
|
||||
"/tmp",
|
||||
"--database-containers",
|
||||
"postgres",
|
||||
"--images-no-stop-required",
|
||||
"redis",
|
||||
]
|
||||
with patch.object(sys, "argv", argv), self.assertRaises(SystemExit):
|
||||
cli.parse_args()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(verbosity=2)
|
||||
51
tests/unit/backup/test_layout.py
Normal file
51
tests/unit/backup/test_layout.py
Normal file
@@ -0,0 +1,51 @@
|
||||
"""Contract of where a backup run puts its directories."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from baudolo.backup import layout as mod
|
||||
|
||||
|
||||
class TestVersionDirectory(unittest.TestCase):
|
||||
def test_it_creates_the_generation_directory(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
created = mod.create_version_directory(tmp, "20260731020304")
|
||||
self.assertTrue(Path(created).is_dir())
|
||||
self.assertEqual(Path(created).name, "20260731020304")
|
||||
|
||||
def test_it_is_idempotent(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
first = mod.create_version_directory(tmp, "20260731")
|
||||
second = mod.create_version_directory(tmp, "20260731")
|
||||
self.assertEqual(first, second)
|
||||
|
||||
def test_it_creates_missing_parents(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
nested = str(Path(tmp) / "machine" / "repo")
|
||||
created = mod.create_version_directory(nested, "20260731")
|
||||
self.assertTrue(Path(created).is_dir())
|
||||
|
||||
|
||||
class TestVolumeDirectory(unittest.TestCase):
|
||||
def test_it_nests_the_volume_under_the_generation(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
created = mod.create_volume_directory(tmp, "postgres_data")
|
||||
self.assertEqual(Path(created).parent, Path(tmp))
|
||||
self.assertTrue(Path(created).is_dir())
|
||||
|
||||
|
||||
class TestMachineId(unittest.TestCase):
|
||||
def test_it_takes_the_hash_without_the_filename(self) -> None:
|
||||
digest = "a" * 64
|
||||
with mock.patch.object(
|
||||
mod, "execute_shell_command", return_value=[f"{digest} /etc/machine-id"]
|
||||
):
|
||||
self.assertEqual(mod.get_machine_id(), digest)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
68
tests/unit/backup/test_policy.py
Normal file
68
tests/unit/backup/test_policy.py
Normal file
@@ -0,0 +1,68 @@
|
||||
"""Contract of the rules deciding what is backed up and what must stop."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from baudolo.backup import policy as mod
|
||||
|
||||
|
||||
class TestIsImageIgnored(unittest.TestCase):
|
||||
def test_an_empty_whitelist_ignores_nothing(self) -> None:
|
||||
self.assertFalse(mod.is_image_ignored("c1", []))
|
||||
|
||||
def test_a_listed_image_is_ignored(self) -> None:
|
||||
with mock.patch.object(mod, "get_image_info", return_value="alpine:3.20"):
|
||||
self.assertTrue(mod.is_image_ignored("c1", ["alpine:3.20"]))
|
||||
|
||||
def test_matching_is_exact(self) -> None:
|
||||
with mock.patch.object(mod, "get_image_info", return_value="alpine:3.21"):
|
||||
self.assertFalse(mod.is_image_ignored("c1", ["alpine:3.20"]))
|
||||
|
||||
|
||||
class TestVolumeIsFullyIgnored(unittest.TestCase):
|
||||
def test_a_volume_without_containers_is_kept(self) -> None:
|
||||
self.assertFalse(mod.volume_is_fully_ignored([], ["alpine:3.20"]))
|
||||
|
||||
def test_it_needs_every_container_to_be_ignored(self) -> None:
|
||||
with mock.patch.object(mod, "get_image_info", side_effect=["a", "b"]):
|
||||
self.assertFalse(mod.volume_is_fully_ignored(["c1", "c2"], ["a"]))
|
||||
|
||||
def test_all_ignored_skips_the_volume(self) -> None:
|
||||
with mock.patch.object(mod, "get_image_info", side_effect=["a", "a"]):
|
||||
self.assertTrue(mod.volume_is_fully_ignored(["c1", "c2"], ["a"]))
|
||||
|
||||
|
||||
class TestRequiresStop(unittest.TestCase):
|
||||
def test_no_containers_means_no_stop(self) -> None:
|
||||
self.assertFalse(mod.requires_stop([], []))
|
||||
|
||||
def test_a_swarm_task_never_forces_a_stop(self) -> None:
|
||||
with mock.patch.object(mod, "is_swarm_task", return_value=True):
|
||||
self.assertFalse(mod.requires_stop(["c1"], []))
|
||||
|
||||
def test_a_whitelisted_image_does_not_force_a_stop(self) -> None:
|
||||
with (
|
||||
mock.patch.object(mod, "is_swarm_task", return_value=False),
|
||||
mock.patch.object(mod, "get_image_info", return_value="alpine:3.20"),
|
||||
):
|
||||
self.assertFalse(mod.requires_stop(["c1"], ["alpine:3.20"]))
|
||||
|
||||
def test_an_unlisted_image_forces_a_stop(self) -> None:
|
||||
with (
|
||||
mock.patch.object(mod, "is_swarm_task", return_value=False),
|
||||
mock.patch.object(mod, "get_image_info", return_value="postgres:17"),
|
||||
):
|
||||
self.assertTrue(mod.requires_stop(["c1"], ["alpine:3.20"]))
|
||||
|
||||
def test_one_unlisted_container_is_enough(self) -> None:
|
||||
with (
|
||||
mock.patch.object(mod, "is_swarm_task", return_value=False),
|
||||
mock.patch.object(mod, "get_image_info", side_effect=["alpine:3.20", "x"]),
|
||||
):
|
||||
self.assertTrue(mod.requires_stop(["c1", "c2"], ["alpine:3.20"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
107
tests/unit/backup/test_snapshot.py
Normal file
107
tests/unit/backup/test_snapshot.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""Contract of the filesystem snapshot used to capture volumes atomically."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from baudolo.backup.snapshot import SnapshotError, volume_snapshot
|
||||
|
||||
|
||||
class Runner:
|
||||
def __init__(self, replies: dict[str, list[str]] | None = None) -> None:
|
||||
self.calls: list[str] = []
|
||||
self.replies = replies or {}
|
||||
|
||||
def __call__(self, command: str) -> list[str]:
|
||||
self.calls.append(command)
|
||||
for prefix, reply in self.replies.items():
|
||||
if command.startswith(prefix):
|
||||
return reply
|
||||
return []
|
||||
|
||||
|
||||
class TestBtrfs(unittest.TestCase):
|
||||
def test_it_creates_a_read_only_snapshot_beside_the_subject(self) -> None:
|
||||
run = Runner()
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertEqual(
|
||||
run.calls[0],
|
||||
"btrfs subvolume snapshot -r /var/lib/docker /var/lib/.baudolo-20260731",
|
||||
)
|
||||
|
||||
def test_it_removes_the_snapshot_afterwards(self) -> None:
|
||||
run = Runner()
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertEqual(run.calls[-1], "btrfs subvolume delete /var/lib/.baudolo-20260731")
|
||||
|
||||
def test_it_maps_a_volume_path_into_the_snapshot(self) -> None:
|
||||
run = Runner()
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run) as resolve:
|
||||
self.assertEqual(
|
||||
resolve("/var/lib/docker/volumes/postgres_data/_data"),
|
||||
"/var/lib/.baudolo-20260731/volumes/postgres_data/_data",
|
||||
)
|
||||
|
||||
def test_it_removes_the_snapshot_even_when_the_body_raises(self) -> None:
|
||||
run = Runner()
|
||||
with self.assertRaises(ZeroDivisionError):
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run):
|
||||
raise ZeroDivisionError
|
||||
self.assertTrue(run.calls[-1].startswith("btrfs subvolume delete"))
|
||||
|
||||
|
||||
class TestZfs(unittest.TestCase):
|
||||
def _run(self) -> Runner:
|
||||
return Runner({"zfs list": ["tank/docker"]})
|
||||
|
||||
def test_it_snapshots_the_dataset_mounted_at_the_subject(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertIn("zfs snapshot tank/docker@baudolo-20260731", run.calls)
|
||||
|
||||
def test_it_destroys_the_snapshot_afterwards(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertEqual(run.calls[-1], "zfs destroy tank/docker@baudolo-20260731")
|
||||
|
||||
def test_it_maps_a_volume_path_through_the_dot_zfs_directory(self) -> None:
|
||||
run = self._run()
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run) as resolve:
|
||||
self.assertEqual(
|
||||
resolve("/var/lib/docker/volumes/postgres_data/_data"),
|
||||
"/var/lib/docker/.zfs/snapshot/baudolo-20260731/volumes/postgres_data/_data",
|
||||
)
|
||||
|
||||
def test_an_unmounted_dataset_is_an_error(self) -> None:
|
||||
run = Runner({"zfs list": [""]})
|
||||
with self.assertRaises(SnapshotError):
|
||||
with volume_snapshot("zfs", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
|
||||
|
||||
class TestRejections(unittest.TestCase):
|
||||
def test_an_unknown_kind_is_rejected(self) -> None:
|
||||
run = Runner()
|
||||
with self.assertRaises(SnapshotError):
|
||||
with volume_snapshot("ext4", "/var/lib/docker", "20260731", run=run):
|
||||
pass
|
||||
self.assertEqual(run.calls, [])
|
||||
|
||||
def test_a_path_outside_the_subject_is_rejected(self) -> None:
|
||||
run = Runner()
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run) as resolve:
|
||||
with self.assertRaises(SnapshotError):
|
||||
resolve("/etc/passwd")
|
||||
|
||||
def test_the_subject_itself_resolves_to_the_snapshot_root(self) -> None:
|
||||
run = Runner()
|
||||
with volume_snapshot("btrfs", "/var/lib/docker", "20260731", run=run) as resolve:
|
||||
self.assertEqual(resolve("/var/lib/docker"), "/var/lib/.baudolo-20260731")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
87
tests/unit/backup/test_volume.py
Normal file
87
tests/unit/backup/test_volume.py
Normal file
@@ -0,0 +1,87 @@
|
||||
"""Contract of the rsync invocation that copies a volume."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from baudolo.backup import volume as mod
|
||||
|
||||
|
||||
class TestBackupVolume(unittest.TestCase):
|
||||
def copy(self, **kwargs) -> str:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
defaults = {
|
||||
"versions_dir": tmp,
|
||||
"volume_name": "demo",
|
||||
"volume_dir": str(Path(tmp) / "gen" / "demo"),
|
||||
"authoritative": False,
|
||||
"source": "/var/lib/docker/volumes/demo/_data/",
|
||||
}
|
||||
defaults.update(kwargs)
|
||||
with mock.patch.object(mod, "execute_shell_command") as run:
|
||||
mod.backup_volume(
|
||||
defaults.pop("versions_dir"),
|
||||
defaults.pop("volume_name"),
|
||||
defaults.pop("volume_dir"),
|
||||
**defaults,
|
||||
)
|
||||
return run.call_args[0][0]
|
||||
|
||||
def test_the_quick_check_pass_carries_no_checksum(self) -> None:
|
||||
self.assertNotIn("--checksum", self.copy(authoritative=False))
|
||||
|
||||
def test_the_authoritative_pass_compares_by_content(self) -> None:
|
||||
self.assertIn("--checksum", self.copy(authoritative=True))
|
||||
|
||||
def test_it_reads_from_the_given_source(self) -> None:
|
||||
command = self.copy(source="/snapshot/volumes/demo/_data/")
|
||||
self.assertIn("/snapshot/volumes/demo/_data/", command)
|
||||
|
||||
def test_it_always_deletes_what_the_source_no_longer_has(self) -> None:
|
||||
self.assertIn("--delete", self.copy())
|
||||
|
||||
def test_it_creates_the_destination(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
dest = Path(tmp) / "gen" / "demo"
|
||||
with mock.patch.object(mod, "execute_shell_command"):
|
||||
mod.backup_volume(
|
||||
tmp, "demo", str(dest), authoritative=False, source="/src/"
|
||||
)
|
||||
self.assertTrue((dest / "files").is_dir())
|
||||
|
||||
def test_source_is_required(self) -> None:
|
||||
with self.assertRaises(TypeError):
|
||||
mod.backup_volume("/v", "demo", "/d", authoritative=False)
|
||||
|
||||
def test_authoritative_is_required(self) -> None:
|
||||
with self.assertRaises(TypeError):
|
||||
mod.backup_volume("/v", "demo", "/d", source="/src/")
|
||||
|
||||
|
||||
class TestLastBackupDir(unittest.TestCase):
|
||||
def test_it_ignores_the_generation_being_written(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
current = Path(tmp) / "20260101" / "demo" / "files"
|
||||
current.mkdir(parents=True)
|
||||
found = mod.get_last_backup_dir(tmp, "demo", str(current) + "/")
|
||||
self.assertIsNone(found)
|
||||
|
||||
def test_it_finds_the_previous_generation(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
older = Path(tmp) / "20260101" / "demo" / "files"
|
||||
older.mkdir(parents=True)
|
||||
current = Path(tmp) / "20260102" / "demo" / "files"
|
||||
current.mkdir(parents=True)
|
||||
found = mod.get_last_backup_dir(tmp, "demo", str(current) + "/")
|
||||
self.assertEqual(found, str(older) + "/")
|
||||
|
||||
def test_a_first_run_has_no_predecessor(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
self.assertIsNone(mod.get_last_backup_dir(tmp, "demo", f"{tmp}/x/"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user