@Override public String getDescription() { return this.getResolvedPath(); }
/** * {@inheritDoc} */ @Override public void transferTo(final TarArchiveOutputStream outputStream, String destinationPathInTar) { recursiveTar(destinationPathInTar, this.getResolvedPath(), this.getResolvedPath(), outputStream); }
@Override public long getSize() { final File file = new File(this.getResolvedPath()); if (file.isFile()) { return file.length(); } else { return 0; } }
@Override public int getFileMode() { return getUnixFileMode(this.getResolvedPath()); }
default SELF withFileFromClasspath(String path, String resourcePath) { final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath); return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getResolvedPath())); } }
/** * {@inheritDoc} */ @Override public void copyFileToContainer(MountableFile mountableFile, String containerPath) { File sourceFile = new File(mountableFile.getResolvedPath()); if (containerPath.endsWith("/") && sourceFile.isFile()) { logger().warn("folder-like containerPath in copyFileToContainer is deprecated, please explicitly specify a file path"); copyFileToContainer((Transferable) mountableFile, containerPath + sourceFile.getName()); } else { copyFileToContainer((Transferable) mountableFile, containerPath); } }
/** * {@inheritDoc} */ @Override public void addFileSystemBind(final String hostPath, final String containerPath, final BindMode mode, final SelinuxContext selinuxContext) { final MountableFile mountableFile = MountableFile.forHostPath(hostPath); binds.add(new Bind(mountableFile.getResolvedPath(), new Volume(containerPath), mode.accessMode, selinuxContext.selContext)); }
/** * {@inheritDoc} */ @Override 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); return self(); }
private void performChecks(final MountableFile mountableFile) { final String mountablePath = mountableFile.getResolvedPath(); assertTrue("The filesystem path '" + mountablePath + "' can be found", new File(mountablePath).exists()); assertFalse("The filesystem path '" + mountablePath + "' does not contain any URL escaping", mountablePath.contains("%20")); }
private boolean checkMountableFile() { DockerClient dockerClient = client(); MountableFile mountableFile = MountableFile.forClasspathResource(ResourceReaper.class.getName().replace(".", "/") + ".class"); Volume volume = new Volume("/dummy"); try { return runInsideDocker( createContainerCmd -> createContainerCmd.withBinds(new Bind(mountableFile.getResolvedPath(), volume, AccessMode.ro)), (__, containerId) -> { try (InputStream stream = dockerClient.copyArchiveFromContainerCmd(containerId, volume.getPath()).exec()) { stream.read(); return true; } catch (Exception e) { return false; } } ); } catch (Exception e) { log.debug("Failure while checking for mountable file support", e); return false; } }
@Test public void forHostPathWithPlus() throws Exception { final Path file = createTempFile("some+path"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString()); performChecks(mountableFile); assertTrue("The resolved path contains the original space", mountableFile.getResolvedPath().contains("+")); assertFalse("The resolved path does not contain an escaped space", mountableFile.getResolvedPath().contains(" ")); }
@Test public void forHostPathWithSpaces() throws Exception { final Path file = createTempFile("some path"); final MountableFile mountableFile = MountableFile.forHostPath(file.toString()); performChecks(mountableFile); assertTrue("The resolved path contains the original space", mountableFile.getResolvedPath().contains(" ")); assertFalse("The resolved path does not contain an escaped space", mountableFile.getResolvedPath().contains("\\ ")); }
@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)); } }
@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)); } }
@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)); } }
@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)); } } }
@Override public long getSize() { final File file = new File(this.getResolvedPath()); if (file.isFile()) { return file.length(); } else { return 0; } }
default SELF withFileFromClasspath(String path, String resourcePath) { final MountableFile mountableFile = MountableFile.forClasspathResource(resourcePath); return ((SELF) this).withFileFromPath(path, Paths.get(mountableFile.getResolvedPath())); } }
private boolean checkMountableFile() { DockerClient dockerClient = client(); MountableFile mountableFile = MountableFile.forClasspathResource(ResourceReaper.class.getName().replace(".", "/") + ".class"); Volume volume = new Volume("/dummy"); try { return runInsideDocker(createContainerCmd -> createContainerCmd.withBinds(new Bind(mountableFile.getResolvedPath(), volume, AccessMode.ro)), (__, containerId) -> { try (InputStream stream = dockerClient.copyArchiveFromContainerCmd(containerId, volume.getPath()).exec()) { stream.read(); return true; } catch (Exception e) { return false; } }); } catch (Exception e) { log.debug("Failure while checking for mountable file support", e); return false; } }