Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/mqtt/testcontainers/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def start(self, configfile: Optional[str] = None) -> Self:
# default config file
configfile = Path(__file__).parent / MosquittoContainer.CONFIG_FILE
self.with_volume_mapping(configfile, "/mosquitto/config/mosquitto.conf")
# since version 2.1.1 - 2026-02-04, which fixed a PUID/PGID issue, the container needs to write to the data directory,
# since version 2.1.1 - 2026-02-04, which fixed a PUID/PGID issue, the container needs to write to the data directory,
# so we mount it as tmpfs for better performance in tests
self.with_tmpfs_mount("/data")

Expand Down
23 changes: 11 additions & 12 deletions modules/redis/testcontainers/redis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from redis.asyncio import Redis as asyncRedis
from testcontainers.core.container import DockerContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready
from testcontainers.core.waiting_utils import WaitStrategy, WaitStrategyTarget


class RedisContainer(DockerContainer):
Expand All @@ -36,19 +36,13 @@ class RedisContainer(DockerContainer):

def __init__(self, image: str = "redis:latest", port: int = 6379, password: Optional[str] = None, **kwargs) -> None:
raise_for_deprecated_parameter(kwargs, "port_to_expose", "port")
super().__init__(image, **kwargs)
super().__init__(image, _wait_strategy=PingWaitStrategy(), **kwargs)
self.port = port
self.password = password
self.with_exposed_ports(self.port)
if self.password:
self.with_command(f"redis-server --requirepass {self.password}")

@wait_container_is_ready(redis.exceptions.ConnectionError)
def _connect(self) -> None:
client = self.get_client()
if not client.ping():
raise redis.exceptions.ConnectionError("Could not connect to Redis")

def get_client(self, **kwargs) -> redis.Redis:
"""
Get a redis client.
Expand All @@ -66,10 +60,15 @@ def get_client(self, **kwargs) -> redis.Redis:
**kwargs,
)

def start(self) -> "RedisContainer":
super().start()
self._connect()
return self

class PingWaitStrategy(WaitStrategy):
def __init__(self) -> None:
super().__init__()
self.with_transient_exceptions(redis.exceptions.ConnectionError)

def wait_until_ready(self, container: WaitStrategyTarget) -> None:
if not self._poll(lambda: container.get_client().ping()):
raise redis.exceptions.ConnectionError("Could not connect to Redis")


class AsyncRedisContainer(RedisContainer):
Expand Down
11 changes: 11 additions & 0 deletions modules/redis/tests/test_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from testcontainers.redis import RedisContainer, AsyncRedisContainer
import pytest
import redis


def test_docker_run_redis():
Expand All @@ -24,6 +25,16 @@ def test_docker_run_redis_with_password():
assert client.get("hello") == "world"


def test_docker_run_start_fails(monkeypatch: pytest.MonkeyPatch):
# Patch config to speed up the test.
monkeypatch.setattr("testcontainers.core.config.testcontainers_config.max_tries", 0.3)
monkeypatch.setattr("testcontainers.core.config.testcontainers_config.sleep_time", 0.02)
# Use a bogus image to make the startup check fail.
config = RedisContainer(image="hello-world")
with pytest.raises(redis.exceptions.ConnectionError, match="Could not connect"):
config.start()


pytest.mark.usefixtures("anyio_backend")


Expand Down
Loading