Skip to content

Commit 49eca8a

Browse files
authored
various features for 0.2.0 (#9)
* features for 0.2.0 * support images built with rules_docker
1 parent e07d634 commit 49eca8a

File tree

8 files changed

+183
-91
lines changed

8 files changed

+183
-91
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ services:
9898
image: junit-image-test:test_container
9999
entrypoint: ["/busybox/sh", "./test_container_entrypoint.sh"]
100100
environment:
101-
- JAVA_BINARY=/path/to/jdk/in/custom/image/bin/java
101+
- JAVA_HOME=/path/to/jdk/in/custom/image/bin/java
102102
- JUNIT_PARAMS=--include-classname com.something.integration.*
103103
```
104+
105+
## Pre compose up script
106+
107+
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.

docker_compose/docker_compose_test.bzl

Lines changed: 90 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,30 @@ load("@rules_pkg//:pkg.bzl", "pkg_tar")
1717
load("@repo_absolute_path//:build_root.bzl", "BUILD_WORKSPACE_DIRECTORY")
1818
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
1919

20+
common_tags = [
21+
"docker", # these tests depend on docker
22+
"exclusive", # these tests should run independent of others
23+
"external", # test has an external dependency; disable test caching
24+
]
25+
2026
def docker_compose_test(
2127
name,
2228
docker_compose_file,
2329
docker_compose_test_container,
30+
pre_compose_up_script = "",
2431
local_image_targets = "",
2532
data = [],
2633
tags = [],
2734
size = "large",
2835
**kwargs):
29-
tags = [
30-
"external", # test has an external dependency; disable test caching
31-
] + tags
32-
36+
tags = common_tags + tags
3337
data = data + [ docker_compose_file ]
38+
if len(pre_compose_up_script):
39+
data = data + [ pre_compose_up_script ]
3440
native.sh_test(
3541
name = name,
3642
srcs = ["@rules_docker_compose//docker_compose:docker_compose_test.sh"],
37-
env = {
38-
"WORKSPACE_PATH": BUILD_WORKSPACE_DIRECTORY,
39-
"DOCKER_COMPOSE_FILE": "$(location " + docker_compose_file + ")",
40-
"DOCKER_COMPOSE_TEST_CONTAINER": docker_compose_test_container,
41-
"LOCAL_IMAGE_TARGETS": local_image_targets.replace(":", "/"),
42-
},
43+
env = _get_env(docker_compose_file, local_image_targets, docker_compose_test_container, pre_compose_up_script),
4344
size = size,
4445
tags = tags,
4546
data = data,
@@ -51,6 +52,7 @@ def junit_docker_compose_test(
5152
name,
5253
docker_compose_file,
5354
docker_compose_test_container,
55+
pre_compose_up_script = "",
5456
local_image_targets = "",
5557
classpath_jars = [],
5658
test_image_base = None,
@@ -60,86 +62,92 @@ def junit_docker_compose_test(
6062
tags = [],
6163
size = "large",
6264
**kwargs):
63-
tags = [
64-
"external", # test has an external dependency; disable test caching
65-
] + tags
66-
65+
tags = common_tags + tags
6766
data = data + [ docker_compose_file ]
67+
if len(pre_compose_up_script):
68+
data = data + [ pre_compose_up_script ]
69+
70+
if test_image_base == None:
71+
fail("if you are definiing test_srcs, you need to provide a test_image_base")
72+
73+
# building an uber jar with test srcs & all dependencies
74+
native.java_binary(
75+
name = name + "_uber_jar",
76+
srcs = test_srcs,
77+
testonly = True,
78+
deps = test_deps,
79+
resources = test_deps,
80+
main_class = "not_used",
81+
)
82+
83+
# uber jar contains test classes
84+
pkg_tar(
85+
name = name + "_uber_jar_tar",
86+
srcs = [name + "_uber_jar_deploy.jar"],
87+
testonly = True,
88+
)
89+
90+
# these are jars that need to be on the classpath for the junit tests to execute
91+
pkg_tar(
92+
name = name + "_required_classpath_jars_tar",
93+
srcs = classpath_jars,
94+
testonly = True,
95+
)
96+
97+
# this is what actually runs the junit jar for your test execution
98+
pkg_tar(
99+
name = name + "_test_container_entrypoint",
100+
srcs = ["@rules_docker_compose//docker_compose:test_container_entrypoint.sh"],
101+
)
68102

69-
# if there are srcs, build a test container that includes the tests
70-
if len(test_srcs):
71-
72-
if test_image_base == None:
73-
fail("if you are definiing test_srcs, you need to provide a test_image_base")
74-
75-
# building an uber jar with test srcs & all dependencies
76-
native.java_binary(
77-
name = name + "_uber_jar",
78-
srcs = test_srcs,
79-
testonly = True,
80-
deps = test_deps,
81-
main_class = "not_used",
82-
)
83-
84-
# uber jar contains test classes
85-
pkg_tar(
86-
name = name + "_uber_jar_tar",
87-
srcs = [name + "_uber_jar"],
88-
testonly = True,
89-
)
90-
91-
# these are jars that need to be on the classpath for the junit tests to execute
92-
pkg_tar(
93-
name = name + "_required_classpath_jars_tar",
94-
srcs = classpath_jars,
95-
testonly = True,
96-
)
97-
98-
# this is what actually runs the junit jar for your test execution
99-
pkg_tar(
100-
name = name + "_test_container_entrypoint",
101-
srcs = ["@rules_docker_compose//docker_compose:test_container_entrypoint.sh"],
102-
)
103-
104-
oci_image(
105-
name = "java_image",
106-
base = test_image_base,
107-
tars = [
108-
name + "_uber_jar_tar",
109-
name + "_required_classpath_jars_tar",
110-
name + "_test_container_entrypoint",
111-
],
112-
testonly = True,
113-
)
114-
115-
oci_tarball(
116-
name = docker_compose_test_container,
117-
image = ":java_image",
118-
repo_tags = ["%s:%s" % (native.package_name(), docker_compose_test_container)],
119-
testonly = True,
120-
)
121-
122-
# this builds & installs the test image.
123-
native.sh_binary(
124-
name = name + "_integration_test_image_fixture",
125-
srcs = [docker_compose_test_container],
126-
testonly = True,
127-
)
128-
129-
data.append(name + "_integration_test_image_fixture")
130-
local_image_targets += "%s:%s" % (native.package_name(), docker_compose_test_container)
103+
oci_image(
104+
name = name.lower() + "_java_image",
105+
base = test_image_base,
106+
tars = [
107+
name + "_uber_jar_tar",
108+
name + "_required_classpath_jars_tar",
109+
name + "_test_container_entrypoint",
110+
],
111+
testonly = True,
112+
)
131113

114+
oci_tarball(
115+
name = docker_compose_test_container,
116+
image = name.lower() + "_java_image",
117+
repo_tags = ["%s:%s" % (native.package_name(), docker_compose_test_container)],
118+
testonly = True,
119+
)
120+
121+
# this builds & installs the test image.
122+
native.sh_binary(
123+
name = name + "_integration_test_image_fixture",
124+
srcs = [docker_compose_test_container],
125+
testonly = True,
126+
)
127+
128+
data.append(name + "_integration_test_image_fixture")
129+
if len(local_image_targets):
130+
local_image_targets += ","
131+
local_image_targets += "%s:%s" % (native.package_name(), docker_compose_test_container)
132132
native.sh_test(
133133
name = name,
134134
srcs = ["@rules_docker_compose//docker_compose:docker_compose_test.sh"],
135-
env = {
136-
"WORKSPACE_PATH": BUILD_WORKSPACE_DIRECTORY,
137-
"DOCKER_COMPOSE_FILE": "$(location " + docker_compose_file + ")",
138-
"DOCKER_COMPOSE_TEST_CONTAINER": docker_compose_test_container,
139-
"LOCAL_IMAGE_TARGETS": local_image_targets.replace(":", "/"),
140-
},
135+
env = _get_env(docker_compose_file, local_image_targets, docker_compose_test_container, pre_compose_up_script),
141136
size = size,
142137
tags = tags,
143138
data = data,
144139
**kwargs,
145140
)
141+
142+
143+
def _get_env(docker_compose_file, local_image_targets, docker_compose_test_container, pre_compose_up_script):
144+
env = {
145+
"WORKSPACE_PATH": BUILD_WORKSPACE_DIRECTORY,
146+
"DOCKER_COMPOSE_FILE": "$(location " + docker_compose_file + ")",
147+
"LOCAL_IMAGE_TARGETS": local_image_targets.replace(":", "/"),
148+
"DOCKER_COMPOSE_TEST_CONTAINER": docker_compose_test_container,
149+
}
150+
151+
if len(pre_compose_up_script):
152+
env["PRE_COMPOSE_UP_SCRIPT"] = "$(location " + pre_compose_up_script + ")"
153+
return env

docker_compose/docker_compose_test.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,36 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
# we should not use "set -e" here because we want the docker-compose down to happen at the end regardless of failure/success.
17+
1618
# start by building any local images that are needed for the docker-compose tests
1719
export IFS=","
1820
echo $LOCAL_IMAGE_TARGETS
1921
for LOCAL_IMAGE_TARGET in $LOCAL_IMAGE_TARGETS; do
20-
s="$LOCAL_IMAGE_TARGET.sh"
21-
$s
22+
# rules_oci image install script
23+
if [ -f "$LOCAL_IMAGE_TARGET.sh" ]; then
24+
"$LOCAL_IMAGE_TARGET.sh"
25+
# rules_docker image install script
26+
elif [ -f "$LOCAL_IMAGE_TARGET.executable" ]; then
27+
"$LOCAL_IMAGE_TARGET.executable"
28+
else
29+
echo "[ERROR] no install script present for $LOCAL_IMAGE_TARGET"
30+
exit 1
31+
fi
2232
done
2333

34+
# PRE_COMPOSE_UP_SCRIPT is set
35+
if [[ -n "$PRE_COMPOSE_UP_SCRIPT" ]]; then
36+
# we want to move to the location of the script before executing it
37+
# so paths are relative to it. After the script is executed, we move
38+
# back to the original location.
39+
location=$(pwd)
40+
cd $WORKSPACE_PATH
41+
cd $(dirname $PRE_COMPOSE_UP_SCRIPT)
42+
$(basename $PRE_COMPOSE_UP_SCRIPT)
43+
cd $location
44+
fi
45+
2446
# we need to use the path of the real compose file in the file-tree.
2547
# if we use the file from inside the sandbox, symlinks will be used for volume mounted files.
2648
ABSOLUTE_COMPOSE_FILE_PATH=$WORKSPACE_PATH/$DOCKER_COMPOSE_FILE

docker_compose/test_container_entrypoint.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
if [[ -z "$JAVA_BINARY" ]]; then
17-
JAVA_BINARY="/usr/bin/java"
16+
if [[ -z "$JAVA_HOME" ]]; then
17+
JAVA_HOME="/usr"
1818
fi
1919

20-
TEST_UBER_JAR=$(find ./ -maxdepth 1 -name '*_uber_jar.jar')
20+
TEST_UBER_JAR=$(find ./ -maxdepth 1 -name '*_uber_jar_deploy.jar')
2121
JUNIT_PLATFORM_CONSOLE_STANDALONE_JAR=$(find ./ -maxdepth 1 -name '*junit-platform-console-standalone*.jar')
2222

2323
# TODO: need to find a better solution than adding all of the jars to the class-path below
2424
# The only one we should need to add is the fat jar because it should contain the rest of them..
2525
# However, it seems like we need to add all of the spring/spring-boot jars like this.
26-
JARS=$(find ./ -maxdepth 1 -name '*.jar')
26+
JARS=$(find ./ -maxdepth 1 -name '*.jar' ! -name '*_uber_jar_deploy.jar')
2727
CLASS_PATH_STRING=""
2828
for JAR in $JARS; do
2929
CLASS_PATH_STRING="$CLASS_PATH_STRING --class-path $JAR"
3030
done
3131

32-
# JUNIT_PARAMS can be set in their compose file to filter for specific tests
32+
# JUNIT_PARAMS can be set in the docker-compose file to filter for specific tests
3333
# e.g. --include-classname com.something.integration.*
34-
$JAVA_BINARY -jar $JUNIT_PLATFORM_CONSOLE_STANDALONE_JAR --class-path $TEST_UBER_JAR --scan-class-path --fail-if-no-tests $CLASS_PATH_STRING $JUNIT_PARAMS
34+
cmd="$JAVA_HOME/bin/java -jar $JUNIT_PLATFORM_CONSOLE_STANDALONE_JAR \
35+
--scan-class-path --fail-if-no-tests $CLASS_PATH_STRING --class-path $TEST_UBER_JAR $JUNIT_PARAMS"
36+
echo $cmd
37+
$cmd
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) 2023, 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//docker_compose:docker_compose_test.bzl", "docker_compose_test")
17+
18+
docker_compose_test(
19+
name = "pre-compose-up-script-test",
20+
docker_compose_file = ":docker-compose.yml",
21+
docker_compose_test_container = "test_container",
22+
pre_compose_up_script = ":src/test/resources/setup_test.sh"
23+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2023, 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: gcr.io/distroless/static-debian12:debug
19+
entrypoint: ["cat", "/resources/generated_file.txt"]
20+
volumes:
21+
- ./src/test/resources:/resources
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# we don't want to check-in the generated file
2+
# because that's what we're actually testing here
3+
# (that the file is generated when the test is executed).
4+
generated_file.txt
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# This file is created as a pre-req to the docker-compose test actually running.
5+
# Locally, the file will always be there but it won't be checked in as it is in .gitignore.
6+
# CI will make sure that the file is generated on every execution.
7+
touch generated_file.txt

0 commit comments

Comments
 (0)