Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/testcontainers/core/wait_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ def wait_until_ready(self, container: "WaitStrategyTarget") -> None:
wrapped.reload()
if wrapped.status not in _NOT_EXITED_STATUSES:
# Get exit information for better debugging
stdout_str, stderr_str = _get_container_logs_for_debugging(container)
status_info = _get_container_status_info(container)
message_pattern = self._message.pattern if hasattr(self._message, "pattern") else str(self._message)

raise RuntimeError(
f"Container exited (status: {status_info['status']}, exit code: {status_info['exit_code']}) "
f"before emitting logs containing '{self._message.pattern if hasattr(self._message, 'pattern') else str(self._message)}'. "
f"before emitting logs containing '{message_pattern}'. "
f"Container error: {status_info['error']}. "
f"Recent stdout: {stdout_str}. "
f"Recent stderr: {stderr_str}. "
f"Hint: Check container logs and ensure the application is configured to start correctly. "
f"The application may be crashing or exiting early."
)
Expand Down
24 changes: 24 additions & 0 deletions tests/core/test_wait_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ def test_wait_until_ready(self, mock_sleep, mock_time, container_logs, expected_
with pytest.raises(TimeoutError):
strategy.wait_until_ready(mock_container)

@patch("time.sleep")
def test_wait_until_ready_includes_logs_when_container_exits(self, mock_sleep):
strategy = LogMessageWaitStrategy("waiting for connections")
mock_container = Mock()
mock_container.get_logs.return_value = (
b"Fatal: unknown option",
b"stderr diagnostic line",
)
mock_wrapped = Mock()
mock_wrapped.status = "exited"
mock_wrapped.reload.return_value = None
mock_container.get_wrapped_container.return_value = mock_wrapped

with patch(
"testcontainers.core.waiting_utils._get_container_status_info",
return_value={"status": "exited", "exit_code": "1", "error": "", "health_status": "none"},
):
with pytest.raises(RuntimeError, match="Recent stdout: .*Fatal: unknown option") as exc_info:
strategy.wait_until_ready(mock_container)

error_message = str(exc_info.value)
assert "Recent stderr: stderr diagnostic line" in error_message
assert "exit code: 1" in error_message


class TestHttpWaitStrategy:
"""Test the HttpWaitStrategy class."""
Expand Down