@@ -9245,14 +9245,6 @@ EOF
92459245 expect_output --substring " writing build ID to file"
92469246}
92479247
9248- @test " bud with --layers and --cache-stages should error" {
9249- _prefetch alpine
9250- target=" layers-cache-stages-error-test-$( safename) "
9251-
9252- run_buildah 125 build $WITH_POLICY_JSON --layers --cache-stages -t ${target} -f $BUDFILES /cache-stages-test/Dockerfile.single-intermediate $BUDFILES /cache-stages-test
9253- expect_output --substring " 'layers' and 'cache-stages' cannot be used together"
9254- }
9255-
92569248@test " bud with --cache-stages on single-stage Dockerfile should not create intermediate images" {
92579249 _prefetch alpine
92589250 target=" single-stage-test-$( safename) "
@@ -9571,3 +9563,60 @@ EOF
95719563 run_buildah rmi --all
95729564}
95739565
9566+ @test " bud with --layers --cache-stages stores labeled layers and subsequent --layers build reuses them" {
9567+ _prefetch alpine
9568+ target=" cache-reuse-test-$( safename) "
9569+ build_id_file=" ${TEST_SCRATCH_DIR} /cache-reuse-build-id.txt"
9570+
9571+ run_buildah build $WITH_POLICY_JSON --layers --cache-stages --stage-labels --build-id-file ${build_id_file} -t ${target} -first -f $BUDFILES /cache-stages-test/Dockerfile.single-intermediate $BUDFILES /cache-stages-test
9572+
9573+ first_build_id=$( cat ${build_id_file} )
9574+
9575+ # Get all intermediate layers from first build and store their image IDs and build IDs
9576+ run_buildah images -a
9577+ all_intermediate_layers_first=$( echo " $output " | awk ' $1 == "<none>" && $2 == "<none>" {print $3}' )
9578+
9579+ # Create associative array: image_id -> build_id
9580+ declare -A first_build_image_to_build_id
9581+ for image_id in $all_intermediate_layers_first ; do
9582+ run_buildah inspect --format ' {{index .OCIv1.Config.Labels "io.buildah.build.id"}}' $image_id
9583+ build_id_label=" $output "
9584+ if [[ -n " $build_id_label " ]]; then
9585+ first_build_image_to_build_id[$image_id ]=" $build_id_label "
9586+ assert " $build_id_label " == " $first_build_id " " all intermediate layers from first build should have build ID: $first_build_id "
9587+ fi
9588+ done
9589+
9590+ first_intermediate_count=$( echo " $all_intermediate_layers_first " | wc -w)
9591+ assert " $first_intermediate_count " -gt 0 " first build should create intermediate layers"
9592+
9593+ # Remove the final tagged image but keep all intermediate layers for cache
9594+ run_buildah rmi ${target} -first
9595+
9596+ # Second should reuse the layers created by first build (cache hit not creating more)
9597+ run_buildah build $WITH_POLICY_JSON --layers -t ${target} -second -f $BUDFILES /cache-stages-test/Dockerfile.single-intermediate $BUDFILES /cache-stages-test
9598+
9599+ expect_output --substring " Using cache"
9600+
9601+ run_buildah images -a
9602+ all_intermediate_layers_second=$( echo " $output " | awk ' $1 == "<none>" && $2 == "<none>" {print $3}' )
9603+
9604+ # Verify that intermediate layers with matching image IDs have matching build IDs
9605+ cached_layers_count=0
9606+ for image_id in $all_intermediate_layers_second ; do
9607+ if [[ -n " ${first_build_image_to_build_id[$image_id]} " ]]; then
9608+ cached_layers_count=$(( cached_layers_count + 1 ))
9609+
9610+ run_buildah inspect --format ' {{index .OCIv1.Config.Labels "io.buildah.build.id"}}' $image_id
9611+ current_build_id=" $output "
9612+ expected_build_id=" ${first_build_image_to_build_id[$image_id]} "
9613+
9614+ assert " $current_build_id " == " $expected_build_id " " image $image_id reused from cache must have same build ID: $expected_build_id "
9615+ fi
9616+ done
9617+
9618+ # COPY --from=... layers are not reused
9619+ assert " $cached_layers_count " -eq 3 " intermediate layers should be reused from cache (have matching image IDs)"
9620+
9621+ run_buildah rmi --all
9622+ }
0 commit comments