Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add annotation for descriptor #4106

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion manifest/ocischema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions manifest/ocischema/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion manifest/ocischema/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions manifest/ocischema/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down