github.com/skf/moby@v1.13.1/distribution/metadata/v2_metadata_service_test.go (about)

     1  package metadata
     2  
     3  import (
     4  	"encoding/hex"
     5  	"io/ioutil"
     6  	"math/rand"
     7  	"os"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/docker/distribution/digest"
    12  	"github.com/docker/docker/layer"
    13  )
    14  
    15  func TestV2MetadataService(t *testing.T) {
    16  	tmpDir, err := ioutil.TempDir("", "blobsum-storage-service-test")
    17  	if err != nil {
    18  		t.Fatalf("could not create temp dir: %v", err)
    19  	}
    20  	defer os.RemoveAll(tmpDir)
    21  
    22  	metadataStore, err := NewFSMetadataStore(tmpDir)
    23  	if err != nil {
    24  		t.Fatalf("could not create metadata store: %v", err)
    25  	}
    26  	V2MetadataService := NewV2MetadataService(metadataStore)
    27  
    28  	tooManyBlobSums := make([]V2Metadata, 100)
    29  	for i := range tooManyBlobSums {
    30  		randDigest := randomDigest()
    31  		tooManyBlobSums[i] = V2Metadata{Digest: randDigest}
    32  	}
    33  
    34  	testVectors := []struct {
    35  		diffID   layer.DiffID
    36  		metadata []V2Metadata
    37  	}{
    38  		{
    39  			diffID: layer.DiffID("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"),
    40  			metadata: []V2Metadata{
    41  				{Digest: digest.Digest("sha256:f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937")},
    42  			},
    43  		},
    44  		{
    45  			diffID: layer.DiffID("sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa"),
    46  			metadata: []V2Metadata{
    47  				{Digest: digest.Digest("sha256:f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937")},
    48  				{Digest: digest.Digest("sha256:9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e")},
    49  			},
    50  		},
    51  		{
    52  			diffID:   layer.DiffID("sha256:03f4658f8b782e12230c1783426bd3bacce651ce582a4ffb6fbbfa2079428ecb"),
    53  			metadata: tooManyBlobSums,
    54  		},
    55  	}
    56  
    57  	// Set some associations
    58  	for _, vec := range testVectors {
    59  		for _, blobsum := range vec.metadata {
    60  			err := V2MetadataService.Add(vec.diffID, blobsum)
    61  			if err != nil {
    62  				t.Fatalf("error calling Set: %v", err)
    63  			}
    64  		}
    65  	}
    66  
    67  	// Check the correct values are read back
    68  	for _, vec := range testVectors {
    69  		metadata, err := V2MetadataService.GetMetadata(vec.diffID)
    70  		if err != nil {
    71  			t.Fatalf("error calling Get: %v", err)
    72  		}
    73  		expectedMetadataEntries := len(vec.metadata)
    74  		if expectedMetadataEntries > 50 {
    75  			expectedMetadataEntries = 50
    76  		}
    77  		if !reflect.DeepEqual(metadata, vec.metadata[len(vec.metadata)-expectedMetadataEntries:len(vec.metadata)]) {
    78  			t.Fatal("Get returned incorrect layer ID")
    79  		}
    80  	}
    81  
    82  	// Test GetMetadata on a nonexistent entry
    83  	_, err = V2MetadataService.GetMetadata(layer.DiffID("sha256:82379823067823853223359023576437723560923756b03560378f4497753917"))
    84  	if err == nil {
    85  		t.Fatal("expected error looking up nonexistent entry")
    86  	}
    87  
    88  	// Test GetDiffID on a nonexistent entry
    89  	_, err = V2MetadataService.GetDiffID(digest.Digest("sha256:82379823067823853223359023576437723560923756b03560378f4497753917"))
    90  	if err == nil {
    91  		t.Fatal("expected error looking up nonexistent entry")
    92  	}
    93  
    94  	// Overwrite one of the entries and read it back
    95  	err = V2MetadataService.Add(testVectors[1].diffID, testVectors[0].metadata[0])
    96  	if err != nil {
    97  		t.Fatalf("error calling Add: %v", err)
    98  	}
    99  	diffID, err := V2MetadataService.GetDiffID(testVectors[0].metadata[0].Digest)
   100  	if err != nil {
   101  		t.Fatalf("error calling GetDiffID: %v", err)
   102  	}
   103  	if diffID != testVectors[1].diffID {
   104  		t.Fatal("GetDiffID returned incorrect diffID")
   105  	}
   106  }
   107  
   108  func randomDigest() digest.Digest {
   109  	b := [32]byte{}
   110  	for i := 0; i < len(b); i++ {
   111  		b[i] = byte(rand.Intn(256))
   112  	}
   113  	d := hex.EncodeToString(b[:])
   114  	return digest.Digest("sha256:" + d)
   115  }