Skip to content

Commit

Permalink
Add test for ocispec.Descriptor Annotations
Browse files Browse the repository at this point in the history
Make sure that Annotations we write into ocispec.Descriptors are
written into the store and can be read back.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
(cherry picked from commit 79248fe)
Signed-off-by: Jared Cordasco <jcordasc@coglib.com>
  • Loading branch information
stefanberger authored and Jared Cordasco committed May 4, 2019
1 parent 2364b03 commit 4bb9151
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions import_test.go
Expand Up @@ -23,6 +23,7 @@ import (

"io/ioutil"
"math/rand"
"reflect"
"runtime"
"testing"

Expand Down Expand Up @@ -95,11 +96,11 @@ func TestImport(t *testing.T) {

c1, d2 := createConfig()

m1, d3 := createManifest(c1, [][]byte{b1})
m1, d3, expManifest := createManifest(c1, [][]byte{b1})

provider := client.ContentStore()

checkManifest := func(ctx context.Context, t *testing.T, d ocispec.Descriptor) {
checkManifest := func(ctx context.Context, t *testing.T, d ocispec.Descriptor, expManifest *ocispec.Manifest) {
m, err := images.Manifest(ctx, provider, d, nil)
if err != nil {
t.Fatalf("unable to read target blob: %+v", err)
Expand All @@ -116,6 +117,15 @@ func TestImport(t *testing.T) {
if m.Layers[0].Digest != d1 {
t.Fatalf("unexpected layer hash %s, expected %s", m.Layers[0].Digest, d1)
}

if expManifest != nil {
if !reflect.DeepEqual(m.Layers, expManifest.Layers) {
t.Fatalf("DeepEqual on Layers failed: %v vs. %v", m.Layers, expManifest.Layers)
}
if !reflect.DeepEqual(m.Config, expManifest.Config) {
t.Fatalf("DeepEqual on Config failed: %v vs. %v", m.Config, expManifest.Config)
}
}
}

for _, tc := range []struct {
Expand Down Expand Up @@ -155,7 +165,7 @@ func TestImport(t *testing.T) {
}

checkImages(t, imgs[0].Target.Digest, imgs, names...)
checkManifest(ctx, t, imgs[0].Target)
checkManifest(ctx, t, imgs[0].Target, nil)
},
},
{
Expand All @@ -182,7 +192,7 @@ func TestImport(t *testing.T) {
}

checkImages(t, d3, imgs, names...)
checkManifest(ctx, t, imgs[0].Target)
checkManifest(ctx, t, imgs[0].Target, expManifest)
},
},
{
Expand All @@ -203,7 +213,7 @@ func TestImport(t *testing.T) {
}

checkImages(t, d3, imgs, names...)
checkManifest(ctx, t, imgs[0].Target)
checkManifest(ctx, t, imgs[0].Target, expManifest)
},
Opts: []ImportOpt{
WithImageRefTranslator(archive.AddRefPrefix("localhost:5000/myimage")),
Expand All @@ -227,7 +237,7 @@ func TestImport(t *testing.T) {
}

checkImages(t, d3, imgs, names...)
checkManifest(ctx, t, imgs[0].Target)
checkManifest(ctx, t, imgs[0].Target, expManifest)
},
Opts: []ImportOpt{
WithImageRefTranslator(archive.FilterRefPrefix("localhost:5000/myimage")),
Expand Down Expand Up @@ -289,7 +299,7 @@ func createConfig() ([]byte, digest.Digest) {
return b, digest.FromBytes(b)
}

func createManifest(config []byte, layers [][]byte) ([]byte, digest.Digest) {
func createManifest(config []byte, layers [][]byte) ([]byte, digest.Digest, *ocispec.Manifest) {
manifest := ocispec.Manifest{
Versioned: specs.Versioned{
SchemaVersion: 2,
Expand All @@ -298,19 +308,25 @@ func createManifest(config []byte, layers [][]byte) ([]byte, digest.Digest) {
MediaType: ocispec.MediaTypeImageConfig,
Digest: digest.FromBytes(config),
Size: int64(len(config)),
Annotations: map[string]string{
"ocispec": "manifest.config.descriptor",
},
},
}
for _, l := range layers {
manifest.Layers = append(manifest.Layers, ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageLayer,
Digest: digest.FromBytes(l),
Size: int64(len(l)),
Annotations: map[string]string{
"ocispec": "manifest.layers.descriptor",
},
})
}

b, _ := json.Marshal(manifest)

return b, digest.FromBytes(b)
return b, digest.FromBytes(b), &manifest
}

func createIndex(manifest []byte, tags ...string) []byte {
Expand Down

0 comments on commit 4bb9151

Please sign in to comment.