-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add DockerHealthcheckWaitStrategy #618
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
184ca99
Add DockerHealthcheckWaitStrategy and isHealthy method to ContainerState
kiview 5dd6229
Add PR link to changelog
kiview 98ef3dd
Use COPY instead of ADD in Dockerfile for healthstrategy check
kiview 5dc9bd1
Use method reference in DockerHealthcheckWaitStrategy
kiview e92538f
Use long running command in DockerHealthcheckWaitStrategyTest
kiview f03659a
Pin Alpine image for healthcheck test to 3.7
kiview c8fcbac
Refactor ContainerState
kiview 4de4253
Small refactorings after review
kiview 79cc554
Add documentation and example for healthcheck strategy
kiview File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add
HEALTHCHECK
support to the DSL?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitly, but another PR I assume?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, up to you