|
| 1 | +package io.github.intisy.docker.unit; |
| 2 | + |
| 3 | +import io.github.intisy.docker.registry.Digests; |
| 4 | +import io.github.intisy.docker.registry.Layer; |
| 5 | +import io.github.intisy.docker.registry.LayerBuilder; |
| 6 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 7 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 8 | +import org.junit.jupiter.api.Tag; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.io.TempDir; |
| 11 | + |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.nio.file.Files; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.zip.GZIPInputStream; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +@Tag("unit") |
| 27 | +public class LayerBuilderTest { |
| 28 | + |
| 29 | + private static Path launcherLikeTree(Path root) throws IOException { |
| 30 | + Path dist = root.resolve("dist"); |
| 31 | + Files.createDirectories(dist.resolve("bin")); |
| 32 | + Files.createDirectories(dist.resolve("lib")); |
| 33 | + Files.write(dist.resolve("bin/launcher"), "#!/bin/sh\nexec java -cp ...\n".getBytes(StandardCharsets.UTF_8)); |
| 34 | + Files.write(dist.resolve("bin/launcher.bat"), "@echo off\n".getBytes(StandardCharsets.UTF_8)); |
| 35 | + Files.write(dist.resolve("lib/launcher.jar"), new byte[] {0x50, 0x4b, 0x03, 0x04}); |
| 36 | + return dist; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void entriesAreRootedAtThePathInImage(@TempDir Path tmp) throws IOException { |
| 41 | + Layer layer = LayerBuilder.fromDirectory( |
| 42 | + launcherLikeTree(tmp), "/opt/spisor/launcher", tmp.resolve("layer.tar.gz")); |
| 43 | + |
| 44 | + List<String> names = entryNames(layer.file()); |
| 45 | + assertTrue(names.contains("opt/spisor/launcher/bin/launcher"), names.toString()); |
| 46 | + assertTrue(names.contains("opt/spisor/launcher/lib/launcher.jar"), names.toString()); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void filesUnderBinAreExecutableAndOthersAreNot(@TempDir Path tmp) throws IOException { |
| 51 | + Layer layer = LayerBuilder.fromDirectory( |
| 52 | + launcherLikeTree(tmp), "/opt/spisor/launcher", tmp.resolve("layer.tar.gz")); |
| 53 | + |
| 54 | + assertEquals(0755, modeOf(layer.file(), "opt/spisor/launcher/bin/launcher")); |
| 55 | + assertEquals(0644, modeOf(layer.file(), "opt/spisor/launcher/lib/launcher.jar")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void twoBuildsOfTheSameTreeProduceTheSameDigest(@TempDir Path tmp) throws IOException { |
| 60 | + Path source = launcherLikeTree(tmp); |
| 61 | + Layer first = LayerBuilder.fromDirectory(source, "/opt/spisor/launcher", tmp.resolve("a.tar.gz")); |
| 62 | + Layer second = LayerBuilder.fromDirectory(source, "/opt/spisor/launcher", tmp.resolve("b.tar.gz")); |
| 63 | + |
| 64 | + assertEquals(first.diffId(), second.diffId()); |
| 65 | + assertEquals(first.digest(), second.digest()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void changingAFileChangesTheDigest(@TempDir Path tmp) throws IOException { |
| 70 | + Path source = launcherLikeTree(tmp); |
| 71 | + Layer before = LayerBuilder.fromDirectory(source, "/opt/spisor/launcher", tmp.resolve("a.tar.gz")); |
| 72 | + Files.write(source.resolve("lib/launcher.jar"), new byte[] {0x50, 0x4b, 0x03, 0x05}); |
| 73 | + Layer after = LayerBuilder.fromDirectory(source, "/opt/spisor/launcher", tmp.resolve("b.tar.gz")); |
| 74 | + |
| 75 | + assertNotEquals(before.diffId(), after.diffId()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * The diff id is the digest of the uncompressed tar and the digest is the digest of the gzip. |
| 80 | + * Conflating them produces a config whose rootfs entry no layer can satisfy, and the registry |
| 81 | + * accepts the push and the node then fails to unpack. |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void diffIdIsTheUncompressedDigestAndDigestIsTheCompressedOne(@TempDir Path tmp) throws IOException { |
| 85 | + Layer layer = LayerBuilder.fromDirectory( |
| 86 | + launcherLikeTree(tmp), "/opt/spisor/launcher", tmp.resolve("layer.tar.gz")); |
| 87 | + |
| 88 | + assertNotEquals(layer.diffId(), layer.digest()); |
| 89 | + assertEquals(layer.digest(), sha256Closing(new FileInputStream(layer.file().toFile()))); |
| 90 | + assertEquals(layer.diffId(), |
| 91 | + sha256Closing(new GZIPInputStream(new FileInputStream(layer.file().toFile())))); |
| 92 | + assertEquals(Files.size(layer.file()), layer.size()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @implNote {@link Digests#sha256(InputStream)} does not close its argument; on Windows a |
| 97 | + * lingering open handle on the layer file blocks {@code @TempDir} cleanup after the test. |
| 98 | + */ |
| 99 | + private static String sha256Closing(InputStream stream) throws IOException { |
| 100 | + try { |
| 101 | + return Digests.sha256(stream); |
| 102 | + } finally { |
| 103 | + stream.close(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static List<String> entryNames(Path archive) throws IOException { |
| 108 | + List<String> names = new ArrayList<String>(); |
| 109 | + TarArchiveInputStream tar = openTar(archive); |
| 110 | + try { |
| 111 | + TarArchiveEntry entry; |
| 112 | + while ((entry = tar.getNextTarEntry()) != null) { |
| 113 | + names.add(entry.getName()); |
| 114 | + } |
| 115 | + } finally { |
| 116 | + tar.close(); |
| 117 | + } |
| 118 | + return names; |
| 119 | + } |
| 120 | + |
| 121 | + private static int modeOf(Path archive, String name) throws IOException { |
| 122 | + TarArchiveInputStream tar = openTar(archive); |
| 123 | + try { |
| 124 | + TarArchiveEntry entry; |
| 125 | + while ((entry = tar.getNextTarEntry()) != null) { |
| 126 | + if (name.equals(entry.getName())) { |
| 127 | + return entry.getMode() & 0777; |
| 128 | + } |
| 129 | + } |
| 130 | + } finally { |
| 131 | + tar.close(); |
| 132 | + } |
| 133 | + throw new IOException("no entry " + name + " in " + archive); |
| 134 | + } |
| 135 | + |
| 136 | + private static TarArchiveInputStream openTar(Path archive) throws IOException { |
| 137 | + InputStream raw = new FileInputStream(archive.toFile()); |
| 138 | + return new TarArchiveInputStream(new GZIPInputStream(raw)); |
| 139 | + } |
| 140 | +} |
0 commit comments