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