Skip to content

Commit

Permalink
Add tests for backup provider resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Dec 29, 2024
1 parent 31f62e1 commit 9bb4d8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
14 changes: 11 additions & 3 deletions db-auto-backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


class BackupProvider(NamedTuple):
name: str
patterns: list[str]
backup_method: Callable[[Container], str]
file_extension: str
Expand Down Expand Up @@ -128,17 +129,22 @@ def backup_redis(container: Container) -> str:

BACKUP_PROVIDERS: list[BackupProvider] = [
BackupProvider(
name="postgres",
patterns=["postgres", "tensorchord/pgvecto-rs", "nextcloud/aio-postgresql"],
backup_method=backup_psql,
file_extension="sql",
),
BackupProvider(
name="mysql",
patterns=["mysql", "mariadb", "linuxserver/mariadb"],
backup_method=backup_mysql,
file_extension="sql",
),
BackupProvider(
patterns=["redis"], backup_method=backup_redis, file_extension="rdb"
name="redis",
patterns=["redis"],
backup_method=backup_redis,
file_extension="rdb",
),
]

Expand Down Expand Up @@ -194,13 +200,15 @@ def backup(now: datetime) -> None:
backup_command = backup_provider.backup_method(container)
_, output = container.exec_run(backup_command, stream=True, demux=True)

description = f"{container.name} ({backup_provider.name})"

with open_file_compressed(
backup_temp_file_path, COMPRESSION
) as backup_temp_file:
with tqdm.wrapattr(
backup_temp_file,
method="write",
desc=container.name,
desc=description,
disable=not SHOW_PROGRESS,
) as f:
for stdout, _ in output:
Expand All @@ -211,7 +219,7 @@ def backup(now: datetime) -> None:
os.replace(backup_temp_file_path, backup_file)

if not SHOW_PROGRESS:
print(container.name)
print(description)

backed_up_containers.append(container.name)

Expand Down
20 changes: 20 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,29 @@ def test_uptime_kuma_success_hook_url(monkeypatch: Any) -> None:
("docker.io/postgres:14-alpine", "postgres"),
("ghcr.io/realorangeone/db-auto-backup:latest", "realorangeone/db-auto-backup"),
("theorangeone/db-auto-backup:latest:latest", "theorangeone/db-auto-backup"),
("lscr.io/linuxserver/mariadb:latest", "linuxserver/mariadb"),
],
)
def test_get_container_names(tag: str, name: str) -> None:
container = MagicMock()
container.image.tags = [tag]
assert db_auto_backup.get_container_names(container) == {name}


@pytest.mark.parametrize(
"container_name,name",
[
("postgres", "postgres"),
("mysql", "mysql"),
("mariadb", "mysql"),
("linuxserver/mariadb", "mysql"),
("tensorchord/pgvecto-rs", "postgres"),
("nextcloud/aio-postgresql", "postgres"),
("redis", "redis"),
],
)
def test_get_backup_provider(container_name: str, name: str) -> None:
provider = db_auto_backup.get_backup_provider([container_name])

assert provider is not None
assert provider.name == name

0 comments on commit 9bb4d8c

Please sign in to comment.