forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartupCheckStrategyTest.java
90 lines (75 loc) · 3.37 KB
/
StartupCheckStrategyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package org.testcontainers.containers.startupcheck;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import lombok.SneakyThrows;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.WaitingConsumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT;
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.IndefiniteOneShotStrategyTest;
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.MinimumDurationStrategyTest;
import static org.testcontainers.containers.startupcheck.StartupCheckStrategyTest.OneShotStrategyTest;
@RunWith(Suite.class)
@Suite.SuiteClasses({OneShotStrategyTest.class, IndefiniteOneShotStrategyTest.class, MinimumDurationStrategyTest.class})
public class StartupCheckStrategyTest {
private static final String HELLO_TESTCONTAINERS = "Hello Testcontainers!";
private static void waitForHello(GenericContainer container) throws TimeoutException {
WaitingConsumer consumer = new WaitingConsumer();
container.followOutput(consumer, STDOUT);
consumer.waitUntil(frame ->
frame.getUtf8String().contains(HELLO_TESTCONTAINERS), 30, TimeUnit.SECONDS);
}
public static class OneShotStrategyTest {
@Rule
// withOneShotStrategy {
public GenericContainer bboxWithOneShot = new GenericContainer("busybox:1.31.1")
.withCommand(String.format("echo %s", HELLO_TESTCONTAINERS))
.withStartupCheckStrategy(
new OneShotStartupCheckStrategy().withTimeout(Duration.ofSeconds(3))
);
// }
@SneakyThrows
@Test
public void testCommandIsExecuted() {
waitForHello(bboxWithOneShot);
assertThat(bboxWithOneShot.isRunning()).isFalse();
}
}
public static class IndefiniteOneShotStrategyTest {
@Rule
// withIndefiniteOneShotStrategy {
public GenericContainer bboxWithIndefiniteOneShot = new GenericContainer("busybox:1.31.1")
.withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS))
.withStartupCheckStrategy(
new IndefiniteWaitOneShotStartupCheckStrategy()
);
// }
@SneakyThrows
@Test
public void testCommandIsExecuted() {
waitForHello(bboxWithIndefiniteOneShot);
assertThat(bboxWithIndefiniteOneShot.isRunning()).isFalse();
}
}
public static class MinimumDurationStrategyTest {
@Rule
// withMinimumDurationStrategy {
public GenericContainer bboxWithMinimumDuration = new GenericContainer("busybox:1.31.1")
.withCommand("sh", "-c", String.format("sleep 5 && echo \"%s\"", HELLO_TESTCONTAINERS))
.withStartupCheckStrategy(
new MinimumDurationRunningStartupCheckStrategy(Duration.ofSeconds(5))
);
// }
@SneakyThrows
@Test
public void testCommandIsExecuted() {
waitForHello(bboxWithMinimumDuration);
assertThat(bboxWithMinimumDuration.isRunning()).isTrue();
}
}
}