|
| 1 | +package io.github.intisy.docker.unit; |
| 2 | + |
| 3 | +import io.github.intisy.docker.registry.ImageConfigOverrides; |
| 4 | +import io.github.intisy.docker.registry.ImagePublisher; |
| 5 | +import io.github.intisy.docker.registry.ImageReference; |
| 6 | +import io.github.intisy.docker.registry.Layer; |
| 7 | +import io.github.intisy.docker.registry.RegistryClient; |
| 8 | +import io.github.intisy.docker.registry.RegistryResponse; |
| 9 | +import org.junit.jupiter.api.Tag; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.io.TempDir; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +@Tag("unit") |
| 26 | +public class ImagePublisherTest { |
| 27 | + private static final String INDEX_DIGEST = |
| 28 | + "sha256:2222222222222222222222222222222222222222222222222222222222222222"; |
| 29 | + private static final String CONFIG_DIGEST = |
| 30 | + "sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"; |
| 31 | + private static final String BASE_LAYER_DIGEST = |
| 32 | + "sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"; |
| 33 | + private static final String NEW_LAYER_DIGEST = |
| 34 | + "sha256:5678567856785678567856785678567856785678567856785678567856785678"; |
| 35 | + |
| 36 | + private static final String INDEX = "{\"schemaVersion\":2,\"manifests\":[{\"digest\":\"" + INDEX_DIGEST |
| 37 | + + "\",\"platform\":{\"os\":\"linux\",\"architecture\":\"amd64\"}}]}"; |
| 38 | + private static final String MANIFEST = "{\"schemaVersion\":2," |
| 39 | + + "\"mediaType\":\"application/vnd.oci.image.manifest.v1+json\"," |
| 40 | + + "\"config\":{\"mediaType\":\"application/vnd.oci.image.config.v1+json\",\"digest\":\"" |
| 41 | + + CONFIG_DIGEST + "\",\"size\":10}," |
| 42 | + + "\"layers\":[{\"mediaType\":\"application/vnd.oci.image.layer.v1.tar+gzip\",\"digest\":\"" |
| 43 | + + BASE_LAYER_DIGEST + "\",\"size\":30}]}"; |
| 44 | + private static final String CONFIG = "{\"architecture\":\"amd64\",\"os\":\"linux\"," |
| 45 | + + "\"config\":{\"Env\":[\"PATH=/usr/bin\"]}," |
| 46 | + + "\"rootfs\":{\"type\":\"layers\",\"diff_ids\":[\"sha256:aaaa\"]},\"history\":[]}"; |
| 47 | + |
| 48 | + private FakeRegistryHttp sourceHttp; |
| 49 | + private FakeRegistryHttp targetHttp; |
| 50 | + |
| 51 | + private RegistryClient source() { |
| 52 | + sourceHttp = new FakeRegistryHttp(); |
| 53 | + sourceHttp.answerText("GET https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/21-jre", |
| 54 | + 200, INDEX, "Content-Type", RegistryClient.OCI_INDEX); |
| 55 | + sourceHttp.answerText("GET https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/" |
| 56 | + + INDEX_DIGEST, 200, MANIFEST, "Content-Type", RegistryClient.OCI_MANIFEST); |
| 57 | + sourceHttp.answer("GET https://registry-1.docker.io/v2/library/eclipse-temurin/blobs/" + CONFIG_DIGEST, |
| 58 | + new RegistryResponse(200, new HashMap<String, String>(), CONFIG.getBytes(StandardCharsets.UTF_8))); |
| 59 | + sourceHttp.answer("GET https://registry-1.docker.io/v2/library/eclipse-temurin/blobs/" + BASE_LAYER_DIGEST, |
| 60 | + new RegistryResponse(200, new HashMap<String, String>(), new byte[] {9, 9, 9})); |
| 61 | + return new RegistryClient(sourceHttp, "registry-1.docker.io", false); |
| 62 | + } |
| 63 | + |
| 64 | + private RegistryClient target(boolean baseLayerAlreadyPresent) { |
| 65 | + targetHttp = new FakeRegistryHttp(); |
| 66 | + String root = "http://registry.spisor.internal/v2/spisor/core"; |
| 67 | + targetHttp.answer("HEAD " + root + "/blobs/" + BASE_LAYER_DIGEST, new RegistryResponse( |
| 68 | + baseLayerAlreadyPresent ? 200 : 404, new HashMap<String, String>(), new byte[0])); |
| 69 | + targetHttp.answer("HEAD " + root + "/blobs/" + NEW_LAYER_DIGEST, |
| 70 | + new RegistryResponse(404, new HashMap<String, String>(), new byte[0])); |
| 71 | + targetHttp.answer("POST " + root + "/blobs/uploads/", new RegistryResponse(202, |
| 72 | + location(root + "/blobs/uploads/u1"), new byte[0])); |
| 73 | + targetHttp.answer("PUT " + root + "/blobs/uploads/u1?digest=" + encoded(BASE_LAYER_DIGEST), |
| 74 | + new RegistryResponse(201, new HashMap<String, String>(), new byte[0])); |
| 75 | + targetHttp.answer("PUT " + root + "/blobs/uploads/u1?digest=" + encoded(NEW_LAYER_DIGEST), |
| 76 | + new RegistryResponse(201, new HashMap<String, String>(), new byte[0])); |
| 77 | + targetHttp.answer("PUT " + root + "/manifests/0.1.0", |
| 78 | + new RegistryResponse(201, new HashMap<String, String>(), new byte[0])); |
| 79 | + targetHttp.answerMissingHeadsAs404(); |
| 80 | + targetHttp.acceptAnyBlobUpload(); |
| 81 | + return new RegistryClient(targetHttp, "registry.spisor.internal", true); |
| 82 | + } |
| 83 | + |
| 84 | + private static java.util.Map<String, String> location(String value) { |
| 85 | + java.util.Map<String, String> headers = new HashMap<String, String>(); |
| 86 | + headers.put("Location", value); |
| 87 | + return headers; |
| 88 | + } |
| 89 | + |
| 90 | + private static String encoded(String digest) { |
| 91 | + return digest.replace(":", "%3A"); |
| 92 | + } |
| 93 | + |
| 94 | + private Layer layer(Path tmp) throws IOException { |
| 95 | + Path file = tmp.resolve("layer.tar.gz"); |
| 96 | + Files.write(file, new byte[] {1, 2, 3, 4}); |
| 97 | + return new Layer(file, |
| 98 | + "sha256:1234123412341234123412341234123412341234123412341234123412341234", |
| 99 | + NEW_LAYER_DIGEST, 4L); |
| 100 | + } |
| 101 | + |
| 102 | + private static ImageConfigOverrides overrides() { |
| 103 | + List<String> entrypoint = Arrays.asList("/opt/spisor/launcher/bin/launcher"); |
| 104 | + return new ImageConfigOverrides().withEntrypoint(entrypoint).withCmd(null); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void publishesConfigLayerAndManifestAndReturnsTheManifestDigest(@TempDir Path tmp) throws IOException { |
| 109 | + String digest = ImagePublisher.publish(source(), ImageReference.parse("eclipse-temurin:21-jre"), |
| 110 | + target(false), ImageReference.parse("registry.spisor.internal/spisor/core:0.1.0"), |
| 111 | + layer(tmp), overrides()); |
| 112 | + |
| 113 | + assertTrue(digest.startsWith("sha256:"), digest); |
| 114 | + List<String> calls = targetHttp.calls(); |
| 115 | + assertTrue(calls.contains("PUT http://registry.spisor.internal/v2/spisor/core/manifests/0.1.0"), |
| 116 | + calls.toString()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * The manifest must be the LAST thing pushed. A registry rejects a manifest whose blobs are |
| 121 | + * not all present, so an ordering slip here produces a MANIFEST_BLOB_UNKNOWN that reads like a |
| 122 | + * corruption problem. |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void theManifestIsPushedLast(@TempDir Path tmp) throws IOException { |
| 126 | + ImagePublisher.publish(source(), ImageReference.parse("eclipse-temurin:21-jre"), |
| 127 | + target(false), ImageReference.parse("registry.spisor.internal/spisor/core:0.1.0"), |
| 128 | + layer(tmp), overrides()); |
| 129 | + |
| 130 | + List<String> calls = targetHttp.calls(); |
| 131 | + assertEquals("PUT http://registry.spisor.internal/v2/spisor/core/manifests/0.1.0", |
| 132 | + calls.get(calls.size() - 1)); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * The base is tens of megabytes. Re-sending it on every publish would make the day-to-day loop |
| 137 | + * unusable, and the HEAD that prevents it is cheap. |
| 138 | + */ |
| 139 | + @Test |
| 140 | + public void aBaseLayerAlreadyInTheTargetIsNotReUploaded(@TempDir Path tmp) throws IOException { |
| 141 | + ImagePublisher.publish(source(), ImageReference.parse("eclipse-temurin:21-jre"), |
| 142 | + target(true), ImageReference.parse("registry.spisor.internal/spisor/core:0.1.0"), |
| 143 | + layer(tmp), overrides()); |
| 144 | + |
| 145 | + assertFalse(sourceHttp.calls().contains("GET https://registry-1.docker.io/v2/library/eclipse-temurin/blobs/" |
| 146 | + + BASE_LAYER_DIGEST), |
| 147 | + "an already-present layer must not even be fetched from the source"); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void theIndexIsResolvedToTheAmd64ManifestBeforeAnythingIsRead(@TempDir Path tmp) throws IOException { |
| 152 | + ImagePublisher.publish(source(), ImageReference.parse("eclipse-temurin:21-jre"), |
| 153 | + target(false), ImageReference.parse("registry.spisor.internal/spisor/core:0.1.0"), |
| 154 | + layer(tmp), overrides()); |
| 155 | + |
| 156 | + assertEquals("GET https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/21-jre", |
| 157 | + sourceHttp.calls().get(0)); |
| 158 | + assertEquals("GET https://registry-1.docker.io/v2/library/eclipse-temurin/manifests/" + INDEX_DIGEST, |
| 159 | + sourceHttp.calls().get(1)); |
| 160 | + } |
| 161 | +} |
0 commit comments