mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-20 21:22:54 +00:00
ruff was never wired into this repository: no target, no CI step, no pin. It reported 45 findings across sources and tests, so nothing enforced what the codebase already mostly followed. Adds `make ruff` (check + format --check), `make ruff-fix`, and `make lint` as its alias, and makes `make test` run lint as a fourth parallel spur. The CI workflow calls `make test`, so it is covered there too. The linter is pinned in a `lint` extra: a ruff minor bump changes which rules fire, and with the suite gating on a clean run an unpinned linter would fail it on an unrelated day. The 45 findings are fixed rather than configured away. Three needed a decision instead of the mechanical fix: - The generation timestamp keeps its local wall clock (DTZ005 waived). Generations sort by that name, and UTC would order new ones before the existing ones wherever the offset is positive - "newest generation" is what every restore path selects on. - The per-volume `copy` closure now binds volume_name and vol_dir as default arguments (B023). It only worked because it is called inside the same iteration. - The two CLI top-level handlers keep their blind except (BLE001 waived): turning any failure into exit 1 is what a CLI boundary is for. The two in run.py did not need it and were narrowed to what they actually catch. Also drops the comments that restate the code: the section banners in restore/__main__.py, the filename repeated as line 1 of nine test files, step narration above the statement it narrates, and a block in app.py documenting parameters that had moved to another module. What names a trip-wire stays - the snapshot destination rule, the mysql-binary absence in MariaDB 11 images, the session-scoped FOREIGN_KEY_CHECKS, the spooled temp file for multi-GB dumps, and the negative control that loses its discriminating power if it ever passes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
182 lines
5.4 KiB
Python
182 lines
5.4 KiB
Python
import unittest
|
|
|
|
from .helpers import (
|
|
POSTGRES_DATA_DIR,
|
|
POSTGRES_IMAGE,
|
|
backup_path,
|
|
cleanup_docker,
|
|
create_minimal_compose_dir,
|
|
ensure_empty_dir,
|
|
latest_version_dir,
|
|
require_docker,
|
|
run,
|
|
unique,
|
|
wait_for_postgres,
|
|
write_databases_csv,
|
|
)
|
|
|
|
|
|
class TestE2EDumpOnlySqlMixedRun(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls) -> None:
|
|
require_docker()
|
|
cls.prefix = unique("baudolo-e2e-dump-only-sql-mixed-run")
|
|
cls.backups_dir = f"/tmp/{cls.prefix}/Backups"
|
|
ensure_empty_dir(cls.backups_dir)
|
|
|
|
cls.compose_dir = create_minimal_compose_dir(f"/tmp/{cls.prefix}")
|
|
cls.repo_name = cls.prefix
|
|
|
|
# --- Volumes ---
|
|
cls.db_volume = f"{cls.prefix}-vol-db"
|
|
cls.files_volume = f"{cls.prefix}-vol-files"
|
|
|
|
# Track for cleanup
|
|
cls.containers: list[str] = []
|
|
cls.volumes = [cls.db_volume, cls.files_volume]
|
|
|
|
run(["docker", "volume", "create", cls.db_volume])
|
|
run(["docker", "volume", "create", cls.files_volume])
|
|
|
|
# Put a marker into the non-db volume
|
|
run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"--rm",
|
|
"-v",
|
|
f"{cls.files_volume}:/data",
|
|
"alpine:3.20",
|
|
"sh",
|
|
"-lc",
|
|
"echo 'hello-non-db' > /data/hello.txt",
|
|
]
|
|
)
|
|
|
|
# --- Start Postgres container using the DB volume ---
|
|
cls.pg_container = f"{cls.prefix}-pg"
|
|
cls.containers.append(cls.pg_container)
|
|
|
|
cls.pg_password = "postgres"
|
|
cls.pg_db = "testdb"
|
|
cls.pg_user = "postgres"
|
|
|
|
run(
|
|
[
|
|
"docker",
|
|
"run",
|
|
"-d",
|
|
"--name",
|
|
cls.pg_container,
|
|
"-e",
|
|
f"POSTGRES_PASSWORD={cls.pg_password}",
|
|
"-v",
|
|
f"{cls.db_volume}:{POSTGRES_DATA_DIR}",
|
|
POSTGRES_IMAGE,
|
|
]
|
|
)
|
|
wait_for_postgres(cls.pg_container, user="postgres", timeout_s=90)
|
|
|
|
# Create deterministic content in DB so dump is non-empty
|
|
run(
|
|
[
|
|
"docker",
|
|
"exec",
|
|
cls.pg_container,
|
|
"sh",
|
|
"-lc",
|
|
f'psql -U postgres -c "CREATE DATABASE {cls.pg_db};" || true',
|
|
],
|
|
check=True,
|
|
)
|
|
run(
|
|
[
|
|
"docker",
|
|
"exec",
|
|
cls.pg_container,
|
|
"sh",
|
|
"-lc",
|
|
(
|
|
f"psql -U postgres -d {cls.pg_db} -c "
|
|
'"CREATE TABLE IF NOT EXISTS t (id INT PRIMARY KEY, v TEXT);'
|
|
"INSERT INTO t(id,v) VALUES (1,'hello-db') "
|
|
'ON CONFLICT (id) DO UPDATE SET v=EXCLUDED.v;"'
|
|
),
|
|
],
|
|
check=True,
|
|
)
|
|
|
|
# databases.csv with an entry => dump should succeed
|
|
cls.databases_csv = f"/tmp/{cls.prefix}/databases.csv"
|
|
write_databases_csv(
|
|
cls.databases_csv,
|
|
[(cls.pg_container, cls.pg_db, cls.pg_user, cls.pg_password)],
|
|
)
|
|
|
|
cmd = [
|
|
"baudolo",
|
|
"--compose-dir",
|
|
cls.compose_dir,
|
|
"--databases-csv",
|
|
cls.databases_csv,
|
|
"--database-containers",
|
|
cls.pg_container,
|
|
"--images-no-stop-required",
|
|
POSTGRES_IMAGE,
|
|
"--dump-only-sql",
|
|
"--backups-dir",
|
|
cls.backups_dir,
|
|
"--repo-name",
|
|
cls.repo_name,
|
|
]
|
|
cp = run(cmd, capture=True, check=True)
|
|
cls.stdout = cp.stdout
|
|
cls.stderr = cp.stderr
|
|
|
|
cls.hash, cls.version = latest_version_dir(cls.backups_dir, cls.repo_name)
|
|
|
|
@classmethod
|
|
def tearDownClass(cls) -> None:
|
|
cleanup_docker(containers=cls.containers, volumes=cls.volumes)
|
|
|
|
def test_db_volume_has_dump_and_no_files_dir(self) -> None:
|
|
base = backup_path(
|
|
self.backups_dir, self.repo_name, self.version, self.db_volume
|
|
)
|
|
|
|
dumps = base / "sql"
|
|
files = base / "files"
|
|
|
|
self.assertTrue(dumps.exists(), f"Expected dumps dir for DB volume at: {dumps}")
|
|
self.assertFalse(
|
|
files.exists(),
|
|
f"Did not expect files dir for DB volume when dump succeeded at: {files}",
|
|
)
|
|
|
|
# Optional: at least one dump file exists
|
|
dump_files = list(dumps.glob("*.sql")) + list(dumps.glob("*.sql.gz"))
|
|
self.assertTrue(
|
|
dump_files,
|
|
f"Expected at least one SQL dump file in {dumps}, found none.",
|
|
)
|
|
|
|
def test_non_db_volume_has_files_dir(self) -> None:
|
|
base = backup_path(
|
|
self.backups_dir, self.repo_name, self.version, self.files_volume
|
|
)
|
|
files = base / "files"
|
|
self.assertTrue(
|
|
files.exists(),
|
|
f"Expected files dir for non-DB volume at: {files}",
|
|
)
|
|
|
|
def test_dump_only_sql_does_not_disable_non_db_files_backup(self) -> None:
|
|
# Regression guard: even with --dump-only-sql, non-DB volumes must still be backed up as files
|
|
base = backup_path(
|
|
self.backups_dir, self.repo_name, self.version, self.files_volume
|
|
)
|
|
self.assertTrue(
|
|
(base / "files").exists(),
|
|
f"Expected non-DB volume files backup to exist at: {base / 'files'}",
|
|
)
|