Search in sources :

Example 6 with GenericContainer

use of org.testcontainers.containers.GenericContainer in project testcontainers-java by testcontainers.

the class GenericContainerRuleTest method createContainerCmdHookTest.

@Test
public void createContainerCmdHookTest() {
    // Use random name to avoid the conflicts between the tests
    String randomName = Base58.randomString(5);
    try (GenericContainer container = new GenericContainer<>("redis:3.0.2").withCommand("redis-server", "--help").withCreateContainerCmdModifier(cmd -> cmd.withName("overrideMe")).withCreateContainerCmdModifier(cmd -> cmd.withName(randomName)).withCreateContainerCmdModifier(cmd -> cmd.withCmd("redis-server", "--port", "6379"))) {
        container.start();
        assertEquals("Name is configured", "/" + randomName, container.getContainerInfo().getName());
        assertEquals("Command is configured", "[redis-server, --port, 6379]", Arrays.toString(container.getContainerInfo().getConfig().getCmd()));
    }
}
Also used : Document(org.bson.Document) Socket(java.net.Socket) Arrays(java.util.Arrays) MongoCollection(com.mongodb.client.MongoCollection) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) TimeoutException(java.util.concurrent.TimeoutException) MongoDatabase(com.mongodb.client.MongoDatabase) Matcher(java.util.regex.Matcher) Files(com.google.common.io.Files) Duration(java.time.Duration) VisibleAssertions(org.rnorth.visibleassertions.VisibleAssertions) GenericContainer(org.testcontainers.containers.GenericContainer) TestEnvironment(org.testcontainers.utility.TestEnvironment) NotFoundException(com.github.dockerjava.api.exception.NotFoundException) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ImmutableMap(com.google.common.collect.ImmutableMap) com.rabbitmq.client(com.rabbitmq.client) FileUtils(org.apache.commons.io.FileUtils) TimeUnit(java.util.concurrent.TimeUnit) Unreliables(org.rnorth.ducttape.unreliables.Unreliables) SHARED(org.testcontainers.containers.SelinuxContext.SHARED) READ_WRITE(org.testcontainers.containers.BindMode.READ_WRITE) java.io(java.io) MongoClient(com.mongodb.MongoClient) MountableFile(org.testcontainers.utility.MountableFile) org.junit(org.junit) Pattern(java.util.regex.Pattern) Base58(org.testcontainers.utility.Base58) RetryCountExceededException(org.rnorth.ducttape.RetryCountExceededException) READ_ONLY(org.testcontainers.containers.BindMode.READ_ONLY) GenericContainer(org.testcontainers.containers.GenericContainer)

Example 7 with GenericContainer

use of org.testcontainers.containers.GenericContainer in project testcontainers-java by testcontainers.

the class GenericContainerRuleTest method copyToContainerTest.

@Test
public void copyToContainerTest() throws Exception {
    final File tempResultFolder = Files.createTempDir();
    try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2").withCommand("top")) {
        alpineCopyToContainer.start();
        final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
        alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/");
        alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt");
        File expectedFile = new File(mountableFile.getResolvedPath());
        File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_to_container.txt");
        assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile));
    }
}
Also used : MountableFile(org.testcontainers.utility.MountableFile) GenericContainer(org.testcontainers.containers.GenericContainer) MountableFile(org.testcontainers.utility.MountableFile)

Example 8 with GenericContainer

use of org.testcontainers.containers.GenericContainer 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 9 with GenericContainer

use of org.testcontainers.containers.GenericContainer 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 10 with GenericContainer

use of org.testcontainers.containers.GenericContainer in project testcontainers-java by testcontainers.

the class AbstractWaitStrategyTest method waitUntilReadyAndTimeout.

/**
 * Expects that the WaitStrategy throws a {@link RetryCountExceededException} after unsuccessful connection
 * to a container with a listening port.
 *
 * @param shellCommand the shell command to execute
 */
protected void waitUntilReadyAndTimeout(String shellCommand) {
    final GenericContainer container = startContainerWithCommand(shellCommand);
    // start() blocks until successful or timeout
    VisibleAssertions.assertThrows("an exception is thrown when timeout occurs (" + WAIT_TIMEOUT_MILLIS + "ms)", ContainerLaunchException.class, container::start);
}
Also used : GenericContainer(org.testcontainers.containers.GenericContainer)

Aggregations

GenericContainer (org.testcontainers.containers.GenericContainer)30 File (java.io.File)6 IOException (java.io.IOException)6 ImageFromDockerfile (org.testcontainers.images.builder.ImageFromDockerfile)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Arrays (java.util.Arrays)4 AssumptionViolatedException (org.junit.AssumptionViolatedException)4 Test (org.junit.jupiter.api.Test)4 WaitingConsumer (org.testcontainers.containers.output.WaitingConsumer)4 OneShotStartupCheckStrategy (org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy)4 PrintWriter (java.io.PrintWriter)3 Path (java.nio.file.Path)3 List (java.util.List)3 BuildResult (org.gradle.testkit.runner.BuildResult)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 ImageReference (org.springframework.boot.buildpack.platform.docker.type.ImageReference)3 HttpWaitStrategy (org.testcontainers.containers.wait.HttpWaitStrategy)3 MountableFile (org.testcontainers.utility.MountableFile)3