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

fix(core): Avoid hanging upon bad docker host connection #742

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/testcontainers/core/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from testcontainers.core.config import ConnectionMode
from testcontainers.core.config import testcontainers_config as c
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.exceptions import ContainerStartException
from testcontainers.core.exceptions import ContainerConnectException, ContainerStartException
from testcontainers.core.labels import LABEL_SESSION_ID, SESSION_ID
from testcontainers.core.network import Network
from testcontainers.core.utils import is_arm, setup_logger
Expand Down Expand Up @@ -228,15 +228,21 @@
.with_env("RYUK_RECONNECTION_TIMEOUT", c.ryuk_reconnection_timeout)
.start()
)
wait_for_logs(Reaper._container, r".* Started!")
wait_for_logs(Reaper._container, r".* Started!", timeout=20, raise_on_exit=True)

container_host = Reaper._container.get_container_host_ip()
container_port = int(Reaper._container.get_exposed_port(8080))

if not container_host or not container_port:
raise ContainerConnectException(

Check warning on line 237 in core/testcontainers/core/container.py

View check run for this annotation

Codecov / codecov/patch

core/testcontainers/core/container.py#L237

Added line #L237 was not covered by tests
f"Could not obtain network details for {Reaper._container._container.id}. Host: {container_host} Port: {container_port}"
)

last_connection_exception: Optional[Exception] = None
for _ in range(50):
try:
Reaper._socket = socket()
Reaper._socket.settimeout(1)
Reaper._socket.connect((container_host, container_port))
last_connection_exception = None
break
Expand Down
4 changes: 4 additions & 0 deletions core/testcontainers/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ContainerStartException(RuntimeError):
pass


class ContainerConnectException(RuntimeError):
pass


class ContainerIsNotRunning(RuntimeError):
pass

Expand Down