diff --git a/core/testcontainers/core/container.py b/core/testcontainers/core/container.py index cf61a85bf..4fc80e4f9 100644 --- a/core/testcontainers/core/container.py +++ b/core/testcontainers/core/container.py @@ -82,6 +82,8 @@ def __init__( for vol in volumes: self.with_volume_mapping(*vol) + self.tmpfs: dict[str, str] = {} + self.image = image self._docker = DockerClient(**(docker_client_kw or {})) self._container: Optional[Container] = None @@ -198,6 +200,7 @@ def start(self) -> Self: ports=cast("dict[int, Optional[int]]", self.ports), name=self._name, volumes=self.volumes, + tmpfs=self.tmpfs, **{**network_kwargs, **self._kwargs}, ) @@ -270,6 +273,16 @@ def with_volume_mapping(self, host: Union[str, PathLike[str]], container: str, m self.volumes[str(host)] = mapping return self + def with_tmpfs_mount(self, container_path: str, size: Optional[str] = None) -> Self: + """Mount a tmpfs volume on the container. + + :param container_path: Container path to mount tmpfs on (e.g., '/data') + :param size: Optional size limit (e.g., '256m', '1g'). If None, unbounded. + :return: Self for chaining + """ + self.tmpfs[container_path] = size or "" + return self + def get_wrapped_container(self) -> "Container": return self._container diff --git a/modules/mqtt/testcontainers/mqtt/__init__.py b/modules/mqtt/testcontainers/mqtt/__init__.py index 54a2d87ac..854ec21f8 100644 --- a/modules/mqtt/testcontainers/mqtt/__init__.py +++ b/modules/mqtt/testcontainers/mqtt/__init__.py @@ -121,6 +121,10 @@ 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, + # so we mount it as tmpfs for better performance in tests + self.with_tmpfs_mount("/data") + # if self.password: # # TODO: add authentication # pass diff --git a/modules/mqtt/testcontainers/mqtt/testcontainers-mosquitto-default-configuration.conf b/modules/mqtt/testcontainers/mqtt/testcontainers-mosquitto-default-configuration.conf index 13728cec0..b3c67048f 100644 --- a/modules/mqtt/testcontainers/mqtt/testcontainers-mosquitto-default-configuration.conf +++ b/modules/mqtt/testcontainers/mqtt/testcontainers-mosquitto-default-configuration.conf @@ -1,7 +1,6 @@ # see https://mosquitto.org/man/mosquitto-conf-5.html -protocol mqtt -user root +listener 1883 log_dest stdout allow_anonymous true @@ -14,7 +13,4 @@ log_timestamp_format %Y-%m-%d %H:%M:%S persistence true persistence_location /data/ -listener 1883 -protocol mqtt - sys_interval 1 diff --git a/modules/mqtt/tests/test_mosquitto.py b/modules/mqtt/tests/test_mosquitto.py index 63ce7fcd9..1e058103c 100644 --- a/modules/mqtt/tests/test_mosquitto.py +++ b/modules/mqtt/tests/test_mosquitto.py @@ -2,7 +2,7 @@ from testcontainers.mqtt import MosquittoContainer -VERSIONS = ["1.6.15", "2.0.18"] +VERSIONS = ["1.6.15", "2.0.18", "2.1.2-alpine"] @pytest.mark.parametrize("version", VERSIONS)