Skip to content

Commit 1d57547

Browse files
raise an exception when docker compose fails to start #258
1 parent 13742a5 commit 1d57547

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

core/testcontainers/compose/compose.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import subprocess
21
from dataclasses import dataclass, field, fields
32
from functools import cached_property
43
from json import loads
54
from os import PathLike
65
from re import split
6+
from subprocess import CompletedProcess, run as subprocess_run
77
from typing import Callable, Literal, Optional, TypeVar, Union
88
from urllib.error import HTTPError, URLError
99
from urllib.request import urlopen
@@ -197,7 +197,7 @@ def start(self) -> None:
197197
# pull means running a separate command before starting
198198
if self.pull:
199199
pull_cmd = [*base_cmd, "pull"]
200-
self._call_command(cmd=pull_cmd)
200+
self._run_command(cmd=pull_cmd)
201201

202202
up_cmd = [*base_cmd, "up"]
203203

@@ -214,7 +214,7 @@ def start(self) -> None:
214214
if self.services:
215215
up_cmd.extend(self.services)
216216

217-
self._call_command(cmd=up_cmd)
217+
self._run_command(cmd=up_cmd)
218218

219219
def stop(self, down=True) -> None:
220220
"""
@@ -225,7 +225,7 @@ def stop(self, down=True) -> None:
225225
down_cmd += ["down", "--volumes"]
226226
else:
227227
down_cmd += ["stop"]
228-
self._call_command(cmd=down_cmd)
228+
self._run_command(cmd=down_cmd)
229229

230230
def get_logs(self, *services: str) -> tuple[str, str]:
231231
"""
@@ -239,11 +239,7 @@ def get_logs(self, *services: str) -> tuple[str, str]:
239239
"""
240240
logs_cmd = [*self.compose_command_property, "logs", *services]
241241

242-
result = subprocess.run(
243-
logs_cmd,
244-
cwd=self.context,
245-
capture_output=True,
246-
)
242+
result = self._run_command(cmd=logs_cmd)
247243
return result.stdout.decode("utf-8"), result.stderr.decode("utf-8")
248244

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

265261
containers = []
@@ -322,22 +318,22 @@ def exec_in_container(
322318
if not service_name:
323319
service_name = self.get_container().Service
324320
exec_cmd = [*self.compose_command_property, "exec", "-T", service_name, *command]
325-
result = subprocess.run(
326-
exec_cmd,
327-
cwd=self.context,
328-
capture_output=True,
329-
check=True,
330-
)
321+
result = self._run_command(cmd=exec_cmd)
331322

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

334-
def _call_command(
325+
def _run_command(
335326
self,
336327
cmd: Union[str, list[str]],
337328
context: Optional[str] = None,
338-
) -> None:
329+
) -> CompletedProcess[bytes]:
339330
context = context or self.context
340-
subprocess.call(cmd, cwd=context)
331+
return subprocess_run(
332+
cmd,
333+
capture_output=True,
334+
check=True,
335+
cwd=context,
336+
)
341337

342338
def get_service_port(
343339
self,

0 commit comments

Comments
 (0)