|
| 1 | +package io.github.intisy.docker.unit; |
| 2 | + |
| 3 | +import io.github.intisy.docker.registry.PlatformSelector; |
| 4 | +import io.github.intisy.docker.registry.RegistryClient; |
| 5 | +import org.junit.jupiter.api.Tag; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 16 | +import static org.junit.jupiter.api.Assertions.fail; |
| 17 | + |
| 18 | +@Tag("unit") |
| 19 | +public class PlatformSelectorTest { |
| 20 | + |
| 21 | + /** |
| 22 | + * The arm64 entry is deliberately first in the fixture. Taking the first entry is the bug this |
| 23 | + * test exists to prevent: it yields an image that pulls perfectly and then fails to exec. |
| 24 | + */ |
| 25 | + @Test |
| 26 | + public void picksAmd64EvenWhenArm64ComesFirst() throws IOException { |
| 27 | + assertEquals("sha256:2222222222222222222222222222222222222222222222222222222222222222", |
| 28 | + PlatformSelector.digestFor(fixture(), "linux", "amd64")); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void missingPlatformFailsAndNamesWhatWasAvailable() throws IOException { |
| 33 | + try { |
| 34 | + PlatformSelector.digestFor(fixture(), "linux", "s390x"); |
| 35 | + fail("expected an IOException"); |
| 36 | + } catch (IOException expected) { |
| 37 | + assertTrue(expected.getMessage().contains("linux/s390x"), expected.getMessage()); |
| 38 | + assertTrue(expected.getMessage().contains("linux/amd64"), expected.getMessage()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void indexMediaTypesAreRecognisedAndManifestsAreNot() { |
| 44 | + assertTrue(PlatformSelector.isIndex(RegistryClient.OCI_INDEX)); |
| 45 | + assertTrue(PlatformSelector.isIndex(RegistryClient.DOCKER_MANIFEST_LIST)); |
| 46 | + assertFalse(PlatformSelector.isIndex(RegistryClient.OCI_MANIFEST)); |
| 47 | + assertFalse(PlatformSelector.isIndex(null)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Registries append parameters to Content-Type. Matching the header exactly would classify a |
| 52 | + * real index as a plain manifest and then try to read layers out of it. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + public void indexMediaTypeWithParametersStillCounts() { |
| 56 | + assertTrue(PlatformSelector.isIndex(RegistryClient.OCI_INDEX + "; charset=utf-8")); |
| 57 | + } |
| 58 | + |
| 59 | + private static String fixture() throws IOException { |
| 60 | + InputStream in = PlatformSelectorTest.class.getResourceAsStream("/registry/temurin-index.json"); |
| 61 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 62 | + byte[] buffer = new byte[8192]; |
| 63 | + int read; |
| 64 | + while ((read = in.read(buffer)) != -1) { |
| 65 | + out.write(buffer, 0, read); |
| 66 | + } |
| 67 | + in.close(); |
| 68 | + return new String(out.toByteArray(), StandardCharsets.UTF_8); |
| 69 | + } |
| 70 | +} |
0 commit comments