Search in sources :

Example 6 with WaitingConsumer

use of org.testcontainers.containers.output.WaitingConsumer in project testcontainers-java by testcontainers.

the class DockerNetworkModeTest method getContainerOutput.

private String getContainerOutput(GenericContainer container) throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();
    Consumer<OutputFrame> composedConsumer = waitingConsumer.andThen(toStringConsumer);
    container.followOutput(composedConsumer);
    waitingConsumer.waitUntilEnd(10, TimeUnit.SECONDS);
    return toStringConsumer.toUtf8String();
}
Also used : ToStringConsumer(org.testcontainers.containers.output.ToStringConsumer) OutputFrame(org.testcontainers.containers.output.OutputFrame) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer)

Example 7 with WaitingConsumer

use of org.testcontainers.containers.output.WaitingConsumer in project testcontainers-java by testcontainers.

the class DockerfileTest method filePermissions.

@Test
public void filePermissions() throws TimeoutException {
    WaitingConsumer consumer = new WaitingConsumer();
    ImageFromDockerfile image = new ImageFromDockerfile().withFileFromTransferable("/someFile.txt", new Transferable() {

        @Override
        public long getSize() {
            return 0;
        }

        @Override
        public byte[] getBytes() {
            return new byte[0];
        }

        @Override
        public String getDescription() {
            return "test file";
        }

        @Override
        public int getFileMode() {
            return 0123;
        }
    }).withDockerfileFromBuilder(builder -> builder.from("alpine:3.2").copy("someFile.txt", "/someFile.txt").cmd("stat -c \"%a\" /someFile.txt"));
    GenericContainer container = new GenericContainer(image).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(consumer);
    try {
        container.start();
        consumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("123"), 5, TimeUnit.SECONDS);
    } finally {
        container.stop();
    }
}
Also used : ImageFromDockerfile(org.testcontainers.images.builder.ImageFromDockerfile) Transferable(org.testcontainers.images.builder.Transferable) GenericContainer(org.testcontainers.containers.GenericContainer) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) OneShotStartupCheckStrategy(org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy) Test(org.junit.Test)

Example 8 with WaitingConsumer

use of org.testcontainers.containers.output.WaitingConsumer in project testcontainers-java by testcontainers.

the class OutputStreamTest method testToStringConsumer.

@Test
public void testToStringConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    ToStringConsumer toStringConsumer = new ToStringConsumer();
    Consumer<OutputFrame> composedConsumer = toStringConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);
    waitingConsumer.waitUntilEnd(30, TimeUnit.SECONDS);
    String utf8String = toStringConsumer.toUtf8String();
    assertTrue("the expected first value was found", utf8String.contains("seq=1"));
    assertTrue("the expected last value was found", utf8String.contains("seq=4"));
    assertFalse("a non-expected value was found", utf8String.contains("seq=42"));
}
Also used : ToStringConsumer(org.testcontainers.containers.output.ToStringConsumer) OutputFrame(org.testcontainers.containers.output.OutputFrame) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Test(org.junit.Test)

Example 9 with WaitingConsumer

use of org.testcontainers.containers.output.WaitingConsumer in project testcontainers-java by testcontainers.

the class OutputStreamTest method testLogConsumer.

@Test
public void testLogConsumer() throws TimeoutException {
    WaitingConsumer waitingConsumer = new WaitingConsumer();
    Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(LOGGER);
    Consumer<OutputFrame> composedConsumer = logConsumer.andThen(waitingConsumer);
    container.followOutput(composedConsumer);
    waitingConsumer.waitUntil(frame -> frame.getType() == STDOUT && frame.getUtf8String().contains("seq=2"));
}
Also used : OutputFrame(org.testcontainers.containers.output.OutputFrame) Slf4jLogConsumer(org.testcontainers.containers.output.Slf4jLogConsumer) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) Test(org.junit.Test)

Example 10 with WaitingConsumer

use of org.testcontainers.containers.output.WaitingConsumer in project testcontainers-java by testcontainers.

the class DirectoryTarResourceTest method simpleRecursiveClasspathResourceTest.

@Test
public void simpleRecursiveClasspathResourceTest() throws TimeoutException {
    // This test combines the copying of classpath resources from JAR files with the recursive TAR approach, to allow JARed classpath resources to be copied in to an image
    WaitingConsumer wait = new WaitingConsumer();
    final ToStringConsumer toString = new ToStringConsumer();
    GenericContainer container = new GenericContainer(new ImageFromDockerfile().withDockerfileFromBuilder(builder -> builder.from("alpine:3.3").copy("/tmp/foo", "/foo").cmd("ls -lRt /foo").build()).withFileFromClasspath("/tmp/foo", // here we use /org/junit as a directory that really should exist on the classpath
    "/recursive/dir")).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));
    container.start();
    wait.waitUntilEnd(60, TimeUnit.SECONDS);
    final String results = toString.toUtf8String();
    // ExternalResource.class is known to exist in a subdirectory of /org/junit so should be successfully copied in
    assertTrue("The container has a file that was copied in via a recursive copy from a JAR resource", results.contains("content.txt"));
}
Also used : ImageFromDockerfile(org.testcontainers.images.builder.ImageFromDockerfile) ToStringConsumer(org.testcontainers.containers.output.ToStringConsumer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GenericContainer(org.testcontainers.containers.GenericContainer) WaitingConsumer(org.testcontainers.containers.output.WaitingConsumer) OneShotStartupCheckStrategy(org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy) Test(org.junit.Test)

Aggregations

WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)11 Test (org.junit.Test)9 ToStringConsumer (org.testcontainers.containers.output.ToStringConsumer)5 GenericContainer (org.testcontainers.containers.GenericContainer)4 OutputFrame (org.testcontainers.containers.output.OutputFrame)4 OneShotStartupCheckStrategy (org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy)4 ImageFromDockerfile (org.testcontainers.images.builder.ImageFromDockerfile)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 File (java.io.File)2 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 Predicate (java.util.function.Predicate)1 DockerClientFactory (org.testcontainers.DockerClientFactory)1 ContainerLaunchException (org.testcontainers.containers.ContainerLaunchException)1 Slf4jLogConsumer (org.testcontainers.containers.output.Slf4jLogConsumer)1 Transferable (org.testcontainers.images.builder.Transferable)1 LogUtils (org.testcontainers.utility.LogUtils)1