github.com/cs3org/reva/v2@v2.27.7/pkg/share/manager/jsoncs3/providercache/providercache_test.go (about) 1 // Copyright 2018-2022 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package providercache_test 20 21 import ( 22 "context" 23 "os" 24 "path/filepath" 25 "time" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1" 31 "github.com/cs3org/reva/v2/pkg/share/manager/jsoncs3/providercache" 32 "github.com/cs3org/reva/v2/pkg/storage/utils/metadata" 33 ) 34 35 var _ = Describe("Cache", func() { 36 var ( 37 c providercache.Cache 38 storage metadata.Storage 39 40 storageID = "storageid" 41 spaceID = "spaceid" 42 shareID = "storageid$spaceid!share1" 43 share1 *collaboration.Share 44 ctx context.Context 45 tmpdir string 46 ) 47 48 BeforeEach(func() { 49 ctx = context.Background() 50 share1 = &collaboration.Share{ 51 Id: &collaboration.ShareId{ 52 OpaqueId: "share1", 53 }, 54 } 55 56 var err error 57 tmpdir, err = os.MkdirTemp("", "providercache-test") 58 Expect(err).ToNot(HaveOccurred()) 59 60 err = os.MkdirAll(tmpdir, 0755) 61 Expect(err).ToNot(HaveOccurred()) 62 63 storage, err = metadata.NewDiskStorage(tmpdir) 64 Expect(err).ToNot(HaveOccurred()) 65 66 c = providercache.New(storage, 0*time.Second) 67 Expect(c).ToNot(BeNil()) //nolint:all 68 }) 69 70 AfterEach(func() { 71 if tmpdir != "" { 72 os.RemoveAll(tmpdir) 73 } 74 }) 75 76 Describe("Add", func() { 77 It("adds a share", func() { 78 s, err := c.Get(ctx, storageID, spaceID, shareID, false) 79 Expect(err).ToNot(HaveOccurred()) 80 Expect(s).To(BeNil()) 81 82 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 83 84 s, err = c.Get(ctx, storageID, spaceID, shareID, false) 85 Expect(err).ToNot(HaveOccurred()) 86 Expect(s).ToNot(BeNil()) 87 Expect(s).To(Equal(share1)) 88 }) 89 90 It("sets the etag", func() { 91 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 92 spaces, ok := c.Providers.Load(storageID) 93 Expect(ok).To(BeTrue()) 94 space, ok := spaces.Spaces.Load(spaceID) 95 Expect(ok).To(BeTrue()) 96 Expect(space.Etag).ToNot(BeEmpty()) 97 }) 98 99 It("updates the etag", func() { 100 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 101 spaces, ok := c.Providers.Load(storageID) 102 Expect(ok).To(BeTrue()) 103 space, ok := spaces.Spaces.Load(spaceID) 104 Expect(ok).To(BeTrue()) 105 old := space.Etag 106 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 107 Expect(space.Etag).ToNot(Equal(old)) 108 }) 109 }) 110 111 Context("with an existing entry", func() { 112 BeforeEach(func() { 113 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 114 }) 115 116 Describe("Get", func() { 117 It("returns the entry", func() { 118 s, err := c.Get(ctx, storageID, spaceID, shareID, false) 119 Expect(err).ToNot(HaveOccurred()) 120 Expect(s).ToNot(BeNil()) 121 }) 122 }) 123 124 Describe("Remove", func() { 125 It("removes the entry", func() { 126 s, err := c.Get(ctx, storageID, spaceID, shareID, false) 127 Expect(err).ToNot(HaveOccurred()) 128 Expect(s).ToNot(BeNil()) 129 Expect(s).To(Equal(share1)) 130 131 Expect(c.Remove(ctx, storageID, spaceID, shareID)).To(Succeed()) 132 133 s, err = c.Get(ctx, storageID, spaceID, shareID, false) 134 Expect(err).ToNot(HaveOccurred()) 135 Expect(s).To(BeNil()) 136 }) 137 138 It("updates the etag", func() { 139 Expect(c.Add(ctx, storageID, spaceID, shareID, share1)).To(Succeed()) 140 spaces, ok := c.Providers.Load(storageID) 141 Expect(ok).To(BeTrue()) 142 space, ok := spaces.Spaces.Load(spaceID) 143 Expect(ok).To(BeTrue()) 144 old := space.Etag 145 Expect(c.Remove(ctx, storageID, spaceID, shareID)).To(Succeed()) 146 Expect(space.Etag).ToNot(Equal(old)) 147 }) 148 }) 149 150 Describe("Persist", func() { 151 It("handles non-existent storages", func() { 152 Expect(c.Persist(ctx, "foo", "bar")).To(Succeed()) 153 }) 154 It("handles non-existent spaces", func() { 155 Expect(c.Persist(ctx, storageID, "bar")).To(Succeed()) 156 }) 157 158 It("persists", func() { 159 Expect(c.Persist(ctx, storageID, spaceID)).To(Succeed()) 160 }) 161 162 It("updates the etag", func() { 163 spaces, ok := c.Providers.Load(storageID) 164 Expect(ok).To(BeTrue()) 165 space, ok := spaces.Spaces.Load(spaceID) 166 Expect(ok).To(BeTrue()) 167 oldEtag := space.Etag 168 169 Expect(c.Persist(ctx, storageID, spaceID)).To(Succeed()) 170 Expect(space.Etag).ToNot(Equal(oldEtag)) 171 }) 172 173 }) 174 175 Describe("PersistWithTime", func() { 176 It("does not persist if the etag changed", func() { 177 time.Sleep(1 * time.Nanosecond) 178 path := filepath.Join(tmpdir, "storages/storageid/spaceid.json") 179 now := time.Now() 180 _ = os.Chtimes(path, now, now) // this only works for the file backend 181 Expect(c.Persist(ctx, storageID, spaceID)).ToNot(Succeed()) 182 }) 183 }) 184 185 Describe("PurgeSpace", func() { 186 It("removes the entry", func() { 187 Expect(c.PurgeSpace(ctx, storageID, spaceID)).To(Succeed()) 188 189 s, err := c.Get(ctx, storageID, spaceID, shareID, false) 190 Expect(err).ToNot(HaveOccurred()) 191 Expect(s).To(BeNil()) 192 }) 193 }) 194 195 Describe("All", func() { 196 It("returns all entries", func() { 197 entries, err := c.All(ctx) 198 Expect(err).ToNot(HaveOccurred()) 199 Expect(entries.Count()).To(Equal(1)) 200 }) 201 }) 202 }) 203 })