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>
This commit is contained in:
Sebastiaan van Stijn 2022-11-26 14:00:37 +01:00
parent f2db7faa2f
commit 0b4311d5ce
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 31 additions and 31 deletions

View file

@ -12,7 +12,7 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
var expectedManifestListSerialization = []byte(`{ const expectedManifestListSerialization = `{
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.list.v2+json", "mediaType": "application/vnd.docker.distribution.manifest.list.v2+json",
"manifests": [ "manifests": [
@ -38,7 +38,7 @@ var expectedManifestListSerialization = []byte(`{
} }
} }
] ]
}`) }`
func makeTestManifestList(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) { func makeTestManifestList(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
manifestDescriptors := []ManifestDescriptor{ manifestDescriptors := []ManifestDescriptor{
@ -85,17 +85,17 @@ func TestManifestList(t *testing.T) {
// Check that the canonical field is the same as json.MarshalIndent // Check that the canonical field is the same as json.MarshalIndent
// with these parameters. // with these parameters.
p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ") expected, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
if err != nil { if err != nil {
t.Fatalf("error marshaling manifest list: %v", err) t.Fatalf("error marshaling manifest list: %v", err)
} }
if !bytes.Equal(p, canonical) { if !bytes.Equal(expected, canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(canonical))
} }
// Check that the canonical field has the expected value. // Check that the canonical field has the expected value.
if !bytes.Equal(expectedManifestListSerialization, canonical) { if !bytes.Equal([]byte(expectedManifestListSerialization), canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestListSerialization)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedManifestListSerialization, string(canonical))
} }
var unmarshalled DeserializedManifestList var unmarshalled DeserializedManifestList
@ -136,7 +136,7 @@ func TestManifestList(t *testing.T) {
// Requires changes to distribution/distribution/manifest/manifestlist.ManifestList and .ManifestDescriptor // Requires changes to distribution/distribution/manifest/manifestlist.ManifestList and .ManifestDescriptor
// and associated serialization APIs in manifestlist.go. Or split the OCI index and // and associated serialization APIs in manifestlist.go. Or split the OCI index and
// docker manifest list implementations, which would require a lot of refactoring. // docker manifest list implementations, which would require a lot of refactoring.
var expectedOCIImageIndexSerialization = []byte(`{ const expectedOCIImageIndexSerialization = `{
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json", "mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [ "manifests": [
@ -177,7 +177,7 @@ var expectedOCIImageIndexSerialization = []byte(`{
} }
} }
] ]
}`) }`
func makeTestOCIImageIndex(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) { func makeTestOCIImageIndex(t *testing.T, mediaType string) ([]ManifestDescriptor, *DeserializedManifestList) {
manifestDescriptors := []ManifestDescriptor{ manifestDescriptors := []ManifestDescriptor{
@ -234,17 +234,17 @@ func TestOCIImageIndex(t *testing.T) {
// Check that the canonical field is the same as json.MarshalIndent // Check that the canonical field is the same as json.MarshalIndent
// with these parameters. // with these parameters.
p, err := json.MarshalIndent(&deserialized.ManifestList, "", " ") expected, err := json.MarshalIndent(&deserialized.ManifestList, "", " ")
if err != nil { if err != nil {
t.Fatalf("error marshaling manifest list: %v", err) t.Fatalf("error marshaling manifest list: %v", err)
} }
if !bytes.Equal(p, canonical) { if !bytes.Equal(expected, canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(canonical))
} }
// Check that the canonical field has the expected value. // Check that the canonical field has the expected value.
if !bytes.Equal(expectedOCIImageIndexSerialization, canonical) { if !bytes.Equal([]byte(expectedOCIImageIndexSerialization), canonical) {
t.Fatalf("manifest bytes not equal to expected: %q != %q", string(canonical), string(expectedOCIImageIndexSerialization)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedOCIImageIndexSerialization, string(canonical))
} }
var unmarshalled DeserializedManifestList var unmarshalled DeserializedManifestList

View file

@ -13,7 +13,7 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
var expectedManifestSerialization = []byte(`{ const expectedManifestSerialization = `{
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.oci.image.manifest.v1+json", "mediaType": "application/vnd.oci.image.manifest.v1+json",
"config": { "config": {
@ -37,7 +37,7 @@ var expectedManifestSerialization = []byte(`{
"annotations": { "annotations": {
"hot": "potato" "hot": "potato"
} }
}`) }`
func makeTestManifest(mediaType string) Manifest { func makeTestManifest(mediaType string) Manifest {
return Manifest{ return Manifest{
@ -79,17 +79,17 @@ func TestManifest(t *testing.T) {
// Check that the canonical field is the same as json.MarshalIndent // Check that the canonical field is the same as json.MarshalIndent
// with these parameters. // with these parameters.
p, err := json.MarshalIndent(&mfst, "", " ") expected, err := json.MarshalIndent(&mfst, "", " ")
if err != nil { if err != nil {
t.Fatalf("error marshaling manifest: %v", err) t.Fatalf("error marshaling manifest: %v", err)
} }
if !bytes.Equal(p, canonical) { if !bytes.Equal(expected, canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(canonical))
} }
// Check that canonical field matches expected value. // Check that canonical field matches expected value.
if !bytes.Equal(expectedManifestSerialization, canonical) { if !bytes.Equal([]byte(expectedManifestSerialization), canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestSerialization)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedManifestSerialization, string(canonical))
} }
var unmarshalled DeserializedManifest var unmarshalled DeserializedManifest

View file

@ -21,13 +21,13 @@ func TestManifestMarshaling(t *testing.T) {
// Check that the all field is the same as json.MarshalIndent with these // Check that the all field is the same as json.MarshalIndent with these
// parameters. // parameters.
p, err := json.MarshalIndent(env.signed, "", " ") expected, err := json.MarshalIndent(env.signed, "", " ")
if err != nil { if err != nil {
t.Fatalf("error marshaling manifest: %v", err) t.Fatalf("error marshaling manifest: %v", err)
} }
if !bytes.Equal(p, env.signed.all) { if !bytes.Equal(expected, env.signed.all) {
t.Fatalf("manifest bytes not equal: %q != %q", string(env.signed.all), string(p)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(env.signed.all))
} }
} }

View file

@ -10,7 +10,7 @@ import (
"github.com/distribution/distribution/v3/manifest" "github.com/distribution/distribution/v3/manifest"
) )
var expectedManifestSerialization = []byte(`{ const expectedManifestSerialization = `{
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json", "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": { "config": {
@ -25,7 +25,7 @@ var expectedManifestSerialization = []byte(`{
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" "digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
} }
] ]
}`) }`
func makeTestManifest(mediaType string) Manifest { func makeTestManifest(mediaType string) Manifest {
return Manifest{ return Manifest{
@ -64,17 +64,17 @@ func TestManifest(t *testing.T) {
// Check that the canonical field is the same as json.MarshalIndent // Check that the canonical field is the same as json.MarshalIndent
// with these parameters. // with these parameters.
p, err := json.MarshalIndent(&mfst, "", " ") expected, err := json.MarshalIndent(&mfst, "", " ")
if err != nil { if err != nil {
t.Fatalf("error marshaling manifest: %v", err) t.Fatalf("error marshaling manifest: %v", err)
} }
if !bytes.Equal(p, canonical) { if !bytes.Equal(expected, canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(p)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", string(expected), string(canonical))
} }
// Check that canonical field matches expected value. // Check that canonical field matches expected value.
if !bytes.Equal(expectedManifestSerialization, canonical) { if !bytes.Equal([]byte(expectedManifestSerialization), canonical) {
t.Fatalf("manifest bytes not equal: %q != %q", string(canonical), string(expectedManifestSerialization)) t.Fatalf("manifest bytes not equal:\nexpected:\n%s\nactual:\n%s\n", expectedManifestSerialization, string(canonical))
} }
var unmarshalled DeserializedManifest var unmarshalled DeserializedManifest