From 498194661e68cbe3589b79c2115c914ec6d56fb6 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Mon, 22 Apr 2024 05:23:10 -0400 Subject: [PATCH] Setting --arch should set the TARGETARCH build arg Also fix a long standing FIXME in the test framework. Signed-off-by: Daniel J Walsh --- imagebuildah/build.go | 4 ++ pkg/parse/parse_test.go | 1 + tests/bud.bats | 51 +++++++++++++++------- tests/bud/platform-sets-args/Containerfile | 4 ++ tests/conformance/conformance_test.go | 2 +- 5 files changed, 46 insertions(+), 16 deletions(-) diff --git a/imagebuildah/build.go b/imagebuildah/build.go index 39e9837063..fd214960e4 100644 --- a/imagebuildah/build.go +++ b/imagebuildah/build.go @@ -11,6 +11,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strconv" "strings" "sync" @@ -221,6 +222,9 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B systemContext := options.SystemContext for _, platform := range options.Platforms { platformContext := *systemContext + if platform.OS == "" { + platform.OS = runtime.GOOS + } platformSpec := internalUtil.NormalizePlatform(v1.Platform{ OS: platform.OS, Architecture: platform.Arch, diff --git a/pkg/parse/parse_test.go b/pkg/parse/parse_test.go index c1f8833604..1cb87a6e02 100644 --- a/pkg/parse/parse_test.go +++ b/pkg/parse/parse_test.go @@ -218,6 +218,7 @@ func TestSystemContextFromFlagSet(t *testing.T) { assert.NoError(t, err) assert.Equal(t, sc, &types.SystemContext{ BigFilesTemporaryDir: GetTempDir(), + OSChoice: "linux", DockerInsecureSkipTLSVerify: types.OptionalBoolFalse, DockerRegistryUserAgent: fmt.Sprintf("Buildah/%s", define.Version), }) diff --git a/tests/bud.bats b/tests/bud.bats index c5c3528e2b..db4c499576 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -246,23 +246,44 @@ _EOF # With explicit and full --platform, buildah should not warn. run_buildah build $WITH_POLICY_JSON --platform linux/amd64/v2 \ -t source -f $containerfile - assert "$output" !~ "missing .* build argument" \ - "With explicit --platform, buildah should not warn" + assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \ + "With explicit --platform, buildah should warn about pulling difference in platform" + assert "$output" =~ "TARGETOS=linux" " --platform TARGETOS set correctly" + assert "$output" =~ "TARGETARCH=amd64" " --platform TARGETARCH set correctly" + assert "$output" =~ "TARGETVARIANT=" " --platform TARGETVARIANT set correctly" + assert "$output" =~ "TARGETPLATFORM=linux/amd64/v2" " --platform TARGETPLATFORM set correctly" # Likewise with individual args run_buildah build $WITH_POLICY_JSON --os linux --arch amd64 --variant v2 \ -t source -f $containerfile - assert "$output" !~ "missing .* build argument" \ - "With explicit --os + --arch + --variant, buildah should not warn" - - # FIXME FIXME FIXME: #4319: with --os only, buildah should not warn about OS - if false; then - run_buildah build $WITH_POLICY_JSON --os linux \ - -t source -f $containerfile - assert "$output" !~ "missing.*TARGETOS" \ - "With explicit --os (but no arch/variant), buildah should not warn about TARGETOS" - # FIXME: add --arch test too, and maybe make this cleaner - fi + assert "$output" =~ "image platform \(linux/amd64\) does not match the expected platform" \ + "With explicit --variant, buildah should warn about pulling difference in platform" + assert "$output" =~ "TARGETOS=linux" "--os --arch --variant TARGETOS set correctly" + assert "$output" =~ "TARGETARCH=amd64" "--os --arch --variant TARGETARCH set correctly" + assert "$output" =~ "TARGETVARIANT=" "--os --arch --variant TARGETVARIANT set correctly" + assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--os --arch --variant TARGETPLATFORM set correctly" + + run_buildah build $WITH_POLICY_JSON --os linux -t source -f $containerfile + assert "$output" !~ "WARNING" \ + "With explicit --os (but no arch/variant), buildah should not warn about TARGETOS" + assert "$output" =~ "TARGETOS=linux" "--os TARGETOS set correctly" + assert "$output" =~ "TARGETARCH=amd64" "--os TARGETARCH set correctly" + assert "$output" =~ "TARGETVARIANT=" "--os TARGETVARIANT set correctly" + assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--os TARGETPLATFORM set correctly" + + run_buildah build $WITH_POLICY_JSON --arch amd64 -t source -f $containerfile + assert "$output" !~ "WARNING" \ + "With explicit --os (but no arch/variant), buildah should not warn about TARGETOS" + assert "$output" =~ "TARGETOS=linux" "--arch TARGETOS set correctly" + assert "$output" =~ "TARGETARCH=amd64" "--arch TARGETARCH set correctly" + assert "$output" =~ "TARGETVARIANT=" "--arch TARGETVARIANT set correctly" + assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--arch TARGETPLATFORM set correctly" + + for option in "--arch=arm64" "--os=windows" "--variant=v2"; do + run_buildah 125 build $WITH_POLICY_JSON --platform linux/amd64 ${option} \ + -t source -f $containerfile + assert "$output" =~ "Error: building system context: invalid --platform may not be used with --os, --arch, or --variant" "can't use --platform and one of --os, --arch or --variant together" + done } @test "build-conflicting-isolation-chroot-and-network" { @@ -5721,9 +5742,9 @@ _EOF @test "bud with --pull-always" { _prefetch docker.io/library/alpine run_buildah build --pull-always $WITH_POLICY_JSON -t testpull $BUDFILES/containerfile - expect_output --from="${lines[1]}" "Trying to pull docker.io/library/alpine:latest..." + expect_output --substring "Trying to pull docker.io/library/alpine:latest..." run_buildah build --pull=always $WITH_POLICY_JSON -t testpull $BUDFILES/containerfile - expect_output --from="${lines[1]}" "Trying to pull docker.io/library/alpine:latest..." + expect_output --substring "Trying to pull docker.io/library/alpine:latest..." } @test "bud with --memory and --memory-swap" { diff --git a/tests/bud/platform-sets-args/Containerfile b/tests/bud/platform-sets-args/Containerfile index dce2ec0f3a..ef629eae5c 100644 --- a/tests/bud/platform-sets-args/Containerfile +++ b/tests/bud/platform-sets-args/Containerfile @@ -4,3 +4,7 @@ ARG TARGETOS ARG TARGETPLATFORM ARG TARGETVARIANT ENV nothing="multiarch-safe statement that will result in a built image" +run echo TARGETARCH=${TARGETARCH} +run echo TARGETOS=${TARGETOS} +run echo TARGETPLATFORM=${TARGETPLATFORM} +run echo TARGETVARIANT=${TARGETVARANT} diff --git a/tests/conformance/conformance_test.go b/tests/conformance/conformance_test.go index 53f6f8233e..6432cc19db 100644 --- a/tests/conformance/conformance_test.go +++ b/tests/conformance/conformance_test.go @@ -3566,11 +3566,11 @@ func TestCommit(t *testing.T) { derivedBuilder, err := buildah.NewBuilder(ctx, store, buildah.BuilderOptions{ FromImage: buildahID, }) + require.NoErrorf(t, err, "creating the derived container with buildah") defer func(builder *buildah.Builder) { err := builder.Delete() assert.NoErrorf(t, err, "removing the derived container") }(derivedBuilder) - require.NoErrorf(t, err, "creating the derived container with buildah") var overrideConfig *manifest.Schema2Config if test.derivedConfig != nil { overrideConfig = config.Schema2ConfigFromGoDockerclientConfig(test.derivedConfig)