From 871f47c0ccaa8b94ac65a6614df6d33343b52e96 Mon Sep 17 00:00:00 2001 From: dharanpu Date: Mon, 11 Jun 2018 10:59:29 +0200 Subject: [PATCH] enable copyFileToContainer feature during container startup --- .../testcontainers/containers/Container.java | 9 +++++++ .../containers/ContainerState.java | 16 +++++++++++ .../containers/GenericContainer.java | 16 +++++++++-- .../junit/CopyFileToContainerTest.java | 27 +++++++++++++++++++ core/src/test/resources/logback-test.xml | 2 +- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index da42a6f8036..7aeb015e8f2 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -175,6 +175,15 @@ default SELF withFileSystemBind(String hostPath, String containerPath) { */ SELF withExposedPorts(Integer... ports); + /** + * Set the file to be copied before starting a created container + * + * @param mountableFile a Mountable file with path of source file / folder on host machine + * @param containerPath a destination path on conatiner to which the files / folders to be copied + * @return this + */ + SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath); + /** * Add an environment variable to be passed to the container. * diff --git a/core/src/main/java/org/testcontainers/containers/ContainerState.java b/core/src/main/java/org/testcontainers/containers/ContainerState.java index 1eb638232f5..26eec8c4ebe 100644 --- a/core/src/main/java/org/testcontainers/containers/ContainerState.java +++ b/core/src/main/java/org/testcontainers/containers/ContainerState.java @@ -43,6 +43,22 @@ default boolean isRunning() { } } + /** + * @return is the container created? + */ + default boolean isCreated() { + if (getContainerId() == null) { + return false; + } + + try { + String status = getCurrentContainerInfo().getState().getStatus(); + return "created".equalsIgnoreCase(status) || isRunning(); + } catch (DockerException e) { + return false; + } + } + /** * @return has the container health state 'healthy'? */ diff --git a/core/src/main/java/org/testcontainers/containers/GenericContainer.java b/core/src/main/java/org/testcontainers/containers/GenericContainer.java index 9ec38a2f2bf..170b92699a2 100644 --- a/core/src/main/java/org/testcontainers/containers/GenericContainer.java +++ b/core/src/main/java/org/testcontainers/containers/GenericContainer.java @@ -138,6 +138,8 @@ public class GenericContainer> @Nullable private String workingDirectory = null; + private Map copyToFileContainerPathMap = new HashMap<>(); + /* * Unique instance of DockerClient for use by this container object. */ @@ -231,6 +233,7 @@ private void tryStart(Profiler profiler) { applyConfiguration(createCommand); containerId = createCommand.exec().getId(); + copyToFileContainerPathMap.forEach(this::copyFileToContainer); logger().info("Starting container with ID: {}", containerId); profiler.start("Start container"); @@ -835,6 +838,15 @@ public SELF withWorkingDirectory(String workDir) { return self(); } + /** + * {@inheritDoc} + */ + @Override + public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) { + copyToFileContainerPathMap.put(mountableFile, containerPath); + return self(); + } + /** * Get the IP address that this container may be reached on (may not be the local machine). * @@ -940,8 +952,8 @@ public ExecResult execInContainer(String... command) @Override public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) { - if (!isRunning()) { - throw new IllegalStateException("copyFileToContainer can only be used while the Container is running"); + if (!isCreated()) { + throw new IllegalStateException("copyFileToContainer can only be used with created / running container"); } this.dockerClient diff --git a/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java b/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java new file mode 100644 index 00000000000..64c3c202d85 --- /dev/null +++ b/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java @@ -0,0 +1,27 @@ +package org.testcontainers.junit; + +import org.junit.Assert; +import org.junit.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.utility.MountableFile; +import java.io.File; +import java.io.IOException; + +public class CopyFileToContainerTest { + private static String folderPath = new File("./src/test/resources/mappable-resource/").getAbsolutePath(); + private static String containerPath = "/tmp"; + private static String fileName = "test-resource.txt"; + + @Test + public void checkFileCopied() throws IOException, InterruptedException { + try( + GenericContainer container = new GenericContainer("alpine:latest") + .withCommand("sleep","3000") + .withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath) + ) { + container.start(); + String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout(); + Assert.assertTrue(filesList.contains(fileName)); + } + } +} diff --git a/core/src/test/resources/logback-test.xml b/core/src/test/resources/logback-test.xml index ed0e5b2659e..15bc6deadf2 100644 --- a/core/src/test/resources/logback-test.xml +++ b/core/src/test/resources/logback-test.xml @@ -26,4 +26,4 @@ PROFILER DENY - \ No newline at end of file +