1 package manifestlist
2
3 import (
4 "bytes"
5 "encoding/json"
6 "reflect"
7 "testing"
8
9 "github.com/docker/distribution"
10 "github.com/docker/distribution/manifest/ocischema"
11
12 v1 "github.com/opencontainers/image-spec/specs-go/v1"
13 )
14
15 var expectedManifestListSerialization = []byte(`{
16 "schemaVersion": 2,
17 "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
18 "manifests": [
19 {
20 "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
21 "size": 985,
22 "digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
23 "platform": {
24 "architecture": "amd64",
25 "os": "linux",
26 "features": [
27 "sse4"
28 ]
29 }
30 },
31 {
32 "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
33 "size": 2392,
34 "digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
35 "platform": {
36 "architecture": "sun4m",
37 "os": "sunos"
38 }
39 }
40 ]
41 }`)
42
43 func makeTestManifestList(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
44 manifestDescriptors := []ManifestDescriptor{
45 {
46 Descriptor: distribution.Descriptor{
47 Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
48 Size: 985,
49 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
50 },
51 Platform: PlatformSpec{
52 Architecture: "amd64",
53 OS: "linux",
54 Features: []string{"sse4"},
55 },
56 },
57 {
58 Descriptor: distribution.Descriptor{
59 Digest: "sha256:6346340964309634683409684360934680934608934608934608934068934608",
60 Size: 2392,
61 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
62 },
63 Platform: PlatformSpec{
64 Architecture: "sun4m",
65 OS: "sunos",
66 },
67 },
68 }
69
70 deserialized, err := FromDescriptorsWithMediaType(manifestDescriptors, mediaType)
71 if err != nil {
72 t.Fatalf("error creating DeserializedManifestList: %v", err)
73 }
74
75 return manifestDescriptors, deserialized
76 }
77
78 func TestManifestList(t *testing.T) {
79 manifestDescriptors, deserialized := makeTestManifestList(t, MediaTypeManifestList)
80 mediaType, canonical, _ := deserialized.Payload()
81
82 if mediaType != MediaTypeManifestList {
83 t.Fatalf("unexpected media type: %s", mediaType)
84 }
85
86
87
88 p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
89 if err != nil {
90 t.Fatalf("error marshaling manifest list: %v", err)
91 }
92 if !bytes.Equal(p, canonical) {
93 t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
94 }
95
96
97 if !bytes.Equal(expectedManifestListSerialization, canonical) {
98 t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestListSerialization))
99 }
100
101 var unmarshalled DeserializedManifestList
102 if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
103 t.Fatalf("error unmarshaling manifest: %v", err)
104 }
105
106 if !reflect.DeepEqual(&unmarshalled, deserialized) {
107 t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
108 }
109
110 references := deserialized.References()
111 if len(references) != 2 {
112 t.Fatalf("unexpected number of references: %d", len(references))
113 }
114 for i := range references {
115 if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
116 t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
117 }
118 }
119 }
120
121
122
123
124
125
126
127 var expectedOCIImageIndexSerialization = []byte(`{
128 "schemaVersion": 2,
129 "mediaType": "application/vnd.oci.image.index.v1+json",
130 "manifests": [
131 {
132 "mediaType": "application/vnd.oci.image.manifest.v1+json",
133 "size": 985,
134 "digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
135 "platform": {
136 "architecture": "amd64",
137 "os": "linux",
138 "features": [
139 "sse4"
140 ]
141 }
142 },
143 {
144 "mediaType": "application/vnd.oci.image.manifest.v1+json",
145 "size": 985,
146 "digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
147 "annotations": {
148 "platform": "none"
149 },
150 "platform": {
151 "architecture": "",
152 "os": ""
153 }
154 },
155 {
156 "mediaType": "application/vnd.oci.image.manifest.v1+json",
157 "size": 2392,
158 "digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
159 "annotations": {
160 "what": "for"
161 },
162 "platform": {
163 "architecture": "sun4m",
164 "os": "sunos"
165 }
166 }
167 ]
168 }`)
169
170 func makeTestOCIImageIndex(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
171 manifestDescriptors := []ManifestDescriptor{
172 {
173 Descriptor: distribution.Descriptor{
174 Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
175 Size: 985,
176 MediaType: "application/vnd.oci.image.manifest.v1+json",
177 },
178 Platform: PlatformSpec{
179 Architecture: "amd64",
180 OS: "linux",
181 Features: []string{"sse4"},
182 },
183 },
184 {
185 Descriptor: distribution.Descriptor{
186 Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
187 Size: 985,
188 MediaType: "application/vnd.oci.image.manifest.v1+json",
189 Annotations: map[string]string{"platform": "none"},
190 },
191 },
192 {
193 Descriptor: distribution.Descriptor{
194 Digest: "sha256:6346340964309634683409684360934680934608934608934608934068934608",
195 Size: 2392,
196 MediaType: "application/vnd.oci.image.manifest.v1+json",
197 Annotations: map[string]string{"what": "for"},
198 },
199 Platform: PlatformSpec{
200 Architecture: "sun4m",
201 OS: "sunos",
202 },
203 },
204 }
205
206 deserialized, err := FromDescriptorsWithMediaType(manifestDescriptors, mediaType)
207 if err != nil {
208 t.Fatalf("error creating DeserializedManifestList: %v", err)
209 }
210
211 return manifestDescriptors, deserialized
212 }
213
214 func TestOCIImageIndex(t *testing.T) {
215 manifestDescriptors, deserialized := makeTestOCIImageIndex(t, v1.MediaTypeImageIndex)
216
217 mediaType, canonical, _ := deserialized.Payload()
218
219 if mediaType != v1.MediaTypeImageIndex {
220 t.Fatalf("unexpected media type: %s", mediaType)
221 }
222
223
224
225 p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
226 if err != nil {
227 t.Fatalf("error marshaling manifest list: %v", err)
228 }
229 if !bytes.Equal(p, canonical) {
230 t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p))
231 }
232
233
234 if !bytes.Equal(expectedOCIImageIndexSerialization, canonical) {
235 t.Fatalf("manifest bytes not equal to expected: %q != %q", string(canonical), string(expectedOCIImageIndexSerialization))
236 }
237
238 var unmarshalled DeserializedManifestList
239 if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
240 t.Fatalf("error unmarshaling manifest: %v", err)
241 }
242
243 if !reflect.DeepEqual(&unmarshalled, deserialized) {
244 t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
245 }
246
247 references := deserialized.References()
248 if len(references) != 3 {
249 t.Fatalf("unexpected number of references: %d", len(references))
250 }
251 for i := range references {
252 if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
253 t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
254 }
255 }
256 }
257
258 func mediaTypeTest(t *testing.T, contentType string, mediaType string, shouldError bool) {
259 var m *DeserializedManifestList
260 if contentType == MediaTypeManifestList {
261 _, m = makeTestManifestList(t, mediaType)
262 } else {
263 _, m = makeTestOCIImageIndex(t, mediaType)
264 }
265
266 _, canonical, err := m.Payload()
267 if err != nil {
268 t.Fatalf("error getting payload, %v", err)
269 }
270
271 unmarshalled, descriptor, err := distribution.UnmarshalManifest(
272 contentType,
273 canonical)
274
275 if shouldError {
276 if err == nil {
277 t.Fatalf("bad content type should have produced error")
278 }
279 } else {
280 if err != nil {
281 t.Fatalf("error unmarshaling manifest, %v", err)
282 }
283
284 asManifest := unmarshalled.(*DeserializedManifestList)
285 if asManifest.MediaType != mediaType {
286 t.Fatalf("Bad media type '%v' as unmarshalled", asManifest.MediaType)
287 }
288
289 if descriptor.MediaType != contentType {
290 t.Fatalf("Bad media type '%v' for descriptor", descriptor.MediaType)
291 }
292
293 unmarshalledMediaType, _, _ := unmarshalled.Payload()
294 if unmarshalledMediaType != contentType {
295 t.Fatalf("Bad media type '%v' for payload", unmarshalledMediaType)
296 }
297 }
298 }
299
300 func TestMediaTypes(t *testing.T) {
301 mediaTypeTest(t, MediaTypeManifestList, "", true)
302 mediaTypeTest(t, MediaTypeManifestList, MediaTypeManifestList, false)
303 mediaTypeTest(t, MediaTypeManifestList, MediaTypeManifestList+"XXX", true)
304 mediaTypeTest(t, v1.MediaTypeImageIndex, "", false)
305 mediaTypeTest(t, v1.MediaTypeImageIndex, v1.MediaTypeImageIndex, false)
306 mediaTypeTest(t, v1.MediaTypeImageIndex, v1.MediaTypeImageIndex+"XXX", true)
307 }
308
309 func TestValidateManifest(t *testing.T) {
310 manifest := ocischema.Manifest{
311 Config: distribution.Descriptor{Size: 1},
312 Layers: []distribution.Descriptor{{Size: 2}},
313 }
314 index := ManifestList{
315 Manifests: []ManifestDescriptor{
316 {Descriptor: distribution.Descriptor{Size: 3}},
317 },
318 }
319 t.Run("valid", func(t *testing.T) {
320 b, err := json.Marshal(index)
321 if err != nil {
322 t.Fatal("unexpected error marshaling index", err)
323 }
324 if err := validateIndex(b); err != nil {
325 t.Error("index should be valid", err)
326 }
327 })
328 t.Run("invalid", func(t *testing.T) {
329 b, err := json.Marshal(manifest)
330 if err != nil {
331 t.Fatal("unexpected error marshaling manifest", err)
332 }
333 if err := validateIndex(b); err == nil {
334 t.Error("manifest should not be valid")
335 }
336 })
337 }
338
View as plain text