Search in sources :

Example 16 with GenericContainer

use of org.testcontainers.containers.GenericContainer in project gora by apache.

the class GoraMongodbAuthenticationTestDriver method mongoContainer.

public static GenericContainer mongoContainer(String authMechanisms, String useVersion) {
    GenericContainer _container = new GenericContainer(useVersion).withExposedPorts(27017);
    // https://hub.docker.com/_/mongo
    // These variables, used in conjunction, create a new user and set that user's password.
    // This user is created in the admin authentication database
    // and given the role of root, which is a "superuser" role.
    _container.withEnv("MONGO_INITDB_ROOT_USERNAME", adminUsername);
    _container.withEnv("MONGO_INITDB_ROOT_PASSWORD", adminPassword);
    // To enable authentication, MongoDB will have to restart itself
    int restartCount = 2;
    _container.waitingFor(Wait.forLogMessage("(?i).*waiting for connections.*", restartCount));
    // https://docs.mongodb.com/manual/tutorial/enable-authentication/
    // https://docs.mongodb.com/manual/reference/parameters/#param.authenticationMechanisms
    _container.withCommand("--auth", "--setParameter", "authenticationMechanisms=" + authMechanisms);
    return _container;
}
Also used : GenericContainer(org.testcontainers.containers.GenericContainer)

Example 17 with GenericContainer

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

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

the class GenericContainerRuleTest method shouldCopyFileFromContainerTest.

@Test
public void shouldCopyFileFromContainerTest() throws IOException, InterruptedException {
    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_from_container.txt");
        File expectedFile = new File(mountableFile.getResolvedPath());
        File actualFile = new File(tempResultFolder.getAbsolutePath() + "/test_copy_from_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 19 with GenericContainer

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

the class GenericContainerRuleTest method testIsRunning.

// @Test
// public void simpleRedisTest() {
// String ipAddress = redis.getContainerIpAddress();
// Integer port = redis.getMappedPort(REDIS_PORT);
// 
// // Use Redisson to obtain a List that is backed by Redis
// Config redisConfig = new Config();
// redisConfig.useSingleServer().setAddress(ipAddress + ":" + port);
// 
// Redisson redisson = Redisson.create(redisConfig);
// 
// List<String> testList = redisson.getList("test");
// testList.add("foo");
// testList.add("bar");
// testList.add("baz");
// 
// List<String> testList2 = redisson.getList("test");
// assertEquals("The list contains the expected number of items (redis is working!)", 3, testList2.size());
// assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("foo"));
// assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("bar"));
// assertTrue("The list contains an item that was put in (redis is working!)", testList2.contains("baz"));
// }
@Test
public void testIsRunning() {
    try (GenericContainer container = new GenericContainer().withCommand("top")) {
        assertFalse("Container is not started and not running", container.isRunning());
        container.start();
        assertTrue("Container is started and running", container.isRunning());
    }
}
Also used : GenericContainer(org.testcontainers.containers.GenericContainer)

Example 20 with GenericContainer

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

the class GenericContainerRuleTest method failFastWhenContainerHaltsImmediately.

// TODO investigate intermittent failures
@Test
// TODO investigate intermittent failures
@Ignore
public void failFastWhenContainerHaltsImmediately() throws Exception {
    long startingTimeMs = System.currentTimeMillis();
    final GenericContainer failsImmediately = new GenericContainer("alpine:3.2").withCommand("/bin/sh", "-c", "return false").withMinimumRunningDuration(Duration.ofMillis(100));
    try {
        assertThrows("When we start a container that halts immediately, an exception is thrown", RetryCountExceededException.class, () -> {
            failsImmediately.start();
            return null;
        });
        // Check how long it took, to verify that we ARE bailing out early.
        // Want to strike a balance here; too short and this test will fail intermittently
        // on slow systems and/or due to GC variation, too long and we won't properly test
        // what we're intending to test.
        int allowedSecondsToFailure = GenericContainer.CONTAINER_RUNNING_TIMEOUT_SEC / 2;
        long completedTimeMs = System.currentTimeMillis();
        assertTrue("container should not take long to start up", completedTimeMs - startingTimeMs < 1000L * allowedSecondsToFailure);
    } finally {
        failsImmediately.stop();
    }
}
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