Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use "copy" strategy in
withClasspathResourceMapping
where appropria…
…te (#1814)
- Loading branch information
Showing
2 changed files
with
48 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 43 additions & 6 deletions
49
core/src/test/java/org/testcontainers/junit/CopyFileToContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,62 @@ | ||
package org.testcontainers.junit; | ||
|
||
import org.junit.Assert; | ||
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.IOException; | ||
import java.util.Map; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse; | ||
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue; | ||
|
||
public class CopyFileToContainerTest { | ||
private static String containerPath = "/tmp/mappable-resource/"; | ||
private static String fileName = "test-resource.txt"; | ||
|
||
@Test | ||
public void checkFileCopied() throws IOException, InterruptedException { | ||
try( | ||
GenericContainer container = new GenericContainer("alpine:latest") | ||
.withCommand("sleep","3000") | ||
try ( | ||
GenericContainer container = new GenericContainer() | ||
.withCommand("sleep", "3000") | ||
.withCopyFileToContainer(MountableFile.forClasspathResource("/mappable-resource/"), containerPath) | ||
) { | ||
container.start(); | ||
String filesList = container.execInContainer("ls","/tmp/mappable-resource").getStdout(); | ||
Assert.assertTrue(filesList.contains(fileName)); | ||
String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); | ||
assertTrue("file list contains the file", filesList.contains(fileName)); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldUseCopyForReadOnlyClasspathResources() throws Exception { | ||
try ( | ||
GenericContainer container = new GenericContainer() | ||
.withCommand("sleep", "3000") | ||
.withClasspathResourceMapping("/mappable-resource/", containerPath, BindMode.READ_ONLY) | ||
) { | ||
container.start(); | ||
String filesList = container.execInContainer("ls", "/tmp/mappable-resource").getStdout(); | ||
assertTrue("file list contains the file", filesList.contains(fileName)); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldUseCopyOnlyWithReadOnlyClasspathResources() { | ||
String resource = "/test_copy_to_container.txt"; | ||
GenericContainer<?> container = new GenericContainer<>() | ||
.withClasspathResourceMapping(resource, "/readOnly", BindMode.READ_ONLY) | ||
.withClasspathResourceMapping(resource, "/readOnlyNoSelinux", BindMode.READ_ONLY) | ||
|
||
.withClasspathResourceMapping(resource, "/readOnlyShared", BindMode.READ_ONLY, SelinuxContext.SHARED) | ||
.withClasspathResourceMapping(resource, "/readWrite", BindMode.READ_WRITE); | ||
|
||
Map<MountableFile, String> copyMap = container.getCopyToFileContainerPathMap(); | ||
assertTrue("uses copy for read-only", copyMap.containsValue("/readOnly")); | ||
assertTrue("uses copy for read-only and no Selinux", copyMap.containsValue("/readOnlyNoSelinux")); | ||
|
||
assertFalse("uses mount for read-only with Selinux", copyMap.containsValue("/readOnlyShared")); | ||
assertFalse("uses mount for read-write", copyMap.containsValue("/readWrite")); | ||
} | ||
} |