-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathbuild_test.go
More file actions
1159 lines (993 loc) · 35.8 KB
/
build_test.go
File metadata and controls
1159 lines (993 loc) · 35.8 KB
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package commands_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/buildpacks/lifecycle/api"
"github.com/golang/mock/gomock"
"github.com/heroku/color"
"github.com/pkg/errors"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/spf13/cobra"
"github.com/buildpacks/pack/internal/paths"
"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/commands/testmocks"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/pkg/client"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
projectTypes "github.com/buildpacks/pack/pkg/project/types"
h "github.com/buildpacks/pack/testhelpers"
)
func TestBuildCommand(t *testing.T) {
color.Disable(true)
defer color.Disable(false)
spec.Run(t, "Commands", testBuildCommand, spec.Random(), spec.Report(report.Terminal{}))
}
func testBuildCommand(t *testing.T, when spec.G, it spec.S) {
var (
command *cobra.Command
logger *logging.LogWithWriters
outBuf bytes.Buffer
mockController *gomock.Controller
mockClient *testmocks.MockPackClient
cfg config.Config
)
it.Before(func() {
logger = logging.NewLogWithWriters(&outBuf, &outBuf)
cfg = config.Config{}
mockController = gomock.NewController(t)
mockClient = testmocks.NewMockPackClient(mockController)
command = commands.Build(logger, cfg, mockClient)
})
when("#BuildCommand", func() {
when("no builder is specified", func() {
it("returns a soft error", func() {
mockClient.EXPECT().
InspectBuilder(gomock.Any(), false).
Return(&client.BuilderInfo{Description: ""}, nil).
AnyTimes()
command.SetArgs([]string{"image"})
err := command.Execute()
h.AssertError(t, err, client.NewSoftError().Error())
})
})
when("a builder and image are set", func() {
it("builds an image with a builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithImage("my-builder", "image")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image"})
h.AssertNil(t, command.Execute())
})
it("builds an image with a builder short command arg", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithImage("my-builder", "image")).
Return(nil)
logger.WantVerbose(true)
command.SetArgs([]string{"-B", "my-builder", "image"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Builder 'my-builder' is untrusted")
})
when("the builder is trusted", func() {
it.Before(func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithTrustedBuilder(true)).
Return(nil)
cfg := config.Config{TrustedBuilders: []config.TrustedBuilder{{Name: "my-builder"}}}
command = commands.Build(logger, cfg, mockClient)
})
it("sets the trust builder option", func() {
logger.WantVerbose(true)
command.SetArgs([]string{"image", "--builder", "my-builder"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Builder 'my-builder' is trusted")
})
when("a lifecycle-image is provided", func() {
it("ignoring the mentioned lifecycle image, going with default version", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "some-lifecycle-image"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Warning: Ignoring the provided lifecycle image as the builder is trusted, running the creator in a single container using the provided builder")
})
})
})
when("the builder is suggested", func() {
it("sets the trust builder option", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithTrustedBuilder(true)).
Return(nil)
logger.WantVerbose(true)
command.SetArgs([]string{"image", "--builder", "heroku/builder:22"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Builder 'heroku/builder:22' is trusted")
})
})
})
when("--buildpack-registry flag is specified but experimental isn't set in the config", func() {
it("errors with a descriptive message", func() {
command.SetArgs([]string{"image", "--builder", "my-builder", "--buildpack-registry", "some-registry"})
err := command.Execute()
h.AssertNotNil(t, err)
h.AssertError(t, err, "Support for buildpack registries is currently experimental.")
})
})
when("a network is given", func() {
it("forwards the network onto the client", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithNetwork("my-network")).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--network", "my-network"})
h.AssertNil(t, command.Execute())
})
})
when("--platform", func() {
it("sets platform", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPlatform("linux/amd64")).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--platform", "linux/amd64"})
h.AssertNil(t, command.Execute())
})
})
when("--pull-policy", func() {
it("sets pull-policy=never", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPullPolicy(image.PullNever)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--pull-policy", "never"})
h.AssertNil(t, command.Execute())
})
it("returns error for unknown policy", func() {
command.SetArgs([]string{"image", "--builder", "my-builder", "--pull-policy", "unknown-policy"})
h.AssertError(t, command.Execute(), "parsing pull policy")
})
it("takes precedence over a configured pull policy", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPullPolicy(image.PullNever)).
Return(nil)
cfg := config.Config{PullPolicy: "if-not-present"}
command := commands.Build(logger, cfg, mockClient)
logger.WantVerbose(true)
command.SetArgs([]string{"image", "--builder", "my-builder", "--pull-policy", "never"})
h.AssertNil(t, command.Execute())
})
})
when("--pull-policy is not specified", func() {
when("no pull policy set in config", func() {
it("uses the default policy", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPullPolicy(image.PullAlways)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder"})
h.AssertNil(t, command.Execute())
})
})
when("pull policy is set in config", func() {
it("uses the set policy", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPullPolicy(image.PullNever)).
Return(nil)
cfg := config.Config{PullPolicy: "never"}
command := commands.Build(logger, cfg, mockClient)
logger.WantVerbose(true)
command.SetArgs([]string{"image", "--builder", "my-builder"})
h.AssertNil(t, command.Execute())
})
})
})
when("volume mounts are specified", func() {
it("mounts the volumes", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithVolumes([]string{"a:b", "c:d"})).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--volume", "a:b", "--volume", "c:d"})
h.AssertNil(t, command.Execute())
})
it("warns when running with an untrusted builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithVolumes([]string{"a:b", "c:d"})).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--volume", "a:b", "--volume", "c:d"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Warning: Using untrusted builder with volume mounts")
})
})
when("a default process is specified", func() {
it("sets that process", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsDefaultProcess("my-proc")).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--default-process", "my-proc"})
h.AssertNil(t, command.Execute())
})
})
when("env file", func() {
when("an env file is provided", func() {
var envPath string
it.Before(func() {
envfile, err := os.CreateTemp("", "envfile")
h.AssertNil(t, err)
defer envfile.Close()
envfile.WriteString(`KEY=VALUE`)
envPath = envfile.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(envPath))
})
it("builds an image env variables read from the env file", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{
"KEY": "VALUE",
})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--env-file", envPath})
h.AssertNil(t, command.Execute())
})
})
when("a env file is provided but doesn't exist", func() {
it("fails to run", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--env-file", ""})
err := command.Execute()
h.AssertError(t, err, "parse env file")
})
})
when("an empty env file is provided", func() {
var envPath string
it.Before(func() {
envfile, err := os.CreateTemp("", "envfile")
h.AssertNil(t, err)
defer envfile.Close()
envfile.WriteString(``)
envPath = envfile.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(envPath))
})
it("successfully builds", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--env-file", envPath})
h.AssertNil(t, command.Execute())
})
})
when("two env files are provided with conflicted keys", func() {
var envPath1 string
var envPath2 string
it.Before(func() {
envfile1, err := os.CreateTemp("", "envfile")
h.AssertNil(t, err)
defer envfile1.Close()
envfile1.WriteString("KEY1=VALUE1\nKEY2=IGNORED")
envPath1 = envfile1.Name()
envfile2, err := os.CreateTemp("", "envfile")
h.AssertNil(t, err)
defer envfile2.Close()
envfile2.WriteString("KEY2=VALUE2")
envPath2 = envfile2.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(envPath1))
h.AssertNil(t, os.RemoveAll(envPath2))
})
it("builds an image with the last value of each env variable", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{
"KEY1": "VALUE1",
"KEY2": "VALUE2",
})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--env-file", envPath1, "--env-file", envPath2})
h.AssertNil(t, command.Execute())
})
})
})
when("a cache-image passed", func() {
when("--publish is not used", func() {
it("errors", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache-image", "some-cache-image"})
err := command.Execute()
h.AssertError(t, err, "cache-image flag requires the publish flag")
})
})
when("--publish is used", func() {
it("succeeds", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithCacheImage("some-cache-image")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache-image", "some-cache-image", "--publish"})
h.AssertNil(t, command.Execute())
})
})
})
when("cache flag with 'format=image' is passed", func() {
when("--publish is not used", func() {
it("errors", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache", "type=build;format=image;name=myorg/myimage:cache"})
err := command.Execute()
h.AssertError(t, err, "image cache format requires the 'publish' flag")
})
})
when("--publish is used", func() {
it("succeeds", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithCacheFlags("type=build;format=image;name=myorg/myimage:cache;type=launch;format=volume;")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache", "type=build;format=image;name=myorg/myimage:cache", "--publish"})
h.AssertNil(t, command.Execute())
})
})
when("used together with --cache-image", func() {
it("errors", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache-image", "some-cache-image", "--cache", "type=build;format=image;name=myorg/myimage:cache"})
err := command.Execute()
h.AssertError(t, err, "'cache' flag with 'image' format cannot be used with 'cache-image' flag")
})
})
when("'type=launch;format=image' is used", func() {
it("warns", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithCacheFlags("type=build;format=volume;type=launch;format=image;name=myorg/myimage:cache;")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--cache", "type=launch;format=image;name=myorg/myimage:cache", "--publish"})
h.AssertNil(t, command.Execute())
h.AssertContains(t, outBuf.String(), "Warning: cache definition: 'launch' cache in format 'image' is not supported.")
})
})
})
when("a valid lifecycle-image is provided", func() {
when("only the image repo is provided", func() {
it("uses the provided lifecycle-image and parses it correctly", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("index.docker.io/library/some-lifecycle-image:latest")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "some-lifecycle-image"})
h.AssertNil(t, command.Execute())
})
})
when("a custom image repo is provided", func() {
it("uses the provided lifecycle-image and parses it correctly", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("test.com/some-lifecycle-image:latest")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "test.com/some-lifecycle-image"})
h.AssertNil(t, command.Execute())
})
})
when("a custom image repo is provided with a tag", func() {
it("uses the provided lifecycle-image and parses it correctly", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("test.com/some-lifecycle-image:v1")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "test.com/some-lifecycle-image:v1"})
h.AssertNil(t, command.Execute())
})
})
when("a custom image repo is provided with a digest", func() {
it("uses the provided lifecycle-image and parses it correctly", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("test.com/some-lifecycle-image@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "test.com/some-lifecycle-image@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"})
h.AssertNil(t, command.Execute())
})
})
})
when("an invalid lifecycle-image is provided", func() {
when("the repo name is invalid", func() {
it("returns a parse error", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--lifecycle-image", "some-!nv@l!d-image"})
err := command.Execute()
h.AssertError(t, err, "could not parse reference: some-!nv@l!d-image")
})
})
})
when("a lifecycle-image is not provided", func() {
when("a lifecycle-image is set in the config", func() {
it("uses the lifecycle-image from the config after parsing it", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("index.docker.io/library/some-lifecycle-image:latest")).
Return(nil)
cfg := config.Config{LifecycleImage: "some-lifecycle-image"}
command := commands.Build(logger, cfg, mockClient)
logger.WantVerbose(true)
command.SetArgs([]string{"image", "--builder", "my-builder"})
h.AssertNil(t, command.Execute())
})
})
when("a lifecycle-image is not set in the config", func() {
it("passes an empty lifecycle image and does not throw an error", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLifecycleImage("")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image"})
h.AssertNil(t, command.Execute())
})
})
})
when("env vars are passed as flags", func() {
var (
tmpVar = "tmpVar"
tmpValue = "tmpKey"
)
it.Before(func() {
h.AssertNil(t, os.Setenv(tmpVar, tmpValue))
})
it.After(func() {
h.AssertNil(t, os.Unsetenv(tmpVar))
})
it("sets flag variables", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{
"KEY": "VALUE",
tmpVar: tmpValue,
})).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--env", "KEY=VALUE", "--env", tmpVar})
h.AssertNil(t, command.Execute())
})
})
when("build fails", func() {
it("should show an error", func() {
mockClient.EXPECT().
Build(gomock.Any(), gomock.Any()).
Return(errors.New(""))
command.SetArgs([]string{"--builder", "my-builder", "image"})
err := command.Execute()
h.AssertError(t, err, "failed to build")
})
})
when("user specifies an invalid project descriptor file", func() {
it("should show an error", func() {
projectTomlPath := "/incorrect/path/to/project.toml"
command.SetArgs([]string{"--builder", "my-builder", "--descriptor", projectTomlPath, "image"})
h.AssertNotNil(t, command.Execute())
})
})
when("parsing project descriptor", func() {
when("file is valid", func() {
var projectTomlPath string
it.Before(func() {
projectToml, err := os.CreateTemp("", "project.toml")
h.AssertNil(t, err)
defer projectToml.Close()
projectToml.WriteString(`
[project]
name = "Sample"
[[build.buildpacks]]
id = "example/lua"
version = "1.0"
`)
projectTomlPath = projectToml.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(projectTomlPath))
})
it("should build an image with configuration in descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectTypes.Descriptor{
Project: projectTypes.Project{
Name: "Sample",
},
Build: projectTypes.Build{
Buildpacks: []projectTypes.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "--descriptor", projectTomlPath, "image"})
h.AssertNil(t, command.Execute())
})
})
when("file has a builder specified", func() {
var projectTomlPath string
it.Before(func() {
projectToml, err := os.CreateTemp("", "project.toml")
h.AssertNil(t, err)
defer projectToml.Close()
projectToml.WriteString(`
[project]
name = "Sample"
[build]
builder = "my-builder"
`)
projectTomlPath = projectToml.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(projectTomlPath))
})
when("a builder is not explicitly passed by the user", func() {
it("should build an image with configuration in descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithBuilder("my-builder")).
Return(nil)
command.SetArgs([]string{"--descriptor", projectTomlPath, "image"})
h.AssertNil(t, command.Execute())
})
})
when("a builder is explicitly passed by the user", func() {
it("should build an image with the passed builder flag", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithBuilder("flag-builder")).
Return(nil)
command.SetArgs([]string{"--builder", "flag-builder", "--descriptor", projectTomlPath, "image"})
h.AssertNil(t, command.Execute())
})
})
})
when("file is invalid", func() {
var projectTomlPath string
it.Before(func() {
projectToml, err := os.CreateTemp("", "project.toml")
h.AssertNil(t, err)
defer projectToml.Close()
projectToml.WriteString("project]")
projectTomlPath = projectToml.Name()
})
it.After(func() {
h.AssertNil(t, os.RemoveAll(projectTomlPath))
})
it("should fail to build", func() {
command.SetArgs([]string{"--builder", "my-builder", "--descriptor", projectTomlPath, "image"})
h.AssertNotNil(t, command.Execute())
})
})
when("descriptor path is NOT specified", func() {
when("project.toml exists in source repo", func() {
it.Before(func() {
h.AssertNil(t, os.Chdir("testdata"))
})
it.After(func() {
h.AssertNil(t, os.Chdir(".."))
})
it("should use project.toml in source repo", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectTypes.Descriptor{
Project: projectTypes.Project{
Name: "Sample",
},
Build: projectTypes.Build{
Buildpacks: []projectTypes.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
Env: []projectTypes.EnvVar{{
Name: "KEY1",
Value: "VALUE1",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image"})
h.AssertNil(t, command.Execute())
})
})
when("project.toml does NOT exist in source repo", func() {
it("should use empty descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithEnv(map[string]string{})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image"})
h.AssertNil(t, command.Execute())
})
})
})
when("descriptor path is specified", func() {
when("descriptor file exists", func() {
var projectTomlPath string
it.Before(func() {
projectTomlPath = filepath.Join("testdata", "project.toml")
})
it("should use specified descriptor", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithProjectDescriptor(projectTypes.Descriptor{
Project: projectTypes.Project{
Name: "Sample",
},
Build: projectTypes.Build{
Buildpacks: []projectTypes.Buildpack{{
ID: "example/lua",
Version: "1.0",
}},
Env: []projectTypes.EnvVar{{
Name: "KEY1",
Value: "VALUE1",
}},
},
SchemaVersion: api.MustParse("0.1"),
})).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "--descriptor", projectTomlPath, "image"})
h.AssertNil(t, command.Execute())
})
})
when("descriptor file does NOT exist in source repo", func() {
it("should fail with an error message", func() {
command.SetArgs([]string{"--builder", "my-builder", "--descriptor", "non-existent-path", "image"})
h.AssertError(t, command.Execute(), "stat project descriptor")
})
})
})
})
when("additional tags are specified", func() {
it("forwards additional tags to lifecycle", func() {
expectedTags := []string{"additional-tag-1", "additional-tag-2"}
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithAdditionalTags(expectedTags)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--tag", expectedTags[0], "--tag", expectedTags[1]})
h.AssertNil(t, command.Execute())
})
})
when("gid flag is provided", func() {
when("--gid is a valid value", func() {
it("override build option should be set to true", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithOverrideGroupID(1)).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--gid", "1"})
h.AssertNil(t, command.Execute())
})
})
when("--gid is an invalid value", func() {
it("error must be thrown", func() {
command.SetArgs([]string{"--builder", "my-builder", "image", "--gid", "-1"})
err := command.Execute()
h.AssertError(t, err, "gid flag must be in the range of 0-2147483647")
})
})
})
when("gid flag is not provided", func() {
it("override build option should be set to false", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithOverrideGroupID(-1)).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image"})
h.AssertNil(t, command.Execute())
})
})
when("previous-image flag is provided", func() {
when("image is invalid", func() {
it("error must be thrown", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("previous-image")).
Return(errors.New(""))
command.SetArgs([]string{"--builder", "my-builder", "/x@/y/?!z", "--previous-image", "previous-image"})
err := command.Execute()
h.AssertError(t, err, "failed to build")
})
})
when("previous-image is invalid", func() {
it("error must be thrown", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("%%%")).
Return(errors.New(""))
command.SetArgs([]string{"--builder", "my-builder", "image", "--previous-image", "%%%"})
err := command.Execute()
h.AssertError(t, err, "failed to build")
})
})
when("--publish is false", func() {
it("previous-image should be passed to builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("previous-image")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "image", "--previous-image", "previous-image"})
h.AssertNil(t, command.Execute())
})
})
when("--publish is true", func() {
when("image and previous-image are in same registry", func() {
it("previous-image should be passed to builder", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithPreviousImage("index.docker.io/some/previous:latest")).
Return(nil)
command.SetArgs([]string{"--builder", "my-builder", "index.docker.io/some/image:latest", "--previous-image", "index.docker.io/some/previous:latest", "--publish"})
h.AssertNil(t, command.Execute())
})
})
})
})
when("interactive flag is provided but experimental isn't set in the config", func() {
it("errors with a descriptive message", func() {
command.SetArgs([]string{"image", "--interactive"})
err := command.Execute()
h.AssertNotNil(t, err)
h.AssertError(t, err, "Interactive mode is currently experimental.")
})
})
when("sbom destination directory is provided", func() {
it("forwards the network onto the client", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithSBOMOutputDir("some-output-dir")).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--sbom-output-dir", "some-output-dir"})
h.AssertNil(t, command.Execute())
})
})
when("--creation-time", func() {
when("provided as 'now'", func() {
it("passes it to the builder", func() {
expectedTime := time.Now().UTC()
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithDateTime(&expectedTime)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--creation-time", "now"})
h.AssertNil(t, command.Execute())
})
})
when("provided as unix timestamp", func() {
it("passes it to the builder", func() {
expectedTime, err := time.Parse("2006-01-02T03:04:05Z", "2019-08-19T00:00:01Z")
h.AssertNil(t, err)
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithDateTime(&expectedTime)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder", "--creation-time", "1566172801"})
h.AssertNil(t, command.Execute())
})
})
when("not provided", func() {
it("is nil", func() {
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithDateTime(nil)).
Return(nil)
command.SetArgs([]string{"image", "--builder", "my-builder"})
h.AssertNil(t, command.Execute())
})
})
})
when("export to OCI layout is expected but experimental isn't set in the config", func() {
it("errors with a descriptive message", func() {
command.SetArgs([]string{"oci:image", "--builder", "my-builder"})
err := command.Execute()
h.AssertNotNil(t, err)
h.AssertError(t, err, "Exporting to OCI layout is currently experimental.")
})
})
})
when("export to OCI layout is expected", func() {
var (
sparse bool
previousImage string
layoutDir string
)
it.Before(func() {
layoutDir = filepath.Join(paths.RootDir, "local", "repo")
previousImage = ""
cfg = config.Config{
Experimental: true,
LayoutRepositoryDir: layoutDir,
}
command = commands.Build(logger, cfg, mockClient)
})
when("path to save the image is provided", func() {
it("build is called with oci layout configuration", func() {
sparse = false
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLayoutConfig("image", previousImage, sparse, layoutDir)).
Return(nil)
command.SetArgs([]string{"oci:image", "--builder", "my-builder"})
err := command.Execute()
h.AssertNil(t, err)
})
})
when("previous-image flag is provided", func() {
it("build is called with oci layout configuration", func() {
sparse = false
previousImage = "my-previous-image"
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLayoutConfig("image", previousImage, sparse, layoutDir)).
Return(nil)
command.SetArgs([]string{"oci:image", "--previous-image", "oci:my-previous-image", "--builder", "my-builder"})
err := command.Execute()
h.AssertNil(t, err)
})
})
when("-sparse flag is provided", func() {
it("build is called with oci layout configuration and sparse true", func() {
sparse = true
mockClient.EXPECT().
Build(gomock.Any(), EqBuildOptionsWithLayoutConfig("image", previousImage, sparse, layoutDir)).
Return(nil)
command.SetArgs([]string{"oci:image", "--sparse", "--builder", "my-builder"})
err := command.Execute()
h.AssertNil(t, err)
})
})
})
}
func EqBuildOptionsWithImage(builder, image string) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("Builder=%s and Image=%s", builder, image),
equals: func(o client.BuildOptions) bool {
return o.Builder == builder && o.Image == image
},
}
}
func EqBuildOptionsDefaultProcess(defaultProc string) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("Default Process Type=%s", defaultProc),
equals: func(o client.BuildOptions) bool {
return o.DefaultProcessType == defaultProc
},
}
}
func EqBuildOptionsWithPlatform(platform string) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("Platform=%s", platform),
equals: func(o client.BuildOptions) bool {
return o.Platform == platform
},
}
}
func EqBuildOptionsWithPullPolicy(policy image.PullPolicy) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("PullPolicy=%s", policy),
equals: func(o client.BuildOptions) bool {
return o.PullPolicy == policy
},
}
}
func EqBuildOptionsWithCacheImage(cacheImage string) gomock.Matcher {
return buildOptionsMatcher{
description: fmt.Sprintf("CacheImage=%s", cacheImage),
equals: func(o client.BuildOptions) bool {
return o.CacheImage == cacheImage
},
}
}
func EqBuildOptionsWithCacheFlags(cacheFlags string) gomock.Matcher {
return buildOptionsMatcher{