Yoshi Automation Bot | 49c6755 | 2021-04-02 12:26:40 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2021 Google LLC |
| 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 | # https://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 | |
| 17 | # `-e` enables the script to automatically fail when a command fails |
| 18 | # `-o pipefail` sets the exit code to the rightmost comment to exit with a non-zero |
| 19 | set -eo pipefail |
| 20 | # Enables `**` to include files nested inside sub-folders |
| 21 | shopt -s globstar |
| 22 | |
gcf-owl-bot[bot] | d25d2cb | 2021-07-14 15:22:25 +0000 | [diff] [blame] | 23 | # Exit early if samples don't exist |
| 24 | if ! find samples -name 'requirements.txt' | grep -q .; then |
| 25 | echo "No tests run. './samples/**/requirements.txt' not found" |
Yoshi Automation Bot | 49c6755 | 2021-04-02 12:26:40 -0700 | [diff] [blame] | 26 | exit 0 |
| 27 | fi |
| 28 | |
| 29 | # Disable buffering, so that the logs stream through. |
| 30 | export PYTHONUNBUFFERED=1 |
| 31 | |
| 32 | # Debug: show build environment |
| 33 | env | grep KOKORO |
| 34 | |
| 35 | # Install nox |
| 36 | python3.6 -m pip install --upgrade --quiet nox |
| 37 | |
| 38 | # Use secrets acessor service account to get secrets |
| 39 | if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then |
| 40 | gcloud auth activate-service-account \ |
| 41 | --key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \ |
| 42 | --project="cloud-devrel-kokoro-resources" |
| 43 | fi |
| 44 | |
| 45 | # This script will create 3 files: |
| 46 | # - testing/test-env.sh |
| 47 | # - testing/service-account.json |
| 48 | # - testing/client-secrets.json |
| 49 | ./scripts/decrypt-secrets.sh |
| 50 | |
| 51 | source ./testing/test-env.sh |
| 52 | export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json |
| 53 | |
| 54 | # For cloud-run session, we activate the service account for gcloud sdk. |
| 55 | gcloud auth activate-service-account \ |
| 56 | --key-file "${GOOGLE_APPLICATION_CREDENTIALS}" |
| 57 | |
| 58 | export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json |
| 59 | |
| 60 | echo -e "\n******************** TESTING PROJECTS ********************" |
| 61 | |
| 62 | # Switch to 'fail at end' to allow all tests to complete before exiting. |
| 63 | set +e |
| 64 | # Use RTN to return a non-zero value if the test fails. |
| 65 | RTN=0 |
| 66 | ROOT=$(pwd) |
| 67 | # Find all requirements.txt in the samples directory (may break on whitespace). |
| 68 | for file in samples/**/requirements.txt; do |
| 69 | cd "$ROOT" |
| 70 | # Navigate to the project folder. |
| 71 | file=$(dirname "$file") |
| 72 | cd "$file" |
| 73 | |
| 74 | echo "------------------------------------------------------------" |
| 75 | echo "- testing $file" |
| 76 | echo "------------------------------------------------------------" |
| 77 | |
| 78 | # Use nox to execute the tests for the project. |
| 79 | python3.6 -m nox -s "$RUN_TESTS_SESSION" |
| 80 | EXIT=$? |
| 81 | |
| 82 | # If this is a periodic build, send the test log to the FlakyBot. |
| 83 | # See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot. |
| 84 | if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then |
| 85 | chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot |
| 86 | $KOKORO_GFILE_DIR/linux_amd64/flakybot |
| 87 | fi |
| 88 | |
| 89 | if [[ $EXIT -ne 0 ]]; then |
| 90 | RTN=1 |
| 91 | echo -e "\n Testing failed: Nox returned a non-zero exit code. \n" |
| 92 | else |
| 93 | echo -e "\n Testing completed.\n" |
| 94 | fi |
| 95 | |
| 96 | done |
| 97 | cd "$ROOT" |
| 98 | |
| 99 | # Workaround for Kokoro permissions issue: delete secrets |
| 100 | rm testing/{test-env.sh,client-secrets.json,service-account.json} |
| 101 | |
| 102 | exit "$RTN" |