@@ -3396,6 +3396,13 @@ _EOF
33963396 done
33973397}
33983398
3399+ # assert_no_tempdir_leaks checks $1 has no leftover temp download directories.
3400+ function assert_no_tempdir_leaks() {
3401+ local tmpdir=$1
3402+ local leftovers=$( find $tmpdir -mindepth 1 -maxdepth 1 -print)
3403+ assert " $leftovers " == " "
3404+ }
3405+
33993406# Helper function for several of the tests which pull from http.
34003407#
34013408# Usage: _test_http SUBDIRECTORY URL_PATH [EXTRA ARGS]
@@ -3416,11 +3423,14 @@ function _test_http() {
34163423
34173424 starthttpd " $BUDFILES /$testdir "
34183425 target=scratch-image
3419- run_buildah build $WITH_POLICY_JSON \
3426+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
3427+ mkdir -p $tmpdir
3428+ TMPDIR=$tmpdir run_buildah build $WITH_POLICY_JSON \
34203429 -t ${target} \
34213430 " $@ " \
34223431 http://0.0.0.0:${HTTP_SERVER_PORT} /$urlpath
34233432 stophttpd
3433+ assert_no_tempdir_leaks $tmpdir
34243434 run_buildah from ${target}
34253435}
34263436
@@ -3500,9 +3510,7 @@ function validate_instance_compression {
35003510 mkdir -p " ${tmpdir} "
35013511 TMPDIR=" ${tmpdir} " run_buildah build $WITH_POLICY_JSON -t ${target} " ${gitrepo} "
35023512 run_buildah from " ${target} "
3503- run find " ${tmpdir} " -type d -print
3504- echo " $output "
3505- test " ${# lines[*]} " -le 2
3513+ assert_no_tempdir_leaks $tmpdir
35063514}
35073515
35083516@test " bud-git-context-failure" {
@@ -8180,7 +8188,10 @@ _EOF
81808188file.txt
81818189_EOF
81828190
8183- run_buildah build -f $contextdir /Dockerfile -t add-git-ignoring-files $contextdir
8191+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
8192+ mkdir -p $tmpdir
8193+ TMPDIR=$tmpdir run_buildah build -f $contextdir /Dockerfile -t add-git-ignoring-files $contextdir
8194+ assert_no_tempdir_leaks $tmpdir
81848195}
81858196
81868197@test " bud with ADD with git repository source escape directory" {
@@ -8213,8 +8224,11 @@ ADD http://0.0.0.0:${HTTP_SERVER_PORT}/git/test-bug.git#main:proj${secretdir} /m
82138224RUN cat /mydir/secretfile
82148225_EOF
82158226
8216- run_buildah 125 build -f $contextdir /Containerfile -t escape-image --no-cache $contextdir
8227+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
8228+ mkdir -p $tmpdir
8229+ TMPDIR=$tmpdir run_buildah 125 build -f $contextdir /Containerfile -t escape-image --no-cache $contextdir
82178230 assert " $output " ! ~ " mysecret"
8231+ assert_no_tempdir_leaks $tmpdir
82188232}
82198233
82208234@test " bud with http context symlinked Dockerfile does not write through symlink on fallback" {
@@ -10508,3 +10522,36 @@ _EOF
1050810522 # Verify final image IDs match (complete cache hit)
1050910523 assert " $first_build_final_image_id " " ==" " $second_build_final_image_id " " final image ID should match when cache is fully reused"
1051010524}
10525+
10526+ @test " build-with-additional-build-context URL temp dirs are cleaned up" {
10527+ _prefetch alpine
10528+
10529+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
10530+ mkdir -p $tmpdir
10531+ local tarball_content=${TEST_SCRATCH_DIR} /tarball-src
10532+ mkdir -p $tarball_content /myproject
10533+ echo secret-content > $tarball_content /myproject/file.txt
10534+ local tarball=${TEST_SCRATCH_DIR} /context.tar.gz
10535+ tar -czf $tarball -C $tarball_content .
10536+ local httpdir=${TEST_SCRATCH_DIR} /http
10537+ mkdir -p $httpdir
10538+ cp $tarball $httpdir /context.tar.gz
10539+
10540+ starthttpd $httpdir
10541+ local contextdir=${TEST_SCRATCH_DIR} /bud/context
10542+ mkdir -p $contextdir
10543+ cat > $contextdir /Dockerfile << _EOF
10544+ FROM alpine
10545+ COPY --from=ctx myproject/file.txt /file.txt
10546+ RUN cat /file.txt
10547+ RUN --mount=type=bind,src=myproject,from=ctx2,target=/mnt,z ls /mnt
10548+ _EOF
10549+
10550+ TMPDIR=$tmpdir run_buildah build $WITH_POLICY_JSON \
10551+ --build-context ctx=http://0.0.0.0:${HTTP_SERVER_PORT} /context.tar.gz \
10552+ --build-context ctx2=http://0.0.0.0:${HTTP_SERVER_PORT} /context.tar.gz \
10553+ -t source -f $contextdir /Dockerfile $contextdir
10554+ expect_output --substring " secret-content"
10555+ expect_output --substring " file.txt"
10556+ assert_no_tempdir_leaks $tmpdir
10557+ }
0 commit comments