Search in sources :

Example 21 with GenericContainer

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

the class GenericContainerRuleTest method copyFromContainerShouldFailBecauseNoFileTest.

@Test(expected = NotFoundException.class)
public void copyFromContainerShouldFailBecauseNoFileTest() throws NotFoundException, IOException, InterruptedException {
    try (final GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2").withCommand("top")) {
        alpineCopyToContainer.start();
        alpineCopyToContainer.copyFileFromContainer("/home/test.txt", "src/test/resources/copy-from/test.txt");
    }
}
Also used : GenericContainer(org.testcontainers.containers.GenericContainer)

Example 22 with GenericContainer

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

Example 23 with GenericContainer

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

the class FixedHostPortContainerTest method testFixedHostPortMapping.

@Test
public void testFixedHostPortMapping() throws IOException {
    // first find a free port on the docker host that will work for testing
    GenericContainer portDiscoveryRedis = new GenericContainer("redis:3.0.2").withExposedPorts(REDIS_PORT);
    portDiscoveryRedis.start();
    Integer freePort = portDiscoveryRedis.getMappedPort(REDIS_PORT);
    portDiscoveryRedis.stop();
    // Set up a FixedHostPortGenericContainer as if this were a @Rule
    FixedHostPortGenericContainer redis = new FixedHostPortGenericContainer("redis:3.0.2").withFixedExposedPort(freePort, REDIS_PORT);
    redis.start();
// Config redisConfig = new Config();
// redisConfig.useSingleServer().setAddress(redis.getContainerIpAddress() + ":" + freePort);
// Redisson redisson = Redisson.create(redisConfig);
// 
// redisson.getBucket("test").set("foo");
// 
// assertEquals("The bucket content was successfully set", "foo", redisson.getBucket("test").get());
// assertEquals("The container returns the fixed port from getMappedPort(...)", freePort, redis.getMappedPort(REDIS_PORT));
}
Also used : FixedHostPortGenericContainer(org.testcontainers.containers.FixedHostPortGenericContainer) FixedHostPortGenericContainer(org.testcontainers.containers.FixedHostPortGenericContainer) GenericContainer(org.testcontainers.containers.GenericContainer) Test(org.junit.Test)

Example 24 with GenericContainer

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

the class AbstractWaitStrategyTest method waitUntilReadyAndSucceed.

/**
 * Expects that the WaitStrategy returns successfully after connection to a container with a listening port.
 *
 * @param shellCommand the shell command to execute
 */
protected void waitUntilReadyAndSucceed(String shellCommand) {
    final GenericContainer container = startContainerWithCommand(shellCommand);
    // start() blocks until successful or timeout
    container.start();
    assertTrue(String.format("Expected container to be ready after timeout of %sms", WAIT_TIMEOUT_MILLIS), ready.get());
}
Also used : GenericContainer(org.testcontainers.containers.GenericContainer)

Example 25 with GenericContainer

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

the class LocalStackContainer method before.

@Override
protected void before() throws Throwable {
    Preconditions.check("services list must not be empty", services != null && services.length > 0);
    final String servicesList = Arrays.stream(services).map(Service::getLocalStackName).collect(Collectors.joining(","));
    final Integer[] portsList = Arrays.stream(services).map(Service::getPort).collect(Collectors.toSet()).toArray(new Integer[] {});
    delegate = new GenericContainer("localstack/localstack:0.8.5").withExposedPorts(portsList).withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock", READ_WRITE).waitingFor(new LogMessageWaitStrategy().withRegEx(".*Ready\\.\n")).withEnv("SERVICES", servicesList);
    delegate.start();
}
Also used : LogMessageWaitStrategy(org.testcontainers.containers.wait.LogMessageWaitStrategy) 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