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