github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/distribution/metadata/v1_id_service_test.go (about) 1 package metadata 2 3 import ( 4 "io/ioutil" 5 "os" 6 "runtime" 7 "testing" 8 9 "github.com/docker/docker/layer" 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, runtime.GOOS) 20 if err != nil { 21 t.Fatalf("could not create metadata store: %v", err) 22 } 23 v1IDService := NewV1IDService(metadataStore) 24 25 testVectors := []struct { 26 registry string 27 v1ID string 28 layerID layer.DiffID 29 }{ 30 { 31 registry: "registry1", 32 v1ID: "f0cd5ca10b07f35512fc2f1cbf9a6cefbdb5cba70ac6b0c9e5988f4497f71937", 33 layerID: layer.DiffID("sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"), 34 }, 35 { 36 registry: "registry2", 37 v1ID: "9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e", 38 layerID: layer.DiffID("sha256:86e0e091d0da6bde2456dbb48306f3956bbeb2eae1b5b9a43045843f69fe4aaa"), 39 }, 40 { 41 registry: "registry1", 42 v1ID: "9e3447ca24cb96d86ebd5960cb34d1299b07e0a0e03801d90b9969a2c187dd6e", 43 layerID: layer.DiffID("sha256:03f4658f8b782e12230c1783426bd3bacce651ce582a4ffb6fbbfa2079428ecb"), 44 }, 45 } 46 47 // Set some associations 48 for _, vec := range testVectors { 49 err := v1IDService.Set(vec.v1ID, vec.registry, vec.layerID) 50 if err != nil { 51 t.Fatalf("error calling Set: %v", err) 52 } 53 } 54 55 // Check the correct values are read back 56 for _, vec := range testVectors { 57 layerID, err := v1IDService.Get(vec.v1ID, vec.registry) 58 if err != nil { 59 t.Fatalf("error calling Get: %v", err) 60 } 61 if layerID != vec.layerID { 62 t.Fatal("Get returned incorrect layer ID") 63 } 64 } 65 66 // Test Get on a nonexistent entry 67 _, err = v1IDService.Get("82379823067823853223359023576437723560923756b03560378f4497753917", "registry1") 68 if err == nil { 69 t.Fatal("expected error looking up nonexistent entry") 70 } 71 72 // Overwrite one of the entries and read it back 73 err = v1IDService.Set(testVectors[0].v1ID, testVectors[0].registry, testVectors[1].layerID) 74 if err != nil { 75 t.Fatalf("error calling Set: %v", err) 76 } 77 layerID, err := v1IDService.Get(testVectors[0].v1ID, testVectors[0].registry) 78 if err != nil { 79 t.Fatalf("error calling Get: %v", err) 80 } 81 if layerID != testVectors[1].layerID { 82 t.Fatal("Get returned incorrect layer ID") 83 } 84 }