|
| 1 | +package com.github.dockerjava.core; |
| 2 | + |
| 3 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 4 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 5 | +import org.apache.commons.io.FileUtils; |
| 6 | +import org.apache.commons.io.IOUtils; |
| 7 | +import org.testng.annotations.Test; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileOutputStream; |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +import static java.util.Arrays.asList; |
| 15 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 16 | +import static org.hamcrest.CoreMatchers.is; |
| 17 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | + |
| 19 | +public class CompressArchiveUtilTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void testExecutableFlagIsPreserved() throws Exception { |
| 23 | + File executableFile = createExecutableFile(); |
| 24 | + File archive = CompressArchiveUtil.archiveTARFiles(executableFile.getParentFile(), asList(executableFile), "archive"); |
| 25 | + File expectedFile = extractFileByName(archive, "executableFile.sh.result"); |
| 26 | + |
| 27 | + assertThat("should be executable", expectedFile.canExecute()); |
| 28 | + } |
| 29 | + |
| 30 | + private File createExecutableFile() throws IOException { |
| 31 | + File baseDir = new File(FileUtils.getTempDirectoryPath()); |
| 32 | + File executableFile = new File(baseDir, "executableFile.sh"); |
| 33 | + executableFile.createNewFile(); |
| 34 | + executableFile.setExecutable(true); |
| 35 | + assertThat(executableFile.canExecute(), is(true)); |
| 36 | + return executableFile; |
| 37 | + } |
| 38 | + |
| 39 | + private File extractFileByName(File archive, String filenameToExtract) throws IOException { |
| 40 | + File baseDir = new File(FileUtils.getTempDirectoryPath()); |
| 41 | + File expectedFile = new File(baseDir, filenameToExtract); |
| 42 | + expectedFile.delete(); |
| 43 | + assertThat(expectedFile.exists(), is(false)); |
| 44 | + |
| 45 | + TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new FileInputStream(archive)); |
| 46 | + TarArchiveEntry entry; |
| 47 | + while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) { |
| 48 | + String individualFiles = entry.getName(); |
| 49 | + // there should be only one file in this archive |
| 50 | + assertThat(individualFiles, equalTo("executableFile.sh")); |
| 51 | + IOUtils.copy(tarArchiveInputStream, new FileOutputStream(expectedFile)); |
| 52 | + if ((entry.getMode() & 0755) == 0755) { |
| 53 | + expectedFile.setExecutable(true); |
| 54 | + } |
| 55 | + } |
| 56 | + tarArchiveInputStream.close(); |
| 57 | + return expectedFile; |
| 58 | + } |
| 59 | +} |
0 commit comments