-
Notifications
You must be signed in to change notification settings - Fork 5
/
cache_manager_test.go
257 lines (204 loc) · 7.35 KB
/
cache_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package freezer_test
import (
"bytes"
"encoding/gob"
"os"
"path/filepath"
"testing"
"github.com/ForestEckhardt/freezer"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testCacheManager(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
cacheDir string
cacheManager freezer.CacheManager
)
it.Before(func() {
var err error
cacheDir, err = os.MkdirTemp("", "cache")
Expect(err).ToNot(HaveOccurred())
cacheManager = freezer.NewCacheManager(cacheDir)
})
it.After(func() {
Expect(os.RemoveAll(cacheDir)).To(Succeed())
})
context("Open", func() {
context("when Open is called on the cache manager and there is a buildpacks-cache.db file present", func() {
var inputMap freezer.CacheDB
it.Before(func() {
inputMap = freezer.CacheDB{"buildpack": freezer.CacheEntry{Version: "1.2.3", URI: "some-uri"}}
b := bytes.NewBuffer(nil)
err := gob.NewEncoder(b).Encode(&inputMap)
Expect(err).ToNot(HaveOccurred())
Expect(os.WriteFile(filepath.Join(cacheDir, "buildpacks-cache.db"), b.Bytes(), os.ModePerm))
})
it("returns the cache map stored in the buildpacks-cache.db folder", func() {
err := cacheManager.Open()
Expect(err).ToNot(HaveOccurred())
Expect(cacheManager.Cache).To(Equal(inputMap))
})
})
context("when Open is called on the cache manager and there is no buildpacks-cache.db file present", func() {
it("returns an empty cache map", func() {
err := cacheManager.Open()
Expect(err).ToNot(HaveOccurred())
Expect(cacheManager.Cache).To(Equal(freezer.CacheDB{}))
})
})
context("failure cases", func() {
context("the buildpacks-cache.db file is unable to be created", func() {
it.Before(func() {
Expect(os.Chmod(cacheDir, 0222)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(cacheDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := cacheManager.Open()
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("unable to open the buildpack-cache.db", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(cacheDir, "buildpacks-cache.db"), []byte{}, 0000))
})
it("returns an error", func() {
err := cacheManager.Open()
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("unable to open the buildpack-cache.db", func() {
it.Before(func() {
Expect(os.WriteFile(filepath.Join(cacheDir, "buildpacks-cache.db"), []byte(`%%%`), os.ModePerm))
})
it("returns an error", func() {
err := cacheManager.Open()
Expect(err).To(MatchError(ContainSubstring("unexpected EOF")))
})
})
})
})
context("Close", func() {
context("when Close is called on the cache manager", func() {
it.Before(func() {
err := cacheManager.Open()
Expect(err).ToNot(HaveOccurred())
cacheManager.Cache = freezer.CacheDB{"some-buildpack": freezer.CacheEntry{Version: "1.2.3", URI: "some-uri"}}
})
it("saves the cache map given", func() {
err := cacheManager.Close()
Expect(err).ToNot(HaveOccurred())
var cacheCheck freezer.CacheDB
file, err := os.Open(filepath.Join(cacheDir, "buildpacks-cache.db"))
Expect(err).ToNot(HaveOccurred())
err = gob.NewDecoder(file).Decode(&cacheCheck)
Expect(err).ToNot(HaveOccurred())
Expect(cacheCheck).To(Equal(cacheManager.Cache))
})
})
})
context("Get", func() {
var uri string
it.Before(func() {
err := cacheManager.Open()
Expect(err).ToNot(HaveOccurred())
uri = filepath.Join(cacheDir, "some-uri")
cacheManager.Cache = freezer.CacheDB{"some-buildpack": freezer.CacheEntry{Version: "1.2.3", URI: uri}}
})
context("when the key exists", func() {
context("and the file in uri exists", func() {
it.Before(func() {
Expect(os.WriteFile(uri, []byte(`some-content`), 0644)).To(Succeed())
})
it("returns the entry and ok", func() {
entry, ok, err := cacheManager.Get("some-buildpack")
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(entry).To(Equal(freezer.CacheEntry{Version: "1.2.3", URI: uri}))
})
})
context("and the file in uri does not exists", func() {
it("returns the entry and not ok", func() {
entry, ok, err := cacheManager.Get("some-buildpack")
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
Expect(entry).To(Equal(freezer.CacheEntry{Version: "1.2.3", URI: uri}))
})
})
})
context("when the does not key exist", func() {
it("returns with an empty entry and not ok", func() {
entry, ok, err := cacheManager.Get("some-buildpack-other")
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeFalse())
Expect(entry).To(Equal(freezer.CacheEntry{}))
})
})
context("failure cases", func() {
context("the cached file is cannot be stated", func() {
it.Before(func() {
Expect(os.WriteFile(uri, []byte(`some-content`), 0644)).To(Succeed())
Expect(os.Chmod(cacheDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(cacheDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
_, _, err := cacheManager.Get("some-buildpack")
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
})
})
context("Set", func() {
var uri string
it.Before(func() {
err := cacheManager.Open()
Expect(err).ToNot(HaveOccurred())
uri = filepath.Join(cacheDir, "some-file")
Expect(os.WriteFile(uri, []byte(`some content`), 0644)).To(Succeed())
cacheManager.Cache = freezer.CacheDB{"some-buildpack": freezer.CacheEntry{Version: "1.2.3", URI: uri}}
})
context("when there is an already existing entry", func() {
it("deletes the previous file and sets the new information", func() {
err := cacheManager.Set("some-buildpack", freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"})
Expect(err).NotTo(HaveOccurred())
Expect(uri).NotTo(BeAnExistingFile())
Expect(cacheManager.Cache["some-buildpack"]).To(Equal(freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"}))
})
})
context("when there is not an already existing entry", func() {
it("deletes the previous file and sets the new information", func() {
err := cacheManager.Set("some-buildpack-other", freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"})
Expect(err).NotTo(HaveOccurred())
Expect(uri).To(BeAnExistingFile())
Expect(cacheManager.Cache["some-buildpack-other"]).To(Equal(freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"}))
})
})
context("failure cases", func() {
context("when the previous entry file cannot be removed", func() {
it.Before(func() {
Expect(os.Chmod(cacheDir, 0000)).To(Succeed())
})
it.After(func() {
Expect(os.Chmod(cacheDir, os.ModePerm)).To(Succeed())
})
it("returns an error", func() {
err := cacheManager.Set("some-buildpack", freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"})
Expect(err).To(MatchError(ContainSubstring("permission denied")))
})
})
context("when the cache is nil meaning that the database has not been opened", func() {
it.Before(func() {
cacheManager.Cache = nil
})
it("returns an error", func() {
err := cacheManager.Set("some-buildpack", freezer.CacheEntry{Version: "1.2.4", URI: "some-uri"})
Expect(err).To(MatchError("the cache manager is not loaded properly"))
})
})
})
})
}