Skip to content

Instantly share code, notes, and snippets.

@LvargaDS
Last active February 10, 2024 12:55
Show Gist options
  • Select an option

  • Save LvargaDS/da3562c57abffb6edaa4c32ccefa0350 to your computer and use it in GitHub Desktop.

Select an option

Save LvargaDS/da3562c57abffb6edaa4c32ccefa0350 to your computer and use it in GitHub Desktop.
Small util class for testcontainer to copy whole directory recursively instead of signle file only.
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class CopyDirectoryToTestContainer {
/**
* Method will copy all files from localPathToDirectory (source directory) to the container, into the directory
* passed as containerPathToDirectory parameter.
*
* @param container container instance on which {@link GenericContainer#withCopyFileToContainer(MountableFile, String)} method will be called
* @param localPathToDirectory local directory (full path), to copy all files recursively from
* @param containerPathToDirectory directory path in container, where to put all content from source dir
* @param <X> type of your container
* @return your container after copying filet into it
*/
public static <X extends GenericContainer<X>> GenericContainer<X> copyDir(
GenericContainer<X> container,
String localPathToDirectory,
String containerPathToDirectory) {
if (!containerPathToDirectory.endsWith("/")) {
throw new RuntimeException("containerPathToDirectory parameter has to end with / character (denoting, target is a directory).");
}
Path start = Paths.get(localPathToDirectory);
try (Stream<Path> stream = Files.walk(start)) {
stream.filter(Files::isRegularFile)
.forEach(x -> {
final Path relativePathOfCopyiedFile = start.relativize(x);
final String containerPathWithFilename = containerPathToDirectory + relativePathOfCopyiedFile;
// System.out.println(x.toFile().getAbsolutePath() + "\t-> " + containerPathWithFilename);
container.withCopyFileToContainer(MountableFile.forHostPath(x), containerPathWithFilename);
});
} catch (IOException ex) {
throw new RuntimeException(
"Error while trying to walk through given directory path: \"" + localPathToDirectory + "\"! ex=" + ex.getMessage(),
ex);
}
return container;
}
}
public interface SampleUsage {
GenericContainer<?> liberty =
copyDir(new GenericContainer<>(libertyImage)
.withLabel("purpose", "testContainer"),
"target/libraries",
"/libraries/") // end of copyDir call...
.withExposedPorts(9080, 9443)
.withCopyFileToContainer(
MountableFile.forClasspathResource("server.xml", 511), // 511 == 0777 (octal, rwxrwxrwx)
"/config/config.xml")
.withCopyFileToContainer(MountableFile.forHostPath(
"target/mypp.ear"),
"/config/dropins/myapp.ear")
.waitingFor(Wait.forLogMessage(".*CWWKF0011I: The defaultServer server is ready to run a smarter planet.*", 1)
.withStartupTimeout(Duration.of(1000 * LIBERTY_STARTUP_TIMEOUT_SECONDS, ChronoUnit.SECONDS)))
.withLogConsumer(new Slf4jLogConsumer(LOG_LIBERTY));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment