@@ -2670,6 +2670,80 @@ _EOF
26702670 expect_output " $want_output "
26712671}
26722672
2673+ @test " bud and test inherit-labels" {
2674+ base=registry.fedoraproject.org/fedora-minimal
2675+ _prefetch $base
2676+ _prefetch alpine
2677+ run_buildah --version
2678+ local -a output_fields=($output )
2679+ buildah_version=${output_fields[2]}
2680+ run_buildah build $WITH_POLICY_JSON -t exp -f $BUDFILES /base-with-labels/Containerfile
2681+
2682+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "license"}}' exp
2683+ expect_output " MIT" " license must be MIT from fedora base image"
2684+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "name"}}' exp
2685+ expect_output " fedora-minimal" " name must be fedora from base image"
2686+
2687+ run_buildah build $WITH_POLICY_JSON --inherit-labels=false --label name=world -t exp -f $BUDFILES /base-with-labels/Containerfile
2688+ # no labels should be inherited from base image, only the buildah version label
2689+ # and `hello=world` which we just added using cli flag
2690+ want_output=' map["io.buildah.version":"' $buildah_version ' " "name":"world"]'
2691+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2692+ expect_output " $want_output "
2693+
2694+ # Try building another file with multiple layers
2695+ run_buildah build $WITH_POLICY_JSON --iidfile ${TEST_SCRATCH_DIR} /id1 --layers -t exp -f $BUDFILES /base-with-labels/Containerfile.layer
2696+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "license"}}' exp
2697+ expect_output " MIT" " license must be MIT from fedora base image"
2698+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "name"}}' exp
2699+ expect_output " world" " name must be world from Containerfile"
2700+
2701+ # Now build same file with --inherit-labels=false and verify if we are not using the cache again.
2702+ run_buildah build $WITH_POLICY_JSON --layers --inherit-labels=false --iidfile ${TEST_SCRATCH_DIR} /inherit_false_1 -t exp -f $BUDFILES /base-with-labels/Containerfile.layer
2703+ # Should not contain `Using cache` at all since
2704+ assert " $output " ! ~ " Using cache"
2705+ want_output=' map["io.buildah.version":"' $buildah_version ' " "name":"world"]'
2706+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2707+ expect_output " $want_output "
2708+
2709+ run_buildah build $WITH_POLICY_JSON --layers --inherit-labels=false --iidfile ${TEST_SCRATCH_DIR} /inherit_false_2 -t exp -f $BUDFILES /base-with-labels/Containerfile.layer
2710+ # Should contain `Using cache`
2711+ expect_output --substring " Using cache"
2712+ want_output=' map["io.buildah.version":"' $buildah_version ' " "name":"world"]'
2713+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2714+ expect_output " $want_output "
2715+ assert " $( cat ${TEST_SCRATCH_DIR} /inherit_false_1) " = " $( cat ${TEST_SCRATCH_DIR} /inherit_false_2) " " expected image ids to not change"
2716+
2717+ # Now build same file with --inherit-labels=true and verify if using the cache
2718+ run_buildah build $WITH_POLICY_JSON --iidfile ${TEST_SCRATCH_DIR} /id2 --layers --inherit-labels=true -t exp -f $BUDFILES /base-with-labels/Containerfile.layer
2719+ expect_output --substring " Using cache"
2720+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "license"}}' exp
2721+ expect_output " MIT" " license must be MIT from fedora base image"
2722+ run_buildah inspect --format ' {{ index .Docker.Config.Labels "name"}}' exp
2723+ expect_output " world" " name must be world from Containerfile"
2724+ # Final image id should be exactly same as the one image which was built in the past.
2725+ assert " $( cat ${TEST_SCRATCH_DIR} /id1) " = " $( cat ${TEST_SCRATCH_DIR} /id2) " " expected image ids to not change"
2726+
2727+ # Now build same file with --inherit-labels=false and verify if target stage did not inherit any labels from base stage.
2728+ run_buildah build $WITH_POLICY_JSON --layers --inherit-labels=false -t exp -f $BUDFILES /base-with-labels/Containerfile.multi-stage
2729+ want_output=' map["io.buildah.version":"' $buildah_version ' "]'
2730+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2731+ expect_output " $want_output "
2732+
2733+ # Now build same file with --inherit-labels=true and verify if target stage inherits labels from the base stage.
2734+ run_buildah build $WITH_POLICY_JSON --iidfile ${TEST_SCRATCH_DIR} /id3 --layers --inherit-labels=true -t exp -f $BUDFILES /base-with-labels/Containerfile.multi-stage
2735+ want_output=' map["io.buildah.version":"' $buildah_version ' " "name":"world"]'
2736+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2737+ expect_output " $want_output "
2738+
2739+ # Rebuild again with layers should not build image again at all.
2740+ run_buildah build $WITH_POLICY_JSON --iidfile ${TEST_SCRATCH_DIR} /id4 --layers --inherit-labels=true -t exp -f $BUDFILES /base-with-labels/Containerfile.multi-stage
2741+ want_output=' map["io.buildah.version":"' $buildah_version ' " "name":"world"]'
2742+ run_buildah inspect --format ' {{printf "%q" .Docker.Config.Labels}}' exp
2743+ expect_output " $want_output "
2744+ assert " $( cat ${TEST_SCRATCH_DIR} /id3) " = " $( cat ${TEST_SCRATCH_DIR} /id4) " " expected image ids to not change"
2745+ }
2746+
26732747@test " build using intermediate images should not inherit label" {
26742748 _prefetch alpine
26752749
0 commit comments