-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
604 lines (480 loc) · 15.8 KB
/
examples_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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package main
import (
"archive/tar"
"bufio"
"context"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
ociarchive "github.com/containers/image/v5/oci/archive"
memoryblobcache "github.com/containers/image/v5/pkg/blobinfocache/memory"
imagetypes "github.com/containers/image/v5/types"
"github.com/cucumber/godog"
"github.com/google/shlex"
bkclient "github.com/moby/buildkit/client"
gateway "github.com/moby/buildkit/frontend/gateway/client"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"gitlab.wikimedia.org/repos/releng/blubber/util/imagefs"
)
type ctxKey uint8
const (
wdKey ctxKey = iota
clientKey
imageCfgKey
imageTarfileKey
imageFsKey
)
func defineSteps(ctx *godog.ScenarioContext) {
ctx.Step(`^"([\w-\./]+)" as a working directory`, createWorkingDirectory)
ctx.Step(`^this "([\w-\.]+)"(?: (file|executable))?`, createFile)
ctx.Step(`^you build (and run )?the "([\w-\.]+)" variant`, buildVariant)
ctx.Step(`^the image will (not )?have the following files in "([^"]*)"$`, theImageHasTheFollowingFilesIn)
ctx.Step(`^the image will (not )?have the following files in the default working directory$`, theImageHasTheFollowingFilesInDefaultWorkingDir)
ctx.Step(`^the image will have the (user|group) "([^"]*)" with (?:UID|GID) (\d+)$`, theImageHasTheEntity)
ctx.Step(`^the image runtime user will be "([^"]*)"$`, theImageRuntimeUserIs)
ctx.Step(`^the image entrypoint will be "([^"]*)"$`, theImageEntrypointIs)
ctx.Step(`^the image will include environment variables$`, theImageEnvironmentContains)
ctx.Step(`^the image will not include default arguments$`, theImageCmdIsNullOrEmpty)
ctx.Step(`^the image will include labels$`, theImageLabelsContain)
ctx.Step(`^the image will contain a file "([^"]*)" that looks like$`, theImageContainsFileWithContent)
ctx.Step(`^the entrypoint will have run successfully$`, noop)
}
func TestExamples(t *testing.T) {
suite := godog.TestSuite{
ScenarioInitializer: initializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"examples"},
TestingT: t,
Tags: os.Getenv("BLUBBER_ONLY_EXAMPLES"),
},
}
if os.Getenv("BLUBBER_RUN_EXAMPLES") == "" {
t.Skip("Skipping acceptance tests (set BLUBBER_RUN_EXAMPLES=1 to run them)")
} else {
suite.Run()
}
}
func initializeScenario(ctx *godog.ScenarioContext) {
defineSteps(ctx)
// Clean up any working directory we've created during the scenario
if os.Getenv("BLUBBER_DEBUG_EXAMPLES") == "" {
ctx.After(func(ctx context.Context, _ *godog.Scenario, _ error) (context.Context, error) {
if wd, ok := ctx.Value(wdKey).(*workingDirectory); ok {
wd.Remove()
}
if ifs, ok := ctx.Value(imageFsKey).(imagefs.FS); ok {
ifs.Close()
}
if imageTar, ok := ctx.Value(imageTarfileKey).(*os.File); ok {
os.Remove(imageTar.Name())
}
if client, ok := ctx.Value(clientKey).(*bkclient.Client); ok {
client.Close()
}
return ctx, nil
})
}
}
func noop(ctx context.Context) (context.Context, error) {
return ctx, nil
}
func createWorkingDirectory(ctx context.Context, srcDir string) (context.Context, error) {
wd, err := newWorkingDirectory()
if err != nil {
return ctx, err
}
ctx = context.WithValue(ctx, wdKey, wd)
err = wd.CopyFrom(srcDir)
if err != nil {
return ctx, errors.Wrapf(err, "failed to create a new working directory from %s", srcDir)
}
return ctx, nil
}
func createFile(ctx context.Context, file, fileType string, content []byte) (context.Context, error) {
return withCtxValue[*workingDirectory](ctx, wdKey, func(wd *workingDirectory) (context.Context, error) {
var mode os.FileMode
switch fileType {
case "executable":
mode = os.FileMode(0755)
default:
mode = os.FileMode(0644)
}
return ctx, wd.WriteFile(file, append(content, byte('\n')), mode)
})
}
func buildVariant(ctx context.Context, andRun string, variant string) (context.Context, error) {
runVariant := andRun != ""
return withCtxValue[*workingDirectory](ctx, wdKey, func(wd *workingDirectory) (context.Context, error) {
var err error
blubberImage := os.Getenv("BLUBBER_TEST_IMAGE")
if blubberImage == "" {
return ctx, errors.New("you must set BLUBBER_TEST_IMAGE with the blubber frontend ref to run these tests")
}
// Attempt to retrieve an existing client first
client, ok := ctx.Value(clientKey).(*bkclient.Client)
if !ok {
client, err = bkclient.New(ctx, os.Getenv("BUILDKIT_HOST"))
if err != nil {
return ctx, err
}
ctx = context.WithValue(ctx, clientKey, client)
}
tmptar, err := os.CreateTemp("", "blubber.oci.*.tar")
if err != nil {
return ctx, err
}
ctx = context.WithValue(ctx, imageTarfileKey, tmptar)
solveOpt := bkclient.SolveOpt{
Frontend: "gateway.v0",
FrontendAttrs: map[string]string{
"source": blubberImage,
"filename": "blubber.yaml",
"variant": variant,
"no-cache": "",
"platform": "linux/amd64",
},
LocalDirs: map[string]string{
"context": wd.Path,
"dockerfile": wd.Path,
},
Exports: []bkclient.ExportEntry{
{
Type: bkclient.ExporterOCI,
Output: func(_ map[string]string) (io.WriteCloser, error) {
return tmptar, nil
},
},
},
}
if runVariant {
solveOpt.FrontendAttrs["run-variant"] = "true"
}
_, err = client.Build(ctx, solveOpt, "buildctl", func(ctx context.Context, c gateway.Client) (*gateway.Result, error) {
return c.Solve(ctx, gateway.SolveRequest{
Frontend: solveOpt.Frontend,
FrontendOpt: solveOpt.FrontendAttrs,
})
}, nil)
if err != nil {
return ctx, errors.Wrapf(err, "failed to build variant %s", variant)
}
tmptar.Close()
// Save the image filesystem for future assertions
ref, err := ociarchive.ParseReference(tmptar.Name())
if err != nil {
return ctx, errors.Wrapf(err, "failed to get image reference for OCI tarball %s", tmptar.Name())
}
sys := &imagetypes.SystemContext{
OSChoice: "linux",
ArchitectureChoice: "amd64",
}
cache := memoryblobcache.New()
img, err := ref.NewImage(ctx, sys)
defer img.Close()
if err != nil {
return ctx, errors.Wrapf(err, "failed to get image from ref %s", ref.StringWithinTransport())
}
cfg, err := img.OCIConfig(ctx)
if err != nil {
return ctx, errors.Wrap(err, "failed to get image config")
}
return context.WithValue(context.WithValue(ctx, imageCfgKey, cfg), imageFsKey, imagefs.New(ctx, ref, sys, cache)), err
})
}
// theImageHasTheFollowingFilesIn can be used with any of the following
// table columns:
//
// | owner | group | name |
// | 123 | 123 | some-dir/some-file |
// | 123 | 123 | some-other-file |
//
// Or a very simple listing:
//
// | some-dir/some-file |
// | some-other-file |
func theImageHasTheFollowingFilesIn(ctx context.Context, not string, dir string, files *godog.Table) (context.Context, error) {
negate := false
if not != "" {
negate = true
}
return withImageFS(ctx, func(image fs.FS) (context.Context, error) {
headers := map[int]string{}
for i, row := range files.Rows {
if len(row.Cells) == 1 {
path := filepath.Join(dir, row.Cells[0].Value)
file, err := image.Open(path)
if err == nil {
defer file.Close()
if negate {
return ctx, errors.Errorf("file %s exists in the image and it should not", path)
}
} else if !negate {
return ctx, errors.Wrapf(err, "file %s does not exist in the image", path)
}
} else if i == 0 {
for j, cell := range row.Cells {
headers[j] = cell.Value
}
} else {
fields := make(map[string]string, len(row.Cells))
for j, cell := range row.Cells {
fields[headers[j]] = cell.Value
}
name, ok := fields["name"]
if !ok {
return ctx, errors.New("a file table must have a name column")
}
delete(fields, "name")
path := filepath.Join(dir, name)
file, err := image.Open(path)
if err != nil {
return ctx, errors.Wrapf(err, "file %s does not exist in the image", path)
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return ctx, errors.Wrapf(err, "failed to stat %s", path)
}
header, ok := info.Sys().(*tar.Header)
if !ok {
return ctx, errors.Errorf("failed to get tar header for %s", path)
}
for field, value := range fields {
switch field {
case "owner", "group":
expected, _ := strconv.Atoi(value)
var actual int
if field == "owner" {
actual = header.Uid
} else {
actual = header.Gid
}
if actual != expected {
return ctx, errors.Errorf("expected file %s to have %s %d, but it has %d", path, field, expected, actual)
}
default:
return ctx, errors.Errorf("unknown table field %s", field)
}
}
}
}
return ctx, nil
})
}
func theImageHasTheFollowingFilesInDefaultWorkingDir(ctx context.Context, not string, files *godog.Table) (context.Context, error) {
return theImageHasTheFollowingFilesIn(ctx, not, "/srv/app", files)
}
func theImageContainsFileWithContent(ctx context.Context, path, content string) (context.Context, error) {
return withImageFileData(ctx, path, func(data []byte) (context.Context, error) {
dataStr := string(data)
if !(content == dataStr || (content+"\n") == dataStr) {
return ctx, errors.Errorf("content of %s doesn't match", path)
}
return ctx, nil
})
}
func theImageHasTheEntity(ctx context.Context, ent, name, id string) (context.Context, error) {
source := "/etc/passwd"
if ent == "group" {
source = "/etc/group"
}
return withImageFile(ctx, source, func(reader io.Reader) (context.Context, error) {
found := false
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
record := strings.Split(scanner.Text(), ":")
if len(record) >= 3 {
if record[0] == name && record[2] == id {
found = true
break
}
}
}
err := scanner.Err()
if err != nil {
return ctx, err
}
if !found {
return ctx, errors.Errorf("%s %s with id %s not found in %s", ent, name, id, source)
}
return ctx, nil
})
}
func theImageRuntimeUserIs(ctx context.Context, user string) (context.Context, error) {
return withImage(ctx, func(image *ociv1.Image) (context.Context, error) {
if image.Config.User != user {
return ctx, errors.Errorf("expected image user to be %s but got %s", user, image.Config.User)
}
return ctx, nil
})
}
func theImageEntrypointIs(ctx context.Context, entrypoint string) (context.Context, error) {
return withImage(ctx, func(image *ociv1.Image) (context.Context, error) {
if !(len(image.Config.Entrypoint) == 1 && image.Config.Entrypoint[0] == entrypoint) {
return ctx, errors.Errorf("expected entrypoint to be [%s] but got %s", entrypoint, image.Config.Entrypoint)
}
return ctx, nil
})
}
func theImageEnvironmentContains(ctx context.Context, envTable *godog.Table) (context.Context, error) {
envs := make([]string, len(envTable.Rows))
for i, row := range envTable.Rows {
for _, cell := range row.Cells {
envs[i] = cell.Value
}
}
return withImage(ctx, func(image *ociv1.Image) (context.Context, error) {
imageEnvs := parseEnvs(image.Config.Env)
missing := []string{}
mismatched := []string{}
for _, env := range envs {
k, expected := parseEnv(env)
if actual, ok := imageEnvs[k]; ok {
if actual != expected {
mismatched = append(
mismatched,
fmt.Sprintf("%s=%#v (expected) != %s=%#v (actual)", k, expected, k, actual),
)
}
} else {
missing = append(missing, env)
}
}
if len(missing) > 0 {
return ctx, errors.Errorf("the image environment is missing environment variables: %v", missing)
}
if len(mismatched) > 0 {
return ctx, errors.Errorf("some image environment variables differ: %s", strings.Join(mismatched, ", "))
}
return ctx, nil
})
}
func theImageCmdIsNullOrEmpty(ctx context.Context) (context.Context, error) {
return withImage(ctx, func(image *ociv1.Image) (context.Context, error) {
if image.Config.Cmd != nil && len(image.Config.Cmd) > 0 {
return ctx, errors.Errorf("the image default arguments is not empty, it is: %v", image.Config.Cmd)
}
return ctx, nil
})
}
func theImageLabelsContain(ctx context.Context, labelTable *godog.Table) (context.Context, error) {
expectedLabels := make(map[string]string, len(labelTable.Rows))
for _, row := range labelTable.Rows {
if len(row.Cells) > 1 {
expectedLabels[row.Cells[0].Value] = row.Cells[1].Value
} else if len(row.Cells) > 0 {
expectedLabels[row.Cells[0].Value] = ""
}
}
return withImage(ctx, func(image *ociv1.Image) (context.Context, error) {
missing := []string{}
mismatched := []string{}
for labelName, expected := range expectedLabels {
if actual, ok := image.Config.Labels[labelName]; ok {
if expected != "" && actual != expected {
mismatched = append(
mismatched,
fmt.Sprintf("%s=%#v (expected) != %s=%#v (actual)", labelName, expected, labelName, actual),
)
}
} else {
missing = append(missing, labelName)
}
}
if len(missing) > 0 {
return ctx, errors.Errorf("the image is missing labels: %v", missing)
}
if len(mismatched) > 0 {
return ctx, errors.Errorf("some image labels differ: %s", strings.Join(mismatched, ", "))
}
return ctx, nil
})
}
func withCtxValue[T any](ctx context.Context, key ctxKey, f func(T) (context.Context, error)) (context.Context, error) {
val, ok := ctx.Value(key).(T)
if !ok {
return ctx, errors.New("failed to get the context value")
}
return f(val)
}
func withImage(ctx context.Context, f func(*ociv1.Image) (context.Context, error)) (context.Context, error) {
return withCtxValue[*ociv1.Image](ctx, imageCfgKey, func(image *ociv1.Image) (context.Context, error) {
return f(image)
})
}
func withImageFS(ctx context.Context, f func(fs.FS) (context.Context, error)) (context.Context, error) {
return withCtxValue[imagefs.FS](ctx, imageFsKey, func(image imagefs.FS) (context.Context, error) {
return f(image.WithContext(ctx))
})
}
func withImageFile(ctx context.Context, path string, f func(io.Reader) (context.Context, error)) (context.Context, error) {
return withImageFS(ctx, func(image fs.FS) (context.Context, error) {
file, err := image.Open(path)
if err != nil {
return ctx, errors.Wrapf(err, "failed to open %s from image filesystem", path)
}
return f(file)
})
}
func withImageFileData(ctx context.Context, path string, f func([]byte) (context.Context, error)) (context.Context, error) {
return withImageFile(ctx, path, func(file io.Reader) (context.Context, error) {
data, err := io.ReadAll(file)
if err != nil {
return ctx, errors.Wrapf(err, "failed to read %s", path)
}
return f(data)
})
}
type workingDirectory struct {
Path string
}
func newWorkingDirectory() (*workingDirectory, error) {
path, err := os.MkdirTemp("", "blubber-examples-")
if err != nil {
return nil, err
}
return &workingDirectory{path}, nil
}
func (wd *workingDirectory) WriteFile(name string, data []byte, mode os.FileMode) error {
return os.WriteFile(filepath.Join(wd.Path, name), data, mode)
}
func (wd *workingDirectory) Remove() error {
return os.RemoveAll(wd.Path)
}
func (wd *workingDirectory) CopyFrom(srcDir string) error {
if srcDir[len(srcDir)-1] != '/' {
srcDir = srcDir + "/"
}
// Note the use of "<src>/." syntax which should work with both BSD and GNU
// cp to copy the _contents_ of the source directory into the destination
return exec.Command("cp", "-a", srcDir+".", wd.Path+"/").Run()
}
// parseEnv takes a env variable declaration (which may or may not use double
// quotes to qualify the value) and returns the resulting name and value.
func parseEnv(env string) (string, string) {
kv := strings.SplitN(env, "=", 2)
if len(kv) == 2 {
v, err := shlex.Split(kv[1])
if err == nil && len(v) > 0 {
return kv[0], v[0]
}
return kv[0], ""
}
return env, ""
}
func parseEnvs(env []string) map[string]string {
m := make(map[string]string, len(env))
for _, def := range env {
k, v := parseEnv(def)
m[k] = v
}
return m
}