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 14, 2018
1 parent 3b974e9 commit 0b78a2e
Show file tree
Hide file tree
Showing 5 changed files with 79 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 {
Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created");
return Boolean.TRUE.equals(created);
} 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 (!isRunning() && !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,39 @@
package org.testcontainers.junit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy;
import org.testcontainers.utility.MountableFile;
import java.io.File;
import java.time.Duration;

public class CopyFileToContainerTest {
private static String folderPath = new File("./src/test/resources/mappable-resource/").getAbsolutePath();
private static String containerPath = "/opt";
private static String fileName = "test-resource.txt";
public static GenericContainer container;

@Before
public void before() {
container = new GenericContainer("couchbase:latest")
.withStartupCheckStrategy(new MinimumDurationRunningStartupCheckStrategy(Duration.ofSeconds(5)))
.withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath );
}


@Test
public void checkFileCopied() {
container.start();
try {
String filesList = container.execInContainer("ls","/opt/mappable-resource").getStdout();

Assert.assertTrue(filesList.contains(fileName));
} catch (Exception e) {
e.printStackTrace();
}

}

}
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 0b78a2e

Please sign in to comment.