Java Code Examples for org.testcontainers.utility.MountableFile#forClasspathResource()

The following examples show how to use org.testcontainers.utility.MountableFile#forClasspathResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileOperationsTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void copyFileToContainerFileTest() throws Exception {
    try (
        GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2")
            .withCommand("top")
    ) {
        alpineCopyToContainer.start();
        final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
        alpineCopyToContainer.copyFileToContainer(mountableFile, "/test.txt");

        File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt");
        alpineCopyToContainer.copyFileFromContainer("/test.txt", actualFile.getPath());

        File expectedFile = new File(mountableFile.getResolvedPath());
        assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile));
    }
}
 
Example 2
Source File: FileOperationsTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void copyFileToContainerFolderTest() throws Exception {
    try (
        GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2")
            .withCommand("top")
    ) {
        alpineCopyToContainer.start();
        final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
        alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/");

        File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt");
        alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", actualFile.getPath());

        File expectedFile = new File(mountableFile.getResolvedPath());
        assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile));
    }
}
 
Example 3
Source File: FileOperationsTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void copyFolderToContainerFolderTest() throws Exception {
    try (
        GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2")
            .withCommand("top")
    ) {

        alpineCopyToContainer.start();
        final MountableFile mountableFile = MountableFile.forClasspathResource("mappable-resource/");
        alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/test/");

        File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_to_container.txt");
        alpineCopyToContainer.copyFileFromContainer("/home/test/test-resource.txt", actualFile.getPath());

        File expectedFile = new File(mountableFile.getResolvedPath() + "/test-resource.txt");
        assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile));
    }
}
 
Example 4
Source File: FileOperationsTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
@Test
public void shouldCopyFileFromContainerTest() throws IOException {
    try (
        GenericContainer alpineCopyToContainer = new GenericContainer("alpine:3.2")
            .withCommand("top")
    ) {

        alpineCopyToContainer.start();
        final MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
        alpineCopyToContainer.copyFileToContainer(mountableFile, "/home/");

        File actualFile = new File(temporaryFolder.getRoot().getAbsolutePath() + "/test_copy_from_container.txt");
        alpineCopyToContainer.copyFileFromContainer("/home/test_copy_to_container.txt", actualFile.getPath());

        File expectedFile = new File(mountableFile.getResolvedPath());
        assertTrue("Files aren't same ", FileUtils.contentEquals(expectedFile, actualFile));
    }
}
 
Example 5
Source File: PostgresHandleTest.java    From okapi with Apache License 2.0 5 votes vote down vote up
@BeforeAll
static void beforeAll() {
  MountableFile serverKeyFile = MountableFile.forClasspathResource("server.key");
  MountableFile serverCrtFile = MountableFile.forClasspathResource("server.crt");
  POSTGRESQL_CONTAINER.copyFileToContainer(serverKeyFile, KEY_PATH);
  POSTGRESQL_CONTAINER.copyFileToContainer(serverCrtFile, CRT_PATH);
  exec("chown", "postgres.postgres", KEY_PATH, CRT_PATH);
  exec("chmod", "400", KEY_PATH, CRT_PATH);
  exec("cp", "-p", CONF_PATH, CONF_BAK_PATH);

  serverCrt = getResource("server.crt");
  OkapiLogger.get().debug(() -> config().encodePrettily());
}
 
Example 6
Source File: JdbcDatabaseContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
protected void optionallyMapResourceParameterAsVolume(@NotNull String paramName, @NotNull String pathNameInContainer, @NotNull String defaultResource) {
    String resourceName = parameters.getOrDefault(paramName, defaultResource);

    if (resourceName != null) {
        final MountableFile mountableFile = MountableFile.forClasspathResource(resourceName);
        withCopyFileToContainer(mountableFile, pathNameInContainer);
    }
}
 
Example 7
Source File: GenericContainer.java    From testcontainers-java with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public SELF withClasspathResourceMapping(final String resourcePath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) {
    final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);

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

    return self();
}
 
Example 8
Source File: ClasspathTrait.java    From testcontainers-java with MIT License 4 votes vote down vote up
default SELF withFileFromClasspath(String path, String resourcePath) {
    final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath);

    return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getResolvedPath()));
}
 
Example 9
Source File: ReusabilityUnitTests.java    From testcontainers-java with MIT License 3 votes vote down vote up
@Test
public void differentPath() {
    MountableFile mountableFile = MountableFile.forClasspathResource("test_copy_to_container.txt");
    container.withCopyFileToContainer(mountableFile, "/foo/bar");

    long hash1 = container.hashCopiedFiles().getValue();

    container.getCopyToFileContainerPathMap().clear();

    container.withCopyFileToContainer(mountableFile, "/foo/baz");

    assertThat(container.hashCopiedFiles().getValue()).isNotEqualTo(hash1);
}