mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-20 13:12:48 +00:00
style!: adopt the core lint bar and migrate to it
The package carried no ruff configuration at all, so it ran on the defaults (E4+E7+E9+F) while infinito-nexus-core, its only consumer, holds itself to a far wider selection. Measured against that selection this tree had 224 findings. It now has none. The selector list is core's verbatim so both repositories answer to one bar. target-version stays py39 rather than core's py311, because requires-python still declares >=3.9 and pyupgrade would otherwise propose syntax the declared minimum cannot run. Every ignore carries its reason: S603/S607 in particular, since running docker and dump binaries from PATH in list form is this tool's whole job and is already injection-safe. Two conversions are judgement rather than mechanics. os.path.join(dir, '') was the rsync idiom for a trailing separator, which Path drops, so it becomes an explicit os.sep. os.path.abspath stays where Path.resolve() would follow symlinks and let a symlinked volume test as inside the snapshot subject. BREAKING CHANGE: VersionMismatch is renamed VersionMismatchError. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -95,7 +95,7 @@ def write_databases_csv(path: str, rows: list[tuple[str, str, str, str]]) -> Non
|
||||
database may be '' (empty) to trigger pg_dumpall behavior if you want, but here we use db name.
|
||||
"""
|
||||
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
with Path(path).open("w", encoding="utf-8") as f:
|
||||
f.write("instance;database;username;password\n")
|
||||
f.writelines(f"{inst};{db};{user};{pw}\n" for inst, db, user, pw in rows)
|
||||
|
||||
|
||||
@@ -21,13 +21,14 @@ are verifying is in the DB-dump stage, so testing backup_database() directly
|
||||
keeps the assertion focused and the test runnable both on-host and in DinD.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import pandas
|
||||
import pandas as pd
|
||||
|
||||
from baudolo.backup import db as db_mod
|
||||
from baudolo.generation import DUMP_SUFFIX, SQL_DIR
|
||||
|
||||
from .helpers import (
|
||||
MARIADB_DATA_DIR,
|
||||
@@ -140,7 +141,7 @@ class TestE2EMariaDBAnonymousPreemption(unittest.TestCase):
|
||||
# paths — just the dump that the negative-control proved is failing
|
||||
# under the same preemption setup.
|
||||
with tempfile.TemporaryDirectory() as volume_dir:
|
||||
df = pandas.DataFrame(
|
||||
df = pd.DataFrame(
|
||||
[(self.db_container, self.db_name, self.db_user, self.db_password)],
|
||||
columns=["instance", "database", "username", "password"],
|
||||
)
|
||||
@@ -153,9 +154,9 @@ class TestE2EMariaDBAnonymousPreemption(unittest.TestCase):
|
||||
database_containers=[self.db_container],
|
||||
)
|
||||
self.assertTrue(produced, "backup_database did not produce a dump")
|
||||
dump_path = os.path.join(volume_dir, "sql", f"{self.db_name}.backup.sql")
|
||||
self.assertTrue(os.path.isfile(dump_path), f"expected dump at {dump_path}")
|
||||
with open(dump_path, "r", encoding="utf-8", errors="replace") as f:
|
||||
dump_path = Path(volume_dir) / SQL_DIR / f"{self.db_name}{DUMP_SUFFIX}"
|
||||
self.assertTrue(dump_path.is_file(), f"expected dump at {dump_path}")
|
||||
with dump_path.open(encoding="utf-8", errors="replace") as f:
|
||||
content = f.read()
|
||||
self.assertIn("INSERT INTO", content)
|
||||
self.assertIn("'ok'", content)
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def touch(p: Path) -> None:
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stderr
|
||||
from pathlib import Path
|
||||
|
||||
import pandas as pd
|
||||
|
||||
@@ -15,7 +15,7 @@ EXPECTED_COLUMNS = ["instance", "database", "username", "password"]
|
||||
class TestLoadDatabasesDf(unittest.TestCase):
|
||||
def test_missing_csv_is_handled_with_warning_and_empty_df(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
missing_path = os.path.join(td, "does-not-exist.csv")
|
||||
missing_path = str(Path(td) / "does-not-exist.csv")
|
||||
|
||||
buf = io.StringIO()
|
||||
with redirect_stderr(buf):
|
||||
@@ -31,8 +31,8 @@ class TestLoadDatabasesDf(unittest.TestCase):
|
||||
|
||||
def test_empty_csv_is_handled_with_warning_and_empty_df(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
empty_path = os.path.join(td, "databases.csv")
|
||||
with open(empty_path, "w", encoding="utf-8") as f:
|
||||
empty_path = Path(td) / "databases.csv"
|
||||
with empty_path.open("w", encoding="utf-8") as f:
|
||||
f.write("")
|
||||
|
||||
buf = io.StringIO()
|
||||
@@ -49,10 +49,10 @@ class TestLoadDatabasesDf(unittest.TestCase):
|
||||
|
||||
def test_valid_csv_loads_without_warning(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
csv_path = os.path.join(td, "databases.csv")
|
||||
csv_path = Path(td) / "databases.csv"
|
||||
|
||||
content = "instance;database;username;password\nmyapp;*;dbuser;secret\n"
|
||||
with open(csv_path, "w", encoding="utf-8") as f:
|
||||
with csv_path.open("w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
|
||||
buf = io.StringIO()
|
||||
|
||||
@@ -2,15 +2,13 @@ import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import pandas
|
||||
import pandas as pd
|
||||
|
||||
from baudolo.backup import db as db_mod
|
||||
|
||||
|
||||
def _df(rows):
|
||||
return pandas.DataFrame(
|
||||
rows, columns=["instance", "database", "username", "password"]
|
||||
)
|
||||
return pd.DataFrame(rows, columns=["instance", "database", "username", "password"])
|
||||
|
||||
|
||||
def _capture_dumps(*, db_type, rows, container, dump_tool="mariadb-dump"):
|
||||
|
||||
@@ -10,6 +10,7 @@ from __future__ import annotations
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from baudolo.backup.snapshot import SnapshotError, snapshot_source, unsnapshotted
|
||||
@@ -19,8 +20,8 @@ from baudolo.backup.volume import Backing
|
||||
class TestUnsnapshotted(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.subject = tempfile.mkdtemp()
|
||||
self.mountpoint = os.path.join(self.subject, "volumes", "app", "_data")
|
||||
os.makedirs(self.mountpoint)
|
||||
self.mountpoint = str(Path(self.subject) / "volumes" / "app" / "_data")
|
||||
Path(self.mountpoint).mkdir(parents=True)
|
||||
|
||||
def backing(self, **kwargs) -> Backing:
|
||||
return Backing(kwargs.pop("mountpoint", self.mountpoint), **kwargs)
|
||||
@@ -71,7 +72,7 @@ class TestUnsnapshotted(unittest.TestCase):
|
||||
|
||||
def test_an_unreadable_mountpoint_is_not(self) -> None:
|
||||
reason = unsnapshotted(
|
||||
self.backing(mountpoint=os.path.join(self.subject, "gone")), self.subject
|
||||
self.backing(mountpoint=str(Path(self.subject) / "gone")), self.subject
|
||||
)
|
||||
self.assertIn("could not be read", reason)
|
||||
|
||||
@@ -79,12 +80,12 @@ class TestUnsnapshotted(unittest.TestCase):
|
||||
class TestSnapshotSource(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.subject = tempfile.mkdtemp()
|
||||
self.mountpoint = os.path.join(self.subject, "volumes", "app", "_data")
|
||||
os.makedirs(self.mountpoint)
|
||||
self.snapshot = os.path.join(
|
||||
self.subject, ".baudolo-tag", "volumes", "app", "_data"
|
||||
self.mountpoint = str(Path(self.subject) / "volumes" / "app" / "_data")
|
||||
Path(self.mountpoint).mkdir(parents=True)
|
||||
self.snapshot = str(
|
||||
Path(self.subject) / ".baudolo-tag" / "volumes" / "app" / "_data"
|
||||
)
|
||||
os.makedirs(self.snapshot)
|
||||
Path(self.snapshot).mkdir(parents=True)
|
||||
self.backing = Backing(self.mountpoint)
|
||||
|
||||
def test_a_captured_volume_reads_from_the_snapshot(self) -> None:
|
||||
@@ -114,7 +115,7 @@ class TestSnapshotSource(unittest.TestCase):
|
||||
|
||||
def test_a_volume_created_after_the_snapshot_degrades(self) -> None:
|
||||
source, reason = snapshot_source(
|
||||
lambda path: os.path.join(self.subject, "absent") + "/",
|
||||
lambda path: str(Path(self.subject) / "absent") + "/",
|
||||
self.backing,
|
||||
self.subject,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from baudolo.restore.db import cluster as cluster_mod
|
||||
@@ -53,10 +53,10 @@ def cluster_header(roles: int) -> str:
|
||||
|
||||
|
||||
def dump_file(text: str) -> str:
|
||||
path = os.path.join(tempfile.mkdtemp(), "app.backup.sql")
|
||||
with open(path, "w", encoding="utf-8") as handle:
|
||||
path = Path(tempfile.mkdtemp()) / "app.backup.sql"
|
||||
with path.open("w", encoding="utf-8") as handle:
|
||||
handle.write(text)
|
||||
return path
|
||||
return str(path)
|
||||
|
||||
|
||||
class TestDumpVersion(unittest.TestCase):
|
||||
@@ -78,19 +78,19 @@ class TestDumpVersion(unittest.TestCase):
|
||||
|
||||
def test_cluster_dump_states_its_version_far_below_the_header(self) -> None:
|
||||
path = dump_file(cluster_header(roles=200))
|
||||
with open(path, encoding="utf-8") as handle:
|
||||
with Path(path).open(encoding="utf-8") as handle:
|
||||
offset = next(i for i, line in enumerate(handle) if "Dumped from" in line)
|
||||
self.assertGreater(offset, 100, "fixture must exercise the deep scan")
|
||||
self.assertEqual(ver.dump_version(path, "postgres"), "17.11")
|
||||
|
||||
def test_a_version_beyond_the_scan_limit_is_refused_not_ignored(self) -> None:
|
||||
path = dump_file(cluster_header(roles=ver.SCAN_LINES))
|
||||
with self.assertRaises(ver.VersionMismatch):
|
||||
with self.assertRaises(ver.VersionMismatchError):
|
||||
ver.dump_version(path, "postgres")
|
||||
|
||||
def test_a_dump_without_a_version_header_is_refused(self) -> None:
|
||||
path = dump_file("CREATE TABLE t (id int);\n")
|
||||
with self.assertRaises(ver.VersionMismatch):
|
||||
with self.assertRaises(ver.VersionMismatchError):
|
||||
ver.dump_version(path, "postgres")
|
||||
|
||||
|
||||
@@ -102,13 +102,13 @@ class TestMajorOf(unittest.TestCase):
|
||||
self.assertEqual(ver.major_of("18beta1"), 18)
|
||||
|
||||
def test_refuses_an_unreadable_version(self) -> None:
|
||||
with self.assertRaises(ver.VersionMismatch):
|
||||
with self.assertRaises(ver.VersionMismatchError):
|
||||
ver.major_of("unknown")
|
||||
|
||||
|
||||
class TestAssertReplayable(unittest.TestCase):
|
||||
def test_newer_dump_into_older_engine_is_refused(self) -> None:
|
||||
with self.assertRaises(ver.VersionMismatch) as caught:
|
||||
with self.assertRaises(ver.VersionMismatchError) as caught:
|
||||
ver.assert_replayable("/b/app.sql", "postgres", "17.11", "15.6")
|
||||
self.assertIn("17.11", str(caught.exception))
|
||||
self.assertIn("15.6", str(caught.exception))
|
||||
@@ -154,7 +154,7 @@ class TestGateStopsBeforeDestroying(unittest.TestCase):
|
||||
path = dump_file(POSTGRES_HEADER)
|
||||
with (
|
||||
patch.object(pg_mod, "docker_exec") as replay,
|
||||
self.assertRaises(ver.VersionMismatch),
|
||||
self.assertRaises(ver.VersionMismatchError),
|
||||
):
|
||||
pg_mod.restore_postgres_sql(
|
||||
container="db",
|
||||
@@ -171,7 +171,7 @@ class TestGateStopsBeforeDestroying(unittest.TestCase):
|
||||
path = dump_file(cluster_header(roles=3))
|
||||
with (
|
||||
patch.object(cluster_mod, "docker_exec") as replay,
|
||||
self.assertRaises(ver.VersionMismatch),
|
||||
self.assertRaises(ver.VersionMismatchError),
|
||||
):
|
||||
cluster_mod.restore_cluster_sql(
|
||||
container="db",
|
||||
@@ -188,7 +188,7 @@ class TestGateStopsBeforeDestroying(unittest.TestCase):
|
||||
with (
|
||||
patch.object(mdb_mod, "_pick_client", return_value="mariadb"),
|
||||
patch.object(mdb_mod, "docker_exec") as replay,
|
||||
self.assertRaises(ver.VersionMismatch),
|
||||
self.assertRaises(ver.VersionMismatchError),
|
||||
):
|
||||
mdb_mod.restore_mariadb_sql(
|
||||
container="db",
|
||||
@@ -235,7 +235,7 @@ class TestGateStopsBeforeDestroying(unittest.TestCase):
|
||||
db_name="app",
|
||||
user="app",
|
||||
password="pw",
|
||||
sql_path=os.path.join(tempfile.mkdtemp(), "absent.sql"),
|
||||
sql_path=str(Path(tempfile.mkdtemp()) / "absent.sql"),
|
||||
empty=True,
|
||||
)
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ class TestSeedMain(unittest.TestCase):
|
||||
|
||||
return df
|
||||
|
||||
@patch("baudolo.seed.__main__.os.path.exists", return_value=False)
|
||||
@patch("baudolo.seed.__main__.Path.exists", return_value=False)
|
||||
@patch("baudolo.seed.__main__.pd.read_csv")
|
||||
@patch("baudolo.seed.__main__._empty_df")
|
||||
@patch("baudolo.seed.__main__.pd.concat")
|
||||
@@ -83,7 +83,7 @@ class TestSeedMain(unittest.TestCase):
|
||||
"/tmp/databases.csv", sep=";", index=False
|
||||
)
|
||||
|
||||
@patch("baudolo.seed.__main__.os.path.exists", return_value=True)
|
||||
@patch("baudolo.seed.__main__.Path.exists", return_value=True)
|
||||
@patch("baudolo.seed.__main__.pd.read_csv", side_effect=EmptyDataError("empty"))
|
||||
@patch("baudolo.seed.__main__._empty_df")
|
||||
@patch("baudolo.seed.__main__.pd.concat")
|
||||
@@ -110,8 +110,8 @@ class TestSeedMain(unittest.TestCase):
|
||||
password="pass",
|
||||
)
|
||||
|
||||
exists.assert_called_once_with("/tmp/databases.csv")
|
||||
read_csv.assert_called_once()
|
||||
exists.assert_called_once_with()
|
||||
self.assertEqual(read_csv.call_args.args, ("/tmp/databases.csv",))
|
||||
empty_df.assert_called_once()
|
||||
concat.assert_called_once()
|
||||
|
||||
@@ -133,7 +133,7 @@ class TestSeedMain(unittest.TestCase):
|
||||
"/tmp/databases.csv", sep=";", index=False
|
||||
)
|
||||
|
||||
@patch("baudolo.seed.__main__.os.path.exists", return_value=True)
|
||||
@patch("baudolo.seed.__main__.Path.exists", return_value=True)
|
||||
@patch("baudolo.seed.__main__.pd.read_csv")
|
||||
def test_check_and_add_entry_updates_existing_row(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user