Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to create a container file from a Transferable (#3814) #3815

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
kiview marked this conversation as resolved.
Show resolved Hide resolved
}

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() {
kiview marked this conversation as resolved.
Show resolved Hide resolved
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