Skip to content

Commit

Permalink
Added docker cleanup auto-fixture to improve tests stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Nusnus committed Sep 7, 2024
1 parent c474854 commit 31b9fab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess

import pytest
from celery import Celery
Expand Down Expand Up @@ -45,3 +46,31 @@ def default_worker_app(default_worker_app: Celery) -> Celery:
if app.conf.broker_url and app.conf.broker_url.startswith("sqs"):
app.conf.broker_transport_options["region"] = LOCALSTACK_CREDS["AWS_DEFAULT_REGION"]
return app


@pytest.fixture(scope="module", autouse=True)
def auto_clean_docker_resources():
"""Clean up docker resources after each test module."""

def run_shell_command(command):
try:
subprocess.run(
command,
shell=True,
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except Exception:
pass

docker_cleanup_commands = [
"containers=$(docker ps -aq --filter label=creator=pytest-docker-tools); "
'if [ -n "$containers" ]; then docker rm -f $containers; fi',
"networks=$(docker network ls --filter name=pytest- -q); "
'if [ -n "$networks" ]; then docker network rm $networks; fi',
"volumes=$(docker volume ls --filter name=pytest- -q); "
'if [ -n "$volumes" ]; then docker volume rm $volumes; fi',
]
for command in docker_cleanup_commands:
run_shell_command(command)
5 changes: 5 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ def default_redis_broker() -> RedisContainer:
@pytest.fixture
def default_worker_container() -> CeleryWorkerContainer:
return mocked_container(CeleryWorkerContainer)


@pytest.fixture(scope="module", autouse=True)
def auto_clean_docker_resources():
"""Skip cleanup in the unit tests."""

0 comments on commit 31b9fab

Please sign in to comment.