Skip to content

Commit 2fe1746

Browse files
thomasdarimontThomas Darimont
authored andcommitted
Allow to create a container file from a Transferable (#3814)
1 parent dabc99c commit 2fe1746

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
1313
import org.testcontainers.containers.traits.LinkableContainer;
1414
import org.testcontainers.containers.wait.strategy.WaitStrategy;
15+
import org.testcontainers.images.builder.Transferable;
1516
import org.testcontainers.utility.LogUtils;
1617
import org.testcontainers.utility.MountableFile;
1718

@@ -169,11 +170,20 @@ default SELF withFileSystemBind(String hostPath, String containerPath) {
169170
* Set the file to be copied before starting a created container
170171
*
171172
* @param mountableFile a Mountable file with path of source file / folder on host machine
172-
* @param containerPath a destination path on conatiner to which the files / folders to be copied
173+
* @param containerPath a destination path on container to which the files / folders to be copied
173174
* @return this
174175
*/
175176
SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath);
176177

178+
/**
179+
* Set the Transferable to be copied before starting a created container
180+
*
181+
* @param transferable a Transferable with the contents for the file.
182+
* @param containerPath a destination path on container to which the files / folders to be copied
183+
* @return this
184+
*/
185+
SELF withCopyTransferableToContainer(Transferable transferable, String containerPath);
186+
177187
/**
178188
* Add an environment variable to be passed to the container.
179189
*
@@ -401,6 +411,11 @@ default void followOutput(Consumer<OutputFrame> consumer, OutputFrame.OutputType
401411

402412
List<String> getPortBindings();
403413

414+
@Override
415+
default void copyFileToContainer(MountableFile mountableFile, String containerPath) {
416+
ContainerState.super.copyFileToContainer(mountableFile, containerPath);
417+
}
418+
404419
List<String> getExtraHosts();
405420

406421
Future<String> getImage();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;
8686
import org.testcontainers.images.ImagePullPolicy;
8787
import org.testcontainers.images.RemoteDockerImage;
88+
import org.testcontainers.images.builder.Transferable;
8889
import org.testcontainers.lifecycle.Startable;
8990
import org.testcontainers.lifecycle.Startables;
9091
import org.testcontainers.lifecycle.TestDescription;
@@ -183,6 +184,9 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
183184
// Maintain order in which entries are added, as earlier target location may be a prefix of a later location.
184185
private Map<MountableFile, String> copyToFileContainerPathMap = new LinkedHashMap<>();
185186

187+
// Maintain order in which entries are added, as earlier target location may be a prefix of a later location.
188+
private Map<Transferable, String> copyToTransferableContainerPathMap = new LinkedHashMap<>();
189+
186190
protected final Set<Startable> dependencies = new HashSet<>();
187191

188192
/**
@@ -408,6 +412,8 @@ private void tryStart(Instant startedAt) {
408412

409413
// TODO use single "copy" invocation (and calculate an hash of the resulting tar archive)
410414
copyToFileContainerPathMap.forEach(this::copyFileToContainer);
415+
416+
copyToTransferableContainerPathMap.forEach(this::copyFileToContainer);
411417
}
412418

413419
connectToPortForwardingNetwork(createCommand.getNetworkMode());
@@ -1255,6 +1261,15 @@ public SELF withCopyFileToContainer(MountableFile mountableFile, String containe
12551261
return self();
12561262
}
12571263

1264+
/**
1265+
* {@inheritDoc}
1266+
*/
1267+
@Override
1268+
public SELF withCopyTransferableToContainer(Transferable transferable, String containerPath) {
1269+
copyToTransferableContainerPathMap.put(transferable, containerPath);
1270+
return self();
1271+
}
1272+
12581273
/**
12591274
* Get the IP address that this container may be reached on (may not be the local machine).
12601275
*

core/src/test/java/org/testcontainers/containers/GenericContainerTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import org.testcontainers.DockerClientFactory;
1616
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
1717
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
18+
import org.testcontainers.images.builder.Transferable;
1819

20+
import java.nio.charset.StandardCharsets;
1921
import java.util.concurrent.TimeUnit;
2022
import java.util.function.Predicate;
2123

@@ -52,14 +54,28 @@ public void shouldReportErrorAfterWait() {
5254
try (
5355
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
5456
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
55-
.waitingFor(new WaitForExitedState(state -> state.getExitCode() > 0))
57+
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
5658
.withCommand("sh", "-c", "usleep 100; exit 123")
5759
) {
5860
assertThatThrownBy(container::start)
5961
.hasStackTraceContaining("Container exited with code 123");
6062
}
6163
}
6264

65+
@Test
66+
public void shouldCopyTransferableAsFile() {
67+
try (
68+
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
69+
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
70+
.withCopyTransferableToContainer(Transferable.of("test".getBytes(StandardCharsets.UTF_8)), "/tmp/test")
71+
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
72+
.withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")
73+
) {
74+
assertThatThrownBy(container::start)
75+
.hasStackTraceContaining("Container exited with code 100");
76+
}
77+
}
78+
6379
static class NoopStartupCheckStrategy extends StartupCheckStrategy {
6480

6581
@Override

0 commit comments

Comments
 (0)