1#!/usr/bin/env bash
2# Copyright 2022 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
16## xDS test server/client Docker images
17readonly IMAGE_REPO="gcr.io/grpc-testing/xds-interop"
18
19find_latest() {
20  gcloud container images list-tags --filter='tags~v1\.\d+\.x' "${IMAGE_REPO}/${1}-${2}" --flatten='tags[]' --format='value(tags)' | sort --version-sort | tail -n 1
21}
22
23find_latest_branch() {
24  local latest_branch=$1
25  if [ "${latest_branch}" == "" ]; then
26    cpp_server=$(find_latest cpp server)
27    cpp_client=$(find_latest cpp client)
28    go_server=$(find_latest go server)
29    go_client=$(find_latest go client)
30    java_server=$(find_latest java server)
31    java_client=$(find_latest java client)
32    latest_branch=$( (printf "%s\n" "${cpp_server}" "${cpp_client}" "${go_server}" "${go_client}" "${java_server}" "${java_client}") | sort --version-sort | head -1)
33  fi
34  echo ${latest_branch}
35}
36
37find_oldest_branch() {
38  local oldest_branch=$1
39  local latest_branch=$2
40  if [ "${oldest_branch}" == "" ]; then
41    major_branch=$( echo "${latest_branch}" | cut -f 2 -d.)
42    oldest_branch="v1.$(( major_branch - 9)).x"
43  fi
44  echo ${oldest_branch}
45}
46
47
48
49#######################################
50# Executes the test case
51# Globals:
52#   TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile
53#   KUBE_CONTEXT: The name of kubectl context with GKE cluster access
54#   TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report
55#   SERVER_IMAGE_NAME: Test server Docker image name
56#   CLIENT_IMAGE_NAME: Test client Docker image name
57#   GIT_COMMIT: SHA-1 of git commit being built
58#   TESTING_VERSION: version branch under test: used by the framework to determine the supported PSM
59#                    features.
60# Arguments:
61#   Test case name
62# Outputs:
63#   Writes the output of test execution to stdout, stderr
64#   Test xUnit report to ${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml
65#######################################
66run_test() {
67  if [ "$#" -ne 4 ]; then
68    echo "Usage: run_test client_lang client_branch server_lang server_branch" >&2
69    exit 1
70  fi
71  # Test driver usage:
72  # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#basic-usage
73  local client_lang="$1"
74  local client_branch="$2"
75  local server_lang="$3"
76  local server_branch="$4"
77  local server_image_name="${IMAGE_REPO}/${server_lang}-server"
78  local client_image_name="${IMAGE_REPO}/${client_lang}-client"
79
80  # Check if images exist
81  server_tags="$(gcloud_gcr_list_image_tags "${server_image_name}" "${server_branch}")"
82  echo "${server_tags:?Server image not found}"
83
84  client_tags="$(gcloud_gcr_list_image_tags "${client_image_name}" "${client_branch}")"
85  echo "${client_tags:?Client image not found}"
86
87  local server_image_name_tag="${server_image_name}:${server_branch}"
88  local client_image_name_tag="${client_image_name}:${client_branch}"
89
90  local out_dir="${TEST_XML_OUTPUT_DIR}/${client_branch}-${server_branch}/${client_lang}-${server_lang}"
91  mkdir -pv "${out_dir}"
92  set -x
93  python -m "tests.security_test" \
94    --flagfile="${TEST_DRIVER_FLAGFILE}" \
95    --kube_context="${KUBE_CONTEXT}" \
96    --server_image="${server_image_name_tag}" \
97    --client_image="${client_image_name_tag}" \
98    --testing_version="${TESTING_VERSION}" \
99    --nocheck_local_certs \
100    --force_cleanup \
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