Skip to content

Commit

Permalink
feat(core): DockerCompose.stop now stops only services that it starts…
Browse files Browse the repository at this point in the history
… (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.
  • Loading branch information
aksel authored Jun 27, 2024
1 parent 27f2a6b commit e711800
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ def stop(self, down=True) -> None:
down_cmd += ["down", "--volumes"]
else:
down_cmd += ["stop"]

if self.services:
down_cmd.extend(self.services)

self._run_command(cmd=down_cmd)

def get_logs(self, *services: str) -> tuple[str, str]:
Expand Down
15 changes: 15 additions & 0 deletions core/tests/compose_fixtures/basic_multiple/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
services:
alpine1:
image: alpine:latest
init: true
command:
- sh
- -c
- 'while true; do sleep 0.1 ; date -Ins; done'
alpine2:
image: alpine:latest
init: true
command:
- sh
- -c
- 'while true; do sleep 0.1 ; date -Ins; done'
49 changes: 49 additions & 0 deletions core/tests/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,55 @@ def test_compose_start_stop():
basic.stop()


def test_start_stop_multiple():
"""Start and stop multiple containers individually."""

# Create two DockerCompose instances from the same file, one service each.
dc_a = DockerCompose(context=FIXTURES / "basic_multiple", services=["alpine1"])
dc_b = DockerCompose(context=FIXTURES / "basic_multiple", services=["alpine2"])

# After starting the first instance, alpine1 should be running
dc_a.start()
dc_a.get_container("alpine1") # Raises if it isn't running
dc_b.get_container("alpine1") # Raises if it isn't running

# Both instances report the same number of containers
assert len(dc_a.get_containers()) == 1
assert len(dc_b.get_containers()) == 1

# Although alpine1 is running, alpine2 has not started yet.
with pytest.raises(ContainerIsNotRunning):
dc_a.get_container("alpine2")
with pytest.raises(ContainerIsNotRunning):
dc_b.get_container("alpine2")

# After starting the second instance, alpine2 should also be running
dc_b.start()
dc_a.get_container("alpine2") # No longer raises
dc_b.get_container("alpine2") # No longer raises
assert len(dc_a.get_containers()) == 2
assert len(dc_b.get_containers()) == 2

# After stopping the first instance, alpine1 should no longer be running
dc_a.stop()
dc_a.get_container("alpine2")
dc_b.get_container("alpine2")
assert len(dc_a.get_containers()) == 1
assert len(dc_b.get_containers()) == 1

# alpine1 no longer running
with pytest.raises(ContainerIsNotRunning):
dc_a.get_container("alpine1")
with pytest.raises(ContainerIsNotRunning):
dc_b.get_container("alpine1")

# Stop the second instance
dc_b.stop()

assert len(dc_a.get_containers()) == 0
assert len(dc_b.get_containers()) == 0


def test_compose():
"""stream-of-consciousness e2e test"""
basic = DockerCompose(context=FIXTURES / "basic")
Expand Down

0 comments on commit e711800

Please sign in to comment.