Skip to content

fix(core): raise an exception when docker compose fails to start #258 #485

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

Merged
Merged
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
35 changes: 16 additions & 19 deletions core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import subprocess
from dataclasses import dataclass, field, fields
from functools import cached_property
from json import loads
from os import PathLike
from re import split
from subprocess import CompletedProcess
from subprocess import run as subprocess_run
from typing import Callable, Literal, Optional, TypeVar, Union
from urllib.error import HTTPError, URLError
from urllib.request import urlopen
Expand Down Expand Up @@ -197,7 +198,7 @@ def start(self) -> None:
# pull means running a separate command before starting
if self.pull:
pull_cmd = [*base_cmd, "pull"]
self._call_command(cmd=pull_cmd)
self._run_command(cmd=pull_cmd)

up_cmd = [*base_cmd, "up"]

Expand All @@ -214,7 +215,7 @@ def start(self) -> None:
if self.services:
up_cmd.extend(self.services)

self._call_command(cmd=up_cmd)
self._run_command(cmd=up_cmd)

def stop(self, down=True) -> None:
"""
Expand All @@ -225,7 +226,7 @@ def stop(self, down=True) -> None:
down_cmd += ["down", "--volumes"]
else:
down_cmd += ["stop"]
self._call_command(cmd=down_cmd)
self._run_command(cmd=down_cmd)

def get_logs(self, *services: str) -> tuple[str, str]:
"""
Expand All @@ -239,11 +240,7 @@ def get_logs(self, *services: str) -> tuple[str, str]:
"""
logs_cmd = [*self.compose_command_property, "logs", *services]

result = subprocess.run(
logs_cmd,
cwd=self.context,
capture_output=True,
)
result = self._run_command(cmd=logs_cmd)
return result.stdout.decode("utf-8"), result.stderr.decode("utf-8")

def get_containers(self, include_all=False) -> list[ComposeContainer]:
Expand All @@ -259,7 +256,7 @@ def get_containers(self, include_all=False) -> list[ComposeContainer]:
cmd = [*self.compose_command_property, "ps", "--format", "json"]
if include_all:
cmd = [*cmd, "-a"]
result = subprocess.run(cmd, cwd=self.context, check=True, stdout=subprocess.PIPE)
result = self._run_command(cmd=cmd)
stdout = split(r"\r?\n", result.stdout.decode("utf-8"))

containers = []
Expand Down Expand Up @@ -322,22 +319,22 @@ def exec_in_container(
if not service_name:
service_name = self.get_container().Service
exec_cmd = [*self.compose_command_property, "exec", "-T", service_name, *command]
result = subprocess.run(
exec_cmd,
cwd=self.context,
capture_output=True,
check=True,
)
result = self._run_command(cmd=exec_cmd)

return (result.stdout.decode("utf-8"), result.stderr.decode("utf-8"), result.returncode)

def _call_command(
def _run_command(
self,
cmd: Union[str, list[str]],
context: Optional[str] = None,
) -> None:
) -> CompletedProcess[bytes]:
context = context or self.context
subprocess.call(cmd, cwd=context)
return subprocess_run(
cmd,
capture_output=True,
check=True,
cwd=context,
)

def get_service_port(
self,
Expand Down