|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import os |
4 | | -import sys |
5 | | -from pathlib import Path |
6 | 3 | from typing import TYPE_CHECKING |
7 | 4 |
|
8 | 5 | import psycopg |
9 | 6 | import pytest |
10 | 7 |
|
| 8 | +from pytest_databases._service import DockerService |
11 | 9 | from pytest_databases.docker import DockerServiceRegistry |
12 | | -from pytest_databases.helpers import simple_string_hash |
13 | | - |
14 | | -if TYPE_CHECKING: |
15 | | - from collections.abc import Generator |
16 | | - |
17 | | - |
18 | | -COMPOSE_PROJECT_NAME: str = f"pytest-databases-alloydb-{simple_string_hash(__file__)}" |
19 | | - |
20 | | - |
21 | | -def alloydb_omni_responsive(host: str, port: int, user: str, password: str, database: str) -> bool: |
22 | | - try: |
23 | | - conn = psycopg.connect( |
24 | | - host=host, |
25 | | - port=port, |
26 | | - user=user, |
27 | | - database=database, |
28 | | - password=password, |
29 | | - ) |
30 | | - except Exception: # noqa: BLE001 |
31 | | - return False |
32 | | - |
33 | | - try: |
34 | | - db_open = conn.fetchrow("SELECT 1") |
35 | | - return bool(db_open is not None and db_open[0] == 1) |
36 | | - finally: |
37 | | - conn.close() |
38 | | - |
39 | | - |
40 | | -@pytest.fixture(scope="session") |
41 | | -def alloydb_compose_project_name() -> str: |
42 | | - return os.environ.get("COMPOSE_PROJECT_NAME", COMPOSE_PROJECT_NAME) |
43 | | - |
44 | | - |
45 | | -@pytest.fixture(autouse=False, scope="session") |
46 | | -def alloydb_docker_services( |
47 | | - alloydb_compose_project_name: str, worker_id: str = "main" |
48 | | -) -> Generator[DockerServiceRegistry, None, None]: |
49 | | - if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux": |
50 | | - pytest.skip("Docker not available on this platform") |
51 | | - |
52 | | - with DockerServiceRegistry(worker_id, compose_project_name=alloydb_compose_project_name) as registry: |
53 | | - yield registry |
| 10 | +from pytest_databases.docker.postgres import ( |
| 11 | + _make_connection_string, |
| 12 | + _provide_postgres_service, |
| 13 | + PostgresService as AlloyDBService, |
| 14 | +) |
54 | 15 |
|
| 16 | +__all__ = ("AlloyDBService",) |
55 | 17 |
|
56 | | -@pytest.fixture(scope="session") |
57 | | -def postgres_user() -> str: |
58 | | - return "postgres" |
59 | 18 |
|
60 | | - |
61 | | -@pytest.fixture(scope="session") |
62 | | -def postgres_password() -> str: |
63 | | - return "super-secret" |
64 | | - |
65 | | - |
66 | | -@pytest.fixture(scope="session") |
67 | | -def postgres_database() -> str: |
68 | | - return "postgres" |
69 | | - |
70 | | - |
71 | | -@pytest.fixture(scope="session") |
72 | | -def alloydb_omni_port() -> int: |
73 | | - return 5420 |
74 | | - |
75 | | - |
76 | | -@pytest.fixture(scope="session") |
77 | | -def default_alloydb_omni_service_name() -> str: |
78 | | - return "alloydb" |
79 | | - |
80 | | - |
81 | | -@pytest.fixture(scope="session") |
82 | | -def alloydb_docker_compose_files() -> list[Path]: |
83 | | - return [Path(Path(__file__).parent / "docker-compose.alloydb-omni.yml")] |
84 | | - |
85 | | - |
86 | | -@pytest.fixture(scope="session") |
87 | | -def alloydb_docker_ip(alloydb_docker_services: DockerServiceRegistry) -> str: |
88 | | - return alloydb_docker_services.docker_ip |
| 19 | +if TYPE_CHECKING: |
| 20 | + from collections.abc import Generator |
89 | 21 |
|
90 | 22 |
|
91 | | -# alias to the latest |
92 | 23 | @pytest.fixture(autouse=False, scope="session") |
93 | 24 | def alloydb_omni_service( |
94 | | - alloydb_docker_services: DockerServiceRegistry, |
95 | | - default_alloydb_omni_service_name: str, |
96 | | - alloydb_docker_compose_files: list[Path], |
97 | | - alloydb_omni_port: int, |
98 | | - postgres_database: str, |
99 | | - postgres_user: str, |
100 | | - postgres_password: str, |
101 | | -) -> Generator[None, None, None]: |
102 | | - os.environ["POSTGRES_PASSWORD"] = postgres_password |
103 | | - os.environ["POSTGRES_USER"] = postgres_user |
104 | | - os.environ["POSTGRES_DATABASE"] = postgres_database |
105 | | - os.environ[f"{default_alloydb_omni_service_name.upper()}_PORT"] = str(alloydb_omni_port) |
106 | | - alloydb_docker_services.start( |
107 | | - name=default_alloydb_omni_service_name, |
108 | | - docker_compose_files=alloydb_docker_compose_files, |
109 | | - timeout=45, |
110 | | - pause=1, |
111 | | - check=alloydb_omni_responsive, |
112 | | - port=alloydb_omni_port, |
113 | | - database=postgres_database, |
114 | | - user=postgres_user, |
115 | | - password=postgres_password, |
116 | | - ) |
117 | | - yield |
| 25 | + docker_service: DockerService, |
| 26 | +) -> Generator[DockerServiceRegistry, None, None]: |
| 27 | + with _provide_postgres_service( |
| 28 | + docker_service=docker_service, image="google/alloydbomni", name="alloydb-omni" |
| 29 | + ) as service: |
| 30 | + yield service |
118 | 31 |
|
119 | 32 |
|
120 | 33 | @pytest.fixture(autouse=False, scope="session") |
121 | | -def alloydb_omni_startup_connection( |
122 | | - alloydb_omni_service: DockerServiceRegistry, |
123 | | - alloydb_docker_ip: str, |
124 | | - alloydb_omni_port: int, |
125 | | - postgres_database: str, |
126 | | - postgres_user: str, |
127 | | - postgres_password: str, |
128 | | -) -> Generator[psycopg.Connection, None, None]: |
| 34 | +def alloydb_omni_startup_connection(alloydb_omni_service: AlloyDBService) -> Generator[psycopg.Connection, None, None]: |
129 | 35 | with psycopg.connect( |
130 | | - host=alloydb_docker_ip, |
131 | | - port=alloydb_omni_port, |
132 | | - user=postgres_user, |
133 | | - database=postgres_database, |
134 | | - password=postgres_password, |
| 36 | + _make_connection_string( |
| 37 | + host=alloydb_omni_service.host, |
| 38 | + port=alloydb_omni_service.port, |
| 39 | + user=alloydb_omni_service.user, |
| 40 | + database=alloydb_omni_service.database, |
| 41 | + password=alloydb_omni_service.password, |
| 42 | + ) |
135 | 43 | ) as conn: |
136 | 44 | yield conn |
0 commit comments