|
| 1 | +package copy |
| 2 | + |
| 3 | +import ( |
| 4 | + "io/ioutil" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + internalManifest "github.com/containers/image/v5/internal/manifest" |
| 9 | + digest "github.com/opencontainers/go-digest" |
| 10 | + imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestDetermineSpecificImages(t *testing.T) { |
| 16 | + testCases := []struct { |
| 17 | + id string |
| 18 | + fixture string |
| 19 | + instanceDigests []digest.Digest |
| 20 | + instancePlatforms []imgspecv1.Platform |
| 21 | + expected []digest.Digest |
| 22 | + expectedErrIncludes string |
| 23 | + }{ |
| 24 | + { |
| 25 | + id: "no inputs no outputs", |
| 26 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 27 | + }, |
| 28 | + { |
| 29 | + id: "instances only out of order", |
| 30 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 31 | + instanceDigests: []digest.Digest{ |
| 32 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 33 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 34 | + }, |
| 35 | + expected: []digest.Digest{ |
| 36 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 37 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 38 | + }, |
| 39 | + }, |
| 40 | + { |
| 41 | + id: "instances only in order", |
| 42 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 43 | + instanceDigests: []digest.Digest{ |
| 44 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 45 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 46 | + }, |
| 47 | + expected: []digest.Digest{ |
| 48 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 49 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 50 | + }, |
| 51 | + }, |
| 52 | + { |
| 53 | + id: "platforms only in order", |
| 54 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 55 | + instancePlatforms: []imgspecv1.Platform{ |
| 56 | + { |
| 57 | + OS: "linux", |
| 58 | + Architecture: "ppc64le", |
| 59 | + }, |
| 60 | + { |
| 61 | + OS: "linux", |
| 62 | + Architecture: "s390x", |
| 63 | + }, |
| 64 | + }, |
| 65 | + expected: []digest.Digest{ |
| 66 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 67 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + id: "platforms only out of order", |
| 72 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 73 | + instancePlatforms: []imgspecv1.Platform{ |
| 74 | + { |
| 75 | + OS: "linux", |
| 76 | + Architecture: "s390x", |
| 77 | + }, |
| 78 | + { |
| 79 | + OS: "linux", |
| 80 | + Architecture: "ppc64le", |
| 81 | + }, |
| 82 | + }, |
| 83 | + expected: []digest.Digest{ |
| 84 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 85 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + id: "mixed without duplicates", |
| 90 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 91 | + instancePlatforms: []imgspecv1.Platform{ |
| 92 | + { |
| 93 | + OS: "linux", |
| 94 | + Architecture: "s390x", |
| 95 | + }, |
| 96 | + }, |
| 97 | + instanceDigests: []digest.Digest{ |
| 98 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 99 | + }, |
| 100 | + expected: []digest.Digest{ |
| 101 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 102 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 103 | + }, |
| 104 | + }, |
| 105 | + { |
| 106 | + id: "mixed with duplicates", |
| 107 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 108 | + instancePlatforms: []imgspecv1.Platform{ |
| 109 | + { |
| 110 | + OS: "linux", |
| 111 | + Architecture: "ppc64le", |
| 112 | + }, |
| 113 | + { |
| 114 | + OS: "linux", |
| 115 | + Architecture: "s390x", |
| 116 | + }, |
| 117 | + }, |
| 118 | + instanceDigests: []digest.Digest{ |
| 119 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 120 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 121 | + }, |
| 122 | + expected: []digest.Digest{ |
| 123 | + "sha256:7820f9a86d4ad15a2c4f0c0e5479298df2aa7c2f6871288e2ef8546f3e7b6783", |
| 124 | + "sha256:e4c0df75810b953d6717b8f8f28298d73870e8aa2a0d5e77b8391f16fdfbbbe2", |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + id: "no such platform", |
| 129 | + fixture: "../manifest/fixtures/v2list.manifest.json", |
| 130 | + instancePlatforms: []imgspecv1.Platform{ |
| 131 | + { |
| 132 | + OS: "windows", |
| 133 | + Architecture: "amd64", |
| 134 | + }, |
| 135 | + { |
| 136 | + OS: "darwin", |
| 137 | + Architecture: "arm64", |
| 138 | + }, |
| 139 | + }, |
| 140 | + expectedErrIncludes: "no image found in manifest list for", |
| 141 | + }, |
| 142 | + } |
| 143 | + for _, tc := range testCases { |
| 144 | + t.Run(tc.id, func(t *testing.T) { |
| 145 | + listBlob, err := ioutil.ReadFile(tc.fixture) |
| 146 | + require.NoErrorf(t, err, "unexpected error reading fixture %q", tc.fixture) |
| 147 | + list, err := internalManifest.ListFromBlob(listBlob, internalManifest.GuessMIMEType(listBlob)) |
| 148 | + require.NoErrorf(t, err, "unexpected error parsing fixture %q", tc.fixture) |
| 149 | + options := Options{ |
| 150 | + Instances: tc.instanceDigests, |
| 151 | + InstancePlatforms: tc.instancePlatforms, |
| 152 | + } |
| 153 | + specific, err := determineSpecificImages(&options, list) |
| 154 | + if err != nil && tc.expectedErrIncludes != "" { |
| 155 | + if strings.Contains(err.Error(), tc.expectedErrIncludes) { |
| 156 | + // okay |
| 157 | + return |
| 158 | + } |
| 159 | + } |
| 160 | + require.NoErrorf(t, err, "unexpected error selecting instances") |
| 161 | + var selected []digest.Digest |
| 162 | + for _, instanceDigest := range list.Instances() { |
| 163 | + if specific.Contains(instanceDigest) { |
| 164 | + selected = append(selected, instanceDigest) |
| 165 | + } |
| 166 | + } |
| 167 | + assert.Equalf(t, tc.expected, selected, "given instance list %#v and platforms list %#v", tc.instanceDigests, tc.instancePlatforms) |
| 168 | + }) |
| 169 | + } |
| 170 | +} |
0 commit comments