Skip to content

Commit

Permalink
✨ Add stream_logs to docker.compose.down() (#496)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldemarmiesse committed Nov 11, 2023
1 parent 3599854 commit de920f3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
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

0 comments on commit de920f3

Please sign in to comment.