Skip to content

Commit d4445d6

Browse files
feat(reuse): do not change contract of stop method
1 parent efb1265 commit d4445d6

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

core/testcontainers/core/container.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,7 @@ def _start(self, hash_):
151151

152152
def stop(self, force=True, delete_volume=True) -> None:
153153
if self._container:
154-
if self._reuse and c.tc_properties_testcontainers_reuse_enable:
155-
self._container.stop()
156-
else:
157-
self._container.remove(force=force, v=delete_volume)
154+
self._container.remove(force=force, v=delete_volume)
158155
self.get_docker_client().client.close()
159156

160157
def __enter__(self) -> Self:

core/tests/test_reusable_containers.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled(monkeypatch):
3636
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
3737
monkeypatch.setattr(testcontainers_config, "ryuk_reconnection_timeout", "0.1s")
3838

39-
with DockerContainer("hello-world").with_reuse() as container:
40-
id = container._container.id
41-
wait_for_logs(container, "Hello from Docker!")
39+
container = DockerContainer("hello-world").with_reuse().start()
40+
id = container._container.id
41+
wait_for_logs(container, "Hello from Docker!")
4242

4343
Reaper._socket.close()
4444
# Sleep until Ryuk reaps all dangling containers
@@ -59,15 +59,15 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled(monkeypatch):
5959
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
6060
monkeypatch.setattr(testcontainers_config, "ryuk_disabled", True)
6161

62-
with DockerContainer("hello-world").with_reuse() as container:
63-
id = container._container.id
64-
wait_for_logs(container, "Hello from Docker!")
62+
container = DockerContainer("hello-world").with_reuse().start()
63+
id = container._container.id
64+
wait_for_logs(container, "Hello from Docker!")
6565

6666
containers = DockerClient().client.containers.list(all=True)
6767
assert id in [container.id for container in containers]
6868

6969
# Cleanup after keeping container alive (with_reuse)
70-
container._container.remove(force=True)
70+
container.stop()
7171

7272

7373
def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled_same_id(monkeypatch):
@@ -78,13 +78,15 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled_same_id(monkeyp
7878
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
7979
monkeypatch.setattr(testcontainers_config, "ryuk_disabled", True)
8080

81-
with DockerContainer("hello-world").with_reuse() as container:
82-
id = container._container.id
83-
with DockerContainer("hello-world").with_reuse() as container:
84-
assert id == container._container.id
81+
container_1 = DockerContainer("hello-world").with_reuse().start()
82+
id_1 = container_1._container.id
83+
container_2 = DockerContainer("hello-world").with_reuse().start()
84+
id_2 = container_2._container.id
85+
assert id_1 == id_2
8586

8687
# Cleanup after keeping container alive (with_reuse)
87-
container._container.remove(force=True)
88+
container_1.stop()
89+
# container_2.stop() is not needed since it is the same as container_1
8890

8991

9092
def test_docker_container_labels_hash():

index.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ Fetching passwords from cloud providers:
120120
Reusable Containers (Experimental)
121121
----------------------------------
122122

123-
Containers can be reused across consecutive test runs. To reuse a container, the container configuration must be the same.
123+
.. warning::
124+
Reusable Containers is still an experimental feature and the behavior can change.
125+
Those containers won't stop after all tests are finished.
124126

125-
Containers that are set up for reuse will not be automatically removed. Thus, those containers need to be removed manually.
127+
Containers can be reused across consecutive test runs. To reuse a container, the container has to be started manually by calling the `start()` method. Do not call the `stop()` method directly or indirectly via a `with` statement (context manager). To reuse a container, the container configuration must be the same.
128+
129+
Containers that are set up for reuse will not be automatically removed. Thus, if they are not needed anymore, those containers must be removed manually.
126130

127131
Containers should not be reused in a CI environment.
128132

@@ -131,16 +135,16 @@ How to use?
131135

132136
1. Add :code:`testcontainers.reuse.enable=true` to :code:`~/.testcontainers.properties`
133137
2. Disable ryuk by setting the environment variable :code:`TESTCONTAINERS_RYUK_DISABLED=true`
134-
3. Instantiate a container using :code:`with_reuse`
138+
3. Instantiate a container using :code:`with_reuse()` and :code:`start()`
135139

136140
.. doctest::
137141

138142
>>> from testcontainers.core.container import DockerContainer
139143

140-
>>> with DockerContainer("hello-world").with_reuse() as container:
141-
... first_id = container._container.id
142-
>>> with DockerContainer("hello-world").with_reuse() as container:
143-
... second_id == container._container.id
144+
>>> container = DockerContainer("hello-world").with_reuse().start()
145+
>>> first_id = container._container.id
146+
>>> container = DockerContainer("hello-world").with_reuse().start()
147+
>>> second_id == container._container.id
144148
>>> print(first_id == second_id)
145149
True
146150

0 commit comments

Comments
 (0)