-
Notifications
You must be signed in to change notification settings - Fork 922
Expand file tree
/
Copy pathbud.bats
More file actions
10762 lines (9059 loc) · 426 KB
/
Copy pathbud.bats
File metadata and controls
10762 lines (9059 loc) · 426 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
#!/usr/bin/env bats
load helpers
@test "bud with a path to a Dockerfile (-f) containing a non-directory entry" {
run_buildah 125 build -f $BUDFILES/non-directory-in-path/non-directory/Dockerfile
expect_output --substring "non-directory/Dockerfile: not a directory"
}
@test "bud stdio is usable pipes" {
_prefetch alpine
run_buildah build $BUDFILES/stdio
}
@test "bud: build manifest list and --add-compression zstd" {
start_registry
run_buildah login --tls-verify=false --authfile ${TEST_SCRATCH_DIR}/test.auth --username testuser --password testpassword localhost:${REGISTRY_PORT}
imgname="img-$(safename)"
run_buildah build $WITH_POLICY_JSON -t "${imgname}1" --platform linux/amd64 -f $BUDFILES/dockerfile/Dockerfile
run_buildah build $WITH_POLICY_JSON -t "${imgname}2" --platform linux/arm64 -f $BUDFILES/dockerfile/Dockerfile
run_buildah manifest create foo
run_buildah manifest add foo "${imgname}1"
run_buildah manifest add foo "${imgname}2"
run_buildah manifest push $WITH_POLICY_JSON --authfile ${TEST_SCRATCH_DIR}/test.auth --all --add-compression zstd --tls-verify=false foo docker://localhost:${REGISTRY_PORT}/list
run_buildah manifest inspect --authfile ${TEST_SCRATCH_DIR}/test.auth --tls-verify=false localhost:${REGISTRY_PORT}/list
list="$output"
validate_instance_compression "0" "$list" "amd64" "gzip"
validate_instance_compression "1" "$list" "arm64" "gzip"
validate_instance_compression "2" "$list" "amd64" "zstd"
validate_instance_compression "3" "$list" "arm64" "zstd"
}
@test "bud: build manifest list and --add-compression with containers.conf" {
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/Dockerfile1 << _EOF
FROM alpine
_EOF
cat > $contextdir/containers.conf << _EOF
[engine]
add_compression = ["zstd"]
_EOF
start_registry
run_buildah login --tls-verify=false --authfile ${TEST_SCRATCH_DIR}/test.auth --username testuser --password testpassword localhost:${REGISTRY_PORT}
imgname="img-$(safename)"
run_buildah build $WITH_POLICY_JSON -t "${imgname}1" --platform linux/amd64 -f $contextdir/Dockerfile1
run_buildah build $WITH_POLICY_JSON -t "${imgname}2" --platform linux/arm64 -f $contextdir/Dockerfile1
run_buildah manifest create foo
run_buildah manifest add foo "${imgname}1"
run_buildah manifest add foo "${imgname}2"
CONTAINERS_CONF=$contextdir/containers.conf run_buildah manifest push $WITH_POLICY_JSON --authfile ${TEST_SCRATCH_DIR}/test.auth --all --tls-verify=false foo docker://localhost:${REGISTRY_PORT}/list
run_buildah manifest inspect --authfile ${TEST_SCRATCH_DIR}/test.auth --tls-verify=false localhost:${REGISTRY_PORT}/list
list="$output"
validate_instance_compression "0" "$list" "amd64" "gzip"
validate_instance_compression "1" "$list" "arm64" "gzip"
validate_instance_compression "2" "$list" "amd64" "zstd"
validate_instance_compression "3" "$list" "arm64" "zstd"
}
@test "bud: build manifest list with --add-compression zstd, --compression and --force-compression" {
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/Dockerfile1 << _EOF
FROM alpine
_EOF
start_registry
run_buildah login --tls-verify=false --authfile ${TEST_SCRATCH_DIR}/test.auth --username testuser --password testpassword localhost:${REGISTRY_PORT}
imgname="img-$(safename)"
run_buildah build $WITH_POLICY_JSON -t "${imgname}1" --platform linux/amd64 -f $contextdir/Dockerfile1
run_buildah build $WITH_POLICY_JSON -t "${imgname}2" --platform linux/arm64 -f $contextdir/Dockerfile1
run_buildah manifest create foo
run_buildah manifest add foo "${imgname}1"
run_buildah manifest add foo "${imgname}2"
run_buildah manifest push $WITH_POLICY_JSON --authfile ${TEST_SCRATCH_DIR}/test.auth --all --add-compression zstd --tls-verify=false foo docker://localhost:${REGISTRY_PORT}/list
run_buildah manifest inspect --authfile ${TEST_SCRATCH_DIR}/test.auth --tls-verify=false localhost:${REGISTRY_PORT}/list
list="$output"
validate_instance_compression "0" "$list" "amd64" "gzip"
validate_instance_compression "1" "$list" "arm64" "gzip"
validate_instance_compression "2" "$list" "amd64" "zstd"
validate_instance_compression "3" "$list" "arm64" "zstd"
# Pushing again should keep every thing intact if original compression is `gzip` and `--force-compression` is specified
run_buildah manifest push $WITH_POLICY_JSON --authfile ${TEST_SCRATCH_DIR}/test.auth --all --add-compression zstd --compression-format gzip --force-compression --tls-verify=false foo docker://localhost:${REGISTRY_PORT}/list
run_buildah manifest inspect --authfile ${TEST_SCRATCH_DIR}/test.auth --tls-verify=false localhost:${REGISTRY_PORT}/list
list="$output"
validate_instance_compression "0" "$list" "amd64" "gzip"
validate_instance_compression "1" "$list" "arm64" "gzip"
validate_instance_compression "2" "$list" "amd64" "zstd"
validate_instance_compression "3" "$list" "arm64" "zstd"
# Pushing again without --force-compression but with --compression-format should do the same thing
run_buildah manifest push $WITH_POLICY_JSON --authfile ${TEST_SCRATCH_DIR}/test.auth --all --add-compression zstd --compression-format gzip --tls-verify=false foo docker://localhost:${REGISTRY_PORT}/list
run_buildah manifest inspect --authfile ${TEST_SCRATCH_DIR}/test.auth --tls-verify=false localhost:${REGISTRY_PORT}/list
list="$output"
validate_instance_compression "0" "$list" "amd64" "gzip"
validate_instance_compression "1" "$list" "arm64" "gzip"
validate_instance_compression "2" "$list" "amd64" "zstd"
validate_instance_compression "3" "$list" "arm64" "zstd"
}
@test "Multi-stage should not remove used base-image without --layers" {
run_buildah build -t parent-one -f $BUDFILES/multi-stage-only-base/Containerfile1
run_buildah build -t parent-two -f $BUDFILES/multi-stage-only-base/Containerfile2
run_buildah build -t multi-stage -f $BUDFILES/multi-stage-only-base/Containerfile3
run_buildah images -a
expect_output --substring "parent-one" "parent one must not be removed"
}
@test "no layer should be created on scratch" {
imgname="img-$(safename)"
run_buildah build --layers --label "label1=value1" -t $imgname -f $BUDFILES/from-scratch/Containerfile
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' $imgname
expect_output "0" "layer should not exist"
run_buildah build --layers -t $imgname -f $BUDFILES/from-scratch/Containerfile
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' $imgname
expect_output "0" "layer should not exist"
}
@test "no empty layer for metadata-only instructions without --layers" {
_prefetch alpine
imgname="img-$(safename)"
# Get the base image layer count
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' alpine
base_layers="$output"
# Build without --layers (single-layer mode) with only metadata instructions
run_buildah build --layers=false -t $imgname -f $BUDFILES/metadata-only/Containerfile
run_buildah inspect -f '{{len .Docker.RootFS.DiffIDs}}' $imgname
expect_output "$base_layers" "metadata-only instructions should not add a layer"
}
@test "bud and test --inherit-annotations" {
base=quay.io/libpod/testimage:20241011
_prefetch $base
target=exp
run_buildah --version
local -a output_fields=($output)
buildah_version=${output_fields[2]}
buildah inspect --format '{{ .ImageAnnotations }}' $base
not_want_output='map[]'
assert "$output" != "$not_want_output" "expected some annotations to be set in base image $base"
## Since we are inheriting annotations, built image should contain some default annotations
run_buildah build $WITH_POLICY_JSON --inherit-annotations=true -t $target --from $base $BUDFILES/base-with-labels
not_want_output='map[]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
assert "$output" != "$not_want_output" "expected some annotations to be set in base image $base"
## Since we are inheriting no annotations, built image should not contain any annotations.
run_buildah build $WITH_POLICY_JSON --inherit-annotations=false --created-annotation=false -t $target --from $base $BUDFILES/base-with-labels
# no annotations should be inherited from base image
want_output='map[]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
## Build again but set a new annotation and don't inherit annotations from base image
run_buildah build $WITH_POLICY_JSON --inherit-annotations=false --created-annotation=false --annotation hello=world -t $target --from $base $BUDFILES/base-with-labels
# no annotations should be inherited from base image
want_output='map["hello":"world"]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
## Try similar thing with another Containerfile
run_buildah build $WITH_POLICY_JSON --inherit-annotations=false --created-annotation=false -t $target -f $BUDFILES/base-with-labels/Containerfile2
# no annotations should be inherited from base image
want_output='map[]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
run_buildah build $WITH_POLICY_JSON --inherit-annotations=false --created-annotation=false --annotation hello=world -t $target -f $BUDFILES/base-with-labels/Containerfile2
# no annotations should be inherited from base image
want_output='map["hello":"world"]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
}
@test "bud and test --inherit-annotations with --layers" {
base=quay.io/libpod/testimage:20241011
_prefetch $base
target=exp
run_buildah --version
local -a output_fields=($output)
buildah_version=${output_fields[2]}
buildah inspect --format '{{ .ImageAnnotations }}' $base
not_want_output='map[]'
assert "$output" != "$not_want_output" "expected some annotations to be set in base image $base"
## Build without removing annotations
run_buildah build $WITH_POLICY_JSON --layers --iidfile ${TEST_SCRATCH_DIR}/iid1 -t $target --from $base $BUDFILES/base-with-labels
## Second build must use cache
run_buildah build $WITH_POLICY_JSON --layers --iidfile ${TEST_SCRATCH_DIR}/iid2 -t $target --from $base $BUDFILES/base-with-labels
## Must use cache
expect_output --substring " Using cache"
cmp ${TEST_SCRATCH_DIR}/iid1 ${TEST_SCRATCH_DIR}/iid2
## Since we are inheriting no annotations, this should not use previous image present in the cache.
run_buildah build $WITH_POLICY_JSON --layers --inherit-annotations=false --created-annotation=false -t $target --from $base $BUDFILES/base-with-labels
## should not contain `Using Cache`
assert "$output" !~ "Using cache"
# no annotations should be inherited from base image
want_output='map[]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
## Build again but set a new annotation and don't inherit annotations from base image
run_buildah build $WITH_POLICY_JSON --layers --inherit-annotations=false --created-annotation=false --annotation hello=world -t $target --from $base $BUDFILES/base-with-labels
# no annotations should be inherited from base image
want_output='map["hello":"world"]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
### Run similar test again with multiple instructions now
## Build without removing annotations
run_buildah build $WITH_POLICY_JSON --layers --iidfile ${TEST_SCRATCH_DIR}/iid1 -t $target -f $BUDFILES/base-with-labels/Containerfile2
## Second build must use cache
run_buildah build $WITH_POLICY_JSON --layers --iidfile ${TEST_SCRATCH_DIR}/iid2 -t $target -f $BUDFILES/base-with-labels/Containerfile2
## Must use cache
expect_output --substring " Using cache"
cmp ${TEST_SCRATCH_DIR}/iid1 ${TEST_SCRATCH_DIR}/iid2
## Since we are inheriting no annotations, this should not use previous image present in the cache.
run_buildah build $WITH_POLICY_JSON --layers --inherit-annotations=false --created-annotation=false -t $target -f $BUDFILES/base-with-labels/Containerfile2
## but this time since 1st instruction is cached it should still use cache for some part, only annotations should not be there
expect_output --substring " Using cache"
# no annotations should be inherited from base image
want_output='map[]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
## Build again but set a new annotation and don't inherit annotations from base image
run_buildah build $WITH_POLICY_JSON --layers --inherit-annotations=false --created-annotation=false --annotation hello=world -t $target -f $BUDFILES/base-with-labels/Containerfile2
## but this time since 1st instruction is cached it should still use cache for some part, only last instruction will be changed since we are adding
## annotations to it
expect_output --substring " Using cache"
# no annotations should be inherited from base image
want_output='map["hello":"world"]'
run_buildah inspect --format '{{printf "%q" .ImageAnnotations}}' $target
expect_output "$want_output"
}
@test "bud with no instructions but with CLI flags that require a new image be written" {
_prefetch busybox
local contextdir=${TEST_SCRATCH_DIR}/context
mkdir $contextdir
# Build a multiple-layer image based on busybox.
cat > $contextdir/Dockerfile <<-EOF
FROM busybox
RUN pwd > /pwd.txt
LABEL baseimage=true
EOF
run_buildah build --layers --annotation annotation1=annotationvalue1 --iidfile ${TEST_SCRATCH_DIR}/baseiid.txt ${contextdir}
run cat ${TEST_SCRATCH_DIR}/baseiid.txt
local baseiid=${output##*:}
# Set up to build an image that's based on our image, but with one CLI-motivated change.
cat > $contextdir/Dockerfile <<-EOF
FROM ${baseiid}
EOF
# Try them all.
for mutatorArg in --squash --label=A=B --env=C=D --annotation=E=F --unsetenv=PATH --unsetlabel=io.buildah.version --unsetannotation=org.opencontainers.image.created --inherit-labels=false --inherit-annotations=false ; do
: > ${TEST_SCRATCH_DIR}/iid.txt
run_buildah build --iidfile ${TEST_SCRATCH_DIR}/iid.txt ${mutatorArg} ${contextdir}
test -s ${TEST_SCRATCH_DIR}/iid.txt
run cat ${TEST_SCRATCH_DIR}/iid.txt
local iid="${output##*:}"
assert ${iid} != ${baseiid}
run_buildah inspect -t image ${iid}
case "${mutatorArg}" in
--squash)
# Should have exactly one layer, even though our base had at least two.
run jq '.OCIv1.rootfs.diff_ids|length' <<< "$output"
assert ${status} == 0
assert "${output}" == 1
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq '.OCIv1.rootfs.diff_ids|length' <<< "$output"
assert ${status} == 0
assert "${output}" != 1
;;
--label=A=B)
# Should have had the requested label set.
run jq -r '.OCIv1.config.Labels["A"]' <<< "$output"
assert ${status} == 0
assert "${output}" == B
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq '.OCIv1.config.Labels["A"]' <<< "$output"
assert ${status} == 0
assert "${output}" == null
;;
--annotation=E=F)
# Should have had the requested annotation set.
run jq -r '.ImageAnnotations["E"]' <<< "$output"
assert ${status} == 0
assert "${output}" == F
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq '.ImageAnnotations["E"]' <<< "$output"
assert ${status} == 0
assert "${output}" == null
;;
--unsetenv=PATH)
# Should NOT have had a $PATH set.
run jq -r '.OCIv1.config.Env' <<< "$output"
assert ${status} == 0
assert "${output}" !~ PATH
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq '.OCIv1.config.Env' <<< "$output"
assert ${status} == 0
assert "${output}" =~ PATH
;;
--unsetannotation=org.opencontainers.image.created)
# Should NOT have had a creation annotation set.
run jq -r '.ImageAnnotations' <<< "$output"
assert ${status} == 0
assert "${output}" !~ created
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq '.ImageAnnotations' <<< "$output"
assert ${status} == 0
assert "${output}" =~ org.opencontainers.image.created
;;
--inherit-labels=false)
# Should NOT have had a "baseimage" label.
run jq -r '.OCIv1.config.Labels["baseimage"]' <<< "$output"
assert ${status} == 0
assert "${output}" == null
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq -r '.OCIv1.config.Labels["baseimage"]' <<< "$output"
assert ${status} == 0
assert "${output}" == true
;;
--inherit-annotations=false)
# Should NOT have had an "annotation1" annotation.
run jq -r '.ImageAnnotations["annotation1"]' <<< "$output"
assert ${status} == 0
assert "${output}" == null
# Go back and check the base, as a control.
run_buildah inspect -t image ${baseiid}
run jq -r '.ImageAnnotations["annotation1"]' <<< "$output"
assert ${status} == 0
assert "${output}" == annotationvalue1
;;
esac
done
}
@test "bud: build push with --force-compression" {
skip_if_no_podman
blobcachedir=${TEST_SCRATCH_DIR}/blobcachelocal
mkdir -p ${blobcachedir}
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
# Make sure this is an image never used in any other zstd tests,
# nor with any layers used in zstd tests. That could lead to a
# different test pushing it zstd, and a "did not expect zstd"
# failure below.
echo "$(date --utc --iso-8601=seconds) this is a unique layer $(random_string)" >$contextdir/therecanbeonly1
cat > $contextdir/Containerfile << _EOF
FROM scratch
COPY /therecanbeonly1 /uniquefile
_EOF
imgname="img-$(safename)"
start_registry
run_buildah login --tls-verify=false --authfile ${TEST_SCRATCH_DIR}/test.auth --username testuser --password testpassword localhost:${REGISTRY_PORT}
run_buildah build $WITH_POLICY_JSON -t $imgname --platform linux/amd64 $contextdir
# Helper function. push our image with the given options, and run skopeo inspect
function _test_buildah_push() {
run_buildah push \
--blob-cache=${blobcachedir} \
$WITH_POLICY_JSON \
--authfile ${TEST_SCRATCH_DIR}/test.auth \
--tls-verify=false \
$* \
$imgname \
docker://localhost:${REGISTRY_PORT}/$imgname
echo "# skopeo inspect $imgname"
run podman run --rm \
--mount type=bind,src=${TEST_SCRATCH_DIR}/test.auth,target=/test.auth,Z \
--net host \
quay.io/skopeo/stable inspect \
--authfile=/test.auth \
--tls-verify=false \
--raw \
docker://localhost:${REGISTRY_PORT}/$imgname
echo "$output"
}
# layers should have no trace of zstd since push was with --compression-format gzip
_test_buildah_push --compression-format gzip
assert "$output" !~ "zstd" "zstd found in layers where push was with --compression-format gzip"
# layers should have no trace of zstd since push is --force-compression=false
_test_buildah_push --compression-format zstd --force-compression=false
assert "$output" !~ "zstd" "zstd found even though push was without --force-compression"
# layers should container `zstd`
_test_buildah_push --compression-format zstd
expect_output --substring "zstd" "layers must contain zstd compression"
# layers should container `zstd`
_test_buildah_push --compression-format zstd --force-compression
expect_output --substring "zstd" "layers must contain zstd compression"
}
@test "bud with --dns* flags" {
_prefetch alpine
for dnsopt in --dns --dns-option --dns-search; do
run_buildah 125 build $dnsopt=example.com --network=none $WITH_POLICY_JSON -f $BUDFILES/dns/Dockerfile $BUDFILES/dns
expect_output "Error: the $dnsopt option cannot be used with --network=none" "dns options should not be allowed with --network=none"
done
run_buildah build --dns-search=example.com --dns=223.5.5.5 --dns-option=use-vc $WITH_POLICY_JSON -f $BUDFILES/dns/Dockerfile $BUDFILES/dns
expect_output --substring "search example.com"
expect_output --substring "nameserver 223.5.5.5"
expect_output --substring "options use-vc"
}
@test "build with inline RUN --network=host" {
_prefetch alpine
#hostns=$(readlink /proc/self/ns/net)
run readlink /proc/self/ns/net
hostns="$output"
run_buildah build $WITH_POLICY_JSON -t source -f $BUDFILES/inline-network/Dockerfile1
expect_output --from="${lines[2]}" "${hostns}"
}
@test "build with inline RUN --network=none" {
_prefetch alpine
run_buildah 1 build $WITH_POLICY_JSON -t source -f $BUDFILES/inline-network/Dockerfile2
expect_output --substring "wget: bad address"
}
@test "build with inline RUN --network=fake" {
_prefetch alpine
run_buildah 125 build $WITH_POLICY_JSON -t source -f $BUDFILES/inline-network/Dockerfile3
expect_output --substring "unsupported value"
}
@test "build with inline default RUN --network=default" {
skip_if_chroot
_prefetch alpine
run readlink /proc/self/ns/net
hostns=$output
run_buildah build --network=host $WITH_POLICY_JSON -t source -f $BUDFILES/inline-network/Dockerfile4
firstns=${lines[2]}
assert "${hostns}" == "$firstns"
run_buildah build --network=private $WITH_POLICY_JSON -t source -f $BUDFILES/inline-network/Dockerfile4
secondns=${lines[2]}
assert "$secondns" != "$firstns"
}
@test "bud with ignoresymlink on default file" {
_prefetch alpine
echo hello > ${TEST_SCRATCH_DIR}/private_file
cp -a $BUDFILES/container-ignoresymlink ${TEST_SCRATCH_DIR}/container-ignoresymlink
ln -s ${TEST_SCRATCH_DIR}/private_file ${TEST_SCRATCH_DIR}/container-ignoresymlink/.dockerignore
run_buildah build $WITH_POLICY_JSON -t test -f Dockerfile $BUDFILES/container-ignoresymlink
# Should ignore a .dockerignore or .containerignore that's a symlink to somewhere outside of the build context
expect_output --substring "hello"
}
# Verify https://github.com/containers/buildah/issues/4342
@test "buildkit-mount type=cache should not hang if cache is wiped in between" {
_prefetch alpine
containerfile=$BUDFILES/cache-mount-locked/Containerfile
run_buildah build $WITH_POLICY_JSON --build-arg WIPE_CACHE=1 -t source -f $containerfile $BUDFILES/cache-mount-locked
# build should be success and must contain `hello` from `file` in last step
expect_output --substring "hello"
}
# Test for https://github.com/containers/buildah/pull/4295
@test "build test warning for preconfigured TARGETARCH, TARGETOS, TARGETPLATFORM or TARGETVARIANT" {
containerfile=$BUDFILES/platform-sets-args/Containerfile
# Containerfile must contain one or more (four, as of 2022-10) lines
# of the form 'ARG TARGETxxx' for each of the variables of interest.
local -a checkvars=($(sed -ne 's/^ARG //p' <$containerfile))
assert "${checkvars[*]}" != "" \
"INTERNAL ERROR! No 'ARG xxx' lines in $containerfile!"
ARCH=$(go env GOARCH)
# With explicit and full --platform, buildah should not warn.
run_buildah build $WITH_POLICY_JSON --platform linux/amd64/v2 \
-t source -f $containerfile
assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \
"With explicit --platform, buildah should warn about pulling difference in platform"
assert "$output" =~ "TARGETOS=linux" " --platform TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" " --platform TARGETARCH set correctly"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=[[:cntrl:]]" " --platform TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64/v2" " --platform TARGETPLATFORM set correctly"
# Likewise with individual args
run_buildah build $WITH_POLICY_JSON --os linux --arch amd64 --variant v2 \
-t source -f $containerfile
assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \
"With explicit --variant, buildah should warn about pulling difference in platform"
assert "$output" =~ "TARGETOS=linux" "--os --arch --variant TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" "--os --arch --variant TARGETARCH set correctly"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=[[:cntrl:]]" "--os --arch --variant TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--os --arch --variant TARGETPLATFORM set correctly"
run_buildah build $WITH_POLICY_JSON --os linux -t source -f $containerfile
assert "$output" !~ "WARNING" \
"With explicit --os (but no arch/variant), buildah should not warn about TARGETOS"
assert "$output" =~ "TARGETOS=linux" "--os TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=${ARCH}" "--os TARGETARCH set correctly"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=[[:cntrl:]]" "--os TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/${ARCH}" "--os TARGETPLATFORM set correctly"
run_buildah build $WITH_POLICY_JSON --arch amd64 -t source -f $containerfile
assert "$output" !~ "WARNING" \
"With explicit --os (but no arch/variant), buildah should not warn about TARGETOS"
assert "$output" =~ "TARGETOS=linux" "--arch TARGETOS set correctly"
assert "$output" =~ "TARGETARCH=amd64" "--arch TARGETARCH set correctly"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=[[:cntrl:]]" "--arch TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--arch TARGETPLATFORM set correctly"
for option in "--arch=arm64" "--os=windows" "--variant=v2"; do
run_buildah 125 build $WITH_POLICY_JSON --platform linux/amd64 ${option} \
-t source -f $containerfile
assert "$output" =~ "invalid --platform may not be used with --os, --arch, or --variant" "can't use --platform and one of --os, --arch or --variant together"
done
}
@test "build-conflicting-isolation-chroot-and-network" {
_prefetch alpine
cat > ${TEST_SCRATCH_DIR}/Containerfile << _EOF
FROM alpine
RUN ping -c 1 4.2.2.2
_EOF
run_buildah 125 build --network=none --isolation=chroot $WITH_POLICY_JSON ${TEST_SCRATCH_DIR}
expect_output --substring "cannot set --network other than host with --isolation chroot"
BUILDAH_ISOLATION=chroot run_buildah 125 build --network=none $WITH_POLICY_JSON ${TEST_SCRATCH_DIR}
expect_output --substring "cannot set --network other than host with --isolation chroot"
}
@test "bud with .dockerignore #1" {
_prefetch alpine busybox
run_buildah 125 build -t testbud $WITH_POLICY_JSON -f $BUDFILES/dockerignore/Dockerfile $BUDFILES/dockerignore
expect_output --substring 'building.*"COPY subdir \./".*no such file or directory'
run_buildah build -t testbud $WITH_POLICY_JSON -f $BUDFILES/dockerignore/Dockerfile.succeed $BUDFILES/dockerignore
run_buildah from --name myctr testbud
run_buildah 1 run myctr ls -l test1.txt
run_buildah run myctr ls -l test2.txt
run_buildah 1 run myctr ls -l sub1.txt
run_buildah 1 run myctr ls -l sub2.txt
run_buildah 1 run myctr ls -l subdir/
}
@test "bud --layers with --mount type bind should burst cache if symlink is changed" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile1 << _EOF
First symlink content
_EOF
cat > $contextdir/samplefile2 << _EOF
Second symlink content
_EOF
# Create symlink to samplefile1 and we will mount that
ln -s samplefile1 $contextdir/tomount
pwd
ls $contextdir/
cat $contextdir/tomount
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,source=tomount,target=file,Z cat file
_EOF
# on first run since there is no cache so `samplefile1` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "First symlink content"
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain content from the file since entire build is cached
assert "$output" !~ "First symlink content"
# Modify the symlink
ln -sf samplefile2 $contextdir/tomount
# on third run since we have changed symlink so cache must burst.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "Second symlink content"
}
@test "bud --layers with --mount type bind should burst cache if content is changed" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile << _EOF
samplefile
_EOF
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,target=/test,Z ls /test
_EOF
# on first run since there is no cache so `samplefile` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile"
# on second run since there is cache so `samplefile` should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain `samplefile`
assert "$output" !~ "samplefile"
cat > $contextdir/anotherfile << _EOF
anotherfile
_EOF
# on third run since we have added new file `anotherfile` so cache must burst.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile"
expect_output --substring "anotherfile"
}
@test "bud --layers with --mount type bind should burst and multiple mounts cache if content is changed" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile << _EOF
samplefile
_EOF
cat > $contextdir/testfile << _EOF
Helloworld
_EOF
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,target=/test,Z --mount=type=bind,source=testfile,target=testfile,Z ls /test && cat testfile
_EOF
# on first run since there is no cache so `samplefile` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile"
expect_output --substring "Helloworld"
# on second run since there is cache so `samplefile` should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain `samplefile`
assert "$output" !~ "samplefile"
# Modify sample file 2
cat > $contextdir/testfile << _EOF
Helloworld2
_EOF
# on third run since we have modified `testfile` so cache must burst.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile"
expect_output --substring "Helloworld2"
}
@test "bud --layers with --mount type bind should burst cache if content is changed - source is additional build context" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile << _EOF
samplefile2
_EOF
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,from=one,target=/test,Z ls /test
_EOF
# on first run since there is no cache so `samplefile` must be printed
run_buildah build $WITH_POLICY_JSON --build-context one=$contextdir --layers -t source -f $contextdir/Containerfile
expect_output --substring "samplefile"
# on second run since there is cache so `samplefile` should not be printed
run_buildah build $WITH_POLICY_JSON --build-context one=$contextdir --layers -t source -f $contextdir/Containerfile
# output should not `samplefile` since cache is being used
assert "$output" !~ "samplefile"
cat > $contextdir/anotherfile << _EOF
anotherfile2
_EOF
# on third run since we have added new file `anotherfile` so cache must burst.
run_buildah build $WITH_POLICY_JSON --build-context one=$contextdir --layers -t source -f $contextdir/Containerfile
expect_output --substring "samplefile"
expect_output --substring "anotherfile"
}
@test "bud --layers with --mount type bind should burst cache if content is changed - source is previous stage" {
_prefetch busybox
local contextdir=${TEST_SCRATCH_DIR}/cache
mkdir -p $contextdir
for iteration in 1 2 ; do
for content in 1 2 ; do
# based on reproducer from #6845
cat > $contextdir/Containerfile <<-_EOF
FROM busybox AS base
FROM base AS builder
RUN mkdir /build && echo "$content" > /build/foo.txt
FROM base AS prod
RUN --mount=type=bind,source=/build,from=builder,target=/build cp /build/foo.txt bar.txt
_EOF
run_buildah build $WITH_POLICY_JSON --layers --iidfile ${TEST_SCRATCH_DIR}/$content.$iteration.txt $contextdir
done
done
assert $(< ${TEST_SCRATCH_DIR}/1.1.txt) == $(< ${TEST_SCRATCH_DIR}/1.2.txt)
assert $(< ${TEST_SCRATCH_DIR}/2.1.txt) == $(< ${TEST_SCRATCH_DIR}/2.2.txt)
assert $(< ${TEST_SCRATCH_DIR}/1.1.txt) != $(< ${TEST_SCRATCH_DIR}/2.1.txt)
}
@test "bud --layers should not hit cache if heredoc is changed" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/Dockerfile << _EOF
FROM alpine
RUN <<EOF
echo "Cache burst" >> /hello
echo "Cache burst second line" >> /hello
EOF
RUN cat hello
_EOF
# on first run since there is no cache so `Cache burst` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst second line"
# on second run since there is cache so `Cache burst` should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
# output should not contain cache burst
assert "$output" !~ "Cache burst second line"
cat > $contextdir/Dockerfile << _EOF
FROM alpine
RUN <<EOF
echo "Cache burst add diff" >> /hello
EOF
RUN cat hello
_EOF
# on third run since we have changed heredoc so `Cache burst` must be printed.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst add diff"
}
@test "bud --layers with --mount type bind should preserve cache when file mod time changes but content stays same" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/samplefile << _EOF
samplefile content unchanged
_EOF
cat > $contextdir/Containerfile << _EOF
FROM alpine
RUN --mount=type=bind,source=samplefile,target=file,Z cat file
_EOF
# on first run since there is no cache so content must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
expect_output --substring "samplefile content unchanged"
# on second run since there is cache so content should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain content from the file since entire build is cached
assert "$output" !~ "samplefile content unchanged"
# Change mod time of this file (this changes modification time)
touch -d "@1577836800" $contextdir/samplefile
# on third run since content is unchanged, cache should still be used despite different mod time
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Containerfile $contextdir
# output should not contain content from the file since cache should still be valid
assert "$output" !~ "samplefile content unchanged"
}
@test "bud --layers should not hit cache if heredoc is changed - with ARG" {
_prefetch alpine
local contextdir=${TEST_SCRATCH_DIR}/bud/platform
mkdir -p $contextdir
cat > $contextdir/Dockerfile << _EOF
FROM alpine
ARG key=value
RUN <<EOF
echo "Cache burst" >> /hello
echo "Cache burst second line" >> /hello
EOF
RUN cat hello
_EOF
# on first run since there is no cache so `Cache burst` must be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst second line"
# on second run since there is cache so `Cache burst` should not be printed
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
# output should not contain cache burst
assert "$output" !~ "Cache burst second line"
cat > $contextdir/Dockerfile << _EOF
FROM alpine
ARG key=value
RUN <<EOF
echo "Cache burst add diff" >> /hello
EOF
RUN cat hello
_EOF
# on third run since we have changed heredoc so `Cache burst` must be printed.
run_buildah build $WITH_POLICY_JSON --layers -t source -f $contextdir/Dockerfile
expect_output --substring "Cache burst add diff"
}
@test "bud build without multiple layers, with and without layer-creating instructions" {
local base=busybox
_prefetch $base
contextdir=${TEST_SCRATCH_DIR}/context
mkdir -p ${contextdir}
createrandom ${contextdir}/randomfile
# count the number of layers in the base image
run_buildah inspect --type=image --format "{{ len .Docker.RootFS.DiffIDs }}" $base
local baselayers="$output"
local baselayersplusone=$(( ${baselayers} + 1 ))
run_buildah inspect --type=image --format "{{ len .Docker.History }}" $base
local basehistory="$output"
for instruction in \
"ADD randomfile /" \
"ARG A=B" \
"COPY randomfile /" \
"ENTRYPOINT /bin/bash" \
"ENV A=B C=D" \
"EXPOSE 85" \
"HEALTHCHECK CMD /bin/pwd" \
"LABEL A=B C=D" \
"MAINTAINER who knows" \
"RUN pwd" \
"USER 1:2" \
"VOLUME /pants" \
"WORKDIR /pants" \
; do
cat > ${contextdir}/Dockerfile << _EOF
FROM $base
$instruction
_EOF
# build an image based on $base with one instruction added"
run_buildah build $WITH_POLICY_JSON --iidfile ${TEST_SCRATCH_DIR}/iid.txt ${contextdir}
# expect one new history entry
run_buildah inspect --type=image --format "{{ len .Docker.History }}" $(< ${TEST_SCRATCH_DIR}/iid.txt)
local derivedhistory="$output"
assert $derivedhistory -eq $(( ${basehistory} + 1 )) "expected one new history entry in derived image"
# count the number of layers in the newly-built image
run_buildah inspect --type=image --format "{{ len .Docker.RootFS.DiffIDs }}" $(< ${TEST_SCRATCH_DIR}/iid.txt)
local derivedlayers="$output"
# expectations vary based on what that instruction was
case "$instruction" in
# if it's ADD, COPY, or RUN, accept either the same set of layers, or one more layer added,
# but not with a digest that matches a turns-out-to-be-empty layer
ADD*|COPY*|RUN*)
if test ${baselayersplusone} -eq ${derivedlayers} ; then
run_buildah inspect --type=image --format "{{ index .Docker.RootFS.DiffIDs $baselayers }}" $(< ${TEST_SCRATCH_DIR}/iid.txt)
local lastdiffid="$output"
# sha256sum([0]{})
assert lastdiffid != sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
# sha256sum([1024]{})
assert lastdiffid != sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef
# sha512sum([0]{})
assert lastdiffid != sha512:cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e
# sha512sum([1024]{})
assert lastdiffid != sha512:8efb4f73c5655351c444eb109230c556d39e2c7624e9c11abc9e3fb4b9b9254218cc5085b454a9698d085cfa92198491f07a723be4574adc70617b73eb0b6461
else
assert ${baselayers} -eq ${derivedlayers} "should have omitted turned-out-empty layer"
fi
;;
# every other instruction -> no new layer
*) assert ${baselayers} -eq ${derivedlayers} "should not have added new layers for $instruction" ;;
esac
done
}
@test "bud build with heredoc content" {
_prefetch alpine
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile .
expect_output --substring "print first line from heredoc"
expect_output --substring "print second line from heredoc"
expect_output --substring "Heredoc writing first file"
expect_output --substring "some text of first file"
expect_output --substring "file passed to program"
expect_output --substring "and it contains multiple"
expect_output --substring "(your index page goes here)"
expect_output --substring "(robots content)"
expect_output --substring "(humans content)"
expect_output --substring "this is the output of test6 part1"
expect_output --substring "this is the output of test6 part2"
expect_output --substring "this is the output of test7 part1"
expect_output --substring "this is the output of test7 part2"
expect_output --substring "this is the output of test7 part3"
expect_output --substring "this is the output of test8 part1"
expect_output --substring "this is the output of test8 part2"
# verify that build output contains summary of heredoc content
expect_output --substring 'RUN <<EOF \(echo "print first line from heredoc"...)'
expect_output --substring 'RUN <<EOF \(echo "Heredoc writing first file" >> /file1...)'
expect_output --substring 'RUN cat <<EOF \(file passed to program...)'
expect_output --substring 'ADD <<EOF /index.html \(\(your index page goes here))'
expect_output --substring 'COPY <<robots.txt <<humans.txt /test/ \(\(robots content)) \(\(humans content))'
}
@test "bud build with heredoc with COPY instructionw with .containerignore set" {
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc-ignore/Containerfile --ignorefile $BUDFILES/heredoc-ignore/.containerignore .
expect_output --substring "This is a file"
expect_output --substring "This is a line from file"
}
@test "bud build with heredoc content which is a bash file" {
skip_if_in_container
_prefetch busybox
run_buildah build -t heredoc $WITH_POLICY_JSON -f $BUDFILES/heredoc/Containerfile.bash_file .
expect_output --substring "this is the output of test9"
expect_output --substring "this is the output of test10"
}
@test "bud build with heredoc content with inline interpreter" {
skip_if_in_container
_prefetch busybox