mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-20 21:22:54 +00:00
The backup built command strings and handed them to shell=True, so four interpolated values per dump - user, password, container, database - were each a way out of the command. validate_database covered one of them since the previous commit; now there is nothing to cover: every command is an argv list, and a value can only ever be an argument. execute_to_file absorbs the atomic dump write. The shell redirect into <file>.tmp and the separate mv process become a Python file handle and os.replace, and a failing dump deletes its partial file instead of leaving it. PGPASSWORD moves out of the command string into the child's environment, where a process listing does not show it. docker exec is built in one place, docker_exec_argv; db.py's three hand-built copies and the probe use it. The dead docker_volume_exists goes - never called, and the restore side owns the living twin. The rsync quoting in --link-dest falls away: inside an argv it would have become part of the path. The snapshot module's injected runner changes type with it, which the three e2e drivers implement - the first conversion missed them, btrfs ran with no arguments, and the e2e caught it. Marked breaking for that contract: any external runner injected into volume_snapshot must now accept a list. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
96 lines
3.7 KiB
Python
96 lines
3.7 KiB
Python
"""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_carries_no_kernel_objects_into_a_generation(self) -> None:
|
|
self.assertIn("--no-D", self.copy())
|
|
|
|
def test_it_keeps_no_twin_of_what_the_second_pass_replaces(self) -> None:
|
|
command = self.copy(authoritative=True)
|
|
self.assertEqual(command[:2], ["rsync", "-aP"])
|
|
self.assertNotIn("--backup", command)
|
|
|
|
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()
|