|
| 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 | + |
| 23 | +# Disable buffering, so that the logs stream through. |
| 24 | +export PYTHONUNBUFFERED=1 |
| 25 | + |
| 26 | +# Debug: show build environment |
| 27 | +env | grep KOKORO |
| 28 | + |
| 29 | +# Install nox |
| 30 | +python3.6 -m pip install --upgrade --quiet nox |
| 31 | + |
| 32 | +# Use secrets acessor service account to get secrets |
| 33 | +if [[ -f "${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" ]]; then |
| 34 | + gcloud auth activate-service-account \ |
| 35 | + --key-file="${KOKORO_GFILE_DIR}/secrets_viewer_service_account.json" \ |
| 36 | + --project="cloud-devrel-kokoro-resources" |
| 37 | +fi |
| 38 | + |
| 39 | +cd github/python-pubsub |
| 40 | + |
| 41 | +# This script will create 3 files: |
| 42 | +# - testing/test-env.sh |
| 43 | +# - testing/service-account.json |
| 44 | +# - testing/client-secrets.json |
| 45 | +./scripts/decrypt-secrets.sh |
| 46 | + |
| 47 | +source ./testing/test-env.sh |
| 48 | +export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/testing/service-account.json |
| 49 | + |
| 50 | +# For cloud-run session, we activate the service account for gcloud sdk. |
| 51 | +gcloud auth activate-service-account \ |
| 52 | + --key-file "${GOOGLE_APPLICATION_CREDENTIALS}" |
| 53 | + |
| 54 | +export GOOGLE_CLIENT_SECRETS=$(pwd)/testing/client-secrets.json |
| 55 | + |
| 56 | +echo -e "\n******************** TESTING PROJECTS ********************" |
| 57 | + |
| 58 | +# Switch to 'fail at end' to allow all tests to complete before exiting. |
| 59 | +set +e |
| 60 | +# Use RTN to return a non-zero value if the test fails. |
| 61 | +RTN=0 |
| 62 | +ROOT=$(pwd) |
| 63 | + |
| 64 | +# Clone googleapis/python-pubsublite |
| 65 | +git clone https://github.com/googleapis/python-pubsublite.git |
| 66 | + |
| 67 | +# Find all requirements.txt in the Pub/Sub Lite samples directory (may break on whitespace). |
| 68 | +for file in python-pubsublite/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 pytest to execute tests for py-3.6 |
| 79 | + python3.6 -m venv py-3.6 |
| 80 | + source py-3.6/bin/activate |
| 81 | + # Install python-pubsublite samples tests requirements. |
| 82 | + python -m pip install -r requirements.txt -q |
| 83 | + python -m pip install -r requirements-test.txt -q |
| 84 | + # Install python-pubsub from source. |
| 85 | + python -m pip install -e "$ROOT" -q |
| 86 | + python -m pytest quickstart_test.py |
| 87 | + deactivate py-3.6 |
| 88 | + rm -rf py-3.6/ |
| 89 | + |
| 90 | + EXIT=$? |
| 91 | + |
| 92 | + if [[ $EXIT -ne 0 ]]; then |
| 93 | + RTN=1 |
| 94 | + echo -e "\n Testing failed: Nox returned a non-zero exit code. \n" |
| 95 | + else |
| 96 | + echo -e "\n Testing completed.\n" |
| 97 | + fi |
| 98 | + |
| 99 | +done |
| 100 | +cd "$ROOT" |
| 101 | + |
| 102 | +# Workaround for Kokoro permissions issue: delete secrets |
| 103 | +rm testing/{test-env.sh,client-secrets.json,service-account.json} |
| 104 | + |
| 105 | +exit "$RTN" |
0 commit comments