Files
docker-volume-backup/tests/unit/backup/test_cli.py
Kevin Veen-Birkenbach 2129c5e362 build(lint): gate make test on a clean ruff run
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>
2026-08-17 04:35:50 +02:00

88 lines
2.9 KiB
Python

"""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)
def test_hard_restart_is_rejected_because_nothing_is_stopped(self) -> None:
with self.assertRaises(SystemExit):
parse(
"--snapshot",
"btrfs",
"--snapshot-subject",
"/d",
"--hard-restart-projects",
"mailu",
)
def test_hard_restart_stays_available_without_a_snapshot(self) -> None:
self.assertEqual(
parse("--hard-restart-projects", "mailu").hard_restart_projects, ["mailu"]
)
class TestRequiredFlags(unittest.TestCase):
def test_backups_dir_is_required(self) -> None:
with (
mock.patch("sys.argv", ["baudolo", "--compose-dir", "/compose"]),
self.assertRaises(SystemExit),
):
parse_args()
def test_compose_dir_is_required(self) -> None:
with (
mock.patch("sys.argv", ["baudolo", "--backups-dir", "/backups"]),
self.assertRaises(SystemExit),
):
parse_args()
if __name__ == "__main__":
unittest.main()