github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/distribution/metadata/v1_id_service_test.go (about)

     1  package metadata // import "github.com/demonoid81/moby/distribution/metadata"
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/demonoid81/moby/layer"
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  func TestV1IDService(t *testing.T) {
    13  	tmpDir, err := ioutil.TempDir("", "v1-id-service-test")
    14  	if err != nil {
    15  		t.Fatalf("could not create temp dir: %v", err)
    16  	}
    17  	defer os.RemoveAll(tmpDir)
    18  
    19  	metadataStore, err := NewFSMetadataStore(tmpDir)
    20  	if err != nil {
    21  		t.Fatalf("could not create metadata store: %v", err)
    22  	}
    23  	v1IDService := NewV1IDService(metadataStore)
    24  
    25  	ns := v1IDService.namespace()
    26  
    27  	assert.Equal(t, "v1id", ns)
    28  
    29  	testVectors := []struct {
    30  		registry string
    31  		v1ID     string
    32  		layerID  layer.DiffID
    33  	}{
    34  		{
    35  			registry: "registry1",
    36  			v1ID:     "f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937",
    37  			layerID:  layer.DiffID("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"),
    38  		},
    39  		{
    40  			registry: "registry2",
    41  			v1ID:     "9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e",
    42  			layerID:  layer.DiffID("sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa"),
    43  		},
    44  		{
    45  			registry: "registry1",
    46  			v1ID:     "9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e",
    47  			layerID:  layer.DiffID("sha256:03f4658f8b782e12230c1783426bd3bacce651ce582a4ffb6fbbfa2079428ecb"),
    48  		},
    49  	}
    50  
    51  	// Set some associations
    52  	for _, vec := range testVectors {
    53  		err := v1IDService.Set(vec.v1ID, vec.registry, vec.layerID)
    54  		if err != nil {
    55  			t.Fatalf("error calling Set: %v", err)
    56  		}
    57  	}
    58  
    59  	// Check the correct values are read back
    60  	for _, vec := range testVectors {
    61  		layerID, err := v1IDService.Get(vec.v1ID, vec.registry)
    62  		if err != nil {
    63  			t.Fatalf("error calling Get: %v", err)
    64  		}
    65  		if layerID != vec.layerID {
    66  			t.Fatal("Get returned incorrect layer ID")
    67  		}
    68  	}
    69  
    70  	// Test Get on a nonexistent entry
    71  	_, err = v1IDService.Get("82379823067823853223359023576437723560923756b03560378f4497753917", "registry1")
    72  	if err == nil {
    73  		t.Fatal("expected error looking up nonexistent entry")
    74  	}
    75  
    76  	// Overwrite one of the entries and read it back
    77  	err = v1IDService.Set(testVectors[0].v1ID, testVectors[0].registry, testVectors[1].layerID)
    78  	if err != nil {
    79  		t.Fatalf("error calling Set: %v", err)
    80  	}
    81  	layerID, err := v1IDService.Get(testVectors[0].v1ID, testVectors[0].registry)
    82  	if err != nil {
    83  		t.Fatalf("error calling Get: %v", err)
    84  	}
    85  	if layerID != testVectors[1].layerID {
    86  		t.Fatal("Get returned incorrect layer ID")
    87  	}
    88  }