-
Notifications
You must be signed in to change notification settings - Fork 279
/
testhelpers.go
840 lines (710 loc) · 19.8 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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
package testhelpers
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
"testing"
"time"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/stdcopy"
"github.com/go-git/go-git/v5"
"github.com/google/go-cmp/cmp"
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/buildpacks/pack/internal/stringset"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/pack/pkg/buildpack"
"github.com/buildpacks/pack/pkg/dist"
)
func RandString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = 'a' + byte(rand.Intn(26))
}
return string(b)
}
// Assert deep equality (and provide useful difference as a test failure)
func AssertEq(t *testing.T, actual, expected interface{}, opts ...cmp.Option) {
t.Helper()
if diff := cmp.Diff(expected, actual, opts...); diff != "" {
t.Fatal(diff)
}
}
func AssertFunctionName(t *testing.T, fn interface{}, expected string) {
t.Helper()
name := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
if name == "" {
t.Fatalf("Unable to retrieve function name for %#v. Is it a function?", fn)
}
if !hasMatches(name, fmt.Sprintf(`\.(%s)\.func[\d]+$`, expected)) {
t.Fatalf("Expected func name '%s' to contain '%s'", name, expected)
}
}
// Assert deep equality (and provide useful difference as a test failure)
func AssertNotEq(t *testing.T, actual, expected interface{}) {
t.Helper()
if diff := cmp.Diff(expected, actual); diff == "" {
t.Fatal(diff)
}
}
func AssertTrue(t *testing.T, actual interface{}) {
t.Helper()
AssertEq(t, actual, true)
}
func AssertFalse(t *testing.T, actual interface{}) {
t.Helper()
AssertEq(t, actual, false)
}
func AssertUnique(t *testing.T, items ...interface{}) {
t.Helper()
itemMap := map[interface{}]interface{}{}
for _, item := range items {
itemMap[item] = nil
}
if len(itemMap) != len(items) {
t.Fatalf("Expected items in %v to be unique", items)
}
}
// Assert the simplistic pointer (or literal value) equality
func AssertSameInstance(t *testing.T, actual, expected interface{}) {
t.Helper()
if actual != expected {
t.Fatalf("Expected %s and %s to be the same instance", 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"`, expected, actual.Error())
}
}
func AssertContains(t *testing.T, actual, expected string) {
t.Helper()
if !strings.Contains(actual, expected) {
t.Fatalf(
"Expected '%s' to contain '%s'\n\nDiff:%s",
actual,
expected,
cmp.Diff(expected, actual),
)
}
}
func AssertContainsAllInOrder(t *testing.T, actual bytes.Buffer, expected ...string) {
t.Helper()
var tested []byte
for _, exp := range expected {
b, found := readUntilString(&actual, exp)
tested = append(tested, b...)
if !found {
t.Fatalf("Expected '%s' to include all of '%s' in order", string(tested), strings.Join(expected, ", "))
}
}
}
func readUntilString(b *bytes.Buffer, expected string) (read []byte, found bool) {
for {
s, err := b.ReadBytes(expected[len(expected)-1])
if err != nil {
return append(read, s...), false
}
read = append(read, s...)
if bytes.HasSuffix(read, []byte(expected)) {
return read, true
}
}
}
// AssertContainsMatch matches on content by regular expression
func AssertContainsMatch(t *testing.T, actual, exp string) {
t.Helper()
if !hasMatches(actual, exp) {
t.Fatalf("Expected '%s' to match expression '%s'", actual, exp)
}
}
func AssertNotContainsMatch(t *testing.T, actual, exp string) {
t.Helper()
if hasMatches(actual, exp) {
t.Fatalf("Expected '%s' not to match expression '%s'", actual, exp)
}
}
func AssertNotContains(t *testing.T, actual, expected string) {
t.Helper()
if strings.Contains(actual, expected) {
t.Fatalf("Expected '%s' to not contain '%s'", actual, expected)
}
}
func AssertSliceContains(t *testing.T, slice []string, expected ...string) {
t.Helper()
_, missing, _ := stringset.Compare(slice, expected)
if len(missing) > 0 {
t.Fatalf("Expected %s to contain elements %s", slice, missing)
}
}
func AssertSliceContainsInOrder(t *testing.T, slice []string, expected ...string) {
t.Helper()
AssertSliceContains(t, slice, expected...)
var common []string
expectedSet := stringset.FromSlice(expected)
for _, sliceV := range slice {
if _, ok := expectedSet[sliceV]; ok {
common = append(common, sliceV)
}
}
lastFoundI := -1
for _, expectedV := range expected {
for foundI, foundV := range common {
if expectedV == foundV && lastFoundI < foundI {
lastFoundI = foundI
} else if expectedV == foundV {
t.Fatalf("Expected '%s' come earlier in the slice.\nslice: %v\nexpected order: %v", expectedV, slice, expected)
}
}
}
}
func AssertSliceNotContains(t *testing.T, slice []string, expected ...string) {
t.Helper()
_, missing, _ := stringset.Compare(slice, expected)
if len(missing) != len(expected) {
t.Fatalf("Expected %s not to contain elements %s", slice, expected)
}
}
func AssertSliceContainsMatch(t *testing.T, slice []string, expected ...string) {
t.Helper()
var missing []string
for _, expectedStr := range expected {
var found bool
for _, actualStr := range slice {
if regexp.MustCompile(expectedStr).MatchString(actualStr) {
found = true
break
}
}
if !found {
missing = append(missing, expectedStr)
}
}
if len(missing) > 0 {
t.Fatalf("Expected %s to contain elements %s", slice, missing)
}
}
func AssertSliceContainsOnly(t *testing.T, slice []string, expected ...string) {
t.Helper()
extra, missing, _ := stringset.Compare(slice, expected)
if len(missing) > 0 {
t.Fatalf("Expected %s to contain elements %s", slice, missing)
}
if len(extra) > 0 {
t.Fatalf("Expected %s to not contain elements %s", slice, extra)
}
}
func AssertMatch(t *testing.T, actual string, expected string) {
t.Helper()
if !regexp.MustCompile(expected).MatchString(actual) {
t.Fatalf("Expected '%s' to match regex '%s'", actual, expected)
}
}
// AssertNilE checks for nil value, if not nil it sets test as failed without stopping execution.
func AssertNilE(t *testing.T, actual interface{}) {
t.Helper()
if !isNil(actual) {
t.Errorf("Expected nil: %s", actual)
}
}
// AssertNil checks for nil value, if not nil it fails the test and stops execution immediately.
func AssertNil(t *testing.T, actual interface{}) {
t.Helper()
if !isNil(actual) {
t.Fatalf("Expected nil: %s", actual)
}
}
func AssertNotNil(t *testing.T, actual interface{}) {
t.Helper()
if isNil(actual) {
t.Fatal("Expected not nil")
}
}
func AssertTarball(t *testing.T, path string) {
t.Helper()
f, err := os.Open(filepath.Clean(path))
AssertNil(t, err)
defer f.Close()
reader := tar.NewReader(f)
_, err = reader.Next()
AssertNil(t, err)
}
func isNil(value interface{}) bool {
return value == nil || (reflect.TypeOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil())
}
func hasMatches(actual, exp string) bool {
regex := regexp.MustCompile(exp)
matches := regex.FindAll([]byte(actual), -1)
return len(matches) > 0
}
var dockerCliVal client.CommonAPIClient
var dockerCliOnce sync.Once
var dockerCliErr error
func dockerCli(t *testing.T) client.CommonAPIClient {
dockerCliOnce.Do(func() {
dockerCliVal, dockerCliErr = client.NewClientWithOpts(client.FromEnv, client.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 CreateImage(t *testing.T, dockerCli client.CommonAPIClient, repoName, dockerFile string) {
t.Helper()
buildContext := archive.CreateSingleFileTarReader("Dockerfile", dockerFile)
defer buildContext.Close()
resp, err := dockerCli.ImageBuild(context.Background(), buildContext, dockertypes.ImageBuildOptions{
Tags: []string{repoName},
SuppressOutput: true,
Remove: true,
ForceRemove: true,
})
AssertNil(t, err)
defer resp.Body.Close()
err = checkResponse(resp.Body)
AssertNil(t, errors.Wrapf(err, "building image %s", style.Symbol(repoName)))
}
func CreateImageFromDir(t *testing.T, dockerCli client.CommonAPIClient, repoName string, dir string) {
t.Helper()
buildContext := archive.ReadDirAsTar(dir, "/", 0, 0, -1, true, false, nil)
resp, err := dockerCli.ImageBuild(context.Background(), buildContext, dockertypes.ImageBuildOptions{
Tags: []string{repoName},
Remove: true,
ForceRemove: true,
SuppressOutput: false,
})
AssertNil(t, err)
defer resp.Body.Close()
err = checkResponse(resp.Body)
AssertNil(t, errors.Wrapf(err, "building image %s", style.Symbol(repoName)))
}
func CheckImageBuildResult(response dockertypes.ImageBuildResponse, err error) error {
if err != nil {
return err
}
defer response.Body.Close()
return checkResponse(response.Body)
}
func checkResponse(responseBody io.Reader) error {
body, err := ioutil.ReadAll(responseBody)
if err != nil {
return errors.Wrap(err, "reading body")
}
messages := strings.Builder{}
for _, line := range bytes.Split(body, []byte("\n")) {
if len(line) == 0 {
continue
}
var msg jsonmessage.JSONMessage
err := json.Unmarshal(line, &msg)
if err != nil {
return errors.Wrapf(err, "expected JSON: %s", string(line))
}
if msg.Stream != "" {
messages.WriteString(msg.Stream)
}
if msg.Error != nil {
return errors.WithMessage(msg.Error, messages.String())
}
}
return nil
}
func CreateImageOnRemote(t *testing.T, dockerCli client.CommonAPIClient, registryConfig *TestRegistryConfig, repoName, dockerFile string) string {
t.Helper()
imageName := registryConfig.RepoName(repoName)
CreateImage(t, dockerCli, imageName, dockerFile)
AssertNil(t, PushImage(dockerCli, imageName, registryConfig))
return imageName
}
func DockerRmi(dockerCli client.CommonAPIClient, repoNames ...string) error {
var err error
ctx := context.Background()
for _, name := range repoNames {
_, e := dockerCli.ImageRemove(
ctx,
name,
dockertypes.ImageRemoveOptions{Force: true, PruneChildren: true},
)
if e != nil && err == nil {
err = e
}
}
return err
}
func PushImage(dockerCli client.CommonAPIClient, ref string, registryConfig *TestRegistryConfig) error {
rc, err := dockerCli.ImagePush(context.Background(), ref, dockertypes.ImagePushOptions{RegistryAuth: registryConfig.RegistryAuth()})
if err != nil {
return errors.Wrap(err, "pushing image")
}
defer rc.Close()
err = checkResponse(rc)
if err != nil {
return errors.Wrap(err, "push response")
}
return nil
}
func HTTPGetE(url string, headers map[string]string) (string, error) {
client := http.DefaultClient
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", errors.Wrap(err, "making new request")
}
for key, val := range headers {
request.Header.Set(key, val)
}
resp, err := client.Do(request)
if err != nil {
return "", errors.Wrap(err, "doing request")
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
return "", fmt.Errorf("HTTP Status was bad: %s => %d", url, resp.StatusCode)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", errors.Wrap(err, "reading body")
}
return string(b), nil
}
func ImageID(t *testing.T, repoName string) string {
t.Helper()
inspect, _, err := dockerCli(t).ImageInspectWithRaw(context.Background(), repoName)
AssertNil(t, err)
return strings.TrimPrefix(inspect.ID, "sha256:")
}
func Digest(t *testing.T, repoName string) string {
t.Helper()
inspect, _, err := dockerCli(t).ImageInspectWithRaw(context.Background(), repoName)
AssertNil(t, err)
if len(inspect.RepoDigests) < 1 {
t.Fatalf("image '%s' has no repo digests", repoName)
}
parts := strings.Split(inspect.RepoDigests[0], "@")
if len(parts) < 2 {
t.Fatalf("repo digest '%s' malformed", inspect.RepoDigests[0])
}
return parts[1]
}
func TopLayerDiffID(t *testing.T, repoName string) string {
t.Helper()
inspect, _, err := dockerCli(t).ImageInspectWithRaw(context.Background(), repoName)
AssertNil(t, err)
if len(inspect.RootFS.Layers) < 1 {
t.Fatalf("image '%s' has no layers", repoName)
}
return inspect.RootFS.Layers[len(inspect.RootFS.Layers)-1]
}
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) (string, error) {
output, err := cmd.CombinedOutput()
if err != nil {
return string(output), fmt.Errorf("failed to execute command: %v, %s, %s", cmd.Args, err, output)
}
return string(output), nil
}
func PullImageWithAuth(dockerCli client.CommonAPIClient, ref, registryAuth string) error {
rc, err := dockerCli.ImagePull(context.Background(), ref, dockertypes.ImagePullOptions{RegistryAuth: registryAuth})
if err != nil {
return err
}
if _, err := io.Copy(ioutil.Discard, rc); err != nil {
return err
}
return rc.Close()
}
func CopyFile(t *testing.T, src, dst string) {
t.Helper()
err := CopyFileE(src, dst)
AssertNil(t, err)
}
func CopyFileE(src, dst string) error {
fi, err := os.Stat(src)
if err != nil {
return err
}
srcFile, err := os.Open(filepath.Clean(src))
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.OpenFile(filepath.Clean(dst), os.O_RDWR|os.O_CREATE|os.O_TRUNC, fi.Mode())
if err != nil {
return err
}
defer dstFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
modifiedtime := time.Time{}
return os.Chtimes(dst, modifiedtime, modifiedtime)
}
func RecursiveCopy(t *testing.T, src, dst string) {
t.Helper()
err := RecursiveCopyE(src, dst)
AssertNil(t, err)
}
func RecursiveCopyE(src, dst string) error {
fis, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, fi := range fis {
if fi.Mode().IsRegular() {
err = CopyFileE(filepath.Join(src, fi.Name()), filepath.Join(dst, fi.Name()))
if err != nil {
return err
}
}
if fi.IsDir() {
err = os.Mkdir(filepath.Join(dst, fi.Name()), fi.Mode())
if err != nil {
return err
}
err = RecursiveCopyE(filepath.Join(src, fi.Name()), filepath.Join(dst, fi.Name()))
if err != nil {
return err
}
}
}
modifiedtime := time.Time{}
err = os.Chtimes(dst, modifiedtime, modifiedtime)
if err != nil {
return err
}
return os.Chmod(dst, 0775)
}
func RequireDocker(t *testing.T) {
noDocker := os.Getenv("NO_DOCKER")
SkipIf(t, strings.ToLower(noDocker) == "true" || noDocker == "1", "Skipping because docker daemon unavailable")
}
func SkipIf(t *testing.T, expression bool, reason string) {
t.Helper()
if expression {
t.Skip(reason)
}
}
func SkipUnless(t *testing.T, expression bool, reason string) {
t.Helper()
if !expression {
t.Skip(reason)
}
}
func RunContainer(ctx context.Context, dockerCli client.CommonAPIClient, id string, stdout io.Writer, stderr io.Writer) error {
bodyChan, errChan := dockerCli.ContainerWait(ctx, id, container.WaitConditionNextExit)
logs, err := dockerCli.ContainerAttach(ctx, id, dockertypes.ContainerAttachOptions{
Stream: true,
Stdout: true,
Stderr: true,
})
if err != nil {
return err
}
if err := dockerCli.ContainerStart(ctx, id, dockertypes.ContainerStartOptions{}); err != nil {
return errors.Wrap(err, "container start")
}
copyErr := make(chan error)
go func() {
_, err := stdcopy.StdCopy(stdout, stderr, logs.Reader)
copyErr <- err
}()
select {
case body := <-bodyChan:
if body.StatusCode != 0 {
return fmt.Errorf("failed with status code: %d", body.StatusCode)
}
case err := <-errChan:
return err
}
return <-copyErr
}
func CreateTGZ(t *testing.T, srcDir, tarDir string, mode int64) string {
t.Helper()
fh, err := ioutil.TempFile("", "*.tgz")
AssertNil(t, err)
defer fh.Close()
gw := gzip.NewWriter(fh)
defer gw.Close()
writeTAR(t, srcDir, tarDir, mode, gw)
return fh.Name()
}
func CreateTAR(t *testing.T, srcDir, tarDir string, mode int64) string {
t.Helper()
fh, err := ioutil.TempFile("", "*.tgz")
AssertNil(t, err)
defer fh.Close()
writeTAR(t, srcDir, tarDir, mode, fh)
return fh.Name()
}
func writeTAR(t *testing.T, srcDir, tarDir string, mode int64, w io.Writer) {
t.Helper()
tw := tar.NewWriter(w)
defer tw.Close()
err := archive.WriteDirToTar(tw, srcDir, tarDir, 0, 0, mode, true, false, nil)
AssertNil(t, err)
}
func RecursiveCopyNow(t *testing.T, src, dst string) {
t.Helper()
err := os.MkdirAll(dst, 0750)
AssertNil(t, err)
fis, err := ioutil.ReadDir(src)
AssertNil(t, err)
for _, fi := range fis {
if fi.Mode().IsRegular() {
srcFile, err := os.Open(filepath.Join(filepath.Clean(src), fi.Name()))
AssertNil(t, err)
dstFile, err := os.Create(filepath.Join(dst, fi.Name()))
AssertNil(t, err)
_, err = io.Copy(dstFile, srcFile)
AssertNil(t, err)
modifiedTime := time.Now().Local()
err = os.Chtimes(filepath.Join(dst, fi.Name()), modifiedTime, modifiedTime)
AssertNil(t, err)
err = os.Chmod(filepath.Join(dst, fi.Name()), 0664)
AssertNil(t, err)
}
if fi.IsDir() {
err = os.Mkdir(filepath.Join(dst, fi.Name()), fi.Mode())
AssertNil(t, err)
RecursiveCopyNow(t, filepath.Join(src, fi.Name()), filepath.Join(dst, fi.Name()))
}
}
modifiedTime := time.Now().Local()
err = os.Chtimes(dst, modifiedTime, modifiedTime)
AssertNil(t, err)
err = os.Chmod(dst, 0775)
AssertNil(t, err)
}
func AssertTarFileContents(t *testing.T, tarfile, path, expected string) {
t.Helper()
exist, contents := tarFileContents(t, tarfile, path)
if !exist {
t.Fatalf("%s does not exist in %s", path, tarfile)
}
AssertEq(t, contents, expected)
}
func tarFileContents(t *testing.T, tarfile, path string) (exist bool, contents string) {
t.Helper()
r, err := os.Open(filepath.Clean(tarfile))
AssertNil(t, err)
defer r.Close()
tr := tar.NewReader(r)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
AssertNil(t, err)
if header.Name == path {
buf, err := ioutil.ReadAll(tr)
AssertNil(t, err)
return true, string(buf)
}
}
return false, ""
}
func AssertTarHasFile(t *testing.T, tarFile, path string) {
t.Helper()
exist := tarHasFile(t, tarFile, path)
if !exist {
t.Fatalf("%s does not exist in %s", path, tarFile)
}
}
func tarHasFile(t *testing.T, tarFile, path string) (exist bool) {
t.Helper()
r, err := os.Open(filepath.Clean(tarFile))
AssertNil(t, err)
defer r.Close()
tr := tar.NewReader(r)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
AssertNil(t, err)
if header.Name == path {
return true
}
}
return false
}
func AssertBuildpacksHaveDescriptors(t *testing.T, modules []buildpack.BuildModule, descriptors []dist.BuildpackDescriptor) {
AssertEq(t, len(modules), len(descriptors))
for _, mod := range modules {
found := false
modDesc, ok := mod.Descriptor().(*dist.BuildpackDescriptor)
AssertEq(t, ok, true)
for _, descriptor := range descriptors {
if diff := cmp.Diff(*modDesc, descriptor); diff == "" {
found = true
break
}
}
AssertTrue(t, found)
}
}
func AssertGitHeadEq(t *testing.T, path1, path2 string) {
r1, err := git.PlainOpen(path1)
AssertNil(t, err)
r2, err := git.PlainOpen(path2)
AssertNil(t, err)
h1, err := r1.Head()
AssertNil(t, err)
h2, err := r2.Head()
AssertNil(t, err)
AssertEq(t, h1.Hash().String(), h2.Hash().String())
}
func MockWriterAndOutput() (*color.Console, func() string) {
r, w, _ := os.Pipe()
console := color.NewConsole(w)
return console, func() string {
_ = w.Close()
var b bytes.Buffer
_, _ = io.Copy(&b, r)
_ = r.Close()
return b.String()
}
}