-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DockerHealthcheckWaitStrategy (#618)
Also adds a isHealthy() method to the ContainerState interface.
- Loading branch information
Showing
8 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../main/java/org/testcontainers/containers/wait/strategy/DockerHealthcheckWaitStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.testcontainers.containers.wait.strategy; | ||
|
||
import org.rnorth.ducttape.TimeoutException; | ||
import org.rnorth.ducttape.unreliables.Unreliables; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Wait strategy leveraging Docker's built-in healthcheck mechanism. | ||
* | ||
* @see <a href="https://docs.docker.com/engine/reference/builder/#healthcheck">https://docs.docker.com/engine/reference/builder/#healthcheck</a> | ||
*/ | ||
public class DockerHealthcheckWaitStrategy extends AbstractWaitStrategy { | ||
|
||
@Override | ||
protected void waitUntilReady() { | ||
|
||
try { | ||
Unreliables.retryUntilTrue((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, waitStrategyTarget::isHealthy); | ||
} catch (TimeoutException e) { | ||
throw new ContainerLaunchException("Timed out waiting for container to become healthy"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...t/java/org/testcontainers/containers/wait/strategy/DockerHealthcheckWaitStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.testcontainers.containers.wait.strategy; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.ContainerLaunchException; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.images.builder.ImageFromDockerfile; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows; | ||
|
||
public class DockerHealthcheckWaitStrategyTest { | ||
|
||
private GenericContainer container; | ||
|
||
@Before | ||
public void setUp() { | ||
// Using a Dockerfile here, since Dockerfile builder DSL doesn't support HEALTHCHECK | ||
container = new GenericContainer(new ImageFromDockerfile() | ||
.withFileFromClasspath("write_file_and_loop.sh", "health-wait-strategy-dockerfile/write_file_and_loop.sh") | ||
.withFileFromClasspath("Dockerfile", "health-wait-strategy-dockerfile/Dockerfile")) | ||
.waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(3))); | ||
} | ||
|
||
@Test | ||
public void startsOnceHealthy() { | ||
container.start(); | ||
} | ||
|
||
@Test | ||
public void containerStartFailsIfContainerIsUnhealthy() { | ||
container.withCommand("tail", "-f", "/dev/null"); | ||
assertThrows("Container launch fails when unhealthy", ContainerLaunchException.class, container::start); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
core/src/test/resources/health-wait-strategy-dockerfile/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
FROM alpine:3.7 | ||
|
||
HEALTHCHECK --interval=1s CMD test -e /testfile | ||
|
||
COPY write_file_and_loop.sh write_file_and_loop.sh | ||
RUN chmod +x write_file_and_loop.sh | ||
|
||
CMD ["/write_file_and_loop.sh"] |
8 changes: 8 additions & 0 deletions
8
core/src/test/resources/health-wait-strategy-dockerfile/write_file_and_loop.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/ash | ||
|
||
echo sleeping | ||
sleep 2 | ||
echo writing file | ||
touch /testfile | ||
|
||
while true; do sleep 1; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters