Skip to content

Commit 4acf926

Browse files
authored
run tests concurrently and with timeouts (GoogleCloudPlatform#5240)
1 parent e6b5f28 commit 4acf926

File tree

10 files changed

+134
-141
lines changed

10 files changed

+134
-141
lines changed

.kokoro/java11/common.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ env_vars: {
4343
key: "TRAMPOLINE_IMAGE"
4444
value: "gcr.io/cloud-devrel-kokoro-resources/java11"
4545
}
46+
47+
# Access btlr binaries used in the tests
48+
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/btlr"
49+

.kokoro/java11/continuous.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ env_vars: {
1919
key: "TRAMPOLINE_BUILD_FILE"
2020
value: "github/java-docs-samples/.kokoro/tests/run_tests.sh"
2121
}
22+
23+
# Only diff from previous commit
24+
env_vars: {
25+
key: "GIT_DIFF"
26+
value: "HEAD~.. ."
27+
}

.kokoro/java11/presubmit.cfg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
# Tell the trampoline which build file to use.
1818
env_vars: {
1919
key: "TRAMPOLINE_BUILD_FILE"
20-
value: "github/java-docs-samples/.kokoro/tests/run_diff_only.sh"
20+
value: "github/java-docs-samples/.kokoro/tests/run_tests.sh"
2121
}
22+
# Only diff from master
23+
env_vars: {
24+
key: "GIT_DIFF"
25+
value: "origin/master... ."
26+
}

.kokoro/java8/common.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ env_vars: {
4343
key: "TRAMPOLINE_IMAGE"
4444
value: "gcr.io/cloud-devrel-kokoro-resources/java8"
4545
}
46+
47+
# Access btlr binaries used in the tests
48+
gfile_resources: "/bigstore/cloud-devrel-kokoro-resources/btlr"

.kokoro/java8/continuous.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ env_vars: {
2020
value: "github/java-docs-samples/.kokoro/tests/run_tests.sh"
2121
}
2222

23+
# Only diff from previous commit
24+
env_vars: {
25+
key: "GIT_DIFF"
26+
value: "HEAD~.. ."
27+
}

.kokoro/java8/presubmit.cfg

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@
1717
# Tell the trampoline which build file to use.
1818
env_vars: {
1919
key: "TRAMPOLINE_BUILD_FILE"
20-
value: "github/java-docs-samples/.kokoro/tests/run_diff_only.sh"
20+
value: "github/java-docs-samples/.kokoro/tests/run_tests.sh"
2121
}
22+
# Only diff from master
23+
env_vars: {
24+
key: "GIT_DIFF"
25+
value: "origin/master... ."
26+
}

.kokoro/tests/run_diff_only.sh

Lines changed: 0 additions & 16 deletions
This file was deleted.

.kokoro/tests/run_lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set -eo pipefail
1919

2020
# If on kokoro, add btlr to the path and cd into repo root
2121
if [ -n "$KOKORO_GFILE_DIR" ]; then
22-
bltr_dir="$KOKORO_GFILE_DIR/v0.0.1/"
22+
bltr_dir="$KOKORO_GFILE_DIR/v0.0.2/"
2323
chmod +x "${bltr_dir}"btlr
2424
export PATH="$PATH:$bltr_dir"
2525
cd github/java-docs-samples || exit

.kokoro/tests/run_test_java.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
# Copyright 2021 Google Inc.
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+
# Fail the tests if no Java version was found.
17+
POM_JAVA=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml)
18+
ALLOWED_VERSIONS=("1.8" "11")
19+
# shellcheck disable=SC2199
20+
# shellcheck disable=SC2076
21+
if [[ "$POM_JAVA" = "" ]] || [[ ! "${ALLOWED_VERSIONS[@]}" =~ "${POM_JAVA}" ]]; then
22+
RTN=1
23+
echo -e "\n Testing failed: Unable to determine Java version. Please set in pom:"
24+
echo -e "\n<properties>"
25+
echo -e " <maven.compiler.target>1.8</maven.compiler.target>"
26+
echo -e " <maven.compiler.source>1.8</maven.compiler.source>"
27+
echo -e "</properties>\n"
28+
exit 1
29+
fi
30+
31+
# Skip tests that don't have the correct Java version.
32+
# shellcheck disable=SC2076
33+
if ! [[ ",$JAVA_VERSION," =~ ",$POM_JAVA," ]]; then
34+
echo -e "\n Skipping tests: Java version ($POM_JAVA) not required ($JAVA_VERSION)\n"
35+
exit 0
36+
fi
37+
38+
# Use maven to execute the tests for the project.
39+
mvn --quiet --batch-mode --fail-at-end clean verify \
40+
-Dfile.encoding="UTF-8" \
41+
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
42+
-Dmaven.test.redirectTestOutputToFile=true \
43+
-Dbigtable.projectID="${GOOGLE_CLOUD_PROJECT}" \
44+
-Dbigtable.instanceID=instance
45+
EXIT=$?
46+
47+
if [[ $EXIT -ne 0 ]]; then
48+
RTN=1
49+
echo -e "\n Testing failed: Maven returned a non-zero exit code. \n"
50+
else
51+
echo -e "\n Testing completed.\n"
52+
fi
53+
54+
# Build and deploy Cloud Run samples
55+
if [[ "$file" == "run/"* ]]; then
56+
export SAMPLE_NAME=${file#"run/"}
57+
# chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh
58+
"$SCRIPT_DIR"/build_cloud_run.sh
59+
EXIT=$?
60+
61+
if [[ $EXIT -ne 0 ]]; then
62+
RTN=1
63+
echo -e "\n Cloud Run build/deploy failed: gcloud returned a non-zero exit code. \n"
64+
else
65+
echo -e "\n Cloud Run build/deploy completed.\n"
66+
fi
67+
fi
68+
69+
# If this is a periodic build, send the test log to the FlakyBot.
70+
# See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot.
71+
if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then
72+
chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot
73+
$KOKORO_GFILE_DIR/linux_amd64/flakybot
74+
fi
75+
76+
exit $RTN

.kokoro/tests/run_tests.sh

Lines changed: 27 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,28 @@ set -eo pipefail
1919
# Enables `**` to include files nested inside sub-folders
2020
shopt -s globstar
2121

22-
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
23-
2422
# `--script-debug` can be added make local testing of this script easier
2523
if [[ $* == *--script-debug* ]]; then
2624
SCRIPT_DEBUG="true"
27-
JAVA_VERSION="1.8"
25+
export JAVA_VERSION="1.8"
2826
else
2927
SCRIPT_DEBUG="false"
3028
fi
3129

32-
# `--only-diff` will only run tests on projects container changes from the master branch.
33-
if [[ $* == *--only-diff* ]]; then
34-
ONLY_DIFF="true"
35-
else
36-
ONLY_DIFF="false"
37-
fi
38-
3930
# Verify Java versions have been specified
4031
if [[ -z ${JAVA_VERSION+x} ]]; then
4132
echo -e "'JAVA_VERSION' env var should be a comma delimited list of valid java versions."
4233
exit 1
4334
fi
4435

36+
# If on kokoro, add btlr to the path and cd into repo root
37+
if [ -n "$KOKORO_GFILE_DIR" ]; then
38+
bltr_dir="$KOKORO_GFILE_DIR/v0.0.2/"
39+
chmod +x "${bltr_dir}"btlr
40+
export PATH="$PATH:$bltr_dir"
41+
cd github/java-docs-samples || exit
42+
fi
43+
4544
if [[ "$SCRIPT_DEBUG" != "true" ]]; then
4645
# Update `gcloud` and log versioning for debugging.
4746
gcloud components install beta --quiet
@@ -85,15 +84,6 @@ if [[ "$SCRIPT_DEBUG" != "true" ]]; then
8584
gcloud auth activate-service-account \
8685
--key-file="$GOOGLE_APPLICATION_CREDENTIALS" \
8786
--project="$GOOGLE_CLOUD_PROJECT"
88-
89-
cd github/java-docs-samples
90-
fi
91-
92-
# Don't check Appengine-java8 | flexible if using Java 11
93-
if [[ "$KOKORO_JOB_NAME" == */java11/* ]]; then
94-
SKIP_LEGACY_GAE="true"
95-
else
96-
SKIP_LEGACY_GAE="false"
9787
fi
9888

9989
# Package local jetty dependency for Java11 samples
@@ -103,109 +93,24 @@ if [[ ",$JAVA_VERSION," =~ "11" ]]; then
10393
cd ../../
10494
fi
10595

106-
echo -e "\n******************** TESTING PROJECTS ********************"
107-
# Switch to 'fail at end' to allow all tests to complete before exiting.
108-
set +e
109-
# Use RTN to return a non-zero value if the test fails.
110-
RTN=0
111-
ROOT=$(pwd)
112-
# Find all POMs in the repository (may break on whitespace).
113-
for file in **/pom.xml; do
114-
cd "$ROOT"
115-
# Navigate to the project folder.
116-
file=$(dirname "$file")
117-
cd "$file"
118-
119-
# Skip Legacy GAE
120-
if [[ "$SKIP_LEGACY_GAE" = "true" ]]; then
121-
if [[ ",$JAVA_VERSION," =~ "11" ]]; then
122-
case "$file" in
123-
*appengine-java8*)
124-
continue
125-
;;
126-
*flexible*)
127-
continue
128-
;;
129-
esac
130-
fi
131-
fi
132-
133-
# If $DIFF_ONLY is true, skip projects without changes.
134-
if [[ "$ONLY_DIFF" = "true" ]]; then
135-
git diff --quiet origin/master.. .
136-
CHANGED=$?
137-
if [[ "$CHANGED" -eq 0 ]]; then
138-
# echo -e "\n Skipping $file: no changes in folder.\n"
139-
continue
140-
fi
141-
fi
142-
143-
echo "------------------------------------------------------------"
144-
echo "- testing $file"
145-
echo "------------------------------------------------------------"
146-
147-
date
148-
149-
# Fail the tests if no Java version was found.
150-
POM_JAVA=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml)
151-
ALLOWED_VERSIONS=("1.8" "11")
152-
# shellcheck disable=SC2199
153-
# shellcheck disable=SC2076
154-
if [[ "$POM_JAVA" = "" ]] || [[ ! "${ALLOWED_VERSIONS[@]}" =~ "${POM_JAVA}" ]]; then
155-
RTN=1
156-
echo -e "\n Testing failed: Unable to determine Java version. Please set in pom:"
157-
echo -e "\n<properties>"
158-
echo -e " <maven.compiler.target>1.8</maven.compiler.target>"
159-
echo -e " <maven.compiler.source>1.8</maven.compiler.source>"
160-
echo -e "</properties>\n"
161-
continue
162-
fi
163-
164-
# Skip tests that don't have the correct Java version.
165-
# shellcheck disable=SC2076
166-
if ! [[ ",$JAVA_VERSION," =~ ",$POM_JAVA," ]]; then
167-
echo -e "\n Skipping tests: Java version ($POM_JAVA) not required ($JAVA_VERSION)\n"
168-
continue
169-
fi
170-
171-
# Use maven to execute the tests for the project.
172-
mvn --quiet --batch-mode --fail-at-end clean verify \
173-
-Dfile.encoding="UTF-8" \
174-
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
175-
-Dmaven.test.redirectTestOutputToFile=true \
176-
-Dbigtable.projectID="${GOOGLE_CLOUD_PROJECT}" \
177-
-Dbigtable.instanceID=instance
178-
EXIT=$?
179-
180-
if [[ $EXIT -ne 0 ]]; then
181-
RTN=1
182-
echo -e "\n Testing failed: Maven returned a non-zero exit code. \n"
183-
else
184-
echo -e "\n Testing completed.\n"
185-
fi
186-
187-
# Build and deploy Cloud Run samples
188-
if [[ "$file" == "run/"* ]]; then
189-
export SAMPLE_NAME=${file#"run/"}
190-
# chmod 755 "$SCRIPT_DIR"/build_cloud_run.sh
191-
"$SCRIPT_DIR"/build_cloud_run.sh
192-
EXIT=$?
193-
194-
if [[ $EXIT -ne 0 ]]; then
195-
RTN=1
196-
echo -e "\n Cloud Run build/deploy failed: gcloud returned a non-zero exit code. \n"
197-
else
198-
echo -e "\n Cloud Run build/deploy completed.\n"
199-
fi
200-
fi
96+
btlr_args=(
97+
"run"
98+
"--max-cmd-duration=20m"
99+
"**/pom.xml"
100+
)
101+
102+
if [ -n "$GIT_DIFF" ]; then
103+
btlr_args+=(
104+
"--git-diff"
105+
"$GIT_DIFF"
106+
)
107+
fi
201108

202-
# If this is a periodic build, send the test log to the FlakyBot.
203-
# See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot.
204-
if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then
205-
chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot
206-
$KOKORO_GFILE_DIR/linux_amd64/flakybot
207-
fi
109+
echo -e "\n******************** TESTING PROJECTS ********************"
110+
test_prog="$PWD/.kokoro/tests/run_test_java.sh"
208111

209-
done
112+
# Use btlr to run all the tests in each folder
113+
echo "btlr" "${btlr_args[@]}" -- "${test_prog}"
114+
btlr "${btlr_args[@]}" -- "${test_prog}"
210115

211-
exit "$RTN"
116+
exit $RTN

0 commit comments

Comments
 (0)