2018-02-05 16:05:59 -05:00
|
|
|
package metadata // import "github.com/docker/docker/distribution/metadata"
|
2015-11-18 17:18:44 -05:00
|
|
|
|
|
|
|
import (
|
2016-01-05 17:17:42 -05:00
|
|
|
"encoding/hex"
|
2015-11-18 17:18:44 -05:00
|
|
|
"io/ioutil"
|
2016-01-05 17:17:42 -05:00
|
|
|
"math/rand"
|
2015-11-18 17:18:44 -05:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/layer"
|
2019-08-05 10:37:47 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2015-11-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
|
2016-01-13 22:34:27 -05:00
|
|
|
func TestV2MetadataService(t *testing.T) {
|
2015-11-18 17:18:44 -05:00
|
|
|
tmpDir, err := ioutil.TempDir("", "blobsum-storage-service-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2017-08-24 14:48:16 -04:00
|
|
|
metadataStore, err := NewFSMetadataStore(tmpDir)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create metadata store: %v", err)
|
|
|
|
}
|
2016-01-13 22:34:27 -05:00
|
|
|
V2MetadataService := NewV2MetadataService(metadataStore)
|
2015-11-18 17:18:44 -05:00
|
|
|
|
2016-01-13 22:34:27 -05:00
|
|
|
tooManyBlobSums := make([]V2Metadata, 100)
|
2016-01-05 17:17:42 -05:00
|
|
|
for i := range tooManyBlobSums {
|
|
|
|
randDigest := randomDigest()
|
2016-01-13 22:34:27 -05:00
|
|
|
tooManyBlobSums[i] = V2Metadata{Digest: randDigest}
|
2016-01-05 17:17:42 -05:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:18:44 -05:00
|
|
|
testVectors := []struct {
|
|
|
|
diffID layer.DiffID
|
2016-01-13 22:34:27 -05:00
|
|
|
metadata []V2Metadata
|
2015-11-18 17:18:44 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
diffID: layer.DiffID("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"),
|
2016-01-13 22:34:27 -05:00
|
|
|
metadata: []V2Metadata{
|
2016-01-05 17:17:42 -05:00
|
|
|
{Digest: digest.Digest("sha256:f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937")},
|
2015-11-18 17:18:44 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
diffID: layer.DiffID("sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa"),
|
2016-01-13 22:34:27 -05:00
|
|
|
metadata: []V2Metadata{
|
2016-01-05 17:17:42 -05:00
|
|
|
{Digest: digest.Digest("sha256:f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937")},
|
|
|
|
{Digest: digest.Digest("sha256:9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e")},
|
2015-11-18 17:18:44 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2016-01-05 17:17:42 -05:00
|
|
|
diffID: layer.DiffID("sha256:03f4658f8b782e12230c1783426bd3bacce651ce582a4ffb6fbbfa2079428ecb"),
|
2016-01-13 22:34:27 -05:00
|
|
|
metadata: tooManyBlobSums,
|
2015-11-18 17:18:44 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set some associations
|
|
|
|
for _, vec := range testVectors {
|
2016-01-13 22:34:27 -05:00
|
|
|
for _, blobsum := range vec.metadata {
|
|
|
|
err := V2MetadataService.Add(vec.diffID, blobsum)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error calling Set: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the correct values are read back
|
|
|
|
for _, vec := range testVectors {
|
2016-01-13 22:34:27 -05:00
|
|
|
metadata, err := V2MetadataService.GetMetadata(vec.diffID)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error calling Get: %v", err)
|
|
|
|
}
|
2016-01-13 22:34:27 -05:00
|
|
|
expectedMetadataEntries := len(vec.metadata)
|
|
|
|
if expectedMetadataEntries > 50 {
|
|
|
|
expectedMetadataEntries = 50
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
2016-01-13 22:34:27 -05:00
|
|
|
if !reflect.DeepEqual(metadata, vec.metadata[len(vec.metadata)-expectedMetadataEntries:len(vec.metadata)]) {
|
2015-11-18 17:18:44 -05:00
|
|
|
t.Fatal("Get returned incorrect layer ID")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:34:27 -05:00
|
|
|
// Test GetMetadata on a nonexistent entry
|
|
|
|
_, err = V2MetadataService.GetMetadata(layer.DiffID("sha256:82379823067823853223359023576437723560923756b03560378f4497753917"))
|
2015-11-18 17:18:44 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error looking up nonexistent entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test GetDiffID on a nonexistent entry
|
2016-01-13 22:34:27 -05:00
|
|
|
_, err = V2MetadataService.GetDiffID(digest.Digest("sha256:82379823067823853223359023576437723560923756b03560378f4497753917"))
|
2015-11-18 17:18:44 -05:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error looking up nonexistent entry")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite one of the entries and read it back
|
2016-01-13 22:34:27 -05:00
|
|
|
err = V2MetadataService.Add(testVectors[1].diffID, testVectors[0].metadata[0])
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error calling Add: %v", err)
|
|
|
|
}
|
2016-01-13 22:34:27 -05:00
|
|
|
diffID, err := V2MetadataService.GetDiffID(testVectors[0].metadata[0].Digest)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error calling GetDiffID: %v", err)
|
|
|
|
}
|
|
|
|
if diffID != testVectors[1].diffID {
|
|
|
|
t.Fatal("GetDiffID returned incorrect diffID")
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 17:17:42 -05:00
|
|
|
|
|
|
|
func randomDigest() digest.Digest {
|
|
|
|
b := [32]byte{}
|
|
|
|
for i := 0; i < len(b); i++ {
|
|
|
|
b[i] = byte(rand.Intn(256))
|
|
|
|
}
|
|
|
|
d := hex.EncodeToString(b[:])
|
|
|
|
return digest.Digest("sha256:" + d)
|
|
|
|
}
|