Skip to content

Commit

Permalink
enable copyFileToContainer feature during container startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dharanpu committed Jun 18, 2018
1 parent 3b974e9 commit 871f47c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -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");
Expand Down Expand Up @@ -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).
*
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
2 changes: 1 addition & 1 deletion core/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
<Marker>PROFILER</Marker>
<OnMatch>DENY</OnMatch>
</turboFilter>
</configuration>
</configuration>

0 comments on commit 871f47c

Please sign in to comment.