Skip to content

Commit 52b6949

Browse files
authored
Make sure we don't hide exceptions from waitUntilContainerStarted (#6167)
This adds the original exception as the cause. It also adds the text "Wait strategy failed." to each exception message. This addresses two issues: 1. The original exception from waitUntilContainerStarted was not being included as the cause in newly thrown exceptions in tryStart. The exceptions were not completely hidden because they were being logged, however, but the initial cause was less visible. 2. The exception messages focused on state that was found at the time the exception was received but did not mention that the waitUntilContainerStarted method threw an exception. Because of these issues, unless you look at the source or see the logs, it isn't clear that this was caused by an exception from waitUntilContainerStarted. In systems that might highlight the failure message and stack trace and not logs/console output (test failure reports, CI systems, etc.), it makes it harder to debug these issues. Updated tests to check for the updated messages as well as the exception from waitUntilContainerStarted.
1 parent 3b24a7d commit 52b6949

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,25 +493,31 @@ private void tryStart(Instant startedAt) {
493493
}
494494

495495
if (inspectContainerResponse == null) {
496-
throw new IllegalStateException("Container is removed");
496+
throw new IllegalStateException("Wait strategy failed. Container is removed", e);
497497
}
498498

499499
InspectContainerResponse.ContainerState state = inspectContainerResponse.getState();
500500
if (Boolean.TRUE.equals(state.getDead())) {
501-
throw new IllegalStateException("Container is dead");
501+
throw new IllegalStateException("Wait strategy failed. Container is dead", e);
502502
}
503503

504504
if (Boolean.TRUE.equals(state.getOOMKilled())) {
505-
throw new IllegalStateException("Container crashed with out-of-memory (OOMKilled)");
505+
throw new IllegalStateException(
506+
"Wait strategy failed. Container crashed with out-of-memory (OOMKilled)",
507+
e
508+
);
506509
}
507510

508511
String error = state.getError();
509512
if (!StringUtils.isBlank(error)) {
510-
throw new IllegalStateException("Container crashed: " + error);
513+
throw new IllegalStateException("Wait strategy failed. Container crashed: " + error, e);
511514
}
512515

513516
if (!Boolean.TRUE.equals(state.getRunning())) {
514-
throw new IllegalStateException("Container exited with code " + state.getExitCode());
517+
throw new IllegalStateException(
518+
"Wait strategy failed. Container exited with code " + state.getExitCode(),
519+
e
520+
);
515521
}
516522

517523
throw e;

core/src/test/java/org/testcontainers/containers/GenericContainerTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public void shouldReportOOMAfterWait() {
5555
})
5656
.withCommand("sh", "-c", "A='0123456789'; for i in $(seq 0 32); do A=$A$A; done; sleep 10m")
5757
) {
58-
assertThatThrownBy(container::start).hasStackTraceContaining("Container crashed with out-of-memory");
58+
assertThatThrownBy(container::start)
59+
.hasStackTraceContaining("Wait strategy failed. Container crashed with out-of-memory (OOMKilled)")
60+
.hasStackTraceContaining("Nope!");
5961
}
6062
}
6163

@@ -67,7 +69,9 @@ public void shouldReportErrorAfterWait() {
6769
.waitingFor(new WaitForExitedState(state -> state.getExitCode() > 0))
6870
.withCommand("sh", "-c", "usleep 100; exit 123")
6971
) {
70-
assertThatThrownBy(container::start).hasStackTraceContaining("Container exited with code 123");
72+
assertThatThrownBy(container::start)
73+
.hasStackTraceContaining("Wait strategy failed. Container exited with code 123")
74+
.hasStackTraceContaining("Nope!");
7175
}
7276
}
7377

@@ -80,7 +84,9 @@ public void shouldCopyTransferableAsFile() {
8084
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
8185
.withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")
8286
) {
83-
assertThatThrownBy(container::start).hasStackTraceContaining("Container exited with code 100");
87+
assertThatThrownBy(container::start)
88+
.hasStackTraceContaining("Wait strategy failed. Container exited with code 100")
89+
.hasStackTraceContaining("Nope!");
8490
}
8591
}
8692

@@ -93,7 +99,9 @@ public void shouldCopyTransferableAsFileWithFileMode() {
9399
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
94100
.withCommand("sh", "-c", "ls -ll /tmp | grep '\\-rwxrwxrwx\\|test' && exit 100")
95101
) {
96-
assertThatThrownBy(container::start).hasStackTraceContaining("Container exited with code 100");
102+
assertThatThrownBy(container::start)
103+
.hasStackTraceContaining("Wait strategy failed. Container exited with code 100")
104+
.hasStackTraceContaining("Nope!");
97105
}
98106
}
99107

@@ -107,7 +115,9 @@ public void shouldCopyTransferableAfterMountableFile() {
107115
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
108116
.withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")
109117
) {
110-
assertThatThrownBy(container::start).hasStackTraceContaining("Container exited with code 100");
118+
assertThatThrownBy(container::start)
119+
.hasStackTraceContaining("Wait strategy failed. Container exited with code 100")
120+
.hasStackTraceContaining("Nope!");
111121
}
112122
}
113123

0 commit comments

Comments
 (0)