1#!/usr/bin/env bash 2# Copyright 2021 gRPC authors. 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 16set -eo pipefail 17 18# Constants 19readonly GITHUB_REPOSITORY_NAME="grpc" 20readonly TEST_DRIVER_INSTALL_SCRIPT_URL="https://raw.githubusercontent.com/${TEST_DRIVER_REPO_OWNER:-grpc}/grpc/${TEST_DRIVER_BRANCH:-master}/tools/internal_ci/linux/grpc_xds_k8s_install_test_driver.sh" 21## xDS test client Docker images 22readonly CLIENT_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/python-client" 23readonly FORCE_IMAGE_BUILD="${FORCE_IMAGE_BUILD:-0}" 24readonly BUILD_APP_PATH="interop-testing/build/install/grpc-interop-testing" 25readonly LANGUAGE_NAME="Python" 26 27####################################### 28# Builds test app Docker images and pushes them to GCR 29# Globals: 30# BUILD_APP_PATH 31# CLIENT_IMAGE_NAME: Test client Docker image name 32# GIT_COMMIT: SHA-1 of git commit being built 33# Arguments: 34# None 35# Outputs: 36# Writes the output of `gcloud builds submit` to stdout, stderr 37####################################### 38build_test_app_docker_images() { 39 echo "Building ${LANGUAGE_NAME} xDS interop test app Docker images" 40 41 pushd "${SRC_DIR}" 42 docker build \ 43 -f src/python/grpcio_tests/tests_py3_only/interop/Dockerfile.client \ 44 -t "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" \ 45 . 46 47 popd 48 49 gcloud -q auth configure-docker 50 51 docker push "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" 52 if is_version_branch "${TESTING_VERSION}"; then 53 tag_and_push_docker_image "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}" "${TESTING_VERSION}" 54 fi 55} 56 57####################################### 58# Builds test app and its docker images unless they already exist 59# Globals: 60# CLIENT_IMAGE_NAME: Test client Docker image name 61# GIT_COMMIT: SHA-1 of git commit being built 62# FORCE_IMAGE_BUILD 63# Arguments: 64# None 65# Outputs: 66# Writes the output to stdout, stderr 67####################################### 68build_docker_images_if_needed() { 69 # Check if images already exist 70 client_tags="$(gcloud_gcr_list_image_tags "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}")" 71 printf "Client image: %s:%s\n" "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}" 72 echo "${client_tags:-Client image not found}" 73 74 # Build if any of the images are missing, or FORCE_IMAGE_BUILD=1 75 if [[ "${FORCE_IMAGE_BUILD}" == "1" || -z "${client_tags}" ]]; then 76 build_test_app_docker_images 77 else 78 echo "Skipping ${LANGUAGE_NAME} test app build" 79 fi 80} 81 82####################################### 83# Executes the test case 84# Globals: 85# TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile 86# KUBE_CONTEXT: The name of kubectl context with GKE cluster access 87# TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report 88# CLIENT_IMAGE_NAME: Test client Docker image name 89# GIT_COMMIT: SHA-1 of git commit being built 90# TESTING_VERSION: version branch under test: used by the framework to determine the supported PSM 91# features. 92# Arguments: 93# Test case name 94# Outputs: 95# Writes the output of test execution to stdout, stderr 96# Test xUnit report to ${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml 97####################################### 98run_test() { 99 # Test driver usage: 100 # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#basic-usage 101 local test_name="${1:?Usage: run_test test_name}" 102 local out_dir="${TEST_XML_OUTPUT_DIR}/${test_name}" 103 mkdir -pv "${out_dir}" 104 set -x 105 python3 -m "tests.${test_name}" \ 106 --flagfile="${TEST_DRIVER_FLAGFILE}" \ 107 --flagfile="config/url-map.cfg" \ 108 --kube_context="${KUBE_CONTEXT}" \ 109 --client_image="${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" \ 110 --testing_version="${TESTING_VERSION}" \ 111 --collect_app_logs \ 112 --log_dir="${out_dir}" \ 113 --xml_output_file="${out_dir}/sponge_log.xml" \ 114 |& tee "${out_dir}/sponge_log.log" 115} 116 117####################################### 118# Main function: provision software necessary to execute tests, and run them 119# Globals: 120# KOKORO_ARTIFACTS_DIR 121# GITHUB_REPOSITORY_NAME 122# SRC_DIR: Populated with absolute path to the source repo 123# TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing 124# the test driver 125# TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code 126# TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile 127# TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report 128# GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build 129# GIT_COMMIT: Populated with the SHA-1 of git commit being built 130# GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built 131# KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access 132# Arguments: 133# None 134# Outputs: 135# Writes the output of test execution to stdout, stderr 136####################################### 137main() { 138 local script_dir 139 script_dir="$(dirname "$0")" 140 141 # Source the test driver from the master branch. 142 echo "Sourcing test driver install script from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}" 143 source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")" 144 145 activate_gke_cluster GKE_CLUSTER_PSM_BASIC 146 147 set -x 148 if [[ -n "${KOKORO_ARTIFACTS_DIR}" ]]; then 149 kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}" 150 else 151 local_setup_test_driver "${script_dir}" 152 fi 153 build_docker_images_if_needed 154 # Run tests 155 cd "${TEST_DRIVER_FULL_DIR}" 156 run_test url_map || echo "Failed url_map test" 157} 158 159main "$@" 160