-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some very basic unit tests to ensure backups complete successfully
- Loading branch information
1 parent
39e8b67
commit 323598b
Showing
10 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ black==23.1.0 | |
ruff==0.0.256 | ||
mypy==1.1.1 | ||
types-requests | ||
pytest==7.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable | ||
|
||
import docker | ||
import pytest | ||
|
||
BACKUP_DIR = Path.cwd() / "backups" | ||
|
||
|
||
@pytest.fixture | ||
def run_backup(request: Any) -> Callable: | ||
docker_client = docker.from_env() | ||
backup_container = docker_client.containers.get("docker-db-auto-backup-backup-1") | ||
|
||
def clean_backups() -> None: | ||
# HACK: Remove files from inside container to avoid permissions issue | ||
backup_container.exec_run(["rm", "-rf", "/var/backups"]) | ||
|
||
def _run_backup(env: dict) -> Any: | ||
return backup_container.exec_run( | ||
[ | ||
"./db-auto-backup.py", | ||
], | ||
environment={**env, "SCHEDULE": ""}, | ||
) | ||
|
||
request.addfinalizer(clean_backups) | ||
|
||
return _run_backup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from importlib.machinery import SourceFileLoader | ||
from importlib.util import module_from_spec, spec_from_loader | ||
from pathlib import Path | ||
from typing import Any, Callable | ||
|
||
import pytest | ||
|
||
BACKUP_DIR = Path.cwd() / "backups" | ||
|
||
|
||
def import_file(path: Path) -> Any: | ||
""" | ||
Import a module from a file path, returning its contents. | ||
""" | ||
loader = SourceFileLoader(path.name, str(path)) | ||
spec = spec_from_loader(path.name, loader) | ||
assert spec is not None | ||
mod = module_from_spec(spec) | ||
loader.exec_module(mod) | ||
return mod | ||
|
||
|
||
# HACK: The filename isn't compatible with `import foo` syntax | ||
db_auto_backup = import_file(Path.cwd() / "db-auto-backup.py") | ||
|
||
|
||
def test_backup_runs(run_backup: Callable) -> None: | ||
exit_code, _ = run_backup({}) | ||
assert exit_code == 0 | ||
assert BACKUP_DIR.is_dir() | ||
assert sorted(f.name for f in BACKUP_DIR.glob("*")) == [ | ||
"docker-db-auto-backup-mariadb-1.sql", | ||
"docker-db-auto-backup-mysql-1.sql", | ||
"docker-db-auto-backup-psql-1.sql", | ||
"docker-db-auto-backup-redis-1.rdb", | ||
] | ||
for backup_file in BACKUP_DIR.glob("*"): | ||
assert backup_file.stat().st_size > 0 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"algorithm,extension", | ||
[("gzip", ".gz"), ("lzma", ".xz"), ("xz", ".xz"), ("bz2", ".bz2"), ("plain", "")], | ||
) | ||
def test_compressed_file_extension(algorithm: str, extension: str) -> None: | ||
assert db_auto_backup.get_compressed_file_extension(algorithm) == extension |