github.com/uriddle/docker@v0.0.0-20210926094723-4072e6aeb013/distribution/metadata/v1_id_service_test.go (about)

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