Skip to content

Commit

Permalink
Use "copy" strategy in withClasspathResourceMapping where appropria…
Browse files Browse the repository at this point in the history
…te (#1814)
  • Loading branch information
bsideup committed Sep 3, 2019
1 parent 88a4eaf commit c9b66eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Expand Up @@ -967,7 +967,11 @@ public SELF withClasspathResourceMapping(final String resourcePath, final String
public SELF withClasspathResourceMapping(final String resourcePath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) {
final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);

this.addFileSystemBind(mountableFile.getResolvedPath(), containerPath, mode, selinuxContext);
if (mode == BindMode.READ_ONLY && selinuxContext == SelinuxContext.NONE) {
withCopyFileToContainer(mountableFile, containerPath);
} else {
addFileSystemBind(mountableFile.getResolvedPath(), containerPath, mode, selinuxContext);
}

return self();
}
Expand Down
@@ -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"));
}
}

0 comments on commit c9b66eb

Please sign in to comment.