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");
}
}
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"));
}
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));
}
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());
}
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();
}
Aggregations