Search in sources :

Example 1 with ToStringConsumer

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

the class DirectoryTarResourceTest method simpleRecursiveFileTest.

@Test
public void simpleRecursiveFileTest() throws TimeoutException {
    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("cat /foo/src/test/resources/test-recursive-file.txt").build()).withFileFromFile("/tmp/foo", // '.' is expected to be the project base directory, so all source code/resources should be copied in
    new File("."))).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));
    container.start();
    wait.waitUntilEnd(60, TimeUnit.SECONDS);
    final String results = toString.toUtf8String();
    assertTrue("The container has a file that was copied in via a recursive copy", results.contains("Used for DirectoryTarResourceTest"));
}
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) File(java.io.File) OneShotStartupCheckStrategy(org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy) Test(org.junit.Test)

Example 2 with ToStringConsumer

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

the class DirectoryTarResourceTest method simpleRecursiveFileWithPermissionTest.

@Test
public void simpleRecursiveFileWithPermissionTest() throws TimeoutException {
    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", "-al", "/").build()).withFileFromFile("/tmp/foo", new File("/mappable-resource/test-resource.txt"), 0754)).withStartupCheckStrategy(new OneShotStartupCheckStrategy()).withLogConsumer(wait.andThen(toString));
    container.start();
    wait.waitUntilEnd(60, TimeUnit.SECONDS);
    String listing = toString.toUtf8String();
    assertThat("Listing shows that file is copied with mode requested.", Arrays.asList(listing.split("\\n")), exactlyNItems(1, allOf(containsString("-rwxr-xr--"), containsString("foo"))));
}
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) File(java.io.File) OneShotStartupCheckStrategy(org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy) Test(org.junit.Test)

Example 3 with ToStringConsumer

use of org.testcontainers.containers.output.ToStringConsumer 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 4 with ToStringConsumer

use of org.testcontainers.containers.output.ToStringConsumer 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 5 with ToStringConsumer

use of org.testcontainers.containers.output.ToStringConsumer 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

ToStringConsumer (org.testcontainers.containers.output.ToStringConsumer)6 WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)5 Test (org.junit.Test)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 GenericContainer (org.testcontainers.containers.GenericContainer)3 OneShotStartupCheckStrategy (org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy)3 ImageFromDockerfile (org.testcontainers.images.builder.ImageFromDockerfile)3 File (java.io.File)2 OutputFrame (org.testcontainers.containers.output.OutputFrame)2 DockerClient (com.github.dockerjava.api.DockerClient)1 ExecCreateCmdResponse (com.github.dockerjava.api.command.ExecCreateCmdResponse)1 FrameConsumerResultCallback (org.testcontainers.containers.output.FrameConsumerResultCallback)1