From 209ab2ee929772d9d6ccda2b0f91c69426c9e0cd Mon Sep 17 00:00:00 2001 From: Kevin Wittek Date: Wed, 27 Sep 2023 22:12:16 +0200 Subject: [PATCH] Add docs for `copyFile` API (#4661) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sumit Anvekar <2293969+sumitanvekar@users.noreply.github.com> Co-authored-by: Richard North Co-authored-by: EddĂș MelĂ©ndez --- .../containers/GenericContainerTest.java | 4 +++ .../junit/CopyFileToContainerTest.java | 31 +++++++++++++++--- docs/features/files.md | 32 +++++++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java b/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java index 2d30fe98eeb..fb0b216bce2 100644 --- a/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java +++ b/core/src/test/java/org/testcontainers/containers/GenericContainerTest.java @@ -82,11 +82,13 @@ public void shouldReportErrorAfterWait() { @Test public void shouldCopyTransferableAsFile() { try ( + // transferableFile { GenericContainer container = new GenericContainer<>(TestImages.TINY_IMAGE) .withStartupCheckStrategy(new NoopStartupCheckStrategy()) .withCopyToContainer(Transferable.of("test"), "/tmp/test") .waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0)) .withCommand("sh", "-c", "grep -q test /tmp/test && exit 100") + // } ) { assertThatThrownBy(container::start) .hasStackTraceContaining("Wait strategy failed. Container exited with code 100") @@ -97,11 +99,13 @@ public void shouldCopyTransferableAsFile() { @Test public void shouldCopyTransferableAsFileWithFileMode() { try ( + // transferableWithFileMode { GenericContainer container = new GenericContainer<>(TestImages.TINY_IMAGE) .withStartupCheckStrategy(new NoopStartupCheckStrategy()) .withCopyToContainer(Transferable.of("test", 0777), "/tmp/test") .waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0)) .withCommand("sh", "-c", "ls -ll /tmp | grep '\\-rwxrwxrwx\\|test' && exit 100") + // } ) { assertThatThrownBy(container::start) .hasStackTraceContaining("Wait strategy failed. Container exited with code 100") diff --git a/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java b/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java index 92cf5f72154..1aa017d6894 100644 --- a/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java +++ b/core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java @@ -1,5 +1,8 @@ package org.testcontainers.junit; +import com.google.common.io.Files; +import com.google.common.io.Resources; +import org.junit.Before; import org.junit.Test; import org.testcontainers.TestImages; import org.testcontainers.containers.BindMode; @@ -7,6 +10,7 @@ import org.testcontainers.containers.SelinuxContext; import org.testcontainers.utility.MountableFile; +import java.io.File; import java.io.IOException; import java.util.Map; @@ -14,21 +18,40 @@ public class CopyFileToContainerTest { - private static String containerPath = "/tmp/mappable-resource/"; + private static String destinationOnHost; + + private static String directoryInContainer = "/tmp/mappable-resource/"; private static String fileName = "test-resource.txt"; + @Before + public void setup() throws IOException { + destinationOnHost = File.createTempFile("testcontainers", null).getAbsolutePath(); + } + @Test public void checkFileCopied() throws IOException, InterruptedException { try ( + // copyToContainer { GenericContainer container = new GenericContainer<>(TestImages.TINY_IMAGE) .withCommand("sleep", "3000") - .withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath) + .withCopyFileToContainer( + MountableFile.forClasspathResource("/mappable-resource/"), + directoryInContainer + ) + // } ) { container.start(); - String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); + String filesList = container.execInContainer("ls", directoryInContainer).getStdout(); assertThat(filesList).as("file list contains the file").contains(fileName); + + // copyFileFromContainer { + container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost); + // } } + + assertThat(Files.toByteArray(new File(destinationOnHost))) + .isEqualTo(Resources.toByteArray(getClass().getResource("/mappable-resource/" + fileName))); } @Test @@ -36,7 +59,7 @@ public void shouldUseCopyForReadOnlyClasspathResources() throws Exception { try ( GenericContainer container = new GenericContainer<>(TestImages.TINY_IMAGE) .withCommand("sleep", "3000") - .withClasspathResourceMapping("/mappable-resource/", containerPath, BindMode.READ_ONLY) + .withClasspathResourceMapping("/mappable-resource/", directoryInContainer, BindMode.READ_ONLY) ) { container.start(); String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); diff --git a/docs/features/files.md b/docs/features/files.md index 7e5f07b3059..43dd5d276d0 100644 --- a/docs/features/files.md +++ b/docs/features/files.md @@ -1,5 +1,36 @@ # Files and volumes +## Copying files + +Files can be copied into the container before startup, or can be copied from the container after the container has started. + +!!! note + This is the recommended approach for portability cross-docker environments. + +### Copying to a container before startup + + +[Copying files using MountableFile](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyToContainer + + +Using `Transferable`, file content will be placed in the specified location. + + +[Copying files using Transferable](../../core/src/test/java/org/testcontainers/containers/GenericContainerTest.java) inside_block:transferableFile + + +Setting file mode is also possible. + + +[Copying files using Transferable with file mode](../../core/src/test/java/org/testcontainers/containers/GenericContainerTest.java) inside_block:transferableWithFileMode + + +### Copying a file from a running container + + +[Copying files from a container](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyFileFromContainer + + ## File mapping It is possible to map a file or directory from your FileSystem into the container as a volume using `withFileSystemBind`: @@ -18,3 +49,4 @@ new GenericContainer(...) "/etc/redis.conf", BindMode.READ_ONLY) ``` +