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