|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
| 16 | +# `-e` enables the script to automatically fail when a command fails |
| 17 | +# `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero |
16 | 18 | set -eo pipefail |
| 19 | +# Enables `**` to include files nested inside sub-folders |
17 | 20 | shopt -s globstar |
18 | | -# We spin up some subprocesses. Don't kill them on hangup |
19 | | -trap '' HUP |
20 | | - |
21 | | -# Update gcloud and check version |
22 | | -gcloud components update --quiet |
23 | | -echo "********** GCLOUD INFO ***********" |
24 | | -gcloud -v |
25 | | -echo "********** MAVEN INFO ***********" |
26 | | -mvn -v |
27 | | -echo "********** GRADLE INFO ***********" |
28 | | -gradle -v |
29 | | - |
30 | | -# Setup required enviormental variables |
31 | | -export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json |
32 | | -export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing |
33 | | -source ${KOKORO_GFILE_DIR}/aws-secrets.sh |
34 | | -source ${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh |
35 | | -source ${KOKORO_GFILE_DIR}/dlp_secrets.txt |
36 | | -# Activate service account |
37 | | -gcloud auth activate-service-account\ |
38 | | - --key-file=$GOOGLE_APPLICATION_CREDENTIALS \ |
39 | | - --project=$GOOGLE_CLOUD_PROJECT |
40 | | - |
41 | | -echo -e "\n******************** TESTING AFFECTED PROJECTS ********************" |
| 21 | + |
| 22 | +# `--debug` can be added make local testing of this script easier |
| 23 | +if [[ $* == *--script-debug* ]]; then |
| 24 | + SCRIPT_DEBUG="true" |
| 25 | + JAVA_VERSION="1.8" |
| 26 | +else |
| 27 | + SCRIPT_DEBUG="false" |
| 28 | +fi |
| 29 | + |
| 30 | +# `--only-changed` will only run tests on projects container changes from the master branch. |
| 31 | +if [[ $* == *--only-diff* ]]; then |
| 32 | + ONLY_DIFF="true" |
| 33 | +else |
| 34 | + ONLY_DIFF="false" |
| 35 | +fi |
| 36 | + |
| 37 | +# Verify Java versions have been specified |
| 38 | +if [[ -z ${JAVA_VERSION+x} ]]; then |
| 39 | + echo -e "'JAVA_VERSION' env var should be a comma delimited list of valid java versions." |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +if [[ "$SCRIPT_DEBUG" != "true" ]]; then |
| 44 | + # Update `gcloud` and log versioning for debugging. |
| 45 | + gcloud components update --quiet |
| 46 | + echo "********** GCLOUD INFO ***********" |
| 47 | + gcloud -v |
| 48 | + echo "********** MAVEN INFO ***********" |
| 49 | + mvn -v |
| 50 | + echo "********** GRADLE INFO ***********" |
| 51 | + gradle -v |
| 52 | + |
| 53 | + # Setup required env variables |
| 54 | + export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing |
| 55 | + export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json |
| 56 | + source "${KOKORO_GFILE_DIR}/aws-secrets.sh" |
| 57 | + source "${KOKORO_GFILE_DIR}/storage-hmac-credentials.sh" |
| 58 | + source "${KOKORO_GFILE_DIR}/dlp_secrets.txt" |
| 59 | + # Activate service account |
| 60 | + gcloud auth activate-service-account \ |
| 61 | + --key-file="$GOOGLE_APPLICATION_CREDENTIALS" \ |
| 62 | + --project="$GOOGLE_CLOUD_PROJECT" |
| 63 | + |
| 64 | + cd github/java-docs-samples |
| 65 | +fi |
| 66 | + |
| 67 | +echo -e "\n******************** TESTING PROJECTS ********************" |
| 68 | +# Switch to 'fail at end' to allow all tests to complete before exiting. |
42 | 69 | set +e |
43 | | -RESULT=0 |
44 | | -cd github/java-docs-samples |
45 | | -# For every pom.xml (may break on whitespace) |
| 70 | +# Use RTN to return a non-zero value if the test fails. |
| 71 | +RTN=0 |
| 72 | +ROOT=$(pwd) |
| 73 | +# Find all POMs in the repository (may break on whitespace). |
46 | 74 | for file in **/pom.xml; do |
47 | | - # Navigate to project |
| 75 | + cd "$ROOT" |
| 76 | + # Navigate to the project folder. |
48 | 77 | file=$(dirname "$file") |
49 | | - pushd "$file" > /dev/null |
50 | | - |
51 | | - # Only test leafs to prevent testing twice |
52 | | - PARENT=$(grep "<modules>" pom.xml -c) |
53 | | - |
54 | | - # Get the Java version from the pom.xml |
55 | | - VERSION=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml) |
56 | | - |
57 | | - # Check for changes to the current folder |
58 | | - if [ "$PARENT" -eq 0 ] && [[ "$JAVA_VERSION" = *"$VERSION"* ]]; then |
59 | | - echo "------------------------------------------------------------" |
60 | | - echo "- testing $file" |
61 | | - echo "------------------------------------------------------------" |
62 | | - |
63 | | - # Run tests and update RESULT if failed |
64 | | - mvn -q --batch-mode --fail-at-end clean verify \ |
65 | | - -Dfile.encoding="UTF-8" \ |
66 | | - -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \ |
67 | | - -Dmaven.test.redirectTestOutputToFile=true \ |
68 | | - -Dbigtable.projectID="${GOOGLE_CLOUD_PROJECT}" \ |
69 | | - -Dbigtable.instanceID=instance |
70 | | - EXIT=$? |
71 | | - |
72 | | - if [ $EXIT -ne 0 ]; then |
73 | | - echo -e "\n Tests failed. \n" |
74 | | - RESULT=1 |
75 | | - else |
76 | | - echo -e "\n Tests complete. \n" |
| 78 | + cd "$file" |
| 79 | + |
| 80 | + # If $DIFF_ONLY is true, skip projects without changes. |
| 81 | + if [[ "$ONLY_DIFF" = "true" ]]; then |
| 82 | + git diff --quiet origin/master.. . |
| 83 | + CHANGED=$? |
| 84 | + if [[ "$CHANGED" -eq 0 ]]; then |
| 85 | + # echo -e "\n Skipping $file: no changes in folder.\n" |
| 86 | + continue |
77 | 87 | fi |
78 | 88 | fi |
79 | 89 |
|
80 | | - popd > /dev/null |
| 90 | + echo "------------------------------------------------------------" |
| 91 | + echo "- testing $file" |
| 92 | + echo "------------------------------------------------------------" |
| 93 | + |
| 94 | + # Fail the tests if no Java version was found. |
| 95 | + POM_JAVA=$(grep -oP '(?<=<maven.compiler.target>).*?(?=</maven.compiler.target>)' pom.xml) |
| 96 | + if [[ "$POM_JAVA" = "" ]]; then |
| 97 | + RTN=1 |
| 98 | + echo -e "\n Testing failed: Unable to determine Java version. Please set in pom:" |
| 99 | + echo -e "\n<properties>" |
| 100 | + echo -e " <maven.compiler.target>1.8</maven.compiler.target>" |
| 101 | + echo -e " <maven.compiler.source>1.8</maven.compiler.source>" |
| 102 | + echo -e "</properties>\n" |
| 103 | + continue |
| 104 | + fi |
| 105 | + |
| 106 | + # Skip tests that don't have the correct Java version. |
| 107 | + if ! [[ ",$JAVA_VERSION," =~ ",$POM_JAVA," ]]; then |
| 108 | + echo -e "\n Skipping tests: Java version ($POM_JAVA) not required ($JAVA_VERSION)\n" |
| 109 | + continue |
| 110 | + fi |
| 111 | + |
| 112 | + # Use maven to execute the tests for the project. |
| 113 | + mvn -q --batch-mode --fail-at-end clean verify \ |
| 114 | + -Dfile.encoding="UTF-8" \ |
| 115 | + -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \ |
| 116 | + -Dmaven.test.redirectTestOutputToFile=true \ |
| 117 | + -Dbigtable.projectID="${GOOGLE_CLOUD_PROJECT}" \ |
| 118 | + -Dbigtable.instanceID=instance |
| 119 | + EXIT=$? |
| 120 | + |
| 121 | + if [[ $EXIT -ne 0 ]]; then |
| 122 | + RTN=1 |
| 123 | + echo -e "\n Testing failed: Maven returned a non-zero exit code. \n" |
| 124 | + else |
| 125 | + echo -e "\n Testing completed.\n" |
| 126 | + fi |
81 | 127 |
|
82 | 128 | done |
83 | 129 |
|
84 | | -exit $RESULT |
| 130 | +exit "$RTN" |
0 commit comments