Skip to content

Commit 8f49046

Browse files
m4rii0eddumelendez
andauthored
Add ShellStrategy
`ShellStrategy` can also be used as `Wait.forSuccessfulCommand(...)`. Co-authored-by: Eddú Meléndez <[email protected]>
1 parent 48083c1 commit 8f49046

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.testcontainers.containers.wait.strategy;
2+
3+
import org.rnorth.ducttape.TimeoutException;
4+
import org.rnorth.ducttape.unreliables.Unreliables;
5+
import org.testcontainers.containers.ContainerLaunchException;
6+
7+
import java.util.concurrent.TimeUnit;
8+
9+
public class ShellStrategy extends AbstractWaitStrategy {
10+
11+
private String command;
12+
13+
public ShellStrategy withCommand(String command) {
14+
this.command = command;
15+
return this;
16+
}
17+
18+
@Override
19+
protected void waitUntilReady() {
20+
try {
21+
Unreliables.retryUntilTrue(
22+
(int) startupTimeout.getSeconds(),
23+
TimeUnit.SECONDS,
24+
() -> waitStrategyTarget.execInContainer("/bin/sh", "-c", this.command).getExitCode() == 0
25+
);
26+
} catch (TimeoutException e) {
27+
throw new ContainerLaunchException(
28+
"Timed out waiting for container to execute `" + this.command + "` successfully."
29+
);
30+
}
31+
}
32+
}

core/src/main/java/org/testcontainers/containers/wait/strategy/Wait.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,14 @@ public static LogMessageWaitStrategy forLogMessage(String regex, int times) {
6666
public static DockerHealthcheckWaitStrategy forHealthcheck() {
6767
return new DockerHealthcheckWaitStrategy();
6868
}
69+
70+
/**
71+
* Convenience method to return a WaitStrategy for a shell command.
72+
*
73+
* @param command the command to run
74+
* @return ShellStrategy
75+
*/
76+
public static ShellStrategy forSuccessfulCommand(String command) {
77+
return new ShellStrategy().withCommand(command);
78+
}
6979
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.testcontainers.junit.wait.strategy;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.junit.Test;
5+
import org.testcontainers.containers.wait.strategy.ShellStrategy;
6+
7+
import java.util.concurrent.atomic.AtomicBoolean;
8+
9+
/**
10+
* Tests for {@link ShellStrategy}.
11+
*/
12+
public class ShellStrategyTest extends AbstractWaitStrategyTest<ShellStrategy> {
13+
14+
private static final String LOCK_FILE = "/tmp/ready.lock";
15+
16+
@Test
17+
public void testWaitUntilReady_Success() {
18+
waitUntilReadyAndSucceed(String.format("touch %s; sleep 300", LOCK_FILE));
19+
}
20+
21+
@Test
22+
public void testWaitUntilReady_Timeout() {
23+
waitUntilReadyAndTimeout("sleep 300");
24+
}
25+
26+
@NotNull
27+
@Override
28+
protected ShellStrategy buildWaitStrategy(AtomicBoolean ready) {
29+
return createShellStrategy(ready).withCommand(String.format("stat %s", LOCK_FILE));
30+
}
31+
32+
@NotNull
33+
private static ShellStrategy createShellStrategy(AtomicBoolean ready) {
34+
return new ShellStrategy() {
35+
@Override
36+
protected void waitUntilReady() {
37+
super.waitUntilReady();
38+
ready.set(true);
39+
}
40+
};
41+
}
42+
}

0 commit comments

Comments
 (0)