@@ -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" {
@@ -8214,9 +8225,11 @@ ADD http://0.0.0.0:${HTTP_SERVER_PORT}/git/test-bug.git#main:proj${secretdir} /m
82148225RUN cat /mydir/secretfile
82158226_EOF
82168227
8217- run_buildah 125 build -f $contextdir /Containerfile -t escape-image --no-cache $contextdir
8228+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
8229+ mkdir -p $tmpdir
8230+ TMPDIR=$tmpdir run_buildah 125 build -f $contextdir /Containerfile -t escape-image --no-cache $contextdir
82188231 assert " $output " ! ~ " mysecret"
8219- expect_output --substring " no such file or directory "
8232+ assert_no_tempdir_leaks $tmpdir
82208233}
82218234
82228235@test " bud with http context symlinked Dockerfile does not write through symlink on fallback" {
@@ -10510,3 +10523,35 @@ _EOF
1051010523 # Verify final image IDs match (complete cache hit)
1051110524 assert " $first_build_final_image_id " " ==" " $second_build_final_image_id " " final image ID should match when cache is fully reused"
1051210525}
10526+
10527+ @test " build-with-additional-build-context URL temp dirs are cleaned up" {
10528+ _prefetch alpine
10529+
10530+ local tmpdir=${TEST_SCRATCH_DIR} /tmpdir
10531+ mkdir -p $tmpdir
10532+ local tarball_content=${TEST_SCRATCH_DIR} /tarball-src
10533+ mkdir -p $tarball_content /myproject
10534+ echo secret-content > $tarball_content /myproject/file.txt
10535+ local tarball=${TEST_SCRATCH_DIR} /context.tar.gz
10536+ tar -czf $tarball -C $tarball_content .
10537+ local httpdir=${TEST_SCRATCH_DIR} /http
10538+ mkdir -p $httpdir
10539+ cp $tarball $httpdir /context.tar.gz
10540+
10541+ starthttpd $httpdir
10542+ local contextdir=${TEST_SCRATCH_DIR} /bud/context
10543+ mkdir -p $contextdir
10544+ cat > $contextdir /Dockerfile << _EOF
10545+ FROM alpine
10546+ COPY --from=ctx myproject/file.txt /file.txt
10547+ RUN cat /file.txt
10548+ RUN --mount=type=bind,src=myproject,from=ctx2,target=/mnt,z cmp /file.txt /mnt/file.txt
10549+ _EOF
10550+
10551+ TMPDIR=$tmpdir run_buildah build $WITH_POLICY_JSON \
10552+ --build-context ctx=http://0.0.0.0:${HTTP_SERVER_PORT} /context.tar.gz \
10553+ --build-context ctx2=http://0.0.0.0:${HTTP_SERVER_PORT} /context.tar.gz \
10554+ -t source -f $contextdir /Dockerfile $contextdir
10555+ expect_output --substring " secret-content"
10556+ assert_no_tempdir_leaks $tmpdir
10557+ }
0 commit comments