Add annotation for descriptor

Signed-off-by: Tosone <i@tosone.cn>
This commit is contained in:
Tosone 2023-10-16 10:14:19 +08:00
parent c78d6f99ae
commit 4dce8b866e
4 changed files with 67 additions and 2 deletions

View file

@ -37,7 +37,12 @@ func init() {
} }
dgst := digest.FromBytes(b) 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) err := distribution.RegisterManifestSchema(v1.MediaTypeImageIndex, imageIndexFunc)
if err != nil { if err != nil {

View file

@ -8,6 +8,7 @@ import (
"github.com/distribution/distribution/v3" "github.com/distribution/distribution/v3"
"github.com/distribution/distribution/v3/manifest/schema2" "github.com/distribution/distribution/v3/manifest/schema2"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1" 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) { func indexMediaTypeTest(contentType string, mediaType string, shouldError bool) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
var m *DeserializedImageIndex var m *DeserializedImageIndex

View file

@ -30,7 +30,12 @@ func init() {
} }
dgst := digest.FromBytes(b) 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) err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, ocischemaFunc)
if err != nil { if err != nil {

View file

@ -10,6 +10,7 @@ import (
"github.com/distribution/distribution/v3/manifest" "github.com/distribution/distribution/v3/manifest"
"github.com/distribution/distribution/v3/manifest/manifestlist" "github.com/distribution/distribution/v3/manifest/manifestlist"
"github.com/opencontainers/go-digest"
v1 "github.com/opencontainers/image-spec/specs-go/v1" 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) { func manifestMediaTypeTest(mediaType string, shouldError bool) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
mfst := makeTestManifest(mediaType) mfst := makeTestManifest(mediaType)