Skip to content

Commit e711800

Browse files
authoredJun 27, 2024··
feat(core): DockerCompose.stop now stops only services that it starts (does not stop the other services) (#620)
The command would otherwise stop/down all services, not just the services the instance itself started. Useful for e.g. one fixture per service, and you want different scopes for the services.
1 parent 27f2a6b commit e711800

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
 

‎core/testcontainers/compose/compose.py

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def stop(self, down=True) -> None:
236236
down_cmd += ["down", "--volumes"]
237237
else:
238238
down_cmd += ["stop"]
239+
240+
if self.services:
241+
down_cmd.extend(self.services)
242+
239243
self._run_command(cmd=down_cmd)
240244

241245
def get_logs(self, *services: str) -> tuple[str, str]:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
services:
2+
alpine1:
3+
image: alpine:latest
4+
init: true
5+
command:
6+
- sh
7+
- -c
8+
- 'while true; do sleep 0.1 ; date -Ins; done'
9+
alpine2:
10+
image: alpine:latest
11+
init: true
12+
command:
13+
- sh
14+
- -c
15+
- 'while true; do sleep 0.1 ; date -Ins; done'

‎core/tests/test_compose.py

+49
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,55 @@ def test_compose_start_stop():
3737
basic.stop()
3838

3939

40+
def test_start_stop_multiple():
41+
"""Start and stop multiple containers individually."""
42+
43+
# Create two DockerCompose instances from the same file, one service each.
44+
dc_a = DockerCompose(context=FIXTURES / "basic_multiple", services=["alpine1"])
45+
dc_b = DockerCompose(context=FIXTURES / "basic_multiple", services=["alpine2"])
46+
47+
# After starting the first instance, alpine1 should be running
48+
dc_a.start()
49+
dc_a.get_container("alpine1") # Raises if it isn't running
50+
dc_b.get_container("alpine1") # Raises if it isn't running
51+
52+
# Both instances report the same number of containers
53+
assert len(dc_a.get_containers()) == 1
54+
assert len(dc_b.get_containers()) == 1
55+
56+
# Although alpine1 is running, alpine2 has not started yet.
57+
with pytest.raises(ContainerIsNotRunning):
58+
dc_a.get_container("alpine2")
59+
with pytest.raises(ContainerIsNotRunning):
60+
dc_b.get_container("alpine2")
61+
62+
# After starting the second instance, alpine2 should also be running
63+
dc_b.start()
64+
dc_a.get_container("alpine2") # No longer raises
65+
dc_b.get_container("alpine2") # No longer raises
66+
assert len(dc_a.get_containers()) == 2
67+
assert len(dc_b.get_containers()) == 2
68+
69+
# After stopping the first instance, alpine1 should no longer be running
70+
dc_a.stop()
71+
dc_a.get_container("alpine2")
72+
dc_b.get_container("alpine2")
73+
assert len(dc_a.get_containers()) == 1
74+
assert len(dc_b.get_containers()) == 1
75+
76+
# alpine1 no longer running
77+
with pytest.raises(ContainerIsNotRunning):
78+
dc_a.get_container("alpine1")
79+
with pytest.raises(ContainerIsNotRunning):
80+
dc_b.get_container("alpine1")
81+
82+
# Stop the second instance
83+
dc_b.stop()
84+
85+
assert len(dc_a.get_containers()) == 0
86+
assert len(dc_b.get_containers()) == 0
87+
88+
4089
def test_compose():
4190
"""stream-of-consciousness e2e test"""
4291
basic = DockerCompose(context=FIXTURES / "basic")

0 commit comments

Comments
 (0)
Please sign in to comment.