forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry_cache_test.go
305 lines (253 loc) · 7.57 KB
/
registry_cache_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package registry
import (
"bytes"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/heroku/color"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/buildpacks/pack/pkg/logging"
h "github.com/buildpacks/pack/testhelpers"
)
func TestRegistryCache(t *testing.T) {
color.Disable(true)
spec.Run(t, "RegistryCache", testRegistryCache, spec.Parallel(), spec.Report(report.Terminal{}))
}
func testRegistryCache(t *testing.T, when spec.G, it spec.S) {
var (
tmpDir string
err error
registryFixture string
outBuf bytes.Buffer
logger logging.Logger
)
it.Before(func() {
logger = logging.NewLogWithWriters(&outBuf, &outBuf)
tmpDir, err = ioutil.TempDir("", "registry")
h.AssertNil(t, err)
registryFixture = h.CreateRegistryFixture(t, tmpDir, filepath.Join("..", "..", "testdata", "registry"))
})
it.After(func() {
err := os.RemoveAll(tmpDir)
h.AssertNil(t, err)
})
when("#NewDefaultRegistryCache", func() {
it("creates a RegistryCache with default URL", func() {
registryCache, err := NewDefaultRegistryCache(logger, tmpDir)
h.AssertNil(t, err)
normalizedURL, err := url.Parse("https://github.com/buildpacks/registry-index")
h.AssertNil(t, err)
h.AssertEq(t, registryCache.url, normalizedURL)
})
})
when("#NewRegistryCache", func() {
when("home doesn't exist", func() {
it("fails to create a registry cache", func() {
_, err := NewRegistryCache(logger, "/tmp/not-exist", "not-here")
h.AssertError(t, err, "finding home")
})
})
when("registryURL isn't a valid url", func() {
it("fails to create a registry cache", func() {
_, err := NewRegistryCache(logger, tmpDir, "://bad-uri")
h.AssertError(t, err, "parsing registry url")
})
})
it("creates a RegistryCache", func() {
registryCache, err := NewRegistryCache(logger, tmpDir, registryFixture)
h.AssertNil(t, err)
expectedRoot := filepath.Join(tmpDir, "registry")
actualRoot := strings.Split(registryCache.Root, "-")[0]
h.AssertEq(t, actualRoot, expectedRoot)
})
})
when("#LocateBuildpack", func() {
var (
registryCache Cache
)
it.Before(func() {
registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture)
h.AssertNil(t, err)
})
it("locates a buildpack without version (name len 3)", func() {
bp, err := registryCache.LocateBuildpack("example/foo")
h.AssertNil(t, err)
h.AssertNotNil(t, bp)
h.AssertEq(t, bp.Namespace, "example")
h.AssertEq(t, bp.Name, "foo")
h.AssertEq(t, bp.Version, "1.2.0")
})
it("locates a buildpack without version (name len 4)", func() {
bp, err := registryCache.LocateBuildpack("example/java")
h.AssertNil(t, err)
h.AssertNotNil(t, bp)
h.AssertEq(t, bp.Namespace, "example")
h.AssertEq(t, bp.Name, "java")
h.AssertEq(t, bp.Version, "1.0.0")
})
it("locates a buildpack with version", func() {
bp, err := registryCache.LocateBuildpack("example/foo@1.1.0")
h.AssertNil(t, err)
h.AssertNotNil(t, bp)
h.AssertEq(t, bp.Namespace, "example")
h.AssertEq(t, bp.Name, "foo")
h.AssertEq(t, bp.Version, "1.1.0")
})
it("returns error if can't parse buildpack id", func() {
_, err := registryCache.LocateBuildpack("quack")
h.AssertError(t, err, "parsing buildpacks registry id")
})
it("returns error if buildpack id is empty", func() {
_, err := registryCache.LocateBuildpack("example/")
h.AssertError(t, err, "'name' cannot be empty")
})
it("returns error if can't find buildpack with requested id", func() {
_, err := registryCache.LocateBuildpack("example/qu")
h.AssertError(t, err, "reading entry")
})
it("returns error if can't find buildpack with requested version", func() {
_, err := registryCache.LocateBuildpack("example/foo@3.5.6")
h.AssertError(t, err, "could not find version")
})
})
when("#Refresh", func() {
var (
registryCache Cache
)
it.Before(func() {
registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture)
h.AssertNil(t, err)
})
when("registry has new commits", func() {
it("pulls the latest index", func() {
h.AssertNil(t, registryCache.Refresh())
h.AssertGitHeadEq(t, registryFixture, registryCache.Root)
r, err := git.PlainOpen(registryFixture)
h.AssertNil(t, err)
w, err := r.Worktree()
h.AssertNil(t, err)
commit, err := w.Commit("second", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "john@doe.org",
When: time.Now(),
},
})
h.AssertNil(t, err)
_, err = r.CommitObject(commit)
h.AssertNil(t, err)
h.AssertNil(t, registryCache.Refresh())
h.AssertGitHeadEq(t, registryFixture, registryCache.Root)
})
})
when("Root is an empty string", func() {
it("fails to refresh", func() {
registryCache.Root = ""
err = registryCache.Refresh()
h.AssertError(t, err, "initializing")
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(registryCache.RegistryDir))
})
})
})
when("#Initialize", func() {
var (
registryCache Cache
)
it.Before(func() {
registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture)
h.AssertNil(t, err)
})
when("root is empty string", func() {
it.Before(func() {
registryCache.Root = ""
})
it("fails to create registry cache", func() {
err = registryCache.Initialize()
h.AssertError(t, err, "creating registry cache")
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(registryCache.RegistryDir))
})
when("url is empty string", func() {
it("fails to clone cache", func() {
registryCache.url = &url.URL{}
err = registryCache.Initialize()
h.AssertError(t, err, "cloning remote registry")
})
})
})
})
when("#Commit", func() {
bp := Buildpack{
Namespace: "example",
Name: "python",
Version: "1.0.0",
Yanked: false,
Address: "example.com",
}
var (
registryCache Cache
msg = "test commit message"
username = "supra08"
)
it.Before(func() {
registryCache, err = NewRegistryCache(logger, tmpDir, registryFixture)
h.AssertNil(t, err)
err = registryCache.CreateCache()
h.AssertNil(t, err)
})
when("correct buildpack and commit message is passed", func() {
it("creates a file and a commit", func() {
h.AssertNil(t, registryCache.Commit(bp, username, msg))
})
})
when("empty commit message is passed", func() {
it("fails to create commit", func() {
err := registryCache.Commit(bp, username, "")
h.AssertError(t, err, "invalid commit message")
})
})
when("Root is an empty string", func() {
it("fails to create commit", func() {
registryCache.Root = ""
err = registryCache.Commit(bp, username, msg)
h.AssertError(t, err, "opening registry cache: repository does not exist")
})
})
when("name is empty in buildpack", func() {
it("fails to create commit", func() {
bp := Buildpack{
Namespace: "example",
Name: "",
Version: "1.0.0",
Yanked: false,
Address: "example.com",
}
err = registryCache.Commit(bp, username, msg)
h.AssertError(t, err, "'name' cannot be empty")
})
})
when("namespace is empty in buildpack", func() {
it("fails to create commit", func() {
bp := Buildpack{
Namespace: "",
Name: "python",
Version: "1.0.0",
Yanked: false,
Address: "example.com",
}
err = registryCache.Commit(bp, username, msg)
h.AssertError(t, err, "'namespace' cannot be empty")
})
})
})
}