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