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