-
Notifications
You must be signed in to change notification settings - Fork 41
/
testhelpers.go
682 lines (552 loc) · 16.7 KB
/
testhelpers.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package testhelpers
import (
"archive/tar"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"testing"
"time"
"github.com/docker/docker/api/types/image"
dockercli "github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/google/go-cmp/cmp"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/layout"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/pkg/errors"
"github.com/buildpacks/imgutil/layer"
)
func RandString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = 'a' + byte(rand.Intn(26)) // #nosec G404
}
return string(b)
}
// AssertEq asserts deep equality (and provides a useful difference as a test failure)
func AssertEq(t *testing.T, actual, expected interface{}) {
t.Helper()
if diff := cmp.Diff(actual, expected); diff != "" {
t.Fatal(diff)
}
}
func AssertNotEq(t *testing.T, v1, v2 interface{}) {
t.Helper()
if diff := cmp.Diff(v1, v2); diff == "" {
t.Fatalf("expected values not to be equal, both equal to %v", v1)
}
}
func AssertContains(t *testing.T, slice []string, elements ...string) {
t.Helper()
outer:
for _, el := range elements {
for _, actual := range slice {
if diff := cmp.Diff(actual, el); diff == "" {
continue outer
}
}
t.Fatalf("Expected %+v to contain: %s", slice, el)
}
}
func AssertDoesNotContain(t *testing.T, slice []string, elements ...string) {
t.Helper()
for _, el := range elements {
for _, actual := range slice {
if diff := cmp.Diff(actual, el); diff == "" {
t.Fatalf("Expected %+v to NOT contain: %s", slice, el)
}
}
}
}
func AssertMatch(t *testing.T, actual string, expected *regexp.Regexp) {
t.Helper()
if !expected.Match([]byte(actual)) {
t.Fatal(cmp.Diff(actual, expected))
}
}
func AssertError(t *testing.T, actual error, expected string) {
t.Helper()
if actual == nil {
t.Fatalf("Expected an error but got nil")
}
if !strings.Contains(actual.Error(), expected) {
t.Fatalf(
`Expected error to contain "%s", got "%s"\n\n Diff:\n%s`,
expected,
actual.Error(),
cmp.Diff(expected, actual.Error()),
)
}
}
func AssertNil(t *testing.T, actual interface{}) {
t.Helper()
if actual != nil {
t.Fatalf("Expected nil: %s", actual)
}
}
func AssertNotNil(t *testing.T, actual any) {
t.Helper()
if actual == nil {
t.Fatalf("Expected not nil: %s", actual)
}
}
func AssertBlobsLen(t *testing.T, path string, expected int) {
t.Helper()
fis, err := os.ReadDir(filepath.Join(path, "blobs", "sha256"))
AssertNil(t, err)
AssertEq(t, len(fis), expected)
}
func AssertTrue(t *testing.T, p func() bool) {
t.Helper()
if !p() {
t.Fatal("Expected predicate to be true")
}
}
var dockerCliVal dockercli.CommonAPIClient
var dockerCliOnce sync.Once
func DockerCli(t *testing.T) dockercli.CommonAPIClient {
dockerCliOnce.Do(func() {
var dockerCliErr error
dockerCliVal, dockerCliErr = dockercli.NewClientWithOpts(dockercli.FromEnv, dockercli.WithVersion("1.38"))
AssertNil(t, dockerCliErr)
})
return dockerCliVal
}
func Eventually(t *testing.T, test func() bool, every time.Duration, timeout time.Duration) {
t.Helper()
ticker := time.NewTicker(every)
defer ticker.Stop()
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case <-ticker.C:
if test() {
return
}
case <-timer.C:
t.Fatalf("timeout on eventually: %v", timeout)
}
}
}
func PullIfMissing(t *testing.T, docker dockercli.CommonAPIClient, ref string) {
t.Helper()
_, _, err := docker.ImageInspectWithRaw(context.TODO(), ref)
if err == nil {
return
}
if !dockercli.IsErrNotFound(err) {
t.Fatalf("failed inspecting image '%s': %s", ref, err)
}
rc, err := docker.ImagePull(context.Background(), ref, image.PullOptions{})
if err != nil {
// Retry
rc, err = docker.ImagePull(context.Background(), ref, image.PullOptions{})
AssertNil(t, err)
}
defer rc.Close()
AssertNil(t, checkResponseError(rc))
_, err = io.Copy(io.Discard, rc)
AssertNil(t, err)
}
func DockerRmi(dockerCli dockercli.CommonAPIClient, repoNames ...string) error {
var err error
ctx := context.Background()
for _, repoName := range repoNames {
_, e := dockerCli.ImageRemove(
ctx,
repoName,
image.RemoveOptions{PruneChildren: true},
)
if e != nil && err == nil {
err = e
}
}
return err
}
// PushImage pushes an image to a registry, optionally using credentials from any set DOCKER_CONFIG
func PushImage(t *testing.T, _ dockercli.CommonAPIClient, refStr string) {
t.Helper()
Run(t, exec.Command("docker", "push", refStr)) // #nosec G204
}
func Run(t *testing.T, cmd *exec.Cmd) string {
t.Helper()
txt, _, err := RunE(cmd)
AssertNil(t, err)
return txt
}
func RunE(cmd *exec.Cmd) (output string, exitCode int, err error) {
var stderr bytes.Buffer
cmd.Stderr = &stderr
stdout, err := cmd.Output()
if err != nil {
formattedErr := fmt.Errorf("failed to execute command: %v, %s, %s, %s", cmd.Args, err, stderr.String(), stdout)
return "", -1, formattedErr
}
return string(stdout), 0, nil
}
// DeleteRegistryBlob deletes the blob with the given digest from the registry by issuing an HTTP DELETE request.
func DeleteRegistryBlob(t *testing.T, repoName string, digest v1.Hash, encodedAuth string) {
ref, err := name.ParseReference(repoName, name.WeakValidation)
AssertNil(t, err)
urlFromRegistryInfo := url.URL{
Scheme: ref.Context().Registry.Scheme(),
Host: ref.Context().RegistryStr(),
Path: fmt.Sprintf("/v2/%s/blobs/%s", ref.Context().RepositoryStr(), digest),
}
req, err := http.NewRequest(http.MethodDelete, urlFromRegistryInfo.String(), nil)
AssertNil(t, err)
req.Header.Add("Authorization", "Basic "+encodedAuth)
client := &http.Client{}
resp, err := client.Do(req)
AssertNil(t, err)
defer resp.Body.Close()
_, err = io.ReadAll(resp.Body)
AssertNil(t, err)
AssertEq(t, resp.StatusCode, http.StatusAccepted)
}
func ImageID(t *testing.T, repoName string) string {
t.Helper()
inspect, _, err := DockerCli(t).ImageInspectWithRaw(context.Background(), repoName)
AssertNil(t, err)
return inspect.ID
}
func CreateSingleFileTarReader(path, txt string) io.ReadCloser {
pr, pw := io.Pipe()
go func() {
// Use the regular tar.Writer, as this isn't a layer tar.
tw := tar.NewWriter(pw)
if err := tw.WriteHeader(&tar.Header{Name: path, Size: int64(len(txt)), Mode: 0644}); err != nil {
pw.CloseWithError(err)
}
if _, err := tw.Write([]byte(txt)); err != nil {
pw.CloseWithError(err)
}
if err := tw.Close(); err != nil {
pw.CloseWithError(err)
}
if err := pw.Close(); err != nil {
pw.CloseWithError(err)
}
}()
return pr
}
type layerWriter interface {
WriteHeader(*tar.Header) error
Write([]byte) (int, error)
Close() error
}
func getLayerWriter(osType string, file *os.File) layerWriter {
if osType == "windows" {
return layer.NewWindowsWriter(file)
}
return tar.NewWriter(file)
}
func CreateSingleFileLayerTar(layerPath, txt, osType string) (string, error) {
tarFile, err := os.CreateTemp("", "create-single-file-layer-tar-path")
if err != nil {
return "", err
}
defer tarFile.Close()
tw := getLayerWriter(osType, tarFile)
if err := tw.WriteHeader(&tar.Header{Name: layerPath, Size: int64(len(txt)), Mode: 0644}); err != nil {
return "", err
}
if _, err := tw.Write([]byte(txt)); err != nil {
return "", err
}
if err := tw.Close(); err != nil {
return "", err
}
return tarFile.Name(), nil
}
func FetchManifestLayers(t *testing.T, repoName string) []string {
t.Helper()
r, err := name.ParseReference(repoName, name.WeakValidation)
AssertNil(t, err)
auth, err := authn.DefaultKeychain.Resolve(r.Context().Registry)
AssertNil(t, err)
gImg, err := remote.Image(
r,
remote.WithTransport(http.DefaultTransport),
remote.WithAuth(auth),
)
AssertNil(t, err)
gLayers, err := gImg.Layers()
AssertNil(t, err)
var manifestLayers []string
for _, gLayer := range gLayers {
diffID, err := gLayer.DiffID()
AssertNil(t, err)
manifestLayers = append(manifestLayers, diffID.String())
}
return manifestLayers
}
func FetchManifestImageConfigFile(t *testing.T, repoName string) *v1.ConfigFile {
t.Helper()
r, err := name.ParseReference(repoName, name.WeakValidation)
AssertNil(t, err)
auth, err := authn.DefaultKeychain.Resolve(r.Context().Registry)
AssertNil(t, err)
gImg, err := remote.Image(r, remote.WithTransport(http.DefaultTransport), remote.WithAuth(auth))
AssertNil(t, err)
configFile, err := gImg.ConfigFile()
AssertNil(t, err)
AssertNotEq(t, configFile, nil)
return configFile
}
func FetchImageIndexDescriptor(t *testing.T, repoName string) v1.ImageIndex {
t.Helper()
r, err := name.ParseReference(repoName, name.WeakValidation)
AssertNil(t, err)
auth, err := authn.DefaultKeychain.Resolve(r.Context().Registry)
AssertNil(t, err)
index, err := remote.Index(r, remote.WithTransport(http.DefaultTransport), remote.WithAuth(auth))
AssertNil(t, err)
return index
}
func FileDiffID(t *testing.T, path string) string {
tarFile, err := os.Open(filepath.Clean(path))
AssertNil(t, err)
defer tarFile.Close()
hasher := sha256.New()
_, err = io.Copy(hasher, tarFile)
AssertNil(t, err)
diffID := "sha256:" + hex.EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size())))
return diffID
}
// RunnableBaseImage returns an image that can be used by a daemon of the same OS to create an container or run a command
func RunnableBaseImage(os string) string {
if os == "windows" {
// windows/amd64 image from manifest cached on github actions Windows 2019 workers: https://github.com/actions/virtual-environments/blob/master/images/win/Windows2019-Readme.md#docker-images
return "mcr.microsoft.com/windows/nanoserver@sha256:08c883692e527b2bb4d7f6579e7707a30a2aaa66556b265b917177565fd76117"
}
return "busybox@sha256:915f390a8912e16d4beb8689720a17348f3f6d1a7b659697df850ab625ea29d5"
}
func StringElementAt(elements []string, offset int) string {
if offset < 0 {
return elements[len(elements)+offset]
}
return elements[offset]
}
func checkResponseError(r io.Reader) error {
responseBytes, err := io.ReadAll(r)
if err != nil {
return err
}
responseBuf := bytes.NewBuffer(responseBytes)
decoder := json.NewDecoder(responseBuf)
for {
var jsonMessage jsonmessage.JSONMessage
err := decoder.Decode(&jsonMessage)
if err != nil {
return fmt.Errorf("parsing response: %w\n%s", err, responseBuf.String())
}
if jsonMessage.Error != nil {
return errors.Wrap(jsonMessage.Error, "embedded daemon response")
}
if !decoder.More() {
break
}
}
return nil
}
func CreateSingleFileTar(path, txt string) (io.Reader, error) {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tw.WriteHeader(&tar.Header{Name: path, Size: int64(len(txt)), Mode: 0644}); err != nil {
return nil, err
}
if _, err := tw.Write([]byte(txt)); err != nil {
return nil, err
}
if err := tw.Close(); err != nil {
return nil, err
}
return bytes.NewReader(buf.Bytes()), nil
}
func RandomLayer(t *testing.T, tmpDir string) (path string, sha string, contents []byte) {
t.Helper()
r, err := CreateSingleFileTar("/some-file", RandString(10))
AssertNil(t, err)
path = filepath.Join(tmpDir, RandString(10)+".tar")
fh, err := os.Create(path)
AssertNil(t, err)
defer fh.Close()
hasher := sha256.New()
var contentsBuf bytes.Buffer
mw := io.MultiWriter(hasher, fh, &contentsBuf)
_, err = io.Copy(mw, r)
AssertNil(t, err)
sha = hex.EncodeToString(hasher.Sum(make([]byte, 0, hasher.Size())))
return path, "sha256:" + sha, contentsBuf.Bytes()
}
func RemoteRunnableBaseImage(t *testing.T) v1.Image {
testImageName := "busybox"
var opts []remote.Option
if runtime.GOOS == "windows" {
testImageName = "mcr.microsoft.com/windows/nanoserver@sha256:8bd4389d56e69bebf6e4666251fba42f7cce3d5b768d28816884fb4370155fee" // mcr.microsoft.com/windows/nanoserver:1809
windowsPlatform := v1.Platform{
Architecture: "amd64",
OS: "windows",
OSVersion: "10.0.17763.3532",
}
opts = append(opts, remote.WithPlatform(windowsPlatform))
}
return RemoteImage(t, testImageName, opts)
}
func RemoteImage(t *testing.T, testImageName string, opts []remote.Option) v1.Image {
r, err := name.ParseReference(testImageName, name.WeakValidation)
AssertNil(t, err)
auth, err := authn.DefaultKeychain.Resolve(r.Context().Registry)
AssertNil(t, err)
opts = append(opts, remote.WithAuth(auth))
testImage, err := remote.Image(r, opts...)
AssertNil(t, err)
return testImage
}
func AssertPathExists(t *testing.T, path string) {
t.Helper()
_, err := os.Stat(path)
if os.IsNotExist(err) {
t.Errorf("Expected %q to exist", path)
} else if err != nil {
t.Fatalf("Error stating %q: %v", path, err)
}
}
func AssertPathDoesNotExists(t *testing.T, path string) {
t.Helper()
_, err := os.Stat(path)
if err == nil {
t.Errorf("Expected %q to not exists", path)
}
}
func AssertEqAnnotation(t *testing.T, manifest v1.Descriptor, key, value string) {
t.Helper()
AssertTrue(t, func() bool {
return len(manifest.Annotations) > 0
})
AssertTrue(t, func() bool {
return manifest.Annotations[key] == value
})
}
func AssertOCIMediaTypes(t *testing.T, image v1.Image) {
t.Helper()
mediaType, err := image.MediaType()
AssertNil(t, err)
AssertEq(t, mediaType, types.OCIManifestSchema1)
manifest, err := image.Manifest()
AssertNil(t, err)
AssertNotEq(t, manifest.MediaType, "")
AssertEq(t, manifest.Config.MediaType, types.OCIConfigJSON)
for _, manifestLayer := range manifest.Layers {
AssertEq(t, manifestLayer.MediaType, types.OCILayer)
}
}
func AssertDockerMediaTypes(t *testing.T, image v1.Image) {
t.Helper()
mediaType, err := image.MediaType()
AssertNil(t, err)
AssertEq(t, mediaType, types.DockerManifestSchema2)
manifest, err := image.Manifest()
AssertNil(t, err)
AssertNotEq(t, manifest.MediaType, "")
AssertEq(t, manifest.Config.MediaType, types.DockerConfigJSON)
for _, manifestLayer := range manifest.Layers {
AssertEq(t, manifestLayer.MediaType, types.DockerLayer)
}
}
func ReadImageIndex(t *testing.T, path string) v1.ImageIndex {
t.Helper()
indexPath := filepath.Join(path, "index.json")
AssertPathExists(t, filepath.Join(path, "oci-layout"))
AssertPathExists(t, indexPath)
layoutPath, err := layout.FromPath(path)
AssertNil(t, err)
localIndex, err := layoutPath.ImageIndex()
AssertNil(t, err)
AssertNotNil(t, localIndex)
return localIndex
}
func DigestsFromImageIndex(t *testing.T, index v1.ImageIndex) []v1.Hash {
t.Helper()
manifests, err := index.IndexManifest()
AssertNil(t, err)
var hashes []v1.Hash
for _, manifest := range manifests.Manifests {
hashes = append(hashes, manifest.Digest)
}
return hashes
}
func AssertRemoteImageIndex(t *testing.T, repoName string, mediaType types.MediaType, expectedNumberOfManifests int) {
t.Helper()
remoteIndex := FetchImageIndexDescriptor(t, repoName)
AssertNotNil(t, remoteIndex)
remoteIndexMediaType, err := remoteIndex.MediaType()
AssertNil(t, err)
AssertEq(t, remoteIndexMediaType, mediaType)
remoteIndexManifest, err := remoteIndex.IndexManifest()
AssertNil(t, err)
AssertNotNil(t, remoteIndexManifest)
AssertEq(t, len(remoteIndexManifest.Manifests), expectedNumberOfManifests)
}
func ReadIndexManifest(t *testing.T, path string) *v1.IndexManifest {
t.Helper()
indexPath := filepath.Join(path, "index.json")
AssertPathExists(t, filepath.Join(path, "oci-layout"))
AssertPathExists(t, indexPath)
// check index file
data, err := os.ReadFile(indexPath)
AssertNil(t, err)
index := &v1.IndexManifest{}
err = json.Unmarshal(data, index)
AssertNil(t, err)
return index
}
func ReadManifest(t *testing.T, digest v1.Hash, path string) *v1.Manifest {
t.Helper()
manifestPath := filepath.Join(path, "blobs", digest.Algorithm, digest.Hex)
AssertPathExists(t, manifestPath)
data, err := os.ReadFile(manifestPath)
AssertNil(t, err)
manifest := &v1.Manifest{}
err = json.Unmarshal(data, manifest)
AssertNil(t, err)
return manifest
}
func ReadConfigFile(t *testing.T, manifest *v1.Manifest, path string) *v1.ConfigFile {
t.Helper()
digest := manifest.Config.Digest
configPath := filepath.Join(path, "blobs", digest.Algorithm, digest.Hex)
AssertPathExists(t, configPath)
data, err := os.ReadFile(configPath)
AssertNil(t, err)
configFile := &v1.ConfigFile{}
err = json.Unmarshal(data, configFile)
AssertNil(t, err)
return configFile
}
func ReadManifestAndConfigFile(t *testing.T, path string) (*v1.Manifest, *v1.ConfigFile) {
index := ReadIndexManifest(t, path)
AssertEq(t, len(index.Manifests), 1)
manifest := ReadManifest(t, index.Manifests[0].Digest, path)
configFile := ReadConfigFile(t, manifest, path)
return manifest, configFile
}