|
| 1 | +package io.github.intisy.docker.unit; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import io.github.intisy.docker.registry.Digests; |
| 6 | +import io.github.intisy.docker.registry.ImageAssembler; |
| 7 | +import io.github.intisy.docker.registry.ImageConfigOverrides; |
| 8 | +import io.github.intisy.docker.registry.Layer; |
| 9 | +import org.junit.jupiter.api.Tag; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.ByteArrayOutputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.nio.charset.StandardCharsets; |
| 16 | +import java.nio.file.Paths; |
| 17 | +import java.util.Arrays; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +@Tag("unit") |
| 23 | +public class ImageAssemblerTest { |
| 24 | + private static final Gson GSON = new Gson(); |
| 25 | + private static final String NEW_DIFF_ID = |
| 26 | + "sha256:1234123412341234123412341234123412341234123412341234123412341234"; |
| 27 | + private static final String NEW_DIGEST = |
| 28 | + "sha256:5678567856785678567856785678567856785678567856785678567856785678"; |
| 29 | + |
| 30 | + private static Layer layer() { |
| 31 | + return new Layer(Paths.get("layer.tar.gz"), NEW_DIFF_ID, NEW_DIGEST, 4096L); |
| 32 | + } |
| 33 | + |
| 34 | + private static ImageConfigOverrides overrides() { |
| 35 | + return new ImageConfigOverrides() |
| 36 | + .withEntrypoint(Arrays.asList("/opt/spisor/launcher/bin/launcher", |
| 37 | + "--registry=/var/lib/spisor/registry")) |
| 38 | + .withCmd(null) |
| 39 | + .withWorkingDir("/var/lib/spisor"); |
| 40 | + } |
| 41 | + |
| 42 | + private static ImageAssembler.Assembled assemble() throws IOException { |
| 43 | + return ImageAssembler.append(fixture("base-config.json"), fixture("base-manifest.json"), |
| 44 | + layer(), overrides()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void theNewDiffIdIsAppendedLast() throws IOException { |
| 49 | + JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class); |
| 50 | + com.google.gson.JsonArray diffIds = config.getAsJsonObject("rootfs").getAsJsonArray("diff_ids"); |
| 51 | + |
| 52 | + assertEquals(3, diffIds.size()); |
| 53 | + assertEquals(NEW_DIFF_ID, diffIds.get(2).getAsString()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void theNewLayerDescriptorIsAppendedLastWithItsCompressedDigestAndSize() throws IOException { |
| 58 | + JsonObject manifest = GSON.fromJson(assemble().manifestJson(), JsonObject.class); |
| 59 | + com.google.gson.JsonArray layers = manifest.getAsJsonArray("layers"); |
| 60 | + |
| 61 | + assertEquals(3, layers.size()); |
| 62 | + JsonObject added = layers.get(2).getAsJsonObject(); |
| 63 | + assertEquals(NEW_DIGEST, added.get("digest").getAsString()); |
| 64 | + assertEquals(4096L, added.get("size").getAsLong()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void entrypointAndWorkingDirAreStampedAndCmdIsCleared() throws IOException { |
| 69 | + JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class) |
| 70 | + .getAsJsonObject("config"); |
| 71 | + |
| 72 | + assertEquals("/opt/spisor/launcher/bin/launcher", |
| 73 | + config.getAsJsonArray("Entrypoint").get(0).getAsString()); |
| 74 | + assertEquals("--registry=/var/lib/spisor/registry", |
| 75 | + config.getAsJsonArray("Entrypoint").get(1).getAsString()); |
| 76 | + assertEquals("/var/lib/spisor", config.get("WorkingDir").getAsString()); |
| 77 | + assertTrue(config.get("Cmd") == null || config.get("Cmd").isJsonNull(), |
| 78 | + "a base Cmd left in place would run jshell instead of the launcher"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void theBaseEnvironmentSurvives() throws IOException { |
| 83 | + JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class) |
| 84 | + .getAsJsonObject("config"); |
| 85 | + |
| 86 | + assertTrue(config.getAsJsonArray("Env").toString().contains("JAVA_HOME=/opt/java/openjdk"), |
| 87 | + "dropping the base env would remove JAVA_HOME and PATH and the launcher would not start"); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * The manifest's config descriptor must describe the config document we actually produced. |
| 92 | + * If the digest or the size disagrees by one byte the registry rejects the manifest, and the |
| 93 | + * message it gives is not obviously about this. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void theManifestConfigDescriptorMatchesTheProducedConfigExactly() throws IOException { |
| 97 | + ImageAssembler.Assembled assembled = assemble(); |
| 98 | + JsonObject manifest = GSON.fromJson(assembled.manifestJson(), JsonObject.class); |
| 99 | + JsonObject descriptor = manifest.getAsJsonObject("config"); |
| 100 | + |
| 101 | + byte[] configBytes = assembled.configJson().getBytes(StandardCharsets.UTF_8); |
| 102 | + assertEquals(Digests.sha256(configBytes), descriptor.get("digest").getAsString()); |
| 103 | + assertEquals(configBytes.length, descriptor.get("size").getAsLong()); |
| 104 | + assertEquals(assembled.configDigest(), descriptor.get("digest").getAsString()); |
| 105 | + assertEquals(configBytes.length, assembled.configSize()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void aHistoryEntryIsAdded() throws IOException { |
| 110 | + JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class); |
| 111 | + assertEquals(2, config.getAsJsonArray("history").size()); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void anOciBaseProducesAnOciManifest() throws IOException { |
| 116 | + assertEquals("application/vnd.oci.image.manifest.v1+json", assemble().manifestMediaType()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * A docker-schema2 manifest and an OCI manifest use different layer media types, and a |
| 121 | + * manifest that mixes them is rejected. Taking the added layer's type from the layers already |
| 122 | + * present is what keeps an assembled manifest internally consistent whichever shape the base |
| 123 | + * image happened to use, and base images in the wild are both. |
| 124 | + */ |
| 125 | + @Test |
| 126 | + public void aDockerSchema2BaseKeepsDockerMediaTypesThroughout() throws IOException { |
| 127 | + String dockerManifest = fixture("base-manifest.json") |
| 128 | + .replace("application/vnd.oci.image.manifest.v1+json", |
| 129 | + "application/vnd.docker.distribution.manifest.v2+json") |
| 130 | + .replace("application/vnd.oci.image.config.v1+json", |
| 131 | + "application/vnd.docker.container.image.v1+json") |
| 132 | + .replace("application/vnd.oci.image.layer.v1.tar+gzip", |
| 133 | + "application/vnd.docker.image.rootfs.diff.tar.gzip"); |
| 134 | + |
| 135 | + ImageAssembler.Assembled assembled = |
| 136 | + ImageAssembler.append(fixture("base-config.json"), dockerManifest, layer(), overrides()); |
| 137 | + |
| 138 | + assertEquals("application/vnd.docker.distribution.manifest.v2+json", assembled.manifestMediaType()); |
| 139 | + JsonObject manifest = GSON.fromJson(assembled.manifestJson(), JsonObject.class); |
| 140 | + assertEquals("application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 141 | + manifest.getAsJsonArray("layers").get(2).getAsJsonObject().get("mediaType").getAsString()); |
| 142 | + } |
| 143 | + |
| 144 | + private static String fixture(String name) throws IOException { |
| 145 | + InputStream in = ImageAssemblerTest.class.getResourceAsStream("/registry/" + name); |
| 146 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 147 | + byte[] buffer = new byte[8192]; |
| 148 | + int read; |
| 149 | + while ((read = in.read(buffer)) != -1) { |
| 150 | + out.write(buffer, 0, read); |
| 151 | + } |
| 152 | + in.close(); |
| 153 | + return new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 154 | + } |
| 155 | +} |
0 commit comments