|
1 | 1 | package org.testcontainers.junit; |
2 | 2 |
|
| 3 | +import com.google.common.io.Files; |
| 4 | +import com.google.common.io.Resources; |
| 5 | +import org.junit.Before; |
3 | 6 | import org.junit.Test; |
4 | 7 | import org.testcontainers.TestImages; |
5 | 8 | import org.testcontainers.containers.BindMode; |
6 | 9 | import org.testcontainers.containers.GenericContainer; |
7 | 10 | import org.testcontainers.containers.SelinuxContext; |
8 | 11 | import org.testcontainers.utility.MountableFile; |
9 | 12 |
|
| 13 | +import java.io.File; |
10 | 14 | import java.io.IOException; |
11 | 15 | import java.util.Map; |
12 | 16 |
|
13 | 17 | import static org.assertj.core.api.Assertions.assertThat; |
14 | 18 |
|
15 | 19 | public class CopyFileToContainerTest { |
16 | 20 |
|
17 | | - private static String containerPath = "/tmp/mappable-resource/"; |
| 21 | + private static String destinationOnHost; |
| 22 | + |
| 23 | + private static String directoryInContainer = "/tmp/mappable-resource/"; |
18 | 24 |
|
19 | 25 | private static String fileName = "test-resource.txt"; |
20 | 26 |
|
| 27 | + @Before |
| 28 | + public void setup() throws IOException { |
| 29 | + destinationOnHost = File.createTempFile("testcontainers", null).getAbsolutePath(); |
| 30 | + } |
| 31 | + |
21 | 32 | @Test |
22 | 33 | public void checkFileCopied() throws IOException, InterruptedException { |
23 | 34 | try ( |
| 35 | + // copyToContainer { |
24 | 36 | GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE) |
25 | 37 | .withCommand("sleep", "3000") |
26 | | - .withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath) |
| 38 | + .withCopyFileToContainer( |
| 39 | + MountableFile.forClasspathResource("/mappable-resource/"), |
| 40 | + directoryInContainer |
| 41 | + ) |
| 42 | + // } |
27 | 43 | ) { |
28 | 44 | container.start(); |
29 | | - String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); |
| 45 | + String filesList = container.execInContainer("ls", directoryInContainer).getStdout(); |
30 | 46 | assertThat(filesList).as("file list contains the file").contains(fileName); |
| 47 | + |
| 48 | + // copyFileFromContainer { |
| 49 | + container.copyFileFromContainer(directoryInContainer + fileName, destinationOnHost); |
| 50 | + // } |
31 | 51 | } |
| 52 | + |
| 53 | + assertThat(Files.toByteArray(new File(destinationOnHost))) |
| 54 | + .isEqualTo(Resources.toByteArray(getClass().getResource("/mappable-resource/" + fileName))); |
32 | 55 | } |
33 | 56 |
|
34 | 57 | @Test |
35 | 58 | public void shouldUseCopyForReadOnlyClasspathResources() throws Exception { |
36 | 59 | try ( |
37 | 60 | GenericContainer<?> container = new GenericContainer<>(TestImages.TINY_IMAGE) |
38 | 61 | .withCommand("sleep", "3000") |
39 | | - .withClasspathResourceMapping("/mappable-resource/", containerPath, BindMode.READ_ONLY) |
| 62 | + .withClasspathResourceMapping("/mappable-resource/", directoryInContainer, BindMode.READ_ONLY) |
40 | 63 | ) { |
41 | 64 | container.start(); |
42 | 65 | String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); |
|
0 commit comments