Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class DockerCompose:
pull: bool = False
build: bool = False
wait: bool = True
keep_volumes: bool = False
env_file: Optional[str] = None
services: Optional[list[str]] = None
docker_command_path: Optional[str] = None
Expand All @@ -178,7 +179,7 @@ def __enter__(self) -> "DockerCompose":
return self

def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.stop()
self.stop(not self.keep_volumes)

def docker_compose_command(self) -> list[str]:
"""
Expand Down
17 changes: 17 additions & 0 deletions core/tests/compose_fixtures/basic_volume/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
alpine:
image: alpine:latest
init: true
command:
- sh
- -c
- 'while true; do sleep 0.1 ; date -Ins; done'
read_only: true
volumes:
- type: volume
source: my-data
target: /var/lib/example/data
read_only: false

volumes:
my-data: {}
22 changes: 22 additions & 0 deletions core/tests/test_compose.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
from pathlib import Path
from re import split
from time import sleep
Expand Down Expand Up @@ -147,6 +148,27 @@ def test_compose_logs():
assert not line or container.Service in next(iter(line.split("|")), None)


def test_compose_volumes():
_file_in_volume = "/var/lib/example/data/hello"
volumes = DockerCompose(context=FIXTURES / "basic_volume", keep_volumes=True)
with volumes:
stdout, stderr, exitcode = volumes.exec_in_container(
["/bin/sh", "-c", f"echo hello > {_file_in_volume}"], "alpine"
)
assert exitcode == 0

# execute another time to confirm the file is still there, but we're not keeping the volumes this time
volumes.keep_volumes = False
with volumes:
stdout, stderr, exitcode = volumes.exec_in_container(["cat", _file_in_volume], "alpine")
assert exitcode == 0
assert "hello" in stdout

# third time we expect the file to be missing
with volumes, pytest.raises(subprocess.CalledProcessError):
volumes.exec_in_container(["cat", _file_in_volume], "alpine")


# noinspection HttpUrlsUsage
def test_compose_ports():
# fairly straight forward - can we get the right port to request it
Expand Down
Loading