-
Notifications
You must be signed in to change notification settings - Fork 23
/
load_test.go
803 lines (736 loc) · 16.6 KB
/
load_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
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
package dalec
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"maps"
"os"
"reflect"
"testing"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
//go:embed test/fixtures/unmarshall/source-inline.yml
var sourceInlineTemplate []byte
func TestSourceValidation(t *testing.T) {
cases := []struct {
title string
src Source
expectErr bool
}{
{
title: "has no valid source variant",
src: Source{},
expectErr: true,
},
{
title: "has multiple non-nil source variants",
src: Source{
DockerImage: &SourceDockerImage{
Ref: "nonempty:latest",
},
Git: &SourceGit{},
},
expectErr: true,
},
{
title: "has multiple source types in docker-image command mount",
expectErr: true,
src: Source{
DockerImage: &SourceDockerImage{
Ref: "nonempty:latest",
Cmd: &Command{
Mounts: []SourceMount{{
Dest: "",
Spec: Source{
DockerImage: &SourceDockerImage{
Ref: "",
Cmd: &Command{
Mounts: []SourceMount{
{
Spec: Source{
Git: &SourceGit{},
HTTP: &SourceHTTP{},
},
},
},
},
},
},
}},
},
},
},
},
{
title: "has no non-nil source type in docker-image command mount",
expectErr: true,
src: Source{
DockerImage: &SourceDockerImage{
Ref: "nonempty:latest",
Cmd: &Command{
Mounts: []SourceMount{{
Dest: "",
Spec: Source{},
}},
},
},
},
},
{
title: "has recursive build sources",
expectErr: true,
src: Source{
Build: &SourceBuild{
Source: Source{
Build: &SourceBuild{
DockerfilePath: "/other/nonempty/Dockerfile/path",
Source: Source{
Git: &SourceGit{},
},
},
},
DockerfilePath: "/nonempty/Dockerfile/path",
},
},
},
{
title: "has invalid build subsource",
expectErr: true,
src: Source{
Build: &SourceBuild{
Source: Source{
DockerImage: &SourceDockerImage{
Ref: "",
},
},
DockerfilePath: "/nonempty/Dockerfile/path",
},
},
},
{
title: "has multiple layers of recursion with an error at the bottom",
expectErr: true,
src: Source{
Build: &SourceBuild{
Source: Source{
DockerImage: &SourceDockerImage{
Ref: "nonempty:latest",
Cmd: &Command{
Mounts: []SourceMount{
{
Dest: "/nonempty",
Spec: Source{
DockerImage: &SourceDockerImage{
Ref: "",
},
},
},
},
},
},
},
DockerfilePath: "/nonempty/Dockerfile/path",
},
},
},
{
title: "has inline file and files set",
expectErr: true,
src: Source{
Inline: &SourceInline{
File: &SourceInlineFile{},
Dir: &SourceInlineDir{},
},
},
},
{
title: "has path separator in inline nested file name",
expectErr: true,
src: Source{
Inline: &SourceInline{
Dir: &SourceInlineDir{
Files: map[string]*SourceInlineFile{
"file/with/slash": {},
},
},
},
},
},
{
title: "inline dir has negative UID",
expectErr: true,
src: Source{
Inline: &SourceInline{
Dir: &SourceInlineDir{
UID: -1,
},
},
},
},
{
title: "inline dir has negative GID",
expectErr: true,
src: Source{
Inline: &SourceInline{
Dir: &SourceInlineDir{
GID: -1,
},
},
},
},
{
title: "inline file has negative UID",
expectErr: true,
src: Source{
Inline: &SourceInline{
File: &SourceInlineFile{
UID: -1,
},
},
},
},
{
title: "inline file has negative GID",
expectErr: true,
src: Source{
Inline: &SourceInline{
File: &SourceInlineFile{
GID: -1,
},
},
},
},
{
title: "inline file has path set",
expectErr: true,
src: Source{
Path: "subpath",
Inline: &SourceInline{
File: &SourceInlineFile{},
},
},
},
{
title: "has invalid genator config",
expectErr: true,
src: Source{
Inline: &SourceInline{
File: &SourceInlineFile{},
},
Generate: []*SourceGenerator{{}},
},
},
{
title: "has valid genator",
expectErr: false,
src: Source{
Inline: &SourceInline{
File: &SourceInlineFile{},
},
Generate: []*SourceGenerator{{Gomod: &GeneratorGomod{}}},
},
},
{
title: "docker images with cmd source must specify a path to extract",
expectErr: true,
src: Source{
Path: "",
DockerImage: &SourceDockerImage{
Ref: "notexists:latest",
Cmd: &Command{
Steps: []*BuildStep{
{Command: ":"},
},
},
},
},
},
{
title: "cmd souce mount dest must not be /",
expectErr: true,
src: Source{
Path: "/foo",
DockerImage: &SourceDockerImage{
Ref: "notexists:latest",
Cmd: &Command{
Steps: []*BuildStep{
{Command: ":"},
},
Mounts: []SourceMount{
{
Dest: "/",
Spec: Source{
Inline: &SourceInline{
File: &SourceInlineFile{},
},
},
},
},
},
},
},
},
{
title: "cmd source mount dest must not be a descendent of the extracted source path",
expectErr: true,
src: Source{
Path: "/foo",
DockerImage: &SourceDockerImage{
Ref: "notexists:latest",
Cmd: &Command{
Mounts: []SourceMount{
{
Dest: "/foo",
Spec: Source{
Inline: &SourceInline{
File: &SourceInlineFile{},
},
},
},
},
},
},
},
},
}
for _, tc := range cases {
tc := tc
title := fmt.Sprintf("source %s", tc.title)
t.Run(title, func(tt *testing.T) {
err := tc.src.validate()
if tc.expectErr && err != nil {
return
}
if err != nil {
tt.Fatal(err)
}
if tc.expectErr {
tt.Fatal("expected error, but received none")
}
})
}
}
func TestSourceFillDefaults(t *testing.T) {
cases := []struct {
title string
before Source
after Source
}{
{
title: "fills default context name when source type is context",
before: Source{
Context: &SourceContext{
Name: "",
},
Path: ".",
},
after: Source{
Context: &SourceContext{
Name: "context",
},
Path: ".",
},
},
{
title: "sets nested defaults when source type is docker image",
before: Source{
DockerImage: &SourceDockerImage{
Ref: "busybox:latest",
Cmd: &Command{
Dir: "/build",
Mounts: []SourceMount{
{
Dest: "/build/test",
Spec: Source{
Context: &SourceContext{
Name: "",
},
Path: ".",
},
},
},
},
},
Path: ".",
},
after: Source{
DockerImage: &SourceDockerImage{
Ref: "busybox:latest",
Cmd: &Command{
Dir: "/build",
Mounts: []SourceMount{
{
Dest: "/build/test",
Spec: Source{
Context: &SourceContext{
Name: "context",
},
Path: ".",
},
},
},
},
},
Path: ".",
},
},
}
for _, tc := range cases {
tc := tc
title := fmt.Sprintf("source %s", tc.title)
t.Run(title, func(t *testing.T) {
src := tc.before
expected := tc.after
if err := src.validate(); err != nil {
t.Fatal(err)
}
spec := &Spec{
Sources: map[string]Source{
"test": src,
},
}
spec.FillDefaults()
filledSrc := spec.Sources["test"]
if !reflect.DeepEqual(filledSrc, expected) {
s, err := json.MarshalIndent(&src, "", "\t")
if err != nil {
t.Fatal(err)
}
e, err := json.MarshalIndent(&expected, "", "\t")
if err != nil {
t.Fatal(err)
}
t.Fatalf("\nactual: %s\n-------------\nexpected: %s", string(s), string(e))
}
})
}
}
func TestSourceInlineUnmarshalling(t *testing.T) {
// NOTE: not using text template yaml for this test
// tabs seem to be illegal in yaml indentation
// yaml unmarshalling with strict mode doesn't produce a great error message.
spec, err := LoadSpec(sourceInlineTemplate)
if err != nil {
t.Fatal(err)
}
contents := "Hello world!"
for k, v := range spec.Sources {
t.Run(k, func(t *testing.T) {
if v.Inline.File != nil {
if v.Inline.File.Contents != contents {
t.Fatalf("expected %s, got %s", contents, v.Inline.File.Contents)
}
expected := os.FileMode(0o644)
if v.Inline.File.Permissions != expected {
t.Fatalf("expected %O, got %O", expected, v.Inline.File.Permissions)
}
}
if v.Inline.Dir != nil {
expected := os.FileMode(0o755)
if v.Inline.Dir.Permissions != expected {
t.Fatalf("expected %O, got %O", expected, v.Inline.Dir.Permissions)
}
}
})
}
}
func TestSourceNameWithPathSeparator(t *testing.T) {
spec := &Spec{
Sources: map[string]Source{
"forbidden/name": {
Inline: &SourceInline{
File: &SourceInlineFile{},
},
},
},
}
err := spec.Validate()
if err == nil {
t.Fatal("expected error, but received none")
}
var expected *InvalidSourceError
if !errors.As(err, &expected) {
t.Fatalf("expected %T, got %T", expected, err)
}
if expected.Name != "forbidden/name" {
t.Error("expected error to contain source name")
}
if !errors.Is(err, sourceNamePathSeparatorError) {
t.Errorf("expected error to be sourceNamePathSeparatorError, got: %v", err)
}
}
func TestUnmarshal(t *testing.T) {
t.Run("x-fields are stripped from spec", func(t *testing.T) {
dt := []byte(`
sources:
test:
inline:
file:
contents: "Hello world!"
x-some-field: "some value"
x-some-other-field: "some other value"
X-capitalized-other-field: "some other value capitalized X key"
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
src, ok := spec.Sources["test"]
if !ok {
t.Fatal("expected source to be present")
}
if src.Inline == nil {
t.Fatal("expected inline source to be present")
}
if src.Inline.File == nil {
t.Fatal("expected inline file to be present")
}
const xContents = "Hello world!"
if src.Inline.File.Contents != xContents {
t.Fatalf("expected %q, got %s", xContents, src.Inline.File.Contents)
}
})
t.Run("unknown fields cause parse error", func(t *testing.T) {
dt := []byte(`
sources:
test:
noSuchField: "some value"
`)
_, err := LoadSpec(dt)
if err == nil {
t.Fatal("expected error, but received none")
}
})
}
func TestSpec_SubstituteBuildArgs(t *testing.T) {
spec := &Spec{}
assert.NilError(t, spec.SubstituteArgs(nil))
env := map[string]string{}
assert.NilError(t, spec.SubstituteArgs(env))
// some values we'll be using throughout the test
const (
foo = "foo"
bar = "bar"
argWithDefault = "some default value"
plainOleValue = "some plain old value"
)
env["FOO"] = foo
err := spec.SubstituteArgs(env)
assert.ErrorIs(t, err, errUnknownArg, "args not defined in the spec should error out")
spec.Args = map[string]string{}
spec.Args["FOO"] = ""
assert.NilError(t, spec.SubstituteArgs(env))
pairs := map[string]string{
"FOO": "$FOO",
"BAR": "$BAR",
"WHATEVER": "$VAR_WITH_DEFAULT",
"REGULAR": plainOleValue,
}
spec.PackageConfig = &PackageConfig{
Signer: &PackageSigner{
Args: maps.Clone(pairs),
},
}
spec.Targets = map[string]Target{
"t1": {}, // nil signer
"t2": {
PackageConfig: &PackageConfig{
Signer: &PackageSigner{
Args: maps.Clone(pairs),
},
},
},
}
env["BAR"] = bar
assert.ErrorIs(t, err, errUnknownArg, "args not defined in the spec should error out")
spec.Args["BAR"] = ""
spec.Args["VAR_WITH_DEFAULT"] = argWithDefault
assert.NilError(t, spec.SubstituteArgs(env))
// Base package config
assert.Check(t, cmp.Equal(spec.PackageConfig.Signer.Args["FOO"], foo))
assert.Check(t, cmp.Equal(spec.PackageConfig.Signer.Args["BAR"], bar))
assert.Check(t, cmp.Equal(spec.PackageConfig.Signer.Args["WHATEVER"], argWithDefault))
assert.Check(t, cmp.Equal(spec.PackageConfig.Signer.Args["REGULAR"], plainOleValue))
// targets
assert.Check(t, cmp.Nil(spec.Targets["t1"].Frontend))
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["BAR"], bar))
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["WHATEVER"], argWithDefault))
assert.Check(t, cmp.Equal(spec.Targets["t2"].PackageConfig.Signer.Args["REGULAR"], plainOleValue))
}
func TestBuildArgSubst(t *testing.T) {
t.Run("value provided", func(t *testing.T) {
dt := []byte(`
args:
test:
build:
steps:
- command: echo $TEST
env:
TEST: ${test}
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
err = spec.SubstituteArgs(map[string]string{
"test": "test",
})
assert.NilError(t, err)
assert.Equal(t, spec.Build.Steps[0].Env["TEST"], "test")
})
t.Run("default value", func(t *testing.T) {
dt := []byte(`
args:
test: "test"
build:
steps:
- command: echo $TEST
env:
TEST: ${test}
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
err = spec.SubstituteArgs(map[string]string{})
assert.NilError(t, err)
assert.Equal(t, spec.Build.Steps[0].Env["TEST"], "test")
})
t.Run("build arg undeclared", func(t *testing.T) {
dt := []byte(`
args:
build:
steps:
- command: echo $TEST
env:
TEST: ${test}
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
err = spec.SubstituteArgs(map[string]string{})
assert.ErrorContains(t, err, `error performing shell expansion on build step 0: error performing shell expansion on env var "TEST" for step 0: build arg "test" not declared`)
})
t.Run("multiple undefined build args", func(t *testing.T) {
dt := []byte(`
args:
sources:
test1:
git:
url: phony.git
commit: ${COMMIT1}
test2:
http:
url: ${URL1}
build:
steps:
- command: echo ${COMMIT1}
env:
TEST: ${COMMIT1}
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
err = spec.SubstituteArgs(map[string]string{})
// all occurrences of undefined build args should be reported
assert.ErrorContains(t, err, `error performing shell expansion on source "test1": build arg "COMMIT1" not declared`)
assert.ErrorContains(t, err, `error performing shell expansion on source "test2": build arg "URL1" not declared`)
assert.ErrorContains(t, err, `error performing shell expansion on build step 0: error performing shell expansion on env var "TEST" for step 0: build arg "COMMIT1" not declared`)
})
t.Run("builtin build arg", func(t *testing.T) {
dt := []byte(`
args:
build:
steps:
- command: echo '$OS'
env:
OS: ${TARGETOS}
`)
spec, err := LoadSpec(dt)
if err != nil {
t.Fatal(err)
}
err = spec.SubstituteArgs(map[string]string{})
assert.ErrorContains(t, err,
`opt-in arg "TARGETOS" not present in args`)
})
}
func Test_validatePatch(t *testing.T) {
type testCase struct {
name string
patchSrc Source
subpath bool
}
// Create a test case for each source type.
// For each type we need to specify if it should have a subpath or not.
cases := []testCase{
{
name: "ineline file",
patchSrc: Source{Inline: &SourceInline{File: &SourceInlineFile{}}},
subpath: false,
},
{
name: "inline dir",
patchSrc: Source{Inline: &SourceInline{Dir: &SourceInlineDir{}}},
subpath: true,
},
{
name: "git",
patchSrc: Source{Git: &SourceGit{}},
subpath: true,
},
{
name: "image",
patchSrc: Source{DockerImage: &SourceDockerImage{}},
subpath: true,
},
{
name: "HTTP",
patchSrc: Source{HTTP: &SourceHTTP{}},
subpath: false,
},
{
name: "context",
patchSrc: Source{Context: &SourceContext{}},
subpath: true,
},
{
name: "build",
patchSrc: Source{Build: &SourceBuild{}},
subpath: true,
},
}
// For each case generate 2 tests: 1 with a subpath and 1 without
// Use the subpath field in the test case to determine if the validation
// should return an error.
//
// If subpath is false in the testcase but the test is passing in a subpath then
// an error is expected.
// Likewise when subpath is true but no subpath is given.
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Run("subpath=true", func(t *testing.T) {
ps := PatchSpec{Path: "/test"}
err := validatePatch(ps, tc.patchSrc)
if tc.subpath {
assert.NilError(t, err)
return
}
assert.ErrorIs(t, err, errPatchFileNoSubpath)
})
t.Run("subpath=false", func(t *testing.T) {
ps := PatchSpec{}
err := validatePatch(ps, tc.patchSrc)
if tc.subpath {
assert.ErrorIs(t, err, errPatchRequiresSubpath)
return
}
assert.NilError(t, err)
})
})
}
}