From 42f133369a9f5a121bacefa69dd19944d5cadc8b Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 10 Jan 2022 15:27:26 +0100 Subject: [PATCH] Fix platform handling for empty os/arch values If you run `buildah bud --platform windows/` the os is ignored at the moment and it uses the host os. We should still use the os from the platform in this case. This fixes an issue with podman-remote build where it is possible that only the os is set in the platform string. Signed-off-by: Paul Holzinger --- imagebuildah/build.go | 6 +++++- tests/bud.bats | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/imagebuildah/build.go b/imagebuildah/build.go index 3c8150ef6d3..cefb1857ef6 100644 --- a/imagebuildah/build.go +++ b/imagebuildah/build.go @@ -216,8 +216,12 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B Architecture: platform.Arch, Variant: platform.Variant, }) - if platformSpec.OS != "" && platformSpec.Architecture != "" { + // platforms.Normalize converts an empty os value to GOOS + // so we have to check the original value here to not overwrite the default for no reason + if platform.OS != "" { platformContext.OSChoice = platformSpec.OS + } + if platform.Arch != "" { platformContext.ArchitectureChoice = platformSpec.Architecture platformContext.VariantChoice = platformSpec.Variant } diff --git a/tests/bud.bats b/tests/bud.bats index ffefe58b82c..1964d5c111a 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -2429,6 +2429,30 @@ EOM expect_output arm } +@test "bud with custom platform and empty os or arch" { + run_buildah build --signature-policy ${TESTSDIR}/policy.json \ + -f ${TESTSDIR}/bud/from-scratch/Containerfile \ + -t platform-test \ + --platform=windows/ + + run_buildah inspect --format "{{ .Docker.OS }}" platform-test + expect_output windows + + run_buildah inspect --format "{{ .OCIv1.OS }}" platform-test + expect_output windows + + run_buildah build --signature-policy ${TESTSDIR}/policy.json \ + -f ${TESTSDIR}/bud/from-scratch/Containerfile \ + -t platform-test2 \ + --platform=/arm + + run_buildah inspect --format "{{ .Docker.Architecture }}" platform-test2 + expect_output arm + + run_buildah inspect --format "{{ .OCIv1.Architecture }}" platform-test2 + expect_output arm +} + @test "bud Add with linked tarball" { _prefetch alpine run_buildah build --signature-policy ${TESTSDIR}/policy.json -f ${TESTSDIR}/bud/symlink/Containerfile.add-tar-with-link -t testctr ${TESTSDIR}/bud/symlink