Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add stream_logs to docker.compose.down() #496

Merged
merged 2 commits into from
Nov 11, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 35 additions & 5 deletions python_on_whales/components/compose/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,38 @@ def create(
full_cmd += to_list(services)
run(full_cmd, capture_stdout=False)

@overload
def down(
self,
remove_orphans: bool = ...,
remove_images: Optional[str] = ...,
timeout: Optional[int] = ...,
volumes: bool = ...,
quiet: bool = ...,
stream_logs: Literal[True] = ...,
) -> Iterable[Tuple[str, bytes]]:
...

@overload
def down(
self,
remove_orphans: bool = ...,
remove_images: Optional[str] = ...,
timeout: Optional[int] = ...,
volumes: bool = ...,
quiet: bool = ...,
stream_logs: Literal[False] = ...,
) -> None:
...

def down(
self,
remove_orphans: bool = False,
remove_images: Optional[str] = None,
timeout: Optional[int] = None,
volumes: bool = False,
quiet: bool = False,
stream_logs: bool = False,
):
"""Stops and removes the containers

Expand All @@ -192,17 +217,22 @@ def down(
quiet: If `False`, send to stderr and stdout the progress spinners with
the messages. If `True`, do not display anything.
"""
if quiet and stream_logs:
raise ValueError(
"It's not possible to have stream_logs=True and quiet=True at the same time. "
"Only one can be activated at a time."
)

full_cmd = self.docker_compose_cmd + ["down"]
full_cmd.add_flag("--remove-orphans", remove_orphans)
full_cmd.add_simple_arg("--rmi", remove_images)
full_cmd.add_simple_arg("--timeout", timeout)
full_cmd.add_flag("--volumes", volumes)

run(full_cmd, capture_stderr=quiet, capture_stdout=quiet)

def events(self):
"""Not yet implemented"""
raise NotImplementedError
if stream_logs:
return stream_stdout_and_stderr(full_cmd)
else:
run(full_cmd, capture_stderr=quiet, capture_stdout=quiet)

def execute(
self,
Expand Down
25 changes: 24 additions & 1 deletion tests/python_on_whales/components/test_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,30 @@ def test_docker_compose_up_down():
compose_compatibility=True,
)
docker.compose.up(["busybox", "alpine"])
docker.compose.down(timeout=1)
output_of_down = docker.compose.down(timeout=1)
assert output_of_down is None
assert docker.compose.ps() == []


def test_docker_compose_up_down_streaming():
docker = DockerClient(
compose_files=[
PROJECT_ROOT
/ "tests/python_on_whales/components/dummy_compose_ends_quickly.yml"
],
compose_compatibility=True,
)
docker.compose.up(["busybox", "alpine"])
logs = list(docker.compose.down(timeout=1, stream_logs=True))

assert len(logs) >= 3
logs_as_big_binary = b""
for log_type, log_value in logs:
assert log_type in ("stdout", "stderr")
logs_as_big_binary += log_value

assert b"Removed" in logs_as_big_binary
assert b"Container components_alpine_1" in logs_as_big_binary


def test_no_containers():
Expand Down