• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2set -eo pipefail
3
4# Constants
5readonly GITHUB_REPOSITORY_NAME="grpc-java"
6readonly 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"
7## xDS test client Docker images
8readonly SERVER_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/java-server"
9readonly CLIENT_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/java-client"
10readonly FORCE_IMAGE_BUILD="${FORCE_IMAGE_BUILD:-0}"
11readonly BUILD_APP_PATH="interop-testing/build/install/grpc-interop-testing"
12
13#######################################
14# Builds the test app using gradle and smoke-checks its binaries
15# Globals:
16#   SRC_DIR
17#   BUILD_APP_PATH
18# Arguments:
19#   None
20# Outputs:
21#   Writes the output of xds-test-client and xds-test-server --help to stderr
22#######################################
23build_java_test_app() {
24  echo "Building Java test app"
25  cd "${SRC_DIR}"
26  GRADLE_OPTS="-Dorg.gradle.jvmargs='-Xmx1g'" \
27  ./gradlew --no-daemon grpc-interop-testing:installDist -x test \
28    -PskipCodegen=true -PskipAndroid=true --console=plain
29
30  # Test-run binaries
31  run_ignore_exit_code "${SRC_DIR}/${BUILD_APP_PATH}/bin/xds-test-client" --help
32  run_ignore_exit_code "${SRC_DIR}/${BUILD_APP_PATH}/bin/xds-test-server" --help
33}
34
35#######################################
36# Builds test app Docker images and pushes them to GCR
37# Globals:
38#   BUILD_APP_PATH
39#   SERVER_IMAGE_NAME: Test server Docker image name
40#   CLIENT_IMAGE_NAME: Test client Docker image name
41#   GIT_COMMIT: SHA-1 of git commit being built
42#   TESTING_VERSION: version branch under test, f.e. v1.42.x, master
43# Arguments:
44#   None
45# Outputs:
46#   Writes the output of `gcloud builds submit` to stdout, stderr
47#######################################
48build_test_app_docker_images() {
49  echo "Building Java xDS interop test app Docker images"
50  local docker_dir="${SRC_DIR}/buildscripts/xds-k8s"
51  local build_dir
52  build_dir="$(mktemp -d)"
53  # Copy Docker files, log properties, and the test app to the build dir
54  cp -v "${docker_dir}/"*.Dockerfile "${build_dir}"
55  cp -v "${docker_dir}/"*.properties "${build_dir}"
56  cp -rv "${SRC_DIR}/${BUILD_APP_PATH}" "${build_dir}"
57  # Pick a branch name for the built image
58  local branch_name='experimental'
59  if is_version_branch "${TESTING_VERSION}"; then
60    branch_name="${TESTING_VERSION}"
61  fi
62  # Run Google Cloud Build
63  gcloud builds submit "${build_dir}" \
64    --config "${docker_dir}/cloudbuild.yaml" \
65    --substitutions "_SERVER_IMAGE_NAME=${SERVER_IMAGE_NAME},_CLIENT_IMAGE_NAME=${CLIENT_IMAGE_NAME},COMMIT_SHA=${GIT_COMMIT},BRANCH_NAME=${branch_name}"
66  # TODO(sergiitk): extra "cosmetic" tags for versioned branches, e.g. v1.34.x
67  # TODO(sergiitk): do this when adding support for custom configs per version
68}
69
70#######################################
71# Builds test app and its docker images unless they already exist
72# Globals:
73#   SERVER_IMAGE_NAME: Test server Docker image name
74#   CLIENT_IMAGE_NAME: Test client Docker image name
75#   GIT_COMMIT: SHA-1 of git commit being built
76#   FORCE_IMAGE_BUILD
77# Arguments:
78#   None
79# Outputs:
80#   Writes the output to stdout, stderr
81#######################################
82build_docker_images_if_needed() {
83  # Check if images already exist
84  server_tags="$(gcloud_gcr_list_image_tags "${SERVER_IMAGE_NAME}" "${GIT_COMMIT}")"
85  printf "Server image: %s:%s\n" "${SERVER_IMAGE_NAME}" "${GIT_COMMIT}"
86  echo "${server_tags:-Server image not found}"
87
88  client_tags="$(gcloud_gcr_list_image_tags "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}")"
89  printf "Client image: %s:%s\n" "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}"
90  echo "${client_tags:-Client image not found}"
91
92  # Build if any of the images are missing, or FORCE_IMAGE_BUILD=1
93  if [[ "${FORCE_IMAGE_BUILD}" == "1" || -z "${server_tags}" || -z "${client_tags}" ]]; then
94    build_java_test_app
95    build_test_app_docker_images
96  else
97    echo "Skipping Java test app build"
98  fi
99}
100
101#######################################
102# Executes the test case
103# Globals:
104#   TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile
105#   KUBE_CONTEXT: The name of kubectl context with GKE cluster access
106#   TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report
107#   CLIENT_IMAGE_NAME: Test client Docker image name
108#   GIT_COMMIT: SHA-1 of git commit being built
109#   TESTING_VERSION: version branch under test: used by the framework to
110#                     determine the supported PSM features.
111# Arguments:
112#   Test case name
113# Outputs:
114#   Writes the output of test execution to stdout, stderr
115#   Test xUnit report to ${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml
116#######################################
117run_test() {
118  # Test driver usage:
119  # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#basic-usage
120  local test_name="${1:?Usage: run_test test_name}"
121  local out_dir="${TEST_XML_OUTPUT_DIR}/${test_name}"
122  mkdir -pv "${out_dir}"
123  set -x
124  python -m "tests.${test_name}" \
125    --flagfile="${TEST_DRIVER_FLAGFILE}" \
126    --flagfile="config/url-map.cfg" \
127    --kube_context="${KUBE_CONTEXT}" \
128    --client_image="${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" \
129    --testing_version="${TESTING_VERSION}" \
130    --collect_app_logs \
131    --log_dir="${out_dir}" \
132    --xml_output_file="${out_dir}/sponge_log.xml" \
133    |& tee "${out_dir}/sponge_log.log"
134}
135
136#######################################
137# Main function: provision software necessary to execute tests, and run them
138# Globals:
139#   KOKORO_ARTIFACTS_DIR
140#   GITHUB_REPOSITORY_NAME
141#   SRC_DIR: Populated with absolute path to the source repo
142#   TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
143#                         the test driver
144#   TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
145#   TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
146#   TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
147#   GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
148#   GIT_COMMIT: Populated with the SHA-1 of git commit being built
149#   GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
150#   KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
151# Arguments:
152#   None
153# Outputs:
154#   Writes the output of test execution to stdout, stderr
155#######################################
156main() {
157  local script_dir
158  script_dir="$(dirname "$0")"
159
160  # Source the test driver from the master branch.
161  echo "Sourcing test driver install script from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}"
162  source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")"
163
164  activate_gke_cluster GKE_CLUSTER_PSM_BASIC
165
166  set -x
167  if [[ -n "${KOKORO_ARTIFACTS_DIR}" ]]; then
168    kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}"
169  else
170    local_setup_test_driver "${script_dir}"
171  fi
172  build_docker_images_if_needed
173  # Run tests
174  cd "${TEST_DRIVER_FULL_DIR}"
175  run_test url_map
176}
177
178main "$@"
179