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

[Feature]: ShellStrategy, a new WaitStrategy #6672

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,29 @@
package org.testcontainers.containers.wait.strategy;

import org.rnorth.ducttape.TimeoutException;
import org.rnorth.ducttape.unreliables.Unreliables;
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
import org.testcontainers.containers.ContainerLaunchException;

import java.util.concurrent.TimeUnit;

public class ShellStrategy extends AbstractWaitStrategy {

private final String command;

public ShellStrategy(String command) {
this.command = command;
}

@Override
protected void waitUntilReady() {
try {
Unreliables.retryUntilTrue(
(int) startupTimeout.getSeconds(),
TimeUnit.SECONDS,
() -> waitStrategyTarget.execInContainer("/bin/sh", "-c", command).getExitCode() == 0
);
} catch (TimeoutException e) {
throw new ContainerLaunchException("Timed out waiting for container to execute command successfully");
}
}
}
Expand Up @@ -66,4 +66,14 @@ public static LogMessageWaitStrategy forLogMessage(String regex, int times) {
public static DockerHealthcheckWaitStrategy forHealthcheck() {
return new DockerHealthcheckWaitStrategy();
}

/**
* Convenience method to return a WaitStrategy for a shell command.
*
* @param command the command to run
* @return ShellStrategy
*/
public static ShellStrategy forSuccessfulCommand(String command) {
return new ShellStrategy(command);
}
}
@@ -0,0 +1,37 @@
package org.testcontainers.junit.wait.strategy;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import org.testcontainers.containers.wait.strategy.ShellStrategy;

import java.util.concurrent.atomic.AtomicBoolean;

/**
* Tests for {@link ShellStrategy}.
*/
public class ShellStrategyTest extends AbstractWaitStrategyTest<ShellStrategy> {

private final String LOCK_FILE = "/tmp/ready.lock";

@Test
public void testWaitUntilReady_Success() {
waitUntilReadyAndSucceed(String.format("touch %s; sleep 300", LOCK_FILE));
}

@Test
public void testWaitUntilReady_Timeout() {
waitUntilReadyAndTimeout("sleep 300");
}

@NotNull
@Override
protected ShellStrategy buildWaitStrategy(AtomicBoolean ready) {
return new ShellStrategy(String.format("stat %s", LOCK_FILE)) {
@Override
protected void waitUntilReady() {
super.waitUntilReady();
ready.set(true);
}
};
}
}