Skip to content

Commit 3a0176e

Browse files
committed
Fix CompressArchiveUtil unit tests
The tar method's contract is very clear, it creates a tar archive from files in a folder. The unit tests should validate that the tar archive was created and that the content is what it is supposed to be. Before, in order to validate that the tar archive was created, the unit tests were un-archiving the tar. This is wrong as you end up implicitly testing the extraction code which could be faulty. In addition, the 2 tests for symlinks were not even checking if the extracted file was a symlink, they were only checking if the file or folder were readable. Rewrite the tests to assert directly the content of the archive. Also fix the 2 symlinks tests to make sure the tar preserve them. Fixing the unit tests exposed that CompressArchiveUtil.tar method does not preserve the symlinks which is a bug. Add @ignore annotation for those 2 tests until the bug is fixed. Related to issue #532
1 parent f6a2d49 commit 3a0176e

1 file changed

Lines changed: 55 additions & 51 deletions

File tree

src/test/java/com/github/dockerjava/core/CompressArchiveUtilTest.java

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
import com.github.dockerjava.core.util.CompressArchiveUtil;
44
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
55
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
6-
import org.apache.commons.io.IOUtils;
6+
import org.junit.Ignore;
77
import org.junit.Rule;
88
import org.junit.Test;
99
import org.junit.rules.TemporaryFolder;
1010

1111
import java.io.BufferedInputStream;
1212
import java.io.File;
1313
import java.io.FileInputStream;
14-
import java.io.FileOutputStream;
1514
import java.io.IOException;
1615
import java.nio.file.Files;
1716
import java.nio.file.Path;
1817
import java.util.zip.GZIPInputStream;
1918

2019
import static java.util.Arrays.asList;
21-
import static org.hamcrest.CoreMatchers.is;
22-
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
2323

2424
public class CompressArchiveUtilTest {
2525

@@ -30,66 +30,70 @@ public class CompressArchiveUtilTest {
3030
public void testExecutableFlagIsPreserved() throws Exception {
3131
File executableFile = tempFolder.newFile("executableFile.sh");
3232
executableFile.setExecutable(true);
33-
assertThat(executableFile.canExecute(), is(true));
33+
assertTrue("should be executable", executableFile.canExecute());
3434

3535
File archive = CompressArchiveUtil.archiveTARFiles(tempFolder.getRoot(), asList(executableFile),
3636
"archive");
37-
File expectedFile = extractFileByName(archive, "executableFile.sh", "executableFile.sh.result");
38-
39-
assertThat("should be executable", expectedFile.canExecute());
40-
}
41-
42-
private File extractFileByName(File archive, String filenameToExtract, String outputName) throws IOException {
43-
File expectedFile = new File(tempFolder.newFolder(), outputName);
44-
expectedFile.delete();
45-
assertThat(expectedFile.exists(), is(false));
46-
47-
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GZIPInputStream(
48-
new BufferedInputStream(new FileInputStream(archive))));
49-
TarArchiveEntry entry;
50-
boolean found = false;
51-
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
52-
String individualFiles = entry.getName();
53-
if (individualFiles.equals(filenameToExtract) || individualFiles.endsWith("/" + filenameToExtract)) {
54-
found = true;
55-
IOUtils.copy(tarArchiveInputStream, new FileOutputStream(expectedFile));
56-
if ((entry.getMode() & 0755) == 0755) {
57-
expectedFile.setExecutable(true);
58-
}
59-
break;
60-
}
61-
}
62-
assertThat("should extracted the file", found);
63-
tarArchiveInputStream.close();
64-
return expectedFile;
37+
TarArchiveEntry expectedTarArchiveEntry = getTarArchiveEntry(archive, "executableFile.sh");
38+
assertNotNull(expectedTarArchiveEntry);
39+
assertEquals("should be executable", (expectedTarArchiveEntry.getMode() & 0755), 0755);
6540
}
6641

6742
@Test
43+
@Ignore("Symlink creation is broken so test do not pass")
6844
public void testSymbolicLinkDir() throws IOException {
69-
Path uploadDir = tempFolder.newFolder("upload").toPath();
70-
Path linkTarget = tempFolder.newFolder("link-target").toPath();
71-
Path tmpFile = Files.createTempFile(linkTarget, "link-dir", "rand");
72-
Files.createSymbolicLink(uploadDir.resolve("link-folder"), linkTarget);
45+
Path archiveSourceDir = tempFolder.newFolder("archive-source").toPath();
46+
Path linkTargetDir = archiveSourceDir.resolve("link-target");
47+
linkTargetDir.toFile().mkdir();
48+
Files.createSymbolicLink(archiveSourceDir.resolve("link-folder"), linkTargetDir);
49+
7350
Path tarGzFile = tempFolder.newFile("docker-java.tar.gz").toPath();
7451
//follow link only works for childrenOnly=false
75-
CompressArchiveUtil.tar(uploadDir, tarGzFile, true, false);
76-
File expectedFile = extractFileByName(tarGzFile.toFile(), tmpFile.toFile().getName(), "foo1");
77-
assertThat(expectedFile.canRead(), is(true));
52+
CompressArchiveUtil.tar(archiveSourceDir, tarGzFile, true, false);
53+
TarArchiveEntry expectedTarArchiveEntry = getTarArchiveEntry(tarGzFile.toFile(), "link-folder");
54+
assertNotNull(expectedTarArchiveEntry);
55+
assertTrue("should be a symbolic link", expectedTarArchiveEntry.isSymbolicLink());
56+
assertEquals("link-target", expectedTarArchiveEntry.getLinkName());
7857
}
7958

8059
@Test
60+
@Ignore("Symlink creation is broken so test do not pass")
8161
public void testSymbolicLinkFile() throws IOException {
82-
Path uploadDir = tempFolder.newFolder("upload").toPath();
83-
Path tmpFile = tempFolder.newFile("src").toPath();
84-
Files.createSymbolicLink(uploadDir.resolve("link-file"), tmpFile);
62+
Path archiveSourceDir = tempFolder.newFolder("archive-source").toPath();
63+
Path linkTargetFile = archiveSourceDir.resolve("link-target");
64+
Files.createSymbolicLink(archiveSourceDir.resolve("link-file"), linkTargetFile);
65+
8566
Path tarGzFile = tempFolder.newFile("docker-java.tar.gz").toPath();
86-
boolean childrenOnly = false;
87-
CompressArchiveUtil.tar(uploadDir, tarGzFile, true, childrenOnly);
88-
File expectedFile = extractFileByName(tarGzFile.toFile(), "link-file", "foo1");
89-
assertThat(expectedFile.canRead(), is(true));
90-
childrenOnly = true;
91-
CompressArchiveUtil.tar(uploadDir, tarGzFile, true, childrenOnly);
92-
extractFileByName(tarGzFile.toFile(), "link-file", "foo1");
93-
assertThat(expectedFile.canRead(), is(true));
67+
68+
CompressArchiveUtil.tar(archiveSourceDir, tarGzFile, true, false);
69+
TarArchiveEntry expectedTarArchiveEntry = getTarArchiveEntry(tarGzFile.toFile(), "link-file");
70+
assertNotNull(expectedTarArchiveEntry);
71+
assertTrue("should be a symbolic link", expectedTarArchiveEntry.isSymbolicLink());
72+
assertEquals("link-target", expectedTarArchiveEntry.getLinkName());
73+
74+
CompressArchiveUtil.tar(archiveSourceDir, tarGzFile, true, true);
75+
expectedTarArchiveEntry = getTarArchiveEntry(tarGzFile.toFile(), "link-file");
76+
assertNotNull(expectedTarArchiveEntry);
77+
assertTrue("should be a symbolic link", expectedTarArchiveEntry.isSymbolicLink());
78+
assertEquals("link-target", expectedTarArchiveEntry.getLinkName());
79+
}
80+
81+
private TarArchiveEntry getTarArchiveEntry(File tarArchive, String filename) throws IOException {
82+
TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(
83+
new GZIPInputStream(new BufferedInputStream(new FileInputStream(tarArchive))));
84+
try {
85+
TarArchiveEntry entry;
86+
while ((entry = tarArchiveInputStream.getNextTarEntry()) != null) {
87+
if (entry.getName().equals(filename)
88+
|| entry.getName().endsWith("/" + filename)
89+
|| entry.getName().equals(filename + "/")
90+
|| entry.getName().endsWith("/" + filename + "/")) {
91+
return entry;
92+
}
93+
}
94+
} finally {
95+
tarArchiveInputStream.close();
96+
}
97+
return null;
9498
}
9599
}

0 commit comments

Comments
 (0)