Skip to content

Commit b6853ea

Browse files
authored
feat: Remove volumes when stopping services (#52)
1 parent d479fde commit b6853ea

File tree

16 files changed

+41
-75
lines changed

16 files changed

+41
-75
lines changed

src/pytest_databases/docker/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import subprocess # noqa: S404
66
import time
77
import timeit
8+
from contextlib import AbstractContextManager
89
from typing import TYPE_CHECKING, Any, Callable, Iterable
910

1011
from pytest_databases.helpers import simple_string_hash
1112

1213
if TYPE_CHECKING:
1314
from collections.abc import Awaitable, Generator
1415
from pathlib import Path
16+
from types import TracebackType
1517

1618
TRUE_VALUES = {"True", "true", "1", "yes", "Y", "T"}
1719

@@ -47,7 +49,7 @@ def wait_until_responsive(
4749
COMPOSE_PROJECT_NAME: str = f"pytest-databases-{simple_string_hash(__file__)}"
4850

4951

50-
class DockerServiceRegistry:
52+
class DockerServiceRegistry(AbstractContextManager):
5153
def __init__(
5254
self,
5355
worker_id: str,
@@ -65,6 +67,15 @@ def __init__(
6567
)
6668
self._before_start = list(before_start) if before_start else []
6769

70+
def __exit__(
71+
self,
72+
/,
73+
__exc_type: type[BaseException] | None,
74+
__exc_value: BaseException | None,
75+
__traceback: TracebackType | None,
76+
) -> None:
77+
self.down()
78+
6879
@staticmethod
6980
def _get_docker_ip() -> str:
7081
docker_host = os.environ.get("DOCKER_HOST", "").strip()
@@ -114,4 +125,4 @@ def stop(self, name: str) -> None:
114125

115126
def down(self) -> None:
116127
if not SKIP_DOCKER_COMPOSE:
117-
self.run_command("down", "-t", "10")
128+
self.run_command("down", "-t", "10", "--volumes")

src/pytest_databases/docker/alloydb_omni.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ def alloydb_docker_services(
4949
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
5050
pytest.skip("Docker not available on this platform")
5151

52-
registry = DockerServiceRegistry(worker_id, compose_project_name=alloydb_compose_project_name)
53-
try:
52+
with DockerServiceRegistry(worker_id, compose_project_name=alloydb_compose_project_name) as registry:
5453
yield registry
55-
finally:
56-
registry.down()
5754

5855

5956
@pytest.fixture(scope="session")

src/pytest_databases/docker/azure_blob.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,11 @@ def azure_blob_docker_services(
6262
azure_blob_service_startup_delay: int,
6363
worker_id: str = "main",
6464
) -> Generator[DockerServiceRegistry, None, None]:
65-
registry = DockerServiceRegistry(
65+
with DockerServiceRegistry(
6666
worker_id,
6767
compose_project_name=azure_blob_compose_project_name,
68-
)
69-
try:
68+
) as registry:
7069
yield registry
71-
finally:
72-
registry.down()
7370

7471

7572
@pytest.fixture(scope="session")

src/pytest_databases/docker/bigquery.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ def bigquery_docker_services(
5353
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
5454
pytest.skip("Docker not available on this platform")
5555

56-
registry = DockerServiceRegistry(worker_id, compose_project_name=bigquery_compose_project_name)
57-
try:
56+
with DockerServiceRegistry(worker_id, compose_project_name=bigquery_compose_project_name) as registry:
5857
yield registry
59-
finally:
60-
registry.down()
6158

6259

6360
@pytest.fixture(scope="session")

src/pytest_databases/docker/cockroachdb.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ def cockroachdb_docker_services(
4444
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
4545
pytest.skip("Docker not available on this platform")
4646

47-
registry = DockerServiceRegistry(worker_id, compose_project_name=cockroachdb_compose_project_name)
48-
try:
47+
with DockerServiceRegistry(worker_id, compose_project_name=cockroachdb_compose_project_name) as registry:
4948
yield registry
50-
finally:
51-
registry.down()
5249

5350

5451
@pytest.fixture(scope="session")

src/pytest_databases/docker/dragonfly.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ def dragonfly_docker_services(
2929
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
3030
pytest.skip("Docker not available on this platform")
3131

32-
registry = DockerServiceRegistry(worker_id, compose_project_name=dragonfly_compose_project_name)
33-
try:
32+
with DockerServiceRegistry(worker_id, compose_project_name=dragonfly_compose_project_name) as registry:
3433
yield registry
35-
finally:
36-
registry.down()
3734

3835

3936
@pytest.fixture(scope="session")

src/pytest_databases/docker/elastic_search.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ def elasticsearch_docker_services(
5151
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
5252
pytest.skip("Docker not available on this platform")
5353

54-
registry = DockerServiceRegistry(worker_id, compose_project_name=elasticsearch_compose_project_name)
55-
try:
54+
with DockerServiceRegistry(worker_id, compose_project_name=elasticsearch_compose_project_name) as registry:
5655
yield registry
57-
finally:
58-
registry.down()
5956

6057

6158
@pytest.fixture(scope="session")

src/pytest_databases/docker/keydb.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ def keydb_docker_services(
3030
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
3131
pytest.skip("Docker not available on this platform")
3232

33-
registry = DockerServiceRegistry(worker_id, compose_project_name=keydb_compose_project_name)
34-
try:
33+
with DockerServiceRegistry(worker_id, compose_project_name=keydb_compose_project_name) as registry:
3534
yield registry
36-
finally:
37-
registry.down()
3835

3936

4037
@pytest.fixture(scope="session")

src/pytest_databases/docker/mariadb.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ def mariadb_docker_services(
3131
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
3232
pytest.skip("Docker not available on this platform")
3333

34-
registry = DockerServiceRegistry(worker_id, compose_project_name=mariadb_compose_project_name)
35-
try:
34+
with DockerServiceRegistry(worker_id, compose_project_name=mariadb_compose_project_name) as registry:
3635
yield registry
37-
finally:
38-
registry.down()
3936

4037

4138
@pytest.fixture(scope="session")

src/pytest_databases/docker/mssql.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,8 @@ def mssql_docker_services(
4848
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
4949
pytest.skip("Docker not available on this platform")
5050

51-
registry = DockerServiceRegistry(worker_id, compose_project_name=mssql_compose_project_name)
52-
try:
51+
with DockerServiceRegistry(worker_id, compose_project_name=mssql_compose_project_name) as registry:
5352
yield registry
54-
finally:
55-
registry.down()
5653

5754

5855
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)