Skip to content

raise an exception when docker compose fails to start #258

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

Closed
Closed
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
11 changes: 8 additions & 3 deletions compose/testcontainers/compose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Iterable, List, Optional, Tuple, Union

from testcontainers.core.waiting_utils import wait_container_is_ready
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.exceptions import ContainerStartException, NoSuchPortExposed


class DockerCompose:
Expand Down Expand Up @@ -110,7 +110,10 @@ def start(self) -> None:
if self.build:
up_cmd.append('--build')

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

if return_code:
raise ContainerStartException(f"Docker compose failed with return code {return_code}")

def stop(self) -> None:
"""
Expand Down Expand Up @@ -194,7 +197,9 @@ def _get_service_info(self, service: str, port: int) -> List[str]:
def _call_command(self, cmd: Union[str, List[str]], filepath: Optional[str] = None) -> None:
if filepath is None:
filepath = self.filepath
subprocess.call(cmd, cwd=filepath)

return subprocess.call(cmd, cwd=filepath)


@wait_container_is_ready(requests.exceptions.ConnectionError)
def wait_for(self, url: str) -> 'DockerCompose':
Expand Down
8 changes: 7 additions & 1 deletion compose/tests/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@

from testcontainers.compose import DockerCompose
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import NoSuchPortExposed
from testcontainers.core.exceptions import ContainerStartException, NoSuchPortExposed
from testcontainers.core.waiting_utils import wait_for_logs


ROOT = os.path.dirname(__file__)


def test_can_throw_exception_if_docker_command_fails():
with pytest.raises(ContainerStartException):
with DockerCompose(ROOT, compose_file_name='does-not-exist.yml'):
...


def test_can_spawn_service_via_compose():
with DockerCompose(ROOT) as compose:
host = compose.get_service_host("hub", 4444)
Expand Down