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