Skip to content

Commit 209ab2e

Browse files
kiviewsumitanvekarrnortheddumelendez
authored
Add docs for copyFile API (#4661)
Co-authored-by: Sumit Anvekar <2293969+sumitanvekar@users.noreply.github.com> Co-authored-by: Richard North <rich.north@gmail.com> Co-authored-by: Eddú Meléndez <eddu.melendez@gmail.com>
1 parent 8b6776c commit 209ab2e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

core/src/test/java/org/testcontainers/containers/GenericContainerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ public void shouldReportErrorAfterWait() {
8282
@Test
8383
public void shouldCopyTransferableAsFile() {
8484
try (
85+
// transferableFile {
8586
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
8687
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
8788
.withCopyToContainer(Transferable.of("test"), "/tmp/test")
8889
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
8990
.withCommand("sh", "-c", "grep -q test /tmp/test && exit 100")
91+
// }
9092
) {
9193
assertThatThrownBy(container::start)
9294
.hasStackTraceContaining("Wait strategy failed. Container exited with code 100")
@@ -97,11 +99,13 @@ public void shouldCopyTransferableAsFile() {
9799
@Test
98100
public void shouldCopyTransferableAsFileWithFileMode() {
99101
try (
102+
// transferableWithFileMode {
100103
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
101104
.withStartupCheckStrategy(new NoopStartupCheckStrategy())
102105
.withCopyToContainer(Transferable.of("test", 0777), "/tmp/test")
103106
.waitingFor(new WaitForExitedState(state -> state.getExitCodeLong() > 0))
104107
.withCommand("sh", "-c", "ls -ll /tmp | grep '\\-rwxrwxrwx\\|test' && exit 100")
108+
// }
105109
) {
106110
assertThatThrownBy(container::start)
107111
.hasStackTraceContaining("Wait strategy failed. Container exited with code 100")

core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
11
package org.testcontainers.junit;
22

3+
import com.google.common.io.Files;
4+
import com.google.common.io.Resources;
5+
import org.junit.Before;
36
import org.junit.Test;
47
import org.testcontainers.TestImages;
58
import org.testcontainers.containers.BindMode;
69
import org.testcontainers.containers.GenericContainer;
710
import org.testcontainers.containers.SelinuxContext;
811
import org.testcontainers.utility.MountableFile;
912

13+
import java.io.File;
1014
import java.io.IOException;
1115
import java.util.Map;
1216

1317
import static org.assertj.core.api.Assertions.assertThat;
1418

1519
public class CopyFileToContainerTest {
1620

17-
private static String containerPath = "/tmp/mappable-resource/";
21+
private static String destinationOnHost;
22+
23+
private static String directoryInContainer = "/tmp/mappable-resource/";
1824

1925
private static String fileName = "test-resource.txt";
2026

27+
@Before
28+
public void setup() throws IOException {
29+
destinationOnHost = File.createTempFile("testcontainers", null).getAbsolutePath();
30+
}
31+
2132
@Test
2233
public void checkFileCopied() throws IOException, InterruptedException {
2334
try (
35+
// copyToContainer {
2436
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
2537
.withCommand("sleep", "3000")
26-
.withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath)
38+
.withCopyFileToContainer(
39+
MountableFile.forClasspathResource("/mappable-resource/"),
40+
directoryInContainer
41+
)
42+
// }
2743
) {
2844
container.start();
29-
String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout();
45+
String filesList = container.execInContainer("ls", directoryInContainer).getStdout();
3046
assertThat(filesList).as("file list contains the file").contains(fileName);
47+
48+
// copyFileFromContainer {
49+
container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost);
50+
// }
3151
}
52+
53+
assertThat(Files.toByteArray(new File(destinationOnHost)))
54+
.isEqualTo(Resources.toByteArray(getClass().getResource("/mappable-resource/" + fileName)));
3255
}
3356

3457
@Test
3558
public void shouldUseCopyForReadOnlyClasspathResources() throws Exception {
3659
try (
3760
GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE)
3861
.withCommand("sleep", "3000")
39-
.withClasspathResourceMapping("/mappable-resource/", containerPath, BindMode.READ_ONLY)
62+
.withClasspathResourceMapping("/mappable-resource/", directoryInContainer, BindMode.READ_ONLY)
4063
) {
4164
container.start();
4265
String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout();

docs/features/files.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
# Files and volumes
22

3+
## Copying files
4+
5+
Files can be copied into the container before startup, or can be copied from the container after the container has started.
6+
7+
!!! note
8+
This is the recommended approach for portability cross-docker environments.
9+
10+
### Copying to a container before startup
11+
12+
<!--codeinclude-->
13+
[Copying files using MountableFile](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyToContainer
14+
<!--/codeinclude-->
15+
16+
Using `Transferable`, file content will be placed in the specified location.
17+
18+
<!--codeinclude-->
19+
[Copying files using Transferable](../../core/src/test/java/org/testcontainers/containers/GenericContainerTest.java) inside_block:transferableFile
20+
<!--/codeinclude-->
21+
22+
Setting file mode is also possible.
23+
24+
<!--codeinclude-->
25+
[Copying files using Transferable with file mode](../../core/src/test/java/org/testcontainers/containers/GenericContainerTest.java) inside_block:transferableWithFileMode
26+
<!--/codeinclude-->
27+
28+
### Copying a file from a running container
29+
30+
<!--codeinclude-->
31+
[Copying files from a container](../../core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java) inside_block:copyFileFromContainer
32+
<!--/codeinclude-->
33+
334
## File mapping
435

536
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(...)
1849
"/etc/redis.conf",
1950
BindMode.READ_ONLY)
2051
```
52+

0 commit comments

Comments
 (0)