Skip to content

Commit 0b78a2e

Browse files
author
dharanpu
committed
enable copyFileToContainer feature during container startup
1 parent 3b974e9 commit 0b78a2e

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
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+
Boolean created = getCurrentContainerInfo().getState().getStatus().equalsIgnoreCase("Created");
56+
return Boolean.TRUE.equals(created);
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
@@ -138,6 +138,8 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
138138
@Nullable
139139
private String workingDirectory = null;
140140

141+
private Map<MountableFile, String> copyToFileContainerPathMap = new HashMap<>();
142+
141143
/*
142144
* Unique instance of DockerClient for use by this container object.
143145
*/
@@ -231,6 +233,7 @@ private void tryStart(Profiler profiler) {
231233
applyConfiguration(createCommand);
232234

233235
containerId = createCommand.exec().getId();
236+
copyToFileContainerPathMap.forEach(this::copyFileToContainer);
234237

235238
logger().info("Starting container with ID: {}", containerId);
236239
profiler.start("Start container");
@@ -835,6 +838,15 @@ public SELF withWorkingDirectory(String workDir) {
835838
return self();
836839
}
837840

841+
/**
842+
* {@inheritDoc}
843+
*/
844+
@Override
845+
public SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath) {
846+
copyToFileContainerPathMap.put(mountableFile, containerPath);
847+
return self();
848+
}
849+
838850
/**
839851
* Get the IP address that this container may be reached on (may not be the local machine).
840852
*
@@ -940,8 +952,8 @@ public ExecResult execInContainer(String... command)
940952
@Override
941953
public void copyFileToContainer(MountableFile mountableLocalFile, String containerPath) {
942954

943-
if (!isRunning()) {
944-
throw new IllegalStateException("copyFileToContainer can only be used while the Container is running");
955+
if (!isRunning() && !isCreated()) {
956+
throw new IllegalStateException("copyFileToContainer can only be used with created / running container");
945957
}
946958

947959
this.dockerClient
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.testcontainers.junit;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.testcontainers.containers.GenericContainer;
7+
import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy;
8+
import org.testcontainers.utility.MountableFile;
9+
import java.io.File;
10+
import java.time.Duration;
11+
12+
public class CopyFileToContainerTest {
13+
private static String folderPath = new File("./src/test/resources/mappable-resource/").getAbsolutePath();
14+
private static String containerPath = "/opt";
15+
private static String fileName = "test-resource.txt";
16+
public static GenericContainer container;
17+
18+
@Before
19+
public void before() {
20+
container = new GenericContainer("couchbase:latest")
21+
.withStartupCheckStrategy(new MinimumDurationRunningStartupCheckStrategy(Duration.ofSeconds(5)))
22+
.withCopyFileToContainer(MountableFile.forHostPath(folderPath), containerPath );
23+
}
24+
25+
26+
@Test
27+
public void checkFileCopied() {
28+
container.start();
29+
try {
30+
String filesList = container.execInContainer("ls","/opt/mappable-resource").getStdout();
31+
32+
Assert.assertTrue(filesList.contains(fileName));
33+
} catch (Exception e) {
34+
e.printStackTrace();
35+
}
36+
37+
}
38+
39+
}

core/src/test/resources/logback-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
<Marker>PROFILER</Marker>
2727
<OnMatch>DENY</OnMatch>
2828
</turboFilter>
29-
</configuration>
29+
</configuration>

0 commit comments

Comments
 (0)