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