Skip to content

Commit 65e70c8

Browse files
f
1 parent 546d303 commit 65e70c8

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

core/testcontainers/core/config.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
RYUK_DOCKER_SOCKET: str = environ.get("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE", "/var/run/docker.sock")
1616
RYUK_RECONNECTION_TIMEOUT: str = environ.get("RYUK_RECONNECTION_TIMEOUT", "10s")
1717
TC_HOST_OVERRIDE: Optional[str] = environ.get("TC_HOST", environ.get("TESTCONTAINERS_HOST_OVERRIDE"))
18-
REUSE_ENABLED: bool = environ.get("TESTCONTAINERS_REUSE_ENABLED", "false") == "true"
1918

2019
TC_FILE = ".testcontainers.properties"
2120
TC_GLOBAL = Path.home() / TC_FILE
@@ -58,7 +57,6 @@ class TestcontainersConfiguration:
5857
tc_properties: dict[str, str] = field(default_factory=read_tc_properties)
5958
_docker_auth_config: Optional[str] = field(default_factory=lambda: environ.get("DOCKER_AUTH_CONFIG"))
6059
tc_host_override: Optional[str] = TC_HOST_OVERRIDE
61-
reuse_enabled: bool = REUSE_ENABLED
6260
"""
6361
https://github.com/testcontainers/testcontainers-go/blob/dd76d1e39c654433a3d80429690d07abcec04424/docker.go#L644
6462
if os env TC_HOST is set, use it
@@ -87,8 +85,8 @@ def tc_properties_tc_host(self) -> Union[str, None]:
8785
return self.tc_properties.get("tc.host")
8886

8987
@property
90-
def tc_properties_testcontainers_reuse_enabled(self) -> bool:
91-
enabled = self.tc_properties.get("testcontainers.reuse.enabled")
88+
def tc_properties_testcontainers_reuse_enable(self) -> bool:
89+
enabled = self.tc_properties.get("testcontainers.reuse.enable")
9290
return enabled == "true"
9391

9492
@property

core/testcontainers/core/container.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22
import hashlib
3+
import logging
34
from platform import system
45
from socket import socket
56
from typing import TYPE_CHECKING, Optional
@@ -50,7 +51,7 @@ def __init__(
5051
self._name = None
5152
self._network: Optional[Network] = None
5253
self._network_aliases: Optional[list[str]] = None
53-
self._reuse: bool = kwargs.pop("reuse", False)
54+
self._reuse: bool = False
5455
self._kwargs = kwargs
5556

5657
def with_env(self, key: str, value: str) -> Self:
@@ -106,9 +107,16 @@ def start(self) -> Self:
106107
)
107108
hash_ = hashlib.sha256(bytes(str(args), encoding="utf-8")).hexdigest()
108109

109-
# TODO: warn user if self._reuse=True but reuse_enabled=False
110-
reuse_enabled = c.reuse_enabled or c.tc_properties_testcontainers_reuse_enabled
111-
if self._reuse and reuse_enabled:
110+
# TODO: check also if ryuk is disabled
111+
if self._reuse and not c.tc_properties_testcontainers_reuse_enable:
112+
logging.warning(
113+
"Reuse was requested (`with_reuse`) but the environment does not "
114+
+ "support the reuse of containers. To enable container reuse, add "
115+
+ "the property 'testcontainers.reuse.enable=true' to a file at "
116+
+ "~/.testcontainers.properties (you may need to create it)."
117+
)
118+
119+
if self._reuse and c.tc_properties_testcontainers_reuse_enable:
112120
docker_client = self.get_docker_client()
113121
container = docker_client.find_container_by_hash(hash_)
114122
if container:
@@ -143,7 +151,7 @@ def _start(self, hash_):
143151

144152
def stop(self, force=True, delete_volume=True) -> None:
145153
if self._container:
146-
if self._reuse and c.reuse_enabled:
154+
if self._reuse and c.tc_properties_testcontainers_reuse_enable:
147155
self._container.stop()
148156
else:
149157
self._container.remove(force=force, v=delete_volume)

core/tests/test_reusable_containers.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from time import sleep
22

33
from docker.models.containers import Container
4-
from docker import DockerClient as DockerDockerClient
54

65
from testcontainers.core.config import testcontainers_config
76
from testcontainers.core.container import DockerContainer
@@ -31,7 +30,8 @@ def test_docker_container_with_reuse_reuse_disabled():
3130
def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled(monkeypatch):
3231
# Make sure Ryuk cleanup is not active from previous test runs
3332
Reaper.delete_instance()
34-
monkeypatch.setattr(testcontainers_config, "reuse_enabled", True)
33+
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
34+
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
3535
monkeypatch.setattr(testcontainers_config, "ryuk_reconnection_timeout", "0.1s")
3636

3737
with DockerContainer("hello-world").with_reuse() as container:
@@ -52,7 +52,8 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled(monkeypatch):
5252
def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled(monkeypatch):
5353
# Make sure Ryuk cleanup is not active from previous test runs
5454
Reaper.delete_instance()
55-
monkeypatch.setattr(testcontainers_config, "reuse_enabled", True)
55+
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
56+
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
5657
monkeypatch.setattr(testcontainers_config, "ryuk_disabled", True)
5758
with DockerContainer("hello-world").with_reuse() as container:
5859
assert container._reuse == True

0 commit comments

Comments
 (0)