From 4dce8b866e3a565fa2ab076e0b079e1b73f86a5a Mon Sep 17 00:00:00 2001 From: Tosone Date: Mon, 16 Oct 2023 10:14:19 +0800 Subject: [PATCH] Add annotation for descriptor Signed-off-by: Tosone --- manifest/ocischema/index.go | 7 ++++++- manifest/ocischema/index_test.go | 25 ++++++++++++++++++++++++ manifest/ocischema/manifest.go | 7 ++++++- manifest/ocischema/manifest_test.go | 30 +++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/manifest/ocischema/index.go b/manifest/ocischema/index.go index 3ae824f40..834b02ab6 100644 --- a/manifest/ocischema/index.go +++ b/manifest/ocischema/index.go @@ -37,7 +37,12 @@ func init() { } dgst := digest.FromBytes(b) - return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: v1.MediaTypeImageIndex}, err + return m, distribution.Descriptor{ + MediaType: v1.MediaTypeImageIndex, + Digest: dgst, + Size: int64(len(b)), + Annotations: m.Annotations, + }, err } err := distribution.RegisterManifestSchema(v1.MediaTypeImageIndex, imageIndexFunc) if err != nil { diff --git a/manifest/ocischema/index_test.go b/manifest/ocischema/index_test.go index cb07bb17a..4cfe7b65f 100644 --- a/manifest/ocischema/index_test.go +++ b/manifest/ocischema/index_test.go @@ -8,6 +8,7 @@ import ( "github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3/manifest/schema2" + "github.com/opencontainers/go-digest" v1 "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -134,6 +135,30 @@ func TestOCIImageIndex(t *testing.T) { } } +func TestOCIManifestIndexUnmarshal(t *testing.T) { + _, descriptor, err := distribution.UnmarshalManifest(v1.MediaTypeImageIndex, []byte(expectedOCIImageIndexSerialization)) + if err != nil { + t.Fatalf("unmarshal manifest index failed: %v", err) + } + _, deserialized := makeTestOCIImageIndex(t, v1.MediaTypeImageIndex) + + if !reflect.DeepEqual(descriptor.Annotations, deserialized.Annotations) { + t.Fatalf("manifest index annotation not equal:\nexpected:\n%v\nactual:\n%v\n", deserialized.Annotations, descriptor.Annotations) + } + if len(descriptor.Annotations) != 2 { + t.Fatalf("manifest index annotation length should be 2") + } + if descriptor.Size != int64(len([]byte(expectedOCIImageIndexSerialization))) { + t.Fatalf("manifest index size is not correct:\nexpected:\n%d\nactual:\n%v\n", int64(len([]byte(expectedOCIImageIndexSerialization))), descriptor.Size) + } + if descriptor.Digest.String() != digest.FromBytes([]byte(expectedOCIImageIndexSerialization)).String() { + t.Fatalf("manifest index digest is not correct:\nexpected:\n%s\nactual:\n%s\n", digest.FromBytes([]byte(expectedOCIImageIndexSerialization)), descriptor.Digest) + } + if descriptor.MediaType != v1.MediaTypeImageIndex { + t.Fatalf("manifest index media type is not correct:\nexpected:\n%s\nactual:\n%s\n", v1.MediaTypeImageManifest, descriptor.MediaType) + } +} + func indexMediaTypeTest(contentType string, mediaType string, shouldError bool) func(*testing.T) { return func(t *testing.T) { var m *DeserializedImageIndex diff --git a/manifest/ocischema/manifest.go b/manifest/ocischema/manifest.go index 973cd64ae..ab793afc9 100644 --- a/manifest/ocischema/manifest.go +++ b/manifest/ocischema/manifest.go @@ -30,7 +30,12 @@ func init() { } dgst := digest.FromBytes(b) - return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: v1.MediaTypeImageManifest}, err + return m, distribution.Descriptor{ + MediaType: v1.MediaTypeImageManifest, + Digest: dgst, + Size: int64(len(b)), + Annotations: m.Annotations, + }, err } err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, ocischemaFunc) if err != nil { diff --git a/manifest/ocischema/manifest_test.go b/manifest/ocischema/manifest_test.go index 7cde85a31..ca6d45dbe 100644 --- a/manifest/ocischema/manifest_test.go +++ b/manifest/ocischema/manifest_test.go @@ -10,6 +10,7 @@ import ( "github.com/distribution/distribution/v3/manifest" "github.com/distribution/distribution/v3/manifest/manifestlist" + "github.com/opencontainers/go-digest" v1 "github.com/opencontainers/image-spec/specs-go/v1" ) @@ -142,6 +143,35 @@ func TestManifest(t *testing.T) { } } +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) + } +} + func manifestMediaTypeTest(mediaType string, shouldError bool) func(*testing.T) { return func(t *testing.T) { mfst := makeTestManifest(mediaType)