diff --git a/src/main/java/com/github/junit5docker/DockerExtension.java b/src/main/java/com/github/junit5docker/DockerExtension.java index 0e8b30f..8a73323 100644 --- a/src/main/java/com/github/junit5docker/DockerExtension.java +++ b/src/main/java/com/github/junit5docker/DockerExtension.java @@ -13,6 +13,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import java.util.regex.Pattern; import java.util.stream.Stream; import static java.util.concurrent.CompletableFuture.supplyAsync; @@ -56,7 +57,7 @@ private void waitForLogAccordingTo(WaitFor waitFor) { String expectedLog = waitFor.value(); if (!WaitFor.NOTHING.equals(expectedLog)) { ExecutorService executor = Executors.newSingleThreadExecutor(); - CompletableFuture logFound = supplyAsync(findFirstLogContaining(expectedLog), executor); + CompletableFuture logFound = supplyAsync(findFirstLogContaining(waitFor), executor); executor.shutdown(); try { boolean termination = executor.awaitTermination(waitFor.timeoutInMillis(), TimeUnit.MILLISECONDS); @@ -72,10 +73,16 @@ private void waitForLogAccordingTo(WaitFor waitFor) { } } - private Supplier findFirstLogContaining(String logToFind) { + private Supplier findFirstLogContaining(WaitFor logToFind) { return () -> { try (Stream logs = dockerClient.logs(containerId)) { - return logs.anyMatch(log -> log.contains(logToFind)); + if (WaitFor.Engine.PLAIN_TEXT.equals(logToFind.engine())) { + return logs.anyMatch(log -> log.contains(logToFind.value())); + }else{ + // Regex + Pattern pattern = Pattern.compile(logToFind.value()); + return logs.anyMatch(log -> pattern.matcher(log).find()); + } } }; } diff --git a/src/main/java/com/github/junit5docker/WaitFor.java b/src/main/java/com/github/junit5docker/WaitFor.java index 30e78d9..0936ebf 100644 --- a/src/main/java/com/github/junit5docker/WaitFor.java +++ b/src/main/java/com/github/junit5docker/WaitFor.java @@ -24,8 +24,24 @@ */ String value(); + /** + * @return engine use to parse log + */ + Engine engine() default Engine.PLAIN_TEXT ; + /** * @return the time in milliseconds to wait for the log before giving up. */ int timeoutInMillis() default DEFAULT_TIMEOUT; + + enum Engine { + /** + * search for simple string + */ + PLAIN_TEXT, + /** + * search for regex pattern + */ + REGEX + } } diff --git a/src/test/java/com/github/junit5docker/WaitForLogRegexIT.java b/src/test/java/com/github/junit5docker/WaitForLogRegexIT.java new file mode 100644 index 0000000..181be07 --- /dev/null +++ b/src/test/java/com/github/junit5docker/WaitForLogRegexIT.java @@ -0,0 +1,45 @@ +package com.github.junit5docker; + +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.github.junit5docker.WaitFor.Engine.REGEX; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; + + +@Docker(image = "faustxvi/open-port-later", ports = @Port(exposed = 8801, inner = 8080), + environments = @Environment(key = "WAITING_TIME", value = "1s"), + waitFor = @WaitFor(value="[a-z]{7}",engine = REGEX)) +public class WaitForLogRegexIT { + + @BeforeEach + void verifyContainerIsReady() { + checkConnectionToContainer(); + } + + @Test + void verifyFirstContainerIsStarted() { + checkConnectionToContainer(); + } + + @AfterEach + void verifyContainerIsStillAlive() { + checkConnectionToContainer(); + } + + private void checkConnectionToContainer() { + try (CloseableHttpResponse container = HttpClientBuilder.create().build() + .execute(new HttpGet("http://localhost:8801"))) { + assertThat(container).isNotNull(); + } catch (IOException e) { + fail("The port 8801 should be listening"); + } + } +}