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 11, 2018
1 parent 3b974e9 commit 259e12d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
18 changes: 18 additions & 0 deletions core/src/main/java/org/testcontainers/containers/Container.java
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
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 {
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
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,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).
*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 259e12d

Please sign in to comment.