From 259e12d0bb236d0733e37e2aec54a3f4fcb694b8 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 | 18 +++++++++++++++ .../containers/ContainerState.java | 16 ++++++++++++++ .../containers/GenericContainer.java | 22 +++++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/testcontainers/containers/Container.java b/core/src/main/java/org/testcontainers/containers/Container.java index da42a6f8036..1bc9fcf7166 100644 --- a/core/src/main/java/org/testcontainers/containers/Container.java +++ b/core/src/main/java/org/testcontainers/containers/Container.java @@ -128,6 +128,15 @@ default void addFileSystemBind(final String hostPath, final String containerPath */ void addExposedPorts(int... ports); + /** + * add a 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 + */ + void addCopyFileToContainer(MountableFile mountableFile, String containerPath); + /** * Specify the {@link WaitStrategy} to use to determine if the container is ready. * @@ -175,6 +184,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..fb527eb438f 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 { + Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created"); + return Boolean.TRUE.equals(created); + } 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..39416075271 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,21 @@ public SELF withWorkingDirectory(String workDir) { return self(); } + + @Override + public void addCopyFileToContainer(MountableFile mountableFile, String containerPath) { + copyToFileContainerPathMap.put(mountableFile, containerPath); + } + + /** + * {@inheritDoc} + */ + @Override + public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) { + this.addCopyFileToContainer(mountableFile, containerPath); + return self(); + } + /** * Get the IP address that this container may be reached on (may not be the local machine). * @@ -940,8 +958,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 (!isRunning() && !isCreated()) { + throw new IllegalStateException("opyFileToContainer can only be used with created / running container"); } this.dockerClient