Skip to content

Commit 8674094

Browse files
Allan Clarkchickenandpork
authored andcommitted
feat: allow an optional platform definition for container arch
1 parent 51c555d commit 8674094

File tree

6 files changed

+136
-1
lines changed

6 files changed

+136
-1
lines changed

docker_compose_test/docker_compose_test.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ load("@rules_pkg//:pkg.bzl", "pkg_tar")
1717
load("@repo_absolute_path//:build_root.bzl", "BUILD_WORKSPACE_DIRECTORY")
1818
load("@rules_go//go:def.bzl", "go_test")
1919
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_load")
20+
load(":multi_platform.bzl", "multi_platform")
2021

2122
common_tags = [
2223
"docker", # these tests depend on docker
@@ -63,6 +64,7 @@ def go_docker_compose_test(
6364
data = [],
6465
tags = [],
6566
size = "large",
67+
platforms = None,
6668
**kwargs,
6769
):
6870
tags = common_tags + tags
@@ -79,8 +81,12 @@ def go_docker_compose_test(
7981
tags = tags + go_test_target_tags,
8082
testonly = True,
8183
)
84+
if platforms:
85+
multi_platform(name = name + ".go__test", actual = name + ".go_test", platforms = platforms, testonly = True)
86+
else:
87+
native.alias(name = name + ".go__test", actual = name + ".go_test", testonly = True)
8288

83-
compiled_tests_target = ":" + name + ".go_test"
89+
compiled_tests_target = ":" + name + ".go__test"
8490

8591
pkg_tar(
8692
name = name + ".compiled_go_test_target",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"a rule transitioning an oci_image to multiple platforms"
2+
# based (Apache-2.0) on rules_oci/examples/transition.bzl
3+
4+
5+
def _multiplatform_transition(settings, attr):
6+
"""this function simply returns a string-list of the platforms as a transition."""
7+
return [
8+
{"//command_line_option:platforms": str(platform)}
9+
for platform in attr.platforms
10+
]
11+
12+
multiplatform_transition = transition(
13+
# outputs a transition as a list of platforms as defined in the calling rule; these will cause
14+
# the referencing rule to split its definition based in iterations of the selected platforms as
15+
# thought given as a command-line argument
16+
17+
implementation = _multiplatform_transition,
18+
inputs = [],
19+
outputs = ["//command_line_option:platforms"],
20+
)
21+
22+
def _impl(ctx):
23+
"""this function mostly acts as an alias() rule but splits across combinations of 'platforms'"""
24+
return DefaultInfo(files = depset(ctx.files.actual))
25+
26+
multi_platform = rule(
27+
doc = "causes the rule chain of dependencies to be split across values of 'platforms' and replacing the default value (based on the host)",
28+
implementation = _impl,
29+
attrs = {
30+
"actual": attr.label(cfg = multiplatform_transition),
31+
"platforms": attr.label_list(),
32+
"_allowlist_function_transition": attr.label(
33+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
34+
),
35+
},
36+
)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2024, Salesforce, Inc.
2+
# SPDX-License-Identifier: Apache-2
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
load("@rules_docker_compose_test//docker_compose_test:docker_compose_test.bzl", "go_docker_compose_test")
17+
18+
# As compared to examples/go-test-image-test, this example will succeed on non-linux hosts that run
19+
# linux-archutecture containers (ie Mac "darwin")
20+
#
21+
# This example will compile linux unittests on a non-linux host, build a multi-arch OCI image,and
22+
# use docker-compose to turn up the appropriate container to execute the corresponding architecture
23+
# of compiled go testcase.
24+
#
25+
# We effectively overrule the default assumption that platform == host platform by providing two
26+
# choices in a transition defined in multi_platform.bzl which causes the targets to duplicate
27+
# across platform values and align corresponding platform values.
28+
#
29+
# We do this by:
30+
# 1. providing a list of platforms to iterate
31+
# 2. iterating that list in a transition around the `go_test()` target
32+
#
33+
# the same `cd examples && bazel test //go-test-image-test-with-platforms` demonstrates
34+
35+
ARCH=["arm64", "x86_64"]
36+
37+
[platform(
38+
name = "linux_{}".format(a),
39+
constraint_values = [
40+
"@platforms//os:linux", # yes, Mac docker runs linux images
41+
"@platforms//cpu:{}".format(a),
42+
],
43+
) for a in ARCH ]
44+
45+
# The following go_docker_compose_test adds a "platforms" attribute, and pathname-based changes:
46+
47+
go_docker_compose_test(
48+
name = "go-test-image-test-with-platforms",
49+
docker_compose_file = ":docker-compose.yml",
50+
docker_compose_test_container = "test_container", # container name is subdir:test_container
51+
platforms = [ "linux_{}".format(a) for a in ARCH ], # override default platform==host asusmption
52+
test_srcs = [ "example_test.go", "another_test.go" ],
53+
test_deps = [],
54+
test_image_base = "@ubuntu",
55+
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "testing"
4+
5+
// identical to //examples/go-test-image-test/another_test.go
6+
func TestExample2(t *testing.T) {
7+
t.Log("bar")
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright (c) 2024, Salesforce, Inc.
2+
# SPDX-License-Identifier: Apache-2
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
services:
17+
# service name matches <docker_compose_test_container>
18+
test_container:
19+
# image is <subdir>:<docker_compose_test_container>
20+
image: go-test-image-test-with-platforms:test_container
21+
# test binary is /tests/<target_name>.go_test
22+
entrypoint: ["tests/go-test-image-test-with-platforms.go_test"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "testing"
4+
5+
// identical to //examples/go-test-image-test/example_test.go
6+
func TestExample(t *testing.T) {
7+
t.Log("foo")
8+
}

0 commit comments

Comments
 (0)