Skip to content

Commit 22cab1f

Browse files
dharanikesavbsideup
authored andcommitted
enable copyFileToContainer feature during container startup (#742)
1 parent 577b509 commit 22cab1f

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

core/src/main/java/org/testcontainers/containers/Container.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ default SELF withFileSystemBind(String hostPath, String containerPath) {
175175
*/
176176
SELF withExposedPorts(Integer... ports);
177177

178+
/**
179+
* Set the file to be copied before starting a created container
180+
*
181+
* @param mountableFile a Mountable file with path of source file / folder on host machine
182+
* @param containerPath a destination path on conatiner to which the files / folders to be copied
183+
* @return this
184+
*/
185+
SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath);
186+
178187
/**
179188
* Add an environment variable to be passed to the container.
180189
*

core/src/main/java/org/testcontainers/containers/ContainerState.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ default boolean isRunning() {
4343
}
4444
}
4545

46+
/**
47+
* @return is the container created?
48+
*/
49+
default boolean isCreated() {
50+
if (getContainerId() == null) {
51+
return false;
52+
}
53+
54+
try {
55+
String status = getCurrentContainerInfo().getState().getStatus();
56+
return "created".equalsIgnoreCase(status) || isRunning();
57+
} catch (DockerException e) {
58+
return false;
59+
}
60+
}
61+
4662
/**
4763
* @return has the container health state 'healthy'?
4864
*/

core/src/main/java/org/testcontainers/containers/GenericContainer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
142142
@Nullable
143143
private String workingDirectory = null;
144144

145+
private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();
146+
145147
/*
146148
* Unique instance of DockerClient for use by this container object.
147149
*/
@@ -243,6 +245,7 @@ private void tryStart(Profiler profiler) {
243245
applyConfiguration(createCommand);
244246

245247
containerId = createCommand.exec().getId();
248+
copyToFileContainerPathMap.forEach(this::copyFileToContainer);
246249

247250
logger().info("Starting container with ID: {}", containerId);
248251
profiler.start("Start container");
@@ -894,6 +897,15 @@ public SELF withWorkingDirectory(String workDir) {
894897
return self();
895898
}
896899

900+
/**
901+
* {@inheritDoc}
902+
*/
903+
@Override
904+
public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) {
905+
copyToFileContainerPathMap.put(mountableFile, containerPath);
906+
return self();
907+
}
908+
897909
/**
898910
* Get the IP address that this container may be reached on (may not be the local machine).
899911
*
@@ -999,8 +1011,8 @@ public ExecResult execInContainer(String... command)
9991011
@Override
10001012
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) {
10011013

1002-
if (!isRunning()) {
1003-
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
1014+
if (!isCreated()) {
1015+
throw new IllegalStateException("copyFileToContainer can only be used with created / running container");
10041016
}
10051017

10061018
this.dockerClient
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.testcontainers.containers.GenericContainer;
6+
import org.testcontainers.utility.MountableFile;
7+
import java.io.IOException;
8+
9+
public class CopyFileToContainerTest {
10+
private static String containerPath = "/tmp";
11+
private static String fileName = "test-resource.txt";
12+
13+
@Test
14+
public void checkFileCopied() throws IOException, InterruptedException {
15+
try(
16+
GenericContainer container = new GenericContainer("alpine:latest")
17+
.withCommand("sleep","3000")
18+
.withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath)
19+
) {
20+
container.start();
21+
String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout();
22+
Assert.assertTrue(filesList.contains(fileName));
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)