github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/distribution/metadata/v2_metadata_service_test.go (about)

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