-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
workflows_test.go
420 lines (381 loc) · 12.7 KB
/
workflows_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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 The Kubernetes Authors
package workflows_test
import (
"bytes"
"io/fs"
"path/filepath"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
"github.com/spf13/afero"
envp "sigs.k8s.io/controller-runtime/tools/setup-envtest/env"
"sigs.k8s.io/controller-runtime/tools/setup-envtest/remote"
"sigs.k8s.io/controller-runtime/tools/setup-envtest/store"
"sigs.k8s.io/controller-runtime/tools/setup-envtest/versions"
wf "sigs.k8s.io/controller-runtime/tools/setup-envtest/workflows"
)
func ver(major, minor, patch int) versions.Concrete {
return versions.Concrete{
Major: major,
Minor: minor,
Patch: patch,
}
}
func shouldHaveError() {
var err error
var code int
if cause := recover(); envp.CheckRecover(cause, func(caughtCode int, caughtErr error) {
err = caughtErr
code = caughtCode
}) {
panic(cause)
}
Expect(err).To(HaveOccurred(), "should write an error")
Expect(code).NotTo(BeZero(), "should exit with a non-zero code")
}
const (
testStorePath = ".teststore"
)
var _ = Describe("Workflows", func() {
var (
env *envp.Env
out *bytes.Buffer
server *ghttp.Server
remoteItems []item
)
BeforeEach(func() {
out = new(bytes.Buffer)
baseFs := afero.Afero{Fs: afero.NewMemMapFs()}
env = &envp.Env{
Log: testLog,
VerifySum: true, // on by default
FS: baseFs,
Store: &store.Store{Root: afero.NewBasePathFs(baseFs, testStorePath)},
Out: out,
Platform: versions.PlatformItem{ // default
Platform: versions.Platform{
OS: "linux",
Arch: "amd64",
},
},
Client: &remote.Client{
Log: testLog.WithName("remote-client"),
Bucket: "kubebuilder-tools-test", // test custom bucket functionality too
Server: "localhost:-1",
Insecure: true, // no https in httptest :-(
},
}
server = ghttp.NewServer()
env.Client.Server = server.Addr()
fakeStore(env.FS, testStorePath)
remoteItems = remoteVersions
})
JustBeforeEach(func() {
handleRemoteVersions(server, remoteItems)
})
AfterEach(func() {
server.Close()
server = nil
})
Describe("use", func() {
var flow wf.Use
BeforeEach(func() {
// some defaults for most tests
env.Version = versions.Spec{
Selector: ver(1, 16, 0),
}
flow = wf.Use{
PrintFormat: envp.PrintPath,
}
})
It("should initialize the store if it doesn't exist", func() {
Expect(env.FS.RemoveAll(testStorePath)).To(Succeed())
// need to set this to a valid remote version cause our store is now empty
env.Version = versions.Spec{Selector: ver(1, 16, 4)}
flow.Do(env)
Expect(env.FS.Stat(testStorePath)).NotTo(BeNil())
})
Context("when use env is set", func() {
BeforeEach(func() {
flow.UseEnv = true
})
It("should fall back to normal behavior when the env is not set", func() {
flow.Do(env)
Expect(out.String()).To(HaveSuffix("/1.16.0-linux-amd64"), "should fall back to a local version")
})
It("should fall back to normal behavior if binaries are missing", func() {
flow.AssetsPath = ".teststore/missing-binaries"
flow.Do(env)
Expect(out.String()).To(HaveSuffix("/1.16.0-linux-amd64"), "should fall back to a local version")
})
It("should use the value of the env if it contains the right binaries", func() {
flow.AssetsPath = ".teststore/good-version"
flow.Do(env)
Expect(out.String()).To(Equal(flow.AssetsPath))
})
It("should not try and check the version of the binaries", func() {
flow.AssetsPath = ".teststore/wrong-version"
flow.Do(env)
Expect(out.String()).To(Equal(flow.AssetsPath))
})
It("should not need to contact the network", func() {
server.Close()
flow.AssetsPath = ".teststore/good-version"
flow.Do(env)
// expect to not get a panic -- if we do, it'll cause the test to fail
})
})
Context("when downloads are disabled", func() {
BeforeEach(func() {
env.NoDownload = true
server.Close()
})
// It("should not contact the network") is a gimme here, because we
// call server.Close() above.
It("should error if no matches are found locally", func() {
defer shouldHaveError()
env.Version.Selector = versions.Concrete{Major: 9001}
flow.Do(env)
})
It("should settle for the latest local match if latest is requested", func() {
env.Version = versions.Spec{
CheckLatest: true,
Selector: versions.PatchSelector{
Major: 1,
Minor: 16,
Patch: versions.AnyPoint,
},
}
flow.Do(env)
// latest on "server" is 1.16.4, shouldn't use that
Expect(out.String()).To(HaveSuffix("/1.16.1-linux-amd64"), "should use the latest local version")
})
})
Context("if latest is requested", func() {
It("should contact the network to see if there's anything newer", func() {
env.Version = versions.Spec{
CheckLatest: true,
Selector: versions.PatchSelector{
Major: 1, Minor: 16, Patch: versions.AnyPoint,
},
}
flow.Do(env)
Expect(out.String()).To(HaveSuffix("/1.16.4-linux-amd64"), "should use the latest remote version")
})
It("should still use the latest local if the network doesn't have anything newer", func() {
env.Version = versions.Spec{
CheckLatest: true,
Selector: versions.PatchSelector{
Major: 1, Minor: 14, Patch: versions.AnyPoint,
},
}
flow.Do(env)
// latest on the server is 1.14.1, latest local is 1.14.26
Expect(out.String()).To(HaveSuffix("/1.14.26-linux-amd64"), "should use the latest local version")
})
})
It("should check local for a match first", func() {
server.Close() // confirm no network
env.Version = versions.Spec{
Selector: versions.TildeSelector{Concrete: ver(1, 16, 0)},
}
flow.Do(env)
// latest on the server is 1.16.4, latest local is 1.16.1
Expect(out.String()).To(HaveSuffix("/1.16.1-linux-amd64"), "should use the latest local version")
})
It("should fall back to the network if no local matches are found", func() {
env.Version = versions.Spec{
Selector: versions.TildeSelector{Concrete: ver(1, 19, 0)},
}
flow.Do(env)
Expect(out.String()).To(HaveSuffix("/1.19.2-linux-amd64"), "should have a remote version")
})
It("should error out if no matches can be found anywhere", func() {
defer shouldHaveError()
env.Version = versions.Spec{
Selector: versions.TildeSelector{Concrete: ver(0, 0, 1)},
}
flow.Do(env)
})
It("should skip local versions matches with non-matching platforms", func() {
env.NoDownload = true // so we get an error
defer shouldHaveError()
env.Version = versions.Spec{
// has non-matching local versions
Selector: ver(1, 13, 0),
}
flow.Do(env)
})
It("should skip remote version matches with non-matching platforms", func() {
defer shouldHaveError()
env.Version = versions.Spec{
// has a non-matching remote version
Selector: versions.TildeSelector{Concrete: ver(1, 11, 1)},
}
flow.Do(env)
})
Describe("verifying the checksum", func() {
BeforeEach(func() {
remoteItems = append(remoteItems, item{
meta: bucketObject{
Name: "kubebuilder-tools-86.75.309-linux-amd64.tar.gz",
Hash: "nottherightone!",
},
contents: remoteItems[0].contents, // need a valid tar.gz file to not error from that
})
env.Version = versions.Spec{
Selector: ver(86, 75, 309),
}
})
Specify("when enabled, should fail if the downloaded md5 checksum doesn't match", func() {
defer shouldHaveError()
flow.Do(env)
})
Specify("when disabled, shouldn't check the checksum at all", func() {
env.VerifySum = false
flow.Do(env)
})
})
})
Describe("list", func() {
// split by fields so we're not matching on whitespace
listFields := func() [][]string {
resLines := strings.Split(strings.TrimSpace(out.String()), "\n")
resFields := make([][]string, len(resLines))
for i, line := range resLines {
resFields[i] = strings.Fields(line)
}
return resFields
}
Context("when downloads are disabled", func() {
BeforeEach(func() {
server.Close() // ensure no network
env.NoDownload = true
})
It("should include local contents sorted by version", func() {
env.Version = versions.AnyVersion
env.Platform.Platform = versions.Platform{OS: "*", Arch: "*"}
wf.List{}.Do(env)
Expect(listFields()).To(Equal([][]string{
{"(installed)", "v1.17.9", "linux/amd64"},
{"(installed)", "v1.16.2", "ifonlysingularitywasstillathing/amd64"},
{"(installed)", "v1.16.2", "linux/yourimagination"},
{"(installed)", "v1.16.1", "linux/amd64"},
{"(installed)", "v1.16.0", "linux/amd64"},
{"(installed)", "v1.14.26", "hyperwarp/pixiedust"},
{"(installed)", "v1.14.26", "linux/amd64"},
}))
})
It("should skip non-matching local contents", func() {
env.Version.Selector = versions.PatchSelector{
Major: 1, Minor: 16, Patch: versions.AnyPoint,
}
env.Platform.Arch = "*"
wf.List{}.Do(env)
Expect(listFields()).To(Equal([][]string{
{"(installed)", "v1.16.2", "linux/yourimagination"},
{"(installed)", "v1.16.1", "linux/amd64"},
{"(installed)", "v1.16.0", "linux/amd64"},
}))
})
})
Context("when downloads are enabled", func() {
Context("when sorting", func() {
BeforeEach(func() {
// shorten the list a bit for expediency
remoteItems = remoteItems[:7]
})
It("should sort local & remote by version", func() {
env.Version = versions.AnyVersion
env.Platform.Platform = versions.Platform{OS: "*", Arch: "*"}
wf.List{}.Do(env)
Expect(listFields()).To(Equal([][]string{
{"(installed)", "v1.17.9", "linux/amd64"},
{"(installed)", "v1.16.2", "ifonlysingularitywasstillathing/amd64"},
{"(installed)", "v1.16.2", "linux/yourimagination"},
{"(installed)", "v1.16.1", "linux/amd64"},
{"(installed)", "v1.16.0", "linux/amd64"},
{"(installed)", "v1.14.26", "hyperwarp/pixiedust"},
{"(installed)", "v1.14.26", "linux/amd64"},
{"(available)", "v1.11.1", "potato/cherrypie"},
{"(available)", "v1.11.0", "darwin/amd64"},
{"(available)", "v1.11.0", "linux/amd64"},
{"(available)", "v1.10.1", "darwin/amd64"},
{"(available)", "v1.10.1", "linux/amd64"},
}))
})
})
It("should skip non-matching remote contents", func() {
env.Version.Selector = versions.PatchSelector{
Major: 1, Minor: 16, Patch: versions.AnyPoint,
}
env.Platform.Arch = "*"
wf.List{}.Do(env)
Expect(listFields()).To(Equal([][]string{
{"(installed)", "v1.16.2", "linux/yourimagination"},
{"(installed)", "v1.16.1", "linux/amd64"},
{"(installed)", "v1.16.0", "linux/amd64"},
{"(available)", "v1.16.4", "linux/amd64"},
}))
})
})
})
Describe("cleanup", func() {
BeforeEach(func() {
server.Close() // ensure no network
flow := wf.Cleanup{}
env.Version = versions.AnyVersion
env.Platform.Arch = "*"
flow.Do(env)
})
It("should remove matching versions from the store & keep non-matching ones", func() {
entries, err := env.FS.ReadDir(".teststore/k8s")
Expect(err).NotTo(HaveOccurred(), "should be able to read the store")
Expect(entries).To(ConsistOf(
WithTransform(fs.FileInfo.Name, Equal("1.16.2-ifonlysingularitywasstillathing-amd64")),
WithTransform(fs.FileInfo.Name, Equal("1.14.26-hyperwarp-pixiedust")),
))
})
})
Describe("sideload", func() {
var (
flow wf.Sideload
// remote version fake contents are prefixed by the
// name for easier debugging, so we can use that here
expectedPrefix = remoteVersions[0].meta.Name
)
BeforeEach(func() {
server.Close() // ensure no network
flow.Input = bytes.NewReader(remoteVersions[0].contents)
flow.PrintFormat = envp.PrintPath
})
It("should initialize the store if it doesn't exist", func() {
env.Version.Selector = ver(1, 10, 0)
Expect(env.FS.RemoveAll(testStorePath)).To(Succeed())
flow.Do(env)
Expect(env.FS.Stat(testStorePath)).NotTo(BeNil())
})
It("should fail if a non-concrete version is given", func() {
defer shouldHaveError()
env.Version = versions.LatestVersion
flow.Do(env)
})
It("should fail if a non-concrete platform is given", func() {
defer shouldHaveError()
env.Version.Selector = ver(1, 10, 0)
env.Platform.Arch = "*"
flow.Do(env)
})
It("should load the given gizipped tarball into our store as the given version", func() {
env.Version.Selector = ver(1, 10, 0)
flow.Do(env)
baseName := env.Platform.BaseName(*env.Version.AsConcrete())
expectedPath := filepath.Join(".teststore/k8s", baseName, "some-file")
outContents, err := env.FS.ReadFile(expectedPath)
Expect(err).NotTo(HaveOccurred(), "should be able to load the unzipped file")
Expect(string(outContents)).To(HavePrefix(expectedPrefix), "should have the debugging prefix")
})
})
})