Skip to content

Commit 32eda23

Browse files
authored
Support go_docker_compose_test + add example (#22)
* support go docker compose test + example * clean up * change cw file
1 parent 3809e8f commit 32eda23

File tree

8 files changed

+214
-10
lines changed

8 files changed

+214
-10
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ services:
7878

7979
### junit_docker_compose_test
8080

81+
```
82+
(cd examples && bazel test junit-image-test)
83+
```
84+
8185
In this example, the test source files, dependencies, classpath jars and test image base are passed into the macro. The macro builds a docker image with all of these and then uses the junit standalone jar to execute the tests. You can use some magic customization environment variables in the docker-compose file.
8286
8387
```starlark
88+
load("@rules_docker_compose_test//docker_compose_test:docker_compose_test.bzl", "junit_docker_compose_test")
89+
8490
junit_docker_compose_test(
8591
name = "junit-image-test",
8692
docker_compose_file = ":docker-compose.yml",
@@ -103,14 +109,42 @@ services:
103109
- JVM_ARGS=-Dfoo.bar=true
104110
```
105111
112+
#### javacopts
113+
114+
You can pass `javacopts` when building the uber junit test binary using `uber_jar_javacopts`.
115+
116+
### go_docker_compose_test
117+
118+
```
119+
(cd examples && bazel test --platforms=@io_bazel_rules_go//go/toolchain:linux_amd64 go-test-image-test)
120+
```
121+
122+
In this example, the test source files, dependencies and test image base are passed into the macro. The macro builds a binary containing the test source files and dependencies which are executed as part of the docker-compose test.
123+
124+
```starlark
125+
load("@rules_docker_compose_test//docker_compose_test:docker_compose_test.bzl", "go_docker_compose_test")
126+
127+
go_docker_compose_test(
128+
name = "go-test-image-test",
129+
docker_compose_file = ":docker-compose.yml",
130+
docker_compose_test_container = "test_container",
131+
test_srcs = glob(["**/*_test.go"]),
132+
test_deps = [],
133+
test_image_base = "@ubuntu",
134+
)
135+
```
136+
137+
```yaml
138+
services:
139+
test_container:
140+
image: go-test-image-test:test_container
141+
entrypoint: ["tests/go-test-image-test.go_test"]
142+
```
143+
106144
## pre_compose_up_script
107145
108146
Sometimes, you may need some logic to run before the compose test containers come up. You can use `pre_compose_up_script` for that purpose. See [examples/pre-compose-up-script-test](examples/pre-compose-up-script-test) for an example.
109147

110148
## extra_docker_compose_up_args
111149

112150
You can append extra arguments to the `docker compose up` command using `extra_docker_compose_up_args`. See [examples/pre-compose-up-script-test](examples/pre-compose-up-script-test) for an example.
113-
114-
## javacopts
115-
116-
You can pass `javacopts` when building the uber junit test binary using `uber_jar_javacopts`.

docker_compose_test/docker_compose_test.bzl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
load("@rules_pkg//:pkg.bzl", "pkg_tar")
1717
load("@repo_absolute_path//:build_root.bzl", "BUILD_WORKSPACE_DIRECTORY")
18+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
1819
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
1920

2021
common_tags = [
@@ -48,6 +49,85 @@ def docker_compose_test(
4849
**kwargs,
4950
)
5051

52+
def go_docker_compose_test(
53+
name,
54+
docker_compose_file,
55+
docker_compose_test_container,
56+
pre_compose_up_script = "",
57+
extra_docker_compose_up_args = "",
58+
local_image_targets = "",
59+
test_image_base = None,
60+
test_srcs = [],
61+
test_deps = [],
62+
data = [],
63+
tags = [],
64+
size = "large",
65+
**kwargs,
66+
):
67+
tags = common_tags + tags
68+
data = data + [ docker_compose_file ]
69+
if len(pre_compose_up_script):
70+
data = data + [ pre_compose_up_script ]
71+
if test_image_base == None:
72+
fail("if you are defining test_srcs, you need to provide a test_image_base")
73+
74+
go_test(
75+
name = name + ".go_test",
76+
srcs = test_srcs,
77+
deps = test_deps,
78+
tags = tags,
79+
testonly = True,
80+
)
81+
82+
compiled_tests_target = ":" + name + ".go_test"
83+
84+
pkg_tar(
85+
name = name + ".compiled_go_test_target",
86+
srcs = [compiled_tests_target],
87+
package_dir = "/tests",
88+
tags = tags,
89+
testonly = True,
90+
)
91+
92+
oci_image(
93+
name = name + ".oci_image",
94+
base = test_image_base,
95+
tars = [
96+
name + ".compiled_go_test_target",
97+
],
98+
tags = tags,
99+
testonly = True,
100+
)
101+
102+
oci_tarball(
103+
name = docker_compose_test_container,
104+
image = name + ".oci_image",
105+
repo_tags = ["%s:%s" % (native.package_name(), docker_compose_test_container)],
106+
tags = tags,
107+
testonly = True,
108+
)
109+
110+
# this builds & installs the test image.
111+
native.sh_binary(
112+
name = name + ".integration_test_image_fixture",
113+
srcs = [docker_compose_test_container],
114+
testonly = True,
115+
)
116+
117+
data.append(name + ".integration_test_image_fixture")
118+
if len(local_image_targets):
119+
local_image_targets += ","
120+
local_image_targets += "%s:%s" % (native.package_name(), docker_compose_test_container)
121+
native.sh_test(
122+
name = name,
123+
srcs = ["@rules_docker_compose_test//docker_compose_test:docker_compose_test.sh"],
124+
env = _get_env(docker_compose_file, local_image_targets, docker_compose_test_container, pre_compose_up_script, extra_docker_compose_up_args),
125+
size = size,
126+
tags = tags,
127+
data = data,
128+
**kwargs,
129+
)
130+
51131

52132
def junit_docker_compose_test(
53133
name,

examples/WORKSPACE

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ local_repository(
2222

2323
http_archive(
2424
name = "rules_oci",
25-
sha256 = "4a276e9566c03491649eef63f27c2816cc222f41ccdebd97d2c5159e84917c3b",
26-
strip_prefix = "rules_oci-1.7.4",
27-
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.7.4/rules_oci-v1.7.4.tar.gz",
25+
sha256 = "46ce9edcff4d3d7b3a550774b82396c0fa619cc9ce9da00c1b09a08b45ea5a14",
26+
strip_prefix = "rules_oci-1.8.0",
27+
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.8.0/rules_oci-v1.8.0.tar.gz",
2828
)
2929

3030
RULES_JVM_EXTERNAL_TAG = "6.0"
@@ -56,6 +56,16 @@ oci_pull(
5656
image = "gcr.io/distroless/java17",
5757
)
5858

59+
oci_pull(
60+
name = "ubuntu",
61+
digest = "sha256:278628f08d4979fb9af9ead44277dbc9c92c2465922310916ad0c46ec9999295",
62+
image = "ubuntu",
63+
platforms = [
64+
"linux/amd64",
65+
"linux/arm64/v8",
66+
],
67+
)
68+
5969
load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")
6070
rules_jvm_external_deps()
6171

@@ -86,3 +96,7 @@ rules_docker_compose_test_repositories()
8696
load("@rules_docker_compose_test//:setup.bzl", "rules_docker_compose_test_dependencies", "repo_absolute_path")
8797
rules_docker_compose_test_dependencies()
8898
repo_absolute_path(name="repo_absolute_path")
99+
100+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
101+
go_rules_dependencies()
102+
go_register_toolchains(version = "1.23.0")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
go_docker_compose_test(
19+
name = "go-test-image-test",
20+
docker_compose_file = ":docker-compose.yml",
21+
docker_compose_test_container = "test_container",
22+
test_srcs = glob(["**/*_test.go"]),
23+
test_deps = [],
24+
test_image_base = "@ubuntu",
25+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestExample2(t *testing.T) {
6+
t.Log("bar")
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
test_container:
18+
image: go-test-image-test:test_container
19+
entrypoint: ["tests/go-test-image-test.go_test"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestExample(t *testing.T) {
6+
t.Log("foo")
7+
}

repositories.bzl

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,25 @@ def rules_docker_compose_test_repositories():
3030

3131
http_archive(
3232
name = "rules_oci",
33-
sha256 = "4a276e9566c03491649eef63f27c2816cc222f41ccdebd97d2c5159e84917c3b",
34-
strip_prefix = "rules_oci-1.7.4",
35-
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.7.4/rules_oci-v1.7.4.tar.gz",
33+
sha256 = "46ce9edcff4d3d7b3a550774b82396c0fa619cc9ce9da00c1b09a08b45ea5a14",
34+
strip_prefix = "rules_oci-1.8.0",
35+
url = "https://github.com/bazel-contrib/rules_oci/releases/download/v1.8.0/rules_oci-v1.8.0.tar.gz",
36+
)
37+
38+
http_archive(
39+
name = "io_bazel_rules_go",
40+
sha256 = "f4a9314518ca6acfa16cc4ab43b0b8ce1e4ea64b81c38d8a3772883f153346b8",
41+
urls = [
42+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.50.1/rules_go-v0.50.1.zip",
43+
"https://github.com/bazelbuild/rules_go/releases/download/v0.50.1/rules_go-v0.50.1.zip",
44+
],
45+
)
46+
47+
http_archive(
48+
name = "platforms",
49+
urls = [
50+
"https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz",
51+
"https://github.com/bazelbuild/platforms/releases/download/0.0.10/platforms-0.0.10.tar.gz",
52+
],
53+
sha256 = "218efe8ee736d26a3572663b374a253c012b716d8af0c07e842e82f238a0a7ee",
3654
)

0 commit comments

Comments
 (0)