Skip to content

feat: allow an optional platform definition for container arch #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docker_compose_test/docker_compose_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@repo_absolute_path//:build_root.bzl", "BUILD_WORKSPACE_DIRECTORY")
load("@rules_go//go:def.bzl", "go_test")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
load(":multi_platform.bzl", "multi_platform")

common_tags = [
"docker", # these tests depend on docker
Expand Down Expand Up @@ -63,6 +64,7 @@ def go_docker_compose_test(
data = [],
tags = [],
size = "large",
platforms = None,
**kwargs,
):
tags = common_tags + tags
Expand All @@ -79,8 +81,12 @@ def go_docker_compose_test(
tags = tags + go_test_target_tags,
testonly = True,
)
if platforms:
multi_platform(name = name + ".go__test", actual = name + ".go_test", platforms = platforms, testonly = True)
else:
native.alias(name = name + ".go__test", actual = name + ".go_test", testonly = True)

compiled_tests_target = ":" + name + ".go_test"
compiled_tests_target = ":" + name + ".go__test"

pkg_tar(
name = name + ".compiled_go_test_target",
Expand Down
36 changes: 36 additions & 0 deletions docker_compose_test/multi_platform.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"a rule transitioning an oci_image to multiple platforms"
# based (Apache-2.0) on rules_oci/examples/transition.bzl


def _multiplatform_transition(settings, attr):
"""this function simply returns a string-list of the platforms as a transition."""
return [
{"//command_line_option:platforms": str(platform)}
for platform in attr.platforms
]

multiplatform_transition = transition(
# outputs a transition as a list of platforms as defined in the calling rule; these will cause
# the referencing rule to split its definition based in iterations of the selected platforms as
# thought given as a command-line argument

implementation = _multiplatform_transition,
inputs = [],
outputs = ["//command_line_option:platforms"],
)

def _impl(ctx):
"""this function mostly acts as an alias() rule but splits across combinations of 'platforms'"""
return DefaultInfo(files = depset(ctx.files.actual))

multi_platform = rule(
doc = "causes the rule chain of dependencies to be split across values of 'platforms' and replacing the default value (based on the host)",
implementation = _impl,
attrs = {
"actual": attr.label(cfg = multiplatform_transition),
"platforms": attr.label_list(),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
)
55 changes: 55 additions & 0 deletions examples/go-test-image-test-with-platforms/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright (c) 2024, Salesforce, Inc.
# SPDX-License-Identifier: Apache-2

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_docker_compose_test//docker_compose_test:docker_compose_test.bzl", "go_docker_compose_test")

# As compared to examples/go-test-image-test, this example will succeed on non-linux hosts that run
# linux-archutecture containers (ie Mac "darwin")
#
# This example will compile linux unittests on a non-linux host, build a multi-arch OCI image,and
# use docker-compose to turn up the appropriate container to execute the corresponding architecture
# of compiled go testcase.
#
# We effectively overrule the default assumption that platform == host platform by providing two
# choices in a transition defined in multi_platform.bzl which causes the targets to duplicate
# across platform values and align corresponding platform values.
#
# We do this by:
# 1. providing a list of platforms to iterate
# 2. iterating that list in a transition around the `go_test()` target
#
# the same `cd examples && bazel test //go-test-image-test-with-platforms` demonstrates

ARCH=["arm64", "x86_64"]

[platform(
name = "linux_{}".format(a),
constraint_values = [
"@platforms//os:linux", # yes, Mac docker runs linux images
"@platforms//cpu:{}".format(a),
],
) for a in ARCH ]

# The following go_docker_compose_test adds a "platforms" attribute, and pathname-based changes:

go_docker_compose_test(
name = "go-test-image-test-with-platforms",
docker_compose_file = ":docker-compose.yml",
docker_compose_test_container = "test_container", # container name is subdir:test_container
platforms = [ "linux_{}".format(a) for a in ARCH ], # override default platform==host asusmption
test_srcs = [ "example_test.go", "another_test.go" ],
test_deps = [],
test_image_base = "@ubuntu",
)
8 changes: 8 additions & 0 deletions examples/go-test-image-test-with-platforms/another_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "testing"

// identical to //examples/go-test-image-test/another_test.go
func TestExample2(t *testing.T) {
t.Log("bar")
}
22 changes: 22 additions & 0 deletions examples/go-test-image-test-with-platforms/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2024, Salesforce, Inc.
# SPDX-License-Identifier: Apache-2

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

services:
# service name matches <docker_compose_test_container>
test_container:
# image is <subdir>:<docker_compose_test_container>
image: go-test-image-test-with-platforms:test_container
# test binary is /tests/<target_name>.go_test
entrypoint: ["tests/go-test-image-test-with-platforms.go_test"]
8 changes: 8 additions & 0 deletions examples/go-test-image-test-with-platforms/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "testing"

// identical to //examples/go-test-image-test/example_test.go
func TestExample(t *testing.T) {
t.Log("foo")
}
Loading