Skip to content

Commit

Permalink
Set empty TARGETVARIANT and BUILDVARIANT
Browse files Browse the repository at this point in the history
Docker does not set these values for ARM64.
This fixes the TestConformance/multistage-builtin-args and TestConformance/builtins tests on ARM64.

Signed-off-by: Jan Rodák <hony.com@seznam.cz>
  • Loading branch information
Honny1 committed Feb 18, 2025
1 parent 48cdd74 commit 49efa52
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 8 additions & 4 deletions imagebuildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,9 @@ func buildDockerfilesOnce(ctx context.Context, store storage.Store, logger *logr
// fill them in using values for the default platform
defaultPlatform := platforms.DefaultSpec()
builtinArgDefaults["TARGETOS"] = defaultPlatform.OS
builtinArgDefaults["TARGETVARIANT"] = defaultPlatform.Variant
builtinArgDefaults["TARGETVARIANT"] = ""
builtinArgDefaults["TARGETARCH"] = defaultPlatform.Architecture
builtinArgDefaults["TARGETPLATFORM"] = defaultPlatform.OS + "/" + defaultPlatform.Architecture
if defaultPlatform.Variant != "" {
builtinArgDefaults["TARGETPLATFORM"] += "/" + defaultPlatform.Variant
}
}
delete(options.Args, "TARGETPLATFORM")

Expand All @@ -453,6 +450,13 @@ func buildDockerfilesOnce(ctx context.Context, store storage.Store, logger *logr
for k, v := range builtinArgDefaults {
b.BuiltinArgDefaults[k] = v
}

buildVariant := b.BuiltinArgDefaults["BUILDVARIANT"]
buildPlatform := b.BuiltinArgDefaults["BUILDPLATFORM"]
cleanedBuildPlatform, _ := strings.CutSuffix(buildPlatform, "/"+buildVariant)
b.BuiltinArgDefaults["BUILDPLATFORM"] = cleanedBuildPlatform
b.BuiltinArgDefaults["BUILDVARIANT"] = ""

defaultContainerConfig, err := config.Default()
if err != nil {
return "", nil, fmt.Errorf("failed to get container config: %w", err)
Expand Down
12 changes: 8 additions & 4 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ _EOF
"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"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=\s" " --platform TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64/v2" " --platform TARGETPLATFORM set correctly"

# Likewise with individual args
Expand All @@ -300,23 +301,26 @@ _EOF
"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"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=\s" "--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=${ARCH}" "--os TARGETARCH set correctly"
assert "$output" =~ "TARGETVARIANT=" "--os TARGETVARIANT set correctly"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=\s" "--os TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/${ARCH}" "--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"
# By default, BUILDVARIANT/TARGETVARIANT should be empty.
assert "$output" =~ "TARGETVARIANT=\s" "--arch TARGETVARIANT set correctly"
assert "$output" =~ "TARGETPLATFORM=linux/amd64" "--arch TARGETPLATFORM set correctly"

for option in "--arch=arm64" "--os=windows" "--variant=v2"; do
Expand Down
2 changes: 1 addition & 1 deletion tests/conformance/conformance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3321,7 +3321,7 @@ var internalTestCases = []testCase{
},

{
name: "multistage-builtin-args",
name: "multistage-builtin-args", // By default, BUILDVARIANT/TARGETVARIANT should be empty.
dockerfile: "Dockerfile.margs",
dockerUseBuildKit: true,
},
Expand Down

0 comments on commit 49efa52

Please sign in to comment.