2016-11-17 18:28:05 +00:00
|
|
|
package ocischema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-24 11:18:39 +00:00
|
|
|
"github.com/distribution/distribution/v3"
|
|
|
|
"github.com/distribution/distribution/v3/manifest"
|
2021-10-21 22:09:39 +00:00
|
|
|
"github.com/distribution/distribution/v3/manifest/manifestlist"
|
|
|
|
|
2023-10-16 02:14:19 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2019-10-09 12:02:21 +00:00
|
|
|
v1 "github.com/opencontainers/image-spec/specs-go/v1"
|
2016-11-17 18:28:05 +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 expectedManifestSerialization = `{
|
2016-11-17 18:28:05 +00:00
|
|
|
"schemaVersion": 2,
|
|
|
|
"mediaType": "application/vnd.oci.image.manifest.v1+json",
|
|
|
|
"config": {
|
|
|
|
"mediaType": "application/vnd.oci.image.config.v1+json",
|
2017-07-21 01:44:02 +00:00
|
|
|
"digest": "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
2022-11-29 21:41:01 +00:00
|
|
|
"size": 985,
|
2017-07-21 01:44:02 +00:00
|
|
|
"annotations": {
|
|
|
|
"apple": "orange"
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
},
|
|
|
|
"layers": [
|
|
|
|
{
|
|
|
|
"mediaType": "application/vnd.oci.image.layer.v1.tar+gzip",
|
2017-07-21 01:44:02 +00:00
|
|
|
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b",
|
2022-11-29 21:41:01 +00:00
|
|
|
"size": 153263,
|
2017-07-21 01:44:02 +00:00
|
|
|
"annotations": {
|
|
|
|
"lettuce": "wrap"
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
2017-07-31 23:34:11 +00:00
|
|
|
],
|
|
|
|
"annotations": {
|
|
|
|
"hot": "potato"
|
|
|
|
}
|
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
|
|
|
}`
|
2016-11-17 18:28:05 +00:00
|
|
|
|
2018-03-14 20:55:45 +00:00
|
|
|
func makeTestManifest(mediaType string) Manifest {
|
|
|
|
return Manifest{
|
|
|
|
Versioned: manifest.Versioned{
|
|
|
|
SchemaVersion: 2,
|
|
|
|
MediaType: mediaType,
|
|
|
|
},
|
2016-11-17 18:28:05 +00:00
|
|
|
Config: distribution.Descriptor{
|
2022-11-29 21:59:06 +00:00
|
|
|
MediaType: v1.MediaTypeImageConfig,
|
2017-07-21 01:44:02 +00:00
|
|
|
Digest: "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b",
|
|
|
|
Size: 985,
|
|
|
|
Annotations: map[string]string{"apple": "orange"},
|
2016-11-17 18:28:05 +00:00
|
|
|
},
|
|
|
|
Layers: []distribution.Descriptor{
|
|
|
|
{
|
2022-11-29 21:59:06 +00:00
|
|
|
MediaType: v1.MediaTypeImageLayerGzip,
|
2017-07-21 01:44:02 +00:00
|
|
|
Digest: "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b",
|
|
|
|
Size: 153263,
|
|
|
|
Annotations: map[string]string{"lettuce": "wrap"},
|
2016-11-17 18:28:05 +00:00
|
|
|
},
|
|
|
|
},
|
2017-07-31 23:34:11 +00:00
|
|
|
Annotations: map[string]string{"hot": "potato"},
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestManifest(t *testing.T) {
|
2022-11-26 12:15:07 +00:00
|
|
|
mfst := makeTestManifest(v1.MediaTypeImageManifest)
|
2016-11-17 18:28:05 +00:00
|
|
|
|
2022-11-26 12:15:07 +00:00
|
|
|
deserialized, err := FromStruct(mfst)
|
2016-11-17 18:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating DeserializedManifest: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-07-11 19:19:47 +00:00
|
|
|
mediaType, canonical, _ := deserialized.Payload()
|
2016-11-17 18:28:05 +00:00
|
|
|
|
2017-07-11 19:19:47 +00:00
|
|
|
if mediaType != v1.MediaTypeImageManifest {
|
2016-11-17 18:28:05 +00:00
|
|
|
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(&mfst, "", " ")
|
2016-11-17 18:28:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error marshaling manifest: %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))
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that canonical field matches 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(expectedManifestSerialization), canonical) {
|
|
|
|
t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedManifestSerialization, string(canonical))
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unmarshalled DeserializedManifest
|
|
|
|
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)
|
|
|
|
}
|
2017-07-31 23:34:11 +00:00
|
|
|
if deserialized.Annotations["hot"] != "potato" {
|
|
|
|
t.Fatalf("unexpected annotation in manifest: %s", deserialized.Annotations["hot"])
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
|
|
|
|
target := deserialized.Target()
|
|
|
|
if target.Digest != "sha256:1a9ec845ee94c202b2d5da74a24f0ed2058318bfa9879fa541efaecba272e86b" {
|
|
|
|
t.Fatalf("unexpected digest in target: %s", target.Digest.String())
|
|
|
|
}
|
2017-07-11 19:19:47 +00:00
|
|
|
if target.MediaType != v1.MediaTypeImageConfig {
|
2016-11-17 18:28:05 +00:00
|
|
|
t.Fatalf("unexpected media type in target: %s", target.MediaType)
|
|
|
|
}
|
|
|
|
if target.Size != 985 {
|
|
|
|
t.Fatalf("unexpected size in target: %d", target.Size)
|
|
|
|
}
|
2017-07-21 01:44:02 +00:00
|
|
|
if target.Annotations["apple"] != "orange" {
|
|
|
|
t.Fatalf("unexpected annotation in target: %s", target.Annotations["apple"])
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
|
|
|
|
references := deserialized.References()
|
|
|
|
if len(references) != 2 {
|
|
|
|
t.Fatalf("unexpected number of references: %d", len(references))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(references[0], target) {
|
|
|
|
t.Fatalf("first reference should be target: %v != %v", references[0], target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the second reference
|
|
|
|
if references[1].Digest != "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" {
|
|
|
|
t.Fatalf("unexpected digest in reference: %s", references[0].Digest.String())
|
|
|
|
}
|
2017-07-11 19:19:47 +00:00
|
|
|
if references[1].MediaType != v1.MediaTypeImageLayerGzip {
|
2016-11-17 18:28:05 +00:00
|
|
|
t.Fatalf("unexpected media type in reference: %s", references[0].MediaType)
|
|
|
|
}
|
|
|
|
if references[1].Size != 153263 {
|
|
|
|
t.Fatalf("unexpected size in reference: %d", references[0].Size)
|
|
|
|
}
|
2017-07-21 01:44:02 +00:00
|
|
|
if references[1].Annotations["lettuce"] != "wrap" {
|
|
|
|
t.Fatalf("unexpected annotation in reference: %s", references[1].Annotations["lettuce"])
|
|
|
|
}
|
2016-11-17 18:28:05 +00:00
|
|
|
}
|
2018-03-14 20:55:45 +00:00
|
|
|
|
2023-10-16 02:14:19 +00:00
|
|
|
func TestManifestUnmarshal(t *testing.T) {
|
|
|
|
_, descriptor, err := distribution.UnmarshalManifest(v1.MediaTypeImageManifest, []byte(expectedManifestSerialization))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unmarshal manifest failed: %v", err)
|
|
|
|
}
|
|
|
|
mfst := makeTestManifest(v1.MediaTypeImageManifest)
|
|
|
|
|
|
|
|
deserialized, err := FromStruct(mfst)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating DeserializedManifest: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(descriptor.Annotations, deserialized.Annotations) {
|
|
|
|
t.Fatalf("manifest annotation not equal:\nexpected:\n%v\nactual:\n%v\n", deserialized.Annotations, descriptor.Annotations)
|
|
|
|
}
|
|
|
|
if len(descriptor.Annotations) != 1 {
|
|
|
|
t.Fatalf("manifest index annotation length should be 1")
|
|
|
|
}
|
|
|
|
if descriptor.Size != int64(len([]byte(expectedManifestSerialization))) {
|
|
|
|
t.Fatalf("manifest size is not correct:\nexpected:\n%d\nactual:\n%v\n", int64(len([]byte(expectedManifestSerialization))), descriptor.Size)
|
|
|
|
}
|
|
|
|
if descriptor.Digest.String() != digest.FromBytes([]byte(expectedManifestSerialization)).String() {
|
|
|
|
t.Fatalf("manifest digest is not correct:\nexpected:\n%s\nactual:\n%s\n", digest.FromBytes([]byte(expectedManifestSerialization)), descriptor.Digest)
|
|
|
|
}
|
|
|
|
if descriptor.MediaType != v1.MediaTypeImageManifest {
|
|
|
|
t.Fatalf("manifest media type is not correct:\nexpected:\n%s\nactual:\n%s\n", v1.MediaTypeImageManifest, descriptor.MediaType)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
func manifestMediaTypeTest(mediaType string, shouldError bool) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
mfst := makeTestManifest(mediaType)
|
2018-03-14 20:55:45 +00:00
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
deserialized, err := FromStruct(mfst)
|
2018-03-14 20:55:45 +00:00
|
|
|
if err != nil {
|
2023-03-31 10:35:30 +00:00
|
|
|
t.Fatalf("error creating DeserializedManifest: %v", err)
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
unmarshalled, descriptor, err := distribution.UnmarshalManifest(
|
|
|
|
v1.MediaTypeImageManifest,
|
|
|
|
deserialized.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.(*DeserializedManifest)
|
|
|
|
if asManifest.MediaType != mediaType {
|
|
|
|
t.Fatalf("Bad media type '%v' as unmarshalled", asManifest.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
if descriptor.MediaType != v1.MediaTypeImageManifest {
|
|
|
|
t.Fatalf("Bad media type '%v' for descriptor", descriptor.MediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
unmarshalledMediaType, _, _ := unmarshalled.Payload()
|
|
|
|
if unmarshalledMediaType != v1.MediaTypeImageManifest {
|
|
|
|
t.Fatalf("Bad media type '%v' for payload", unmarshalledMediaType)
|
|
|
|
}
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:35:30 +00:00
|
|
|
func TestManifestMediaTypes(t *testing.T) {
|
|
|
|
t.Run("No_MediaType", manifestMediaTypeTest("", false))
|
|
|
|
t.Run("ImageManifest", manifestMediaTypeTest(v1.MediaTypeImageManifest, false))
|
|
|
|
t.Run("Bad_MediaType", manifestMediaTypeTest(v1.MediaTypeImageManifest+"XXX", true))
|
2018-03-14 20:55:45 +00:00
|
|
|
}
|
2021-10-21 22:09:39 +00:00
|
|
|
|
|
|
|
func TestValidateManifest(t *testing.T) {
|
2022-11-26 12:15:07 +00:00
|
|
|
mfst := Manifest{
|
2021-10-21 22:09:39 +00:00
|
|
|
Config: distribution.Descriptor{Size: 1},
|
|
|
|
Layers: []distribution.Descriptor{{Size: 2}},
|
|
|
|
}
|
|
|
|
index := manifestlist.ManifestList{
|
|
|
|
Manifests: []manifestlist.ManifestDescriptor{
|
|
|
|
{Descriptor: distribution.Descriptor{Size: 3}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
2022-11-26 12:15:07 +00:00
|
|
|
b, err := json.Marshal(mfst)
|
2021-10-21 22:09:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error marshaling manifest", err)
|
|
|
|
}
|
|
|
|
if err := validateManifest(b); err != nil {
|
|
|
|
t.Error("manifest should be valid", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
b, err := json.Marshal(index)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error marshaling index", err)
|
|
|
|
}
|
|
|
|
if err := validateManifest(b); err == nil {
|
|
|
|
t.Error("index should not be valid")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|