Skip to content

Commit 6fb93a0

Browse files
authored
Fix Redis container from aborting randomly (#405)
* Removed "--maxclients 100000" from Redis containers default CMD * Fix Redis memory overcommit warning by adding Redis configuration options - Added --save "", --appendonly no, and --maxmemory-policy noeviction to Redis startup command - Resolves the vm.overcommit_memory warning without using sysctl * Fix Redis protected mode warning by disabling protected mode Disabled Redis protected mode using the --protected-mode no option to resolve the "Possible SECURITY ATTACK detected" warning during tests. Protected mode is disabled because environments may trigger false positives due to network interactions, causing Redis to exit unexpectedly. This allows Redis to continue running without misinterpreting internal connections as attacks. * Revert "Added docker cleanup auto-fixture to improve tests stability (#396)" This reverts commit acd8010. * Reduced "xdist" and "parallel" tox env reruns from 5 to 3
1 parent b407343 commit 6fb93a0

File tree

6 files changed

+16
-62
lines changed

6 files changed

+16
-62
lines changed

Diff for: src/pytest_celery/vendors/redis/backend/fixtures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def default_redis_backend_command(default_redis_backend_cls: type[RedisContainer
6464
Returns:
6565
list[str]: Docker CMD instruction.
6666
"""
67-
return default_redis_backend_cls.command("--maxclients", "100000")
67+
return default_redis_backend_cls.command()
6868

6969

7070
@pytest.fixture

Diff for: src/pytest_celery/vendors/redis/broker/fixtures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def default_redis_broker_command(default_redis_broker_cls: type[RedisContainer])
6464
Returns:
6565
list[str]: Docker CMD instruction.
6666
"""
67-
return default_redis_broker_cls.command("--maxclients", "100000")
67+
return default_redis_broker_cls.command()
6868

6969

7070
@pytest.fixture

Diff for: src/pytest_celery/vendors/redis/container.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ def command(
4444
wait_for_client: bool = True,
4545
**kwargs: dict,
4646
) -> list[str]:
47-
return ["redis-server", *args]
47+
return [
48+
"redis-server",
49+
"--save",
50+
"",
51+
"--appendonly",
52+
"no",
53+
"--maxmemory-policy",
54+
"noeviction",
55+
"--protected-mode",
56+
"no",
57+
*args,
58+
]
4859

4960
@property
5061
def url(self) -> str:

Diff for: tests/conftest.py

-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22

3-
import docker
43
import pytest
54
from celery import Celery
65

@@ -46,54 +45,3 @@ def default_worker_app(default_worker_app: Celery) -> Celery:
4645
if app.conf.broker_url and app.conf.broker_url.startswith("sqs"):
4746
app.conf.broker_transport_options["region"] = LOCALSTACK_CREDS["AWS_DEFAULT_REGION"]
4847
return app
49-
50-
51-
@pytest.fixture(scope="module", autouse=True)
52-
def auto_clean_docker_resources():
53-
"""Clean up Docker resources after each test module."""
54-
# Used for debugging
55-
verbose = False
56-
57-
def log(message):
58-
if verbose:
59-
print(message)
60-
61-
def cleanup_docker_resources():
62-
"""Function to clean up Docker containers, networks, and volumes based
63-
on labels."""
64-
docker_client = docker.from_env()
65-
66-
try:
67-
# Clean up containers with the label 'creator=pytest-docker-tools'
68-
containers = docker_client.containers.list(all=True, filters={"label": "creator=pytest-docker-tools"})
69-
for con in containers:
70-
con.reload() # Ensure we have the latest status
71-
if con.status != "running": # Only remove non-running containers
72-
log(f"Removing container {con.name}")
73-
con.remove(force=True)
74-
else:
75-
log(f"Skipping running container {con.name}")
76-
77-
# Clean up networks with names starting with 'pytest-'
78-
networks = docker_client.networks.list(names=["pytest-*"])
79-
for network in networks:
80-
if not network.containers: # Check if the network is in use
81-
log(f"Removing network {network.name}")
82-
network.remove()
83-
else:
84-
log(f"Skipping network {network.name}, still in use")
85-
86-
# Clean up volumes with names starting with 'pytest-*'
87-
volumes = docker_client.volumes.list(filters={"name": "pytest-*"})
88-
for volume in volumes:
89-
if not volume.attrs.get("UsageData", {}).get("RefCount", 0): # Check if volume is not in use
90-
log(f"Removing volume {volume.name}")
91-
volume.remove()
92-
else:
93-
log(f"Skipping volume {volume.name}, still in use")
94-
95-
except Exception as e:
96-
log(f"Error occurred while cleaning up Docker resources: {e}")
97-
98-
log("--- Running Docker resource cleanup ---")
99-
cleanup_docker_resources()

Diff for: tests/unit/conftest.py

-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,3 @@ def default_redis_broker() -> RedisContainer:
5555
@pytest.fixture
5656
def default_worker_container() -> CeleryWorkerContainer:
5757
return mocked_container(CeleryWorkerContainer)
58-
59-
60-
@pytest.fixture(scope="module", autouse=True)
61-
def auto_clean_docker_resources():
62-
"""Skip cleanup in the unit tests."""

Diff for: tox.ini

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ commands_pre =
5555
commands =
5656
poetry run pytest tests --exitfirst \
5757
-n auto --dist=loadscope \
58-
--reruns 5 --rerun-except AssertionError \
58+
--reruns 3 --reruns-delay 5 --rerun-except AssertionError \
5959
{posargs}
6060

6161
[testenv:parallel]
@@ -68,7 +68,7 @@ commands_pre =
6868
commands =
6969
tox -e py312-unit,py312-integration,py312-smoke -p auto -o -- --exitfirst \
7070
-n auto --dist=loadscope \
71-
--reruns 5 --reruns-delay 60 --rerun-except AssertionError \
71+
--reruns 3 --reruns-delay 10 --rerun-except AssertionError \
7272
{posargs}
7373

7474
[testenv:mypy]

0 commit comments

Comments
 (0)