Skip to content

Commit

Permalink
enable copyFileToContainer feature during container startup (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharanikesav authored and bsideup committed Jul 10, 2018
1 parent 577b509 commit 22cab1f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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'?
*/
Expand Down
Expand Up @@ -142,6 +142,8 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
@Nullable
private String workingDirectory = null;

private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();

/*
* Unique instance of DockerClient for use by this container object.
*/
Expand Down Expand Up @@ -243,6 +245,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");
Expand Down Expand Up @@ -894,6 +897,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).
*
Expand Down Expand Up @@ -999,8 +1011,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
Expand Down
@@ -0,0 +1,25 @@
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.IOException;

public class CopyFileToContainerTest {
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.forClasspathResource("/mappable-resource/"), containerPath)
) {
container.start();
String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout();
Assert.assertTrue(filesList.contains(fileName));
}
}
}

0 comments on commit 22cab1f

Please sign in to comment.