-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_cli_build_test.go
5598 lines (4885 loc) · 143 KB
/
docker_cli_build_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
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 main
import (
"archive/tar"
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"text/template"
"time"
"github.com/docker/docker/builder/command"
"github.com/docker/docker/pkg/archive"
)
func TestBuildJSONEmptyRun(t *testing.T) {
name := "testbuildjsonemptyrun"
defer deleteImages(name)
_, err := buildImage(
name,
`
FROM busybox
RUN []
`,
true)
if err != nil {
t.Fatal("error when dealing with a RUN statement with empty JSON array")
}
logDone("build - RUN with an empty array should not panic")
}
func TestBuildEmptyWhitespace(t *testing.T) {
name := "testbuildemptywhitespace"
defer deleteImages(name)
_, err := buildImage(
name,
`
FROM busybox
COPY
quux \
bar
`,
true)
if err == nil {
t.Fatal("no error when dealing with a COPY statement with no content on the same line")
}
logDone("build - statements with whitespace and no content should generate a parse error")
}
func TestBuildShCmdJSONEntrypoint(t *testing.T) {
name := "testbuildshcmdjsonentrypoint"
defer deleteImages(name)
_, err := buildImage(
name,
`
FROM busybox
ENTRYPOINT ["/bin/echo"]
CMD echo test
`,
true)
if err != nil {
t.Fatal(err)
}
out, _, err := runCommandWithOutput(
exec.Command(
dockerBinary,
"run",
"--rm",
name))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "/bin/sh -c echo test" {
t.Fatal("CMD did not contain /bin/sh -c")
}
logDone("build - CMD should always contain /bin/sh -c when specified without JSON")
}
func TestBuildEnvironmentReplacementUser(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
_, err := buildImage(name, `
FROM scratch
ENV user foo
USER ${user}
`, true)
if err != nil {
t.Fatal(err)
}
res, err := inspectFieldJSON(name, "Config.User")
if err != nil {
t.Fatal(err)
}
if res != `"foo"` {
t.Fatal("User foo from environment not in Config.User on image")
}
logDone("build - user environment replacement")
}
func TestBuildEnvironmentReplacementVolume(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
_, err := buildImage(name, `
FROM scratch
ENV volume /quux
VOLUME ${volume}
`, true)
if err != nil {
t.Fatal(err)
}
res, err := inspectFieldJSON(name, "Config.Volumes")
if err != nil {
t.Fatal(err)
}
var volumes map[string]interface{}
if err := json.Unmarshal([]byte(res), &volumes); err != nil {
t.Fatal(err)
}
if _, ok := volumes["/quux"]; !ok {
t.Fatal("Volume /quux from environment not in Config.Volumes on image")
}
logDone("build - volume environment replacement")
}
func TestBuildEnvironmentReplacementExpose(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
_, err := buildImage(name, `
FROM scratch
ENV port 80
EXPOSE ${port}
`, true)
if err != nil {
t.Fatal(err)
}
res, err := inspectFieldJSON(name, "Config.ExposedPorts")
if err != nil {
t.Fatal(err)
}
var exposedPorts map[string]interface{}
if err := json.Unmarshal([]byte(res), &exposedPorts); err != nil {
t.Fatal(err)
}
if _, ok := exposedPorts["80/tcp"]; !ok {
t.Fatal("Exposed port 80 from environment not in Config.ExposedPorts on image")
}
logDone("build - expose environment replacement")
}
func TestBuildEnvironmentReplacementWorkdir(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
_, err := buildImage(name, `
FROM busybox
ENV MYWORKDIR /work
RUN mkdir ${MYWORKDIR}
WORKDIR ${MYWORKDIR}
`, true)
if err != nil {
t.Fatal(err)
}
logDone("build - workdir environment replacement")
}
func TestBuildEnvironmentReplacementAddCopy(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
ctx, err := fakeContext(`
FROM scratch
ENV baz foo
ENV quux bar
ENV dot .
ADD ${baz} ${dot}
COPY ${quux} ${dot}
`,
map[string]string{
"foo": "test1",
"bar": "test2",
})
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
logDone("build - add/copy environment replacement")
}
func TestBuildEnvironmentReplacementEnv(t *testing.T) {
name := "testbuildenvironmentreplacement"
defer deleteImages(name)
_, err := buildImage(name,
`
FROM busybox
ENV foo zzz
ENV bar ${foo}
ENV abc1='$foo'
ENV env1=$foo env2=${foo} env3="$foo" env4="${foo}"
RUN [ "$abc1" = '$foo' ] && (echo "$abc1" | grep -q foo)
ENV abc2="\$foo"
RUN [ "$abc2" = '$foo' ] && (echo "$abc2" | grep -q foo)
ENV abc3 '$foo'
RUN [ "$abc3" = '$foo' ] && (echo "$abc3" | grep -q foo)
ENV abc4 "\$foo"
RUN [ "$abc4" = '$foo' ] && (echo "$abc4" | grep -q foo)
`, true)
if err != nil {
t.Fatal(err)
}
res, err := inspectFieldJSON(name, "Config.Env")
if err != nil {
t.Fatal(err)
}
envResult := []string{}
if err = unmarshalJSON([]byte(res), &envResult); err != nil {
t.Fatal(err)
}
found := false
envCount := 0
for _, env := range envResult {
parts := strings.SplitN(env, "=", 2)
if parts[0] == "bar" {
found = true
if parts[1] != "zzz" {
t.Fatalf("Could not find replaced var for env `bar`: got %q instead of `zzz`", parts[1])
}
} else if strings.HasPrefix(parts[0], "env") {
envCount++
if parts[1] != "zzz" {
t.Fatalf("%s should be 'foo' but instead its %q", parts[0], parts[1])
}
}
}
if !found {
t.Fatal("Never found the `bar` env variable")
}
if envCount != 4 {
t.Fatalf("Didn't find all env vars - only saw %d\n%s", envCount, envResult)
}
logDone("build - env environment replacement")
}
func TestBuildHandleEscapes(t *testing.T) {
name := "testbuildhandleescapes"
defer deleteImages(name)
_, err := buildImage(name,
`
FROM scratch
ENV FOO bar
VOLUME ${FOO}
`, true)
if err != nil {
t.Fatal(err)
}
var result map[string]map[string]struct{}
res, err := inspectFieldJSON(name, "Config.Volumes")
if err != nil {
t.Fatal(err)
}
if err = unmarshalJSON([]byte(res), &result); err != nil {
t.Fatal(err)
}
if _, ok := result["bar"]; !ok {
t.Fatal("Could not find volume bar set from env foo in volumes table")
}
deleteImages(name)
_, err = buildImage(name,
`
FROM scratch
ENV FOO bar
VOLUME \${FOO}
`, true)
if err != nil {
t.Fatal(err)
}
res, err = inspectFieldJSON(name, "Config.Volumes")
if err != nil {
t.Fatal(err)
}
if err = unmarshalJSON([]byte(res), &result); err != nil {
t.Fatal(err)
}
if _, ok := result["${FOO}"]; !ok {
t.Fatal("Could not find volume ${FOO} set from env foo in volumes table")
}
deleteImages(name)
// this test in particular provides *7* backslashes and expects 6 to come back.
// Like above, the first escape is swallowed and the rest are treated as
// literals, this one is just less obvious because of all the character noise.
_, err = buildImage(name,
`
FROM scratch
ENV FOO bar
VOLUME \\\\\\\${FOO}
`, true)
if err != nil {
t.Fatal(err)
}
res, err = inspectFieldJSON(name, "Config.Volumes")
if err != nil {
t.Fatal(err)
}
if err = unmarshalJSON([]byte(res), &result); err != nil {
t.Fatal(err)
}
if _, ok := result[`\\\${FOO}`]; !ok {
t.Fatal(`Could not find volume \\\${FOO} set from env foo in volumes table`, result)
}
logDone("build - handle escapes")
}
func TestBuildOnBuildLowercase(t *testing.T) {
name := "testbuildonbuildlowercase"
name2 := "testbuildonbuildlowercase2"
defer deleteImages(name, name2)
_, err := buildImage(name,
`
FROM busybox
onbuild run echo quux
`, true)
if err != nil {
t.Fatal(err)
}
_, out, err := buildImageWithOut(name2, fmt.Sprintf(`
FROM %s
`, name), true)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(out, "quux") {
t.Fatalf("Did not receive the expected echo text, got %s", out)
}
if strings.Contains(out, "ONBUILD ONBUILD") {
t.Fatalf("Got an ONBUILD ONBUILD error with no error: got %s", out)
}
logDone("build - handle case-insensitive onbuild statement")
}
func TestBuildEnvEscapes(t *testing.T) {
name := "testbuildenvescapes"
defer deleteImages(name)
defer deleteAllContainers()
_, err := buildImage(name,
`
FROM busybox
ENV TEST foo
CMD echo \$
`,
true)
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-t", name))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "$" {
t.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
}
logDone("build - env should handle \\$ properly")
}
func TestBuildEnvOverwrite(t *testing.T) {
name := "testbuildenvoverwrite"
defer deleteImages(name)
defer deleteAllContainers()
_, err := buildImage(name,
`
FROM busybox
ENV TEST foo
CMD echo ${TEST}
`,
true)
if err != nil {
t.Fatal(err)
}
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-e", "TEST=bar", "-t", name))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(out) != "bar" {
t.Fatalf("Env TEST was not overwritten with bar when foo was supplied to dockerfile: was %q", strings.TrimSpace(out))
}
logDone("build - env should overwrite builder ENV during run")
}
func TestBuildOnBuildForbiddenMaintainerInSourceImage(t *testing.T) {
name := "testbuildonbuildforbiddenmaintainerinsourceimage"
defer deleteImages("onbuild")
defer deleteImages(name)
defer deleteAllContainers()
createCmd := exec.Command(dockerBinary, "create", "busybox", "true")
out, _, _, err := runCommandWithStdoutStderr(createCmd)
if err != nil {
t.Fatal(out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
commitCmd := exec.Command(dockerBinary, "commit", "--run", "{\"OnBuild\":[\"MAINTAINER docker.io\"]}", cleanedContainerID, "onbuild")
if _, err := runCommand(commitCmd); err != nil {
t.Fatal(err)
}
_, err = buildImage(name,
`FROM onbuild`,
true)
if err != nil {
if !strings.Contains(err.Error(), "maintainer isn't allowed as an ONBUILD trigger") {
t.Fatalf("Wrong error %v, must be about MAINTAINER and ONBUILD in source image", err)
}
} else {
t.Fatal("Error must not be nil")
}
logDone("build - onbuild forbidden maintainer in source image")
}
func TestBuildOnBuildForbiddenFromInSourceImage(t *testing.T) {
name := "testbuildonbuildforbiddenfrominsourceimage"
defer deleteImages("onbuild")
defer deleteImages(name)
defer deleteAllContainers()
createCmd := exec.Command(dockerBinary, "create", "busybox", "true")
out, _, _, err := runCommandWithStdoutStderr(createCmd)
if err != nil {
t.Fatal(out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
commitCmd := exec.Command(dockerBinary, "commit", "--run", "{\"OnBuild\":[\"FROM busybox\"]}", cleanedContainerID, "onbuild")
if _, err := runCommand(commitCmd); err != nil {
t.Fatal(err)
}
_, err = buildImage(name,
`FROM onbuild`,
true)
if err != nil {
if !strings.Contains(err.Error(), "from isn't allowed as an ONBUILD trigger") {
t.Fatalf("Wrong error %v, must be about FROM and ONBUILD in source image", err)
}
} else {
t.Fatal("Error must not be nil")
}
logDone("build - onbuild forbidden from in source image")
}
func TestBuildOnBuildForbiddenChainedInSourceImage(t *testing.T) {
name := "testbuildonbuildforbiddenchainedinsourceimage"
defer deleteImages("onbuild")
defer deleteImages(name)
defer deleteAllContainers()
createCmd := exec.Command(dockerBinary, "create", "busybox", "true")
out, _, _, err := runCommandWithStdoutStderr(createCmd)
if err != nil {
t.Fatal(out, err)
}
cleanedContainerID := stripTrailingCharacters(out)
commitCmd := exec.Command(dockerBinary, "commit", "--run", "{\"OnBuild\":[\"ONBUILD RUN ls\"]}", cleanedContainerID, "onbuild")
if _, err := runCommand(commitCmd); err != nil {
t.Fatal(err)
}
_, err = buildImage(name,
`FROM onbuild`,
true)
if err != nil {
if !strings.Contains(err.Error(), "Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed") {
t.Fatalf("Wrong error %v, must be about chaining ONBUILD in source image", err)
}
} else {
t.Fatal("Error must not be nil")
}
logDone("build - onbuild forbidden chained in source image")
}
func TestBuildOnBuildCmdEntrypointJSON(t *testing.T) {
name1 := "onbuildcmd"
name2 := "onbuildgenerated"
defer deleteImages(name2)
defer deleteImages(name1)
defer deleteAllContainers()
_, err := buildImage(name1, `
FROM busybox
ONBUILD CMD ["hello world"]
ONBUILD ENTRYPOINT ["echo"]
ONBUILD RUN ["true"]`,
false)
if err != nil {
t.Fatal(err)
}
_, err = buildImage(name2, fmt.Sprintf(`FROM %s`, name1), false)
if err != nil {
t.Fatal(err)
}
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-t", name2))
if err != nil {
t.Fatal(err)
}
if !regexp.MustCompile(`(?m)^hello world`).MatchString(out) {
t.Fatal("did not get echo output from onbuild", out)
}
logDone("build - onbuild with json entrypoint/cmd")
}
func TestBuildOnBuildEntrypointJSON(t *testing.T) {
name1 := "onbuildcmd"
name2 := "onbuildgenerated"
defer deleteImages(name2)
defer deleteImages(name1)
defer deleteAllContainers()
_, err := buildImage(name1, `
FROM busybox
ONBUILD ENTRYPOINT ["echo"]`,
false)
if err != nil {
t.Fatal(err)
}
_, err = buildImage(name2, fmt.Sprintf("FROM %s\nCMD [\"hello world\"]\n", name1), false)
if err != nil {
t.Fatal(err)
}
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-t", name2))
if err != nil {
t.Fatal(err)
}
if !regexp.MustCompile(`(?m)^hello world`).MatchString(out) {
t.Fatal("got malformed output from onbuild", out)
}
logDone("build - onbuild with json entrypoint")
}
func TestBuildCacheADD(t *testing.T) {
name := "testbuildtwoimageswithadd"
defer deleteImages(name)
server, err := fakeStorage(map[string]string{
"robots.txt": "hello",
"index.html": "world",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
if _, err := buildImage(name,
fmt.Sprintf(`FROM scratch
ADD %s/robots.txt /`, server.URL()),
true); err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
deleteImages(name)
_, out, err := buildImageWithOut(name,
fmt.Sprintf(`FROM scratch
ADD %s/index.html /`, server.URL()),
true)
if err != nil {
t.Fatal(err)
}
if strings.Contains(out, "Using cache") {
t.Fatal("2nd build used cache on ADD, it shouldn't")
}
logDone("build - build two images with remote ADD")
}
func TestBuildLastModified(t *testing.T) {
name := "testbuildlastmodified"
defer deleteImages(name)
server, err := fakeStorage(map[string]string{
"file": "hello",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
var out, out2 string
dFmt := `FROM busybox
ADD %s/file /
RUN ls -le /file`
dockerfile := fmt.Sprintf(dFmt, server.URL())
if _, out, err = buildImageWithOut(name, dockerfile, false); err != nil {
t.Fatal(err)
}
originMTime := regexp.MustCompile(`root.*/file.*\n`).FindString(out)
// Make sure our regexp is correct
if strings.Index(originMTime, "/file") < 0 {
t.Fatalf("Missing ls info on 'file':\n%s", out)
}
// Build it again and make sure the mtime of the file didn't change.
// Wait a few seconds to make sure the time changed enough to notice
time.Sleep(2 * time.Second)
if _, out2, err = buildImageWithOut(name, dockerfile, false); err != nil {
t.Fatal(err)
}
newMTime := regexp.MustCompile(`root.*/file.*\n`).FindString(out2)
if newMTime != originMTime {
t.Fatalf("MTime changed:\nOrigin:%s\nNew:%s", originMTime, newMTime)
}
// Now 'touch' the file and make sure the timestamp DID change this time
// Create a new fakeStorage instead of just using Add() to help windows
server, err = fakeStorage(map[string]string{
"file": "hello",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
dockerfile = fmt.Sprintf(dFmt, server.URL())
if _, out2, err = buildImageWithOut(name, dockerfile, false); err != nil {
t.Fatal(err)
}
newMTime = regexp.MustCompile(`root.*/file.*\n`).FindString(out2)
if newMTime == originMTime {
t.Fatalf("MTime didn't change:\nOrigin:%s\nNew:%s", originMTime, newMTime)
}
logDone("build - use Last-Modified header")
}
func TestBuildSixtySteps(t *testing.T) {
name := "foobuildsixtysteps"
defer deleteImages(name)
ctx, err := fakeContext("FROM scratch\n"+strings.Repeat("ADD foo /\n", 60),
map[string]string{
"foo": "test1",
})
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
logDone("build - build an image with sixty build steps")
}
func TestBuildAddSingleFileToRoot(t *testing.T) {
name := "testaddimg"
defer deleteImages(name)
ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
RUN echo 'dockerio:x:1001:' >> /etc/group
RUN touch /exists
RUN chown dockerio.dockerio /exists
ADD test_file /
RUN [ $(ls -l /test_file | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /test_file | awk '{print $1}') = '%s' ]
RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`, expectedFileChmod),
map[string]string{
"test_file": "test1",
})
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
logDone("build - add single file to root")
}
// Issue #3960: "ADD src ." hangs
func TestBuildAddSingleFileToWorkdir(t *testing.T) {
name := "testaddsinglefiletoworkdir"
defer deleteImages(name)
ctx, err := fakeContext(`FROM busybox
ADD test_file .`,
map[string]string{
"test_file": "test1",
})
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
done := make(chan struct{})
go func() {
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
close(done)
}()
select {
case <-time.After(5 * time.Second):
t.Fatal("Build with adding to workdir timed out")
case <-done:
}
logDone("build - add single file to workdir")
}
func TestBuildAddSingleFileToExistDir(t *testing.T) {
name := "testaddsinglefiletoexistdir"
defer deleteImages(name)
ctx, err := fakeContext(`FROM busybox
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
RUN echo 'dockerio:x:1001:' >> /etc/group
RUN mkdir /exists
RUN touch /exists/exists_file
RUN chown -R dockerio.dockerio /exists
ADD test_file /exists/
RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
map[string]string{
"test_file": "test1",
})
if err != nil {
t.Fatal(err)
}
defer ctx.Close()
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
logDone("build - add single file to existing dir")
}
func TestBuildCopyAddMultipleFiles(t *testing.T) {
server, err := fakeStorage(map[string]string{
"robots.txt": "hello",
})
if err != nil {
t.Fatal(err)
}
defer server.Close()
name := "testcopymultiplefilestofile"
defer deleteImages(name)
ctx, err := fakeContext(fmt.Sprintf(`FROM busybox
RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
RUN echo 'dockerio:x:1001:' >> /etc/group
RUN mkdir /exists
RUN touch /exists/exists_file
RUN chown -R dockerio.dockerio /exists
COPY test_file1 test_file2 /exists/
ADD test_file3 test_file4 %s/robots.txt /exists/
RUN [ $(ls -l / | grep exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
RUN [ $(ls -l /exists/test_file1 | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/test_file2 | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/test_file3 | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/test_file4 | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/robots.txt | awk '{print $3":"$4}') = 'root:root' ]
RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio' ]
`, server.URL()),
map[string]string{
"test_file1": "test1",
"test_file2": "test2",
"test_file3": "test3",
"test_file4": "test4",
})
defer ctx.Close()
if err != nil {
t.Fatal(err)
}
if _, err := buildImageFromContext(name, ctx, true); err != nil {
t.Fatal(err)
}
logDone("build - multiple file copy/add tests")
}
func TestBuildAddMultipleFilesToFile(t *testing.T) {
name := "testaddmultiplefilestofile"
defer deleteImages(name)
ctx, err := fakeContext(`FROM scratch
ADD file1.txt file2.txt test
`,
map[string]string{
"file1.txt": "test1",
"file2.txt": "test1",
})
defer ctx.Close()
if err != nil {
t.Fatal(err)
}
expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Wrong error: (should contain %q) got:\n%v", expected, err)
}
logDone("build - multiple add files to file")
}
func TestBuildJSONAddMultipleFilesToFile(t *testing.T) {
name := "testjsonaddmultiplefilestofile"
defer deleteImages(name)
ctx, err := fakeContext(`FROM scratch
ADD ["file1.txt", "file2.txt", "test"]
`,
map[string]string{
"file1.txt": "test1",
"file2.txt": "test1",
})
defer ctx.Close()
if err != nil {
t.Fatal(err)
}
expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Wrong error: (should contain %q) got:\n%v", expected, err)
}
logDone("build - multiple add files to file json syntax")
}
func TestBuildAddMultipleFilesToFileWild(t *testing.T) {
name := "testaddmultiplefilestofilewild"
defer deleteImages(name)
ctx, err := fakeContext(`FROM scratch
ADD file*.txt test
`,
map[string]string{
"file1.txt": "test1",
"file2.txt": "test1",
})
defer ctx.Close()
if err != nil {
t.Fatal(err)
}
expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Wrong error: (should contain %q) got:\n%v", expected, err)
}
logDone("build - multiple add files to file wild")
}
func TestBuildJSONAddMultipleFilesToFileWild(t *testing.T) {
name := "testjsonaddmultiplefilestofilewild"
defer deleteImages(name)
ctx, err := fakeContext(`FROM scratch
ADD ["file*.txt", "test"]
`,
map[string]string{
"file1.txt": "test1",
"file2.txt": "test1",
})
defer ctx.Close()
if err != nil {
t.Fatal(err)
}
expected := "When using ADD with more than one source file, the destination must be a directory and end with a /"
if _, err := buildImageFromContext(name, ctx, true); err == nil || !strings.Contains(err.Error(), expected) {
t.Fatalf("Wrong error: (should contain %q) got:\n%v", expected, err)
}
logDone("build - multiple add files to file wild json syntax")
}
func TestBuildCopyMultipleFilesToFile(t *testing.T) {
name := "testcopymultiplefilestofile"
defer deleteImages(name)
ctx, err := fakeContext(`FROM scratch