Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.testcontainers.containers.wait.strategy;

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

import java.util.concurrent.TimeUnit;

public class ShellStrategy extends AbstractWaitStrategy {

private String command;

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

@Override
protected void waitUntilReady() {
try {
Unreliables.retryUntilTrue(
(int) startupTimeout.getSeconds(),
TimeUnit.SECONDS,
() -> waitStrategyTarget.execInContainer("/bin/sh", "-c", this.command).getExitCode() == 0
);
} catch (TimeoutException e) {
throw new ContainerLaunchException(
"Timed out waiting for container to execute `" + this.command + "` successfully."
);
}
}
}
Original file line number Diff line number Diff line change
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().withCommand(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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 static 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 createShellStrategy(ready).withCommand(String.format("stat %s", LOCK_FILE));
}

@NotNull
private static ShellStrategy createShellStrategy(AtomicBoolean ready) {
return new ShellStrategy() {
@Override
protected void waitUntilReady() {
super.waitUntilReady();
ready.set(true);
}
};
}
}