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

Use "copy" strategy in withClasspathResourceMapping where appropriate #1814

Merged
merged 1 commit into from Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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<>()
rnorth marked this conversation as resolved.
Show resolved Hide resolved
.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"));
}
}