2015-12-17 01:26:13 +00:00
|
|
|
package manifestlist
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
2023-03-31 10:35:30 +00:00
|
|
|
"github.com/distribution/distribution/v3/manifest/schema2"
|
2021-10-21 22:09:39 +00:00
|
|
|
|
2019-10-09 12:02:21 +00:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2015-12-17 01:26:13 +00:00
|
|
|
)
|
|
|
|
|
manifest: improve test output and use const
Use consts to make clear these values are fixed, and improve the output
to make it clearer which part is the expected output, and which part
the actual.
Before this:
=== RUN TestManifest
manifest_test.go:87: manifest bytes not equal: "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}" != "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}"
--- FAIL: TestManifest (0.00s)
After this:
=== RUN TestManifest
manifest_test.go:72: manifest bytes not equal:
expected:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
actual:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
--- FAIL: TestManifest (0.00s)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-26 13:00:37 +00:00
|
|
|
const expectedManifestListSerialization = `{
|
2015-12-17 01:26:13 +00:00
|
|
|
"schemaVersion": 2,
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
|
|
|
|
"manifests": [
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
2022-11-29 21:41:01 +00:00
|
|
|
"size": 985,
|
2015-12-17 01:26:13 +00:00
|
|
|
"platform": {
|
|
|
|
"architecture": "amd64",
|
|
|
|
"os": "linux",
|
|
|
|
"features": [
|
|
|
|
"sse4"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
|
|
|
|
"digest": "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
2022-11-29 21:41:01 +00:00
|
|
|
"size": 2392,
|
2015-12-17 01:26:13 +00:00
|
|
|
"platform": {
|
|
|
|
"architecture": "sun4m",
|
|
|
|
"os": "sunos"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
manifest: improve test output and use const
Use consts to make clear these values are fixed, and improve the output
to make it clearer which part is the expected output, and which part
the actual.
Before this:
=== RUN TestManifest
manifest_test.go:87: manifest bytes not equal: "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}" != "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}"
--- FAIL: TestManifest (0.00s)
After this:
=== RUN TestManifest
manifest_test.go:72: manifest bytes not equal:
expected:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
actual:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
--- FAIL: TestManifest (0.00s)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-26 13:00:37 +00:00
|
|
|
}`
|
2015-12-17 01:26:13 +00:00
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
func makeTestManifestList(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
|
2015-12-17 01:26:13 +00:00
|
|
|
manifestDescriptors := []ManifestDescriptor{
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
2022-11-29 21:59:06 +00:00
|
|
|
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
|
2015-12-17 01:26:13 +00:00
|
|
|
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
Size: 985,
|
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
Features: []string{"sse4"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Descriptor: distribution.Descriptor{
|
2022-11-29 21:59:06 +00:00
|
|
|
MediaType: "application/vnd.docker.distribution.manifest.v2+json",
|
2015-12-17 01:26:13 +00:00
|
|
|
Digest: "sha256:6346340964309634683409684360934680934608934608934608934068934608",
|
|
|
|
Size: 2392,
|
|
|
|
},
|
|
|
|
Platform: PlatformSpec{
|
|
|
|
Architecture: "sun4m",
|
|
|
|
OS: "sunos",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
deserialized, err := fromDescriptorsWithMediaType(manifestDescriptors, mediaType)
|
2015-12-17 01:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating DeserializedManifestList: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
return manifestDescriptors, deserialized
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifestList(t *testing.T) {
|
|
|
|
manifestDescriptors, deserialized := makeTestManifestList(t, MediaTypeManifestList)
|
2016-11-17 18:28:05 +00:00
|
|
|
mediaType, canonical, _ := deserialized.Payload()
|
2015-12-17 01:26:13 +00:00
|
|
|
|
|
|
|
if mediaType != MediaTypeManifestList {
|
|
|
|
t.Fatalf("unexpected media type: %s", mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field is the same as json.MarshalIndent
|
|
|
|
// with these parameters.
|
manifest: improve test output and use const
Use consts to make clear these values are fixed, and improve the output
to make it clearer which part is the expected output, and which part
the actual.
Before this:
=== RUN TestManifest
manifest_test.go:87: manifest bytes not equal: "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}" != "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}"
--- FAIL: TestManifest (0.00s)
After this:
=== RUN TestManifest
manifest_test.go:72: manifest bytes not equal:
expected:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
actual:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
--- FAIL: TestManifest (0.00s)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-26 13:00:37 +00:00
|
|
|
expected, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
|
2015-12-17 01:26:13 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshaling manifest list: %v", err)
|
|
|
|
}
|
manifest: improve test output and use const
Use consts to make clear these values are fixed, and improve the output
to make it clearer which part is the expected output, and which part
the actual.
Before this:
=== RUN TestManifest
manifest_test.go:87: manifest bytes not equal: "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}" != "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}"
--- FAIL: TestManifest (0.00s)
After this:
=== RUN TestManifest
manifest_test.go:72: manifest bytes not equal:
expected:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
actual:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
--- FAIL: TestManifest (0.00s)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-26 13:00:37 +00:00
|
|
|
if !bytes.Equal(expected, canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(canonical))
|
2015-12-17 01:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the canonical field has the expected value.
|
manifest: improve test output and use const
Use consts to make clear these values are fixed, and improve the output
to make it clearer which part is the expected output, and which part
the actual.
Before this:
=== RUN TestManifest
manifest_test.go:87: manifest bytes not equal: "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}" != "{\n \"schemaVersion\": 2,\n \"mediaType\": \"application/vnd.oci.image.manifest.v1+json\",\n \"config\": {\n \"mediaType\": \"application/vnd.oci.image.config.v1+json\",\n \"size\": 985,\n \"digest\": \"sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b\",\n \"annotations\": {\n \"apple\": \"orange\"\n }\n },\n \"layers\": [\n {\n \"mediaType\": \"application/vnd.oci.image.layer.v1.tar+gzip\",\n \"size\": 153263,\n \"digest\": \"sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b\",\n \"annotations\": {\n \"lettuce\": \"wrap\"\n }\n }\n ],\n \"annotations\": {\n \"hot\": \"potato\"\n }\n}"
--- FAIL: TestManifest (0.00s)
After this:
=== RUN TestManifest
manifest_test.go:72: manifest bytes not equal:
expected:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
actual:
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 985,
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
}
]
}
--- FAIL: TestManifest (0.00s)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-26 13:00:37 +00:00
|
|
|
if !bytes.Equal([]byte(expectedManifestListSerialization), canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedManifestListSerialization, string(canonical))
|
2015-12-17 01:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalled DeserializedManifestList
|
|
|
|
if err := json.Unmarshal(deserialized.canonical, &unmarshalled); err != nil {
|
|
|
|
t.Fatalf("error unmarshaling manifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(&unmarshalled, deserialized) {
|
|
|
|
t.Fatalf("manifests are different after unmarshaling: %v != %v", unmarshalled, *deserialized)
|
|
|
|
}
|
|
|
|
|
|
|
|
references := deserialized.References()
|
|
|
|
if len(references) != 2 {
|
|
|
|
t.Fatalf("unexpected number of references: %d", len(references))
|
|
|
|
}
|
|
|
|
for i := range references {
|
2019-04-29 13:47:52 +00:00
|
|
|
platform := manifestDescriptors[i].Platform
|
|
|
|
expectedPlatform := &v1.Platform{
|
|
|
|
Architecture: platform.Architecture,
|
|
|
|
OS: platform.OS,
|
|
|
|
OSFeatures: platform.OSFeatures,
|
|
|
|
OSVersion: platform.OSVersion,
|
|
|
|
Variant: platform.Variant,
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(references[i].Platform, expectedPlatform) {
|
|
|
|
t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
|
|
|
|
}
|
|
|
|
references[i].Platform = nil
|
2015-12-17 01:26:13 +00:00
|
|
|
if !reflect.DeepEqual(references[i], manifestDescriptors[i].Descriptor) {
|
|
|
|
t.Fatalf("unexpected value %d returned by References: %v", i, references[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
func mediaTypeTest(contentType string, mediaType string, shouldError bool) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
var m *DeserializedManifestList
|
2018-03-14 20:55:45 +00:00
|
|
|
_, m = makeTestManifestList(t, mediaType)
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
_, canonical, err := m.Payload()
|
2018-03-14 20:55:45 +00:00
|
|
|
if err != nil {
|
2023-03-31 10:35:30 +00:00
|
|
|
t.Fatalf("error getting payload, %v", err)
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
unmarshalled, descriptor, err := distribution.UnmarshalManifest(
|
|
|
|
contentType,
|
|
|
|
canonical)
|
|
|
|
|
|
|
|
if shouldError {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("bad content type should have produced error")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error unmarshaling manifest, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
asManifest := unmarshalled.(*DeserializedManifestList)
|
|
|
|
if asManifest.MediaType != mediaType {
|
|
|
|
t.Fatalf("Bad media type '%v' as unmarshalled", asManifest.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if descriptor.MediaType != contentType {
|
|
|
|
t.Fatalf("Bad media type '%v' for descriptor", descriptor.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshalledMediaType, _, _ := unmarshalled.Payload()
|
|
|
|
if unmarshalledMediaType != contentType {
|
|
|
|
t.Fatalf("Bad media type '%v' for payload", unmarshalledMediaType)
|
|
|
|
}
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMediaTypes(t *testing.T) {
|
2023-03-31 10:35:30 +00:00
|
|
|
t.Run("ManifestList_No_MediaType", mediaTypeTest(MediaTypeManifestList, "", true))
|
|
|
|
t.Run("ManifestList", mediaTypeTest(MediaTypeManifestList, MediaTypeManifestList, false))
|
|
|
|
t.Run("ManifestList_Bad_MediaType", mediaTypeTest(MediaTypeManifestList, MediaTypeManifestList+"XXX", true))
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
2021-10-21 22:09:39 +00:00
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
func TestValidateManifestList(t *testing.T) {
|
|
|
|
manifest := schema2.Manifest{
|
2021-10-21 22:09:39 +00:00
|
|
|
Config: distribution.Descriptor{Size: 1},
|
|
|
|
Layers: []distribution.Descriptor{{Size: 2}},
|
|
|
|
}
|
2023-03-31 10:35:30 +00:00
|
|
|
manifestList := ManifestList{
|
2021-10-21 22:09:39 +00:00
|
|
|
Manifests: []ManifestDescriptor{
|
|
|
|
{Descriptor: distribution.Descriptor{Size: 3}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
2023-03-31 10:35:30 +00:00
|
|
|
b, err := json.Marshal(manifestList)
|
2021-10-21 22:09:39 +00:00
|
|
|
if err != nil {
|
2023-03-31 10:35:30 +00:00
|
|
|
t.Fatal("unexpected error marshaling manifest list", err)
|
2021-10-21 22:09:39 +00:00
|
|
|
}
|
2023-03-31 10:35:30 +00:00
|
|
|
if err := validateManifestList(b); err != nil {
|
|
|
|
t.Error("list should be valid", err)
|
2021-10-21 22:09:39 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
b, err := json.Marshal(manifest)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error marshaling manifest", err)
|
|
|
|
}
|
2023-03-31 10:35:30 +00:00
|
|
|
if err := validateManifestList(b); err == nil {
|
2021-10-21 22:09:39 +00:00
|
|
|
t.Error("manifest should not be valid")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|