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

Add docs for copyFile API #4661

Merged
merged 18 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
@@ -1,41 +1,68 @@
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.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.SelinuxContext;
import org.testcontainers.utility.MountableFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import static org.junit.Assert.assertArrayEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
import static org.testcontainers.TestImages.TINY_IMAGE;

public class CopyFileToContainerTest {
private static String containerPath = "/tmp/mappable-resource/";
public 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 {
// copyToContainer {
try (
GenericContainer<?> container = new GenericContainer<>(TINY_IMAGE)
.withCommand("sleep", "3000")
.withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath)
// withCopyFileToContainer ensure that a file or directory will be copied to the container
// before starting. In this case, we map a classpath directory to a directory inside the container
.withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), directoryInContainer)
) {
container.start();
String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout();

// at this point directoryInContainer should exist, and should contain copies of file(s)
String filesList = container.execInContainer("ls", directoryInContainer).getStdout();
assertTrue("file list contains the file", filesList.contains(fileName));

// ...

// copyFileFromContainer copies a file from a running container
container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost);
}
// }

assertArrayEquals(
Files.toByteArray(new File(destinationOnHost)),
Resources.toByteArray(CopyFileToContainerTest.class.getResource("/mappable-resource/" + fileName))
);
}

@Test
public void shouldUseCopyForReadOnlyClasspathResources() throws Exception {
try (
GenericContainer<?> container = new GenericContainer<>(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();
Expand Down
16 changes: 16 additions & 0 deletions docs/features/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ new GenericContainer(...)
"/etc/redis.conf",
BindMode.READ_ONLY)
```

## Copying files to and from containers
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved

Files can be copied into the container before startup, or can be copied from the container after the container has started.

### Copying to a container before startup

<!--codeinclude-->
[Copying files to a container](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyToContainer
<!--/codeinclude-->

### Copying a file from a running container

<!--codeinclude-->
[Copying files from a container](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyFromContainer
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
<!--/codeinclude-->