Skip to content

Commit

Permalink
Allow to create a container file from a Transferable (testcontainers#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdarimont authored and Thomas Darimont committed Apr 20, 2021
1 parent dabc99c commit 2fe1746
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
17 changes: 16 additions & 1 deletion core/src/main/java/org/testcontainers/containers/Container.java
Expand Up @@ -12,6 +12,7 @@
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
import org.testcontainers.containers.traits.LinkableContainer;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.LogUtils;
import org.testcontainers.utility.MountableFile;

Expand Down Expand Up @@ -169,11 +170,20 @@ default SELF withFileSystemBind(String hostPath, String containerPath) {
* 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
* @param containerPath a destination path on container to which the files / folders to be copied
* @return this
*/
SELF withCopyFileToContainer(MountableFile mountableFile, String containerPath);

/**
* Set the Transferable to be copied before starting a created container
*
* @param transferable a Transferable with the contents for the file.
* @param containerPath a destination path on container to which the files / folders to be copied
* @return this
*/
SELF withCopyTransferableToContainer(Transferable transferable, String containerPath);

/**
* Add an environment variable to be passed to the container.
*
Expand Down Expand Up @@ -401,6 +411,11 @@ default void followOutput(Consumer<OutputFrame> consumer, OutputFrame.OutputType

List<String> getPortBindings();

@Override
default void copyFileToContainer(MountableFile mountableFile, String containerPath) {
ContainerState.super.copyFileToContainer(mountableFile, containerPath);
}

List<String> getExtraHosts();

Future<String> getImage();
Expand Down
Expand Up @@ -85,6 +85,7 @@
import org.testcontainers.containers.wait.strategy.WaitStrategyTarget;
import org.testcontainers.images.ImagePullPolicy;
import org.testcontainers.images.RemoteDockerImage;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.lifecycle.Startable;
import org.testcontainers.lifecycle.Startables;
import org.testcontainers.lifecycle.TestDescription;
Expand Down Expand Up @@ -183,6 +184,9 @@ public class GenericContainer<SELF extends GenericContainer<SELF>>
// Maintain order in which entries are added, as earlier target location may be a prefix of a later location.
private Map<MountableFile, String> copyToFileContainerPathMap = new LinkedHashMap<>();

// Maintain order in which entries are added, as earlier target location may be a prefix of a later location.
private Map<Transferable, String> copyToTransferableContainerPathMap = new LinkedHashMap<>();

protected final Set<Startable> dependencies = new HashSet<>();

/**
Expand Down Expand Up @@ -408,6 +412,8 @@ private void tryStart(Instant startedAt) {

// TODO use single "copy" invocation (and calculate an hash of the resulting tar archive)
copyToFileContainerPathMap.forEach(this::copyFileToContainer);

copyToTransferableContainerPathMap.forEach(this::copyFileToContainer);
}

connectToPortForwardingNetwork(createCommand.getNetworkMode());
Expand Down Expand Up @@ -1255,6 +1261,15 @@ public SELF withCopyFileToContainer(MountableFile mountableFile, String containe
return self();
}

/**
* {@inheritDoc}
*/
@Override
public SELF withCopyTransferableToContainer(Transferable transferable, String containerPath) {
copyToTransferableContainerPathMap.put(transferable, containerPath);
return self();
}

/**
* Get the IP address that this container may be reached on (may not be the local machine).
*
Expand Down
Expand Up @@ -15,7 +15,9 @@
import org.testcontainers.DockerClientFactory;
import org.testcontainers.containers.startupcheck.StartupCheckStrategy;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;
import org.testcontainers.images.builder.Transferable;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

Expand Down Expand Up @@ -52,14 +54,28 @@ public void shouldReportErrorAfterWait() {
try (
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
.waitingFor(new WaitForExitedState(state -> state.getExitCode() > 0))
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
.withCommand("sh", "-c", "usleep 100; exit 123")
) {
assertThatThrownBy(container::start)
.hasStackTraceContaining("Container exited with code 123");
}
}

@Test
public void shouldCopyTransferableAsFile() {
try (
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
.withCopyTransferableToContainer(Transferable.of("test".getBytes(StandardCharsets.UTF_8)), "/tmp/test")
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
.withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")
) {
assertThatThrownBy(container::start)
.hasStackTraceContaining("Container exited with code 100");
}
}

static class NoopStartupCheckStrategy extends StartupCheckStrategy {

@Override
Expand Down

0 comments on commit 2fe1746

Please sign in to comment.