github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/images/store_test.go (about) 1 package images 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/containerd/containerd/content" 10 "github.com/containerd/containerd/content/local" 11 c8derrdefs "github.com/containerd/containerd/errdefs" 12 "github.com/containerd/containerd/leases" 13 "github.com/containerd/containerd/metadata" 14 "github.com/containerd/containerd/namespaces" 15 "github.com/docker/docker/image" 16 "github.com/opencontainers/go-digest" 17 v1 "github.com/opencontainers/image-spec/specs-go/v1" 18 "go.etcd.io/bbolt" 19 "gotest.tools/v3/assert" 20 is "gotest.tools/v3/assert/cmp" 21 ) 22 23 func setupTestStores(t *testing.T) (context.Context, content.Store, *imageStoreWithLease, func(t *testing.T)) { 24 dir, err := os.MkdirTemp("", t.Name()) 25 assert.NilError(t, err) 26 27 backend, err := image.NewFSStoreBackend(filepath.Join(dir, "images")) 28 assert.NilError(t, err) 29 is, err := image.NewImageStore(backend, nil) 30 assert.NilError(t, err) 31 32 db, err := bbolt.Open(filepath.Join(dir, "metadata.db"), 0600, nil) 33 assert.NilError(t, err) 34 35 cs, err := local.NewStore(filepath.Join(dir, "content")) 36 assert.NilError(t, err) 37 mdb := metadata.NewDB(db, cs, nil) 38 39 cleanup := func(t *testing.T) { 40 assert.Check(t, db.Close()) 41 assert.Check(t, os.RemoveAll(dir)) 42 } 43 ctx := namespaces.WithNamespace(context.Background(), t.Name()) 44 images := &imageStoreWithLease{Store: is, ns: t.Name(), leases: metadata.NewLeaseManager(mdb)} 45 46 return ctx, cs, images, cleanup 47 } 48 49 func TestImageDelete(t *testing.T) { 50 ctx, _, images, cleanup := setupTestStores(t) 51 defer cleanup(t) 52 53 t.Run("no lease", func(t *testing.T) { 54 id, err := images.Create([]byte(`{"rootFS": {}}`)) 55 assert.NilError(t, err) 56 defer images.Delete(id) 57 58 ls, err := images.leases.List(ctx) 59 assert.NilError(t, err) 60 assert.Equal(t, len(ls), 0, ls) 61 62 _, err = images.Delete(id) 63 assert.NilError(t, err, "should not error when there is no lease") 64 }) 65 66 t.Run("lease exists", func(t *testing.T) { 67 id, err := images.Create([]byte(`{"rootFS": {}}`)) 68 assert.NilError(t, err) 69 defer images.Delete(id) 70 71 leaseID := imageKey(digest.Digest(id)) 72 _, err = images.leases.Create(ctx, leases.WithID(leaseID)) 73 assert.NilError(t, err) 74 defer images.leases.Delete(ctx, leases.Lease{ID: leaseID}) 75 76 ls, err := images.leases.List(ctx) 77 assert.NilError(t, err) 78 assert.Check(t, is.Equal(len(ls), 1), ls) 79 80 _, err = images.Delete(id) 81 assert.NilError(t, err) 82 83 ls, err = images.leases.List(ctx) 84 assert.NilError(t, err) 85 assert.Check(t, is.Equal(len(ls), 0), ls) 86 }) 87 } 88 89 func TestContentStoreForPull(t *testing.T) { 90 ctx, cs, imgStore, cleanup := setupTestStores(t) 91 defer cleanup(t) 92 93 csP := &contentStoreForPull{ 94 ContentStore: cs, 95 leases: imgStore.leases, 96 } 97 98 data := []byte(`{}`) 99 desc := v1.Descriptor{ 100 Digest: digest.Canonical.FromBytes(data), 101 Size: int64(len(data)), 102 } 103 104 w, err := csP.Writer(ctx, content.WithRef(t.Name()), content.WithDescriptor(desc)) 105 assert.NilError(t, err) 106 107 _, err = w.Write(data) 108 assert.NilError(t, err) 109 defer w.Close() 110 111 err = w.Commit(ctx, desc.Size, desc.Digest) 112 assert.NilError(t, err) 113 114 assert.Equal(t, len(csP.digested), 1) 115 assert.Check(t, is.Equal(csP.digested[0], desc.Digest)) 116 117 // Test already exists 118 csP.digested = nil 119 _, err = csP.Writer(ctx, content.WithRef(t.Name()), content.WithDescriptor(desc)) 120 assert.Check(t, c8derrdefs.IsAlreadyExists(err)) 121 assert.Equal(t, len(csP.digested), 1) 122 assert.Check(t, is.Equal(csP.digested[0], desc.Digest)) 123 }