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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
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 file to be copied before starting a created container
*
* @param transferable a Transferable
* @param containerPath a destination path on container to which the files / folders to be copied
* @return this
*/
SELF withCopyFileToContainer(Transferable transferable, String containerPath);

/**
* Add an environment variable to be passed to the container.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -188,6 +189,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 @@ -415,6 +419,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 @@ -1296,6 +1302,15 @@ public SELF withCopyFileToContainer(MountableFile mountableFile, String containe
return self();
}

/**
* {@inheritDoc}
*/
@Override
public SELF withCopyFileToContainer(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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
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 org.testcontainers.images.builder.ImageFromDockerfile;

import java.util.Arrays;
Expand Down Expand Up @@ -75,6 +78,20 @@ public void shouldReportErrorAfterWait() {
}
}

@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())
.withCopyFileToContainer(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");
}
}

@Test
public void shouldOnlyPublishExposedPorts() {
ImageFromDockerfile image = new ImageFromDockerfile("publish-multiple")
Expand Down