|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import Callable |
| 5 | + |
| 6 | +import pytest |
| 7 | +from infrahub_testcontainers.container import InfrahubDockerCompose |
| 8 | +from testcontainers.core.exceptions import ContainerIsNotRunning |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(name="compose_factory") |
| 12 | +def fixture_compose_factory(tmp_path: Path) -> Callable[[], InfrahubDockerCompose]: |
| 13 | + """Provide a factory that yields an isolated ``InfrahubDockerCompose`` test double.""" |
| 14 | + |
| 15 | + def _factory() -> InfrahubDockerCompose: |
| 16 | + instance = InfrahubDockerCompose.__new__(InfrahubDockerCompose) |
| 17 | + instance.context = tmp_path |
| 18 | + instance.env_vars = { |
| 19 | + "INFRAHUB_TESTING_LOCAL_DB_BACKUP_DIRECTORY": "backups", |
| 20 | + "INFRAHUB_TESTING_INTERNAL_DB_BACKUP_DIRECTORY": "/backups", |
| 21 | + "NEO4J_DOCKER_IMAGE": "neo4j:2025.03.0-enterprise", |
| 22 | + } |
| 23 | + instance.services = [] |
| 24 | + instance.pull = False |
| 25 | + instance.wait = False |
| 26 | + instance.compose_file_name = None |
| 27 | + instance.docker_command_path = None |
| 28 | + instance.project_name = None |
| 29 | + instance.env_file = None |
| 30 | + |
| 31 | + def _noop_service(service_name: str) -> None: |
| 32 | + """Consume a service name without performing any action.""" |
| 33 | + |
| 34 | + _ = service_name |
| 35 | + |
| 36 | + instance.get_container = _noop_service # type: ignore[assignment] |
| 37 | + instance.start_container = _noop_service # type: ignore[assignment] |
| 38 | + instance.exec_calls: list[list[str]] = [] |
| 39 | + |
| 40 | + def _exec(command: list[str], service_name: str) -> tuple[str, str, int]: |
| 41 | + _ = service_name |
| 42 | + instance.exec_calls.append(command) |
| 43 | + if command and command[0] == "pg_dump": |
| 44 | + file_arg = next(arg for arg in command if arg.startswith("--file=")) |
| 45 | + container_path = Path(file_arg.split("=", 1)[1]) |
| 46 | + host_path = instance.external_backup_dir / container_path.name |
| 47 | + host_path.parent.mkdir(parents=True, exist_ok=True) |
| 48 | + host_path.write_bytes(b"backup-bytes") |
| 49 | + return "", "", 0 |
| 50 | + |
| 51 | + instance.exec_in_container = _exec # type: ignore[assignment] |
| 52 | + return instance |
| 53 | + |
| 54 | + return _factory |
| 55 | + |
| 56 | + |
| 57 | +def test_task_manager_db_create_backup_creates_archive(compose_factory: Callable[[], InfrahubDockerCompose]) -> None: |
| 58 | + compose = compose_factory() |
| 59 | + |
| 60 | + result = compose.task_manager_db_create_backup() |
| 61 | + |
| 62 | + assert result.name == "prefect.dump" |
| 63 | + assert result.exists() |
| 64 | + assert compose.exec_calls[0][0] == "pg_dump" |
| 65 | + |
| 66 | + |
| 67 | +def test_task_manager_db_create_backup_respects_destination( |
| 68 | + compose_factory: Callable[[], InfrahubDockerCompose], tmp_path: Path |
| 69 | +) -> None: |
| 70 | + compose = compose_factory() |
| 71 | + destination = tmp_path / "exports" |
| 72 | + |
| 73 | + result = compose.task_manager_db_create_backup(dest_dir=destination) |
| 74 | + |
| 75 | + assert result.parent == destination |
| 76 | + assert result.exists() |
| 77 | + assert (compose.external_backup_dir / "prefect.dump").exists() |
| 78 | + |
| 79 | + |
| 80 | +def test_task_manager_db_restore_backup_runs_expected_commands( |
| 81 | + compose_factory: Callable[[], InfrahubDockerCompose], tmp_path: Path |
| 82 | +) -> None: |
| 83 | + compose = compose_factory() |
| 84 | + |
| 85 | + def _raise(service_name: str) -> None: |
| 86 | + _ = service_name |
| 87 | + raise ContainerIsNotRunning("task-manager-db") |
| 88 | + |
| 89 | + compose.get_container = _raise # type: ignore[assignment] |
| 90 | + started_services: list[str] = [] |
| 91 | + |
| 92 | + def _record_start(service_name: str) -> None: |
| 93 | + started_services.append(service_name) |
| 94 | + |
| 95 | + compose.start_container = _record_start # type: ignore[assignment] |
| 96 | + recorded_commands: list[list[str]] = [] |
| 97 | + |
| 98 | + def _exec(command: list[str], service_name: str) -> tuple[str, str, int]: |
| 99 | + _ = service_name |
| 100 | + recorded_commands.append(command) |
| 101 | + return "", "", 0 |
| 102 | + |
| 103 | + compose.exec_in_container = _exec # type: ignore[assignment] |
| 104 | + |
| 105 | + backup_file = tmp_path / "prefect.dump" |
| 106 | + backup_file.write_bytes(b"backup") |
| 107 | + |
| 108 | + compose.task_manager_db_restore_backup(backup_file) |
| 109 | + |
| 110 | + assert started_services == ["task-manager-db"] |
| 111 | + assert (compose.external_backup_dir / backup_file.name).exists() |
| 112 | + assert [command[0] for command in recorded_commands] == [ |
| 113 | + "psql", |
| 114 | + "psql", |
| 115 | + "psql", |
| 116 | + "pg_restore", |
| 117 | + ] |
0 commit comments