Skip to content

Commit 9bb4d8c

Browse files
committed
Add tests for backup provider resolution
1 parent 31f62e1 commit 9bb4d8c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

db-auto-backup.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
class BackupProvider(NamedTuple):
23+
name: str
2324
patterns: list[str]
2425
backup_method: Callable[[Container], str]
2526
file_extension: str
@@ -128,17 +129,22 @@ def backup_redis(container: Container) -> str:
128129

129130
BACKUP_PROVIDERS: list[BackupProvider] = [
130131
BackupProvider(
132+
name="postgres",
131133
patterns=["postgres", "tensorchord/pgvecto-rs", "nextcloud/aio-postgresql"],
132134
backup_method=backup_psql,
133135
file_extension="sql",
134136
),
135137
BackupProvider(
138+
name="mysql",
136139
patterns=["mysql", "mariadb", "linuxserver/mariadb"],
137140
backup_method=backup_mysql,
138141
file_extension="sql",
139142
),
140143
BackupProvider(
141-
patterns=["redis"], backup_method=backup_redis, file_extension="rdb"
144+
name="redis",
145+
patterns=["redis"],
146+
backup_method=backup_redis,
147+
file_extension="rdb",
142148
),
143149
]
144150

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

203+
description = f"{container.name} ({backup_provider.name})"
204+
197205
with open_file_compressed(
198206
backup_temp_file_path, COMPRESSION
199207
) as backup_temp_file:
200208
with tqdm.wrapattr(
201209
backup_temp_file,
202210
method="write",
203-
desc=container.name,
211+
desc=description,
204212
disable=not SHOW_PROGRESS,
205213
) as f:
206214
for stdout, _ in output:
@@ -211,7 +219,7 @@ def backup(now: datetime) -> None:
211219
os.replace(backup_temp_file_path, backup_file)
212220

213221
if not SHOW_PROGRESS:
214-
print(container.name)
222+
print(description)
215223

216224
backed_up_containers.append(container.name)
217225

tests/tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,29 @@ def test_uptime_kuma_success_hook_url(monkeypatch: Any) -> None:
100100
("docker.io/postgres:14-alpine", "postgres"),
101101
("ghcr.io/realorangeone/db-auto-backup:latest", "realorangeone/db-auto-backup"),
102102
("theorangeone/db-auto-backup:latest:latest", "theorangeone/db-auto-backup"),
103+
("lscr.io/linuxserver/mariadb:latest", "linuxserver/mariadb"),
103104
],
104105
)
105106
def test_get_container_names(tag: str, name: str) -> None:
106107
container = MagicMock()
107108
container.image.tags = [tag]
108109
assert db_auto_backup.get_container_names(container) == {name}
110+
111+
112+
@pytest.mark.parametrize(
113+
"container_name,name",
114+
[
115+
("postgres", "postgres"),
116+
("mysql", "mysql"),
117+
("mariadb", "mysql"),
118+
("linuxserver/mariadb", "mysql"),
119+
("tensorchord/pgvecto-rs", "postgres"),
120+
("nextcloud/aio-postgresql", "postgres"),
121+
("redis", "redis"),
122+
],
123+
)
124+
def test_get_backup_provider(container_name: str, name: str) -> None:
125+
provider = db_auto_backup.get_backup_provider([container_name])
126+
127+
assert provider is not None
128+
assert provider.name == name

0 commit comments

Comments
 (0)