grpc_xds_k8s_lb.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. set -eo pipefail
  16. # Constants
  17. readonly GITHUB_REPOSITORY_NAME="grpc"
  18. readonly 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"
  19. ## xDS test client Docker images
  20. readonly SERVER_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/cpp-server"
  21. readonly CLIENT_IMAGE_NAME="gcr.io/grpc-testing/xds-interop/cpp-client"
  22. readonly FORCE_IMAGE_BUILD="${FORCE_IMAGE_BUILD:-0}"
  23. readonly BUILD_APP_PATH="interop-testing/build/install/grpc-interop-testing"
  24. #######################################
  25. # Builds test app Docker images and pushes them to GCR
  26. # Globals:
  27. # BUILD_APP_PATH
  28. # SERVER_IMAGE_NAME: Test server Docker image name
  29. # CLIENT_IMAGE_NAME: Test client Docker image name
  30. # GIT_COMMIT: SHA-1 of git commit being built
  31. # Arguments:
  32. # None
  33. # Outputs:
  34. # Writes the output of `gcloud builds submit` to stdout, stderr
  35. #######################################
  36. build_test_app_docker_images() {
  37. echo "Building C++ xDS interop test app Docker images"
  38. docker build -f "${SRC_DIR}/tools/dockerfile/interoptest/grpc_interop_cxx_xds/Dockerfile.xds_client" -t "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" "${SRC_DIR}"
  39. docker build -f "${SRC_DIR}/tools/dockerfile/interoptest/grpc_interop_cxx_xds/Dockerfile.xds_server" -t "${SERVER_IMAGE_NAME}:${GIT_COMMIT}" "${SRC_DIR}"
  40. gcloud -q auth configure-docker
  41. docker push "${CLIENT_IMAGE_NAME}:${GIT_COMMIT}"
  42. docker push "${SERVER_IMAGE_NAME}:${GIT_COMMIT}"
  43. }
  44. #######################################
  45. # Builds test app and its docker images unless they already exist
  46. # Globals:
  47. # SERVER_IMAGE_NAME: Test server Docker image name
  48. # CLIENT_IMAGE_NAME: Test client Docker image name
  49. # GIT_COMMIT: SHA-1 of git commit being built
  50. # FORCE_IMAGE_BUILD
  51. # Arguments:
  52. # None
  53. # Outputs:
  54. # Writes the output to stdout, stderr
  55. #######################################
  56. build_docker_images_if_needed() {
  57. # Check if images already exist
  58. server_tags="$(gcloud_gcr_list_image_tags "${SERVER_IMAGE_NAME}" "${GIT_COMMIT}")"
  59. printf "Server image: %s:%s\n" "${SERVER_IMAGE_NAME}" "${GIT_COMMIT}"
  60. echo "${server_tags:-Server image not found}"
  61. client_tags="$(gcloud_gcr_list_image_tags "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}")"
  62. printf "Client image: %s:%s\n" "${CLIENT_IMAGE_NAME}" "${GIT_COMMIT}"
  63. echo "${client_tags:-Client image not found}"
  64. # Build if any of the images are missing, or FORCE_IMAGE_BUILD=1
  65. if [[ "${FORCE_IMAGE_BUILD}" == "1" || -z "${server_tags}" || -z "${client_tags}" ]]; then
  66. build_test_app_docker_images
  67. else
  68. echo "Skipping C++ test app build"
  69. fi
  70. }
  71. #######################################
  72. # Executes the test case
  73. # Globals:
  74. # TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile
  75. # KUBE_CONTEXT: The name of kubectl context with GKE cluster access
  76. # SECONDARY_KUBE_CONTEXT: The name of kubectl context with secondary GKE cluster access, if any
  77. # TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report
  78. # SERVER_IMAGE_NAME: Test server Docker image name
  79. # CLIENT_IMAGE_NAME: Test client Docker image name
  80. # GIT_COMMIT: SHA-1 of git commit being built
  81. # Arguments:
  82. # Test case name
  83. # Outputs:
  84. # Writes the output of test execution to stdout, stderr
  85. # Test xUnit report to ${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml
  86. #######################################
  87. run_test() {
  88. # Test driver usage:
  89. # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#basic-usage
  90. local test_name="${1:?Usage: run_test test_name}"
  91. # testing_version is used by the framework to determine the supported PSM
  92. # features. It's captured from Kokoro job name of the Core repo, which takes
  93. # 2 forms:
  94. # grpc/core/master/linux/...
  95. # grpc/core/v1.42.x/branch/linux/...
  96. python3 -m "tests.${test_name}" \
  97. --flagfile="${TEST_DRIVER_FLAGFILE}" \
  98. --kube_context="${KUBE_CONTEXT}" \
  99. --secondary_kube_context="${SECONDARY_KUBE_CONTEXT}" \
  100. --server_image="${SERVER_IMAGE_NAME}:${GIT_COMMIT}" \
  101. --client_image="${CLIENT_IMAGE_NAME}:${GIT_COMMIT}" \
  102. --testing_version=$(echo "$KOKORO_JOB_NAME" | sed -E 's|^grpc/core/([^/]+)/.*|\1|') \
  103. --xml_output_file="${TEST_XML_OUTPUT_DIR}/${test_name}/sponge_log.xml" \
  104. ${@:2}
  105. }
  106. run_alpha_test() {
  107. local test_name=$1
  108. run_test ${test_name} \
  109. --compute_api_version="v1alpha"
  110. }
  111. #######################################
  112. # Main function: provision software necessary to execute tests, and run them
  113. # Globals:
  114. # KOKORO_ARTIFACTS_DIR
  115. # GITHUB_REPOSITORY_NAME
  116. # SRC_DIR: Populated with absolute path to the source repo
  117. # TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
  118. # the test driver
  119. # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
  120. # TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
  121. # TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
  122. # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
  123. # GIT_COMMIT: Populated with the SHA-1 of git commit being built
  124. # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
  125. # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
  126. # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
  127. # Arguments:
  128. # None
  129. # Outputs:
  130. # Writes the output of test execution to stdout, stderr
  131. #######################################
  132. main() {
  133. local script_dir
  134. script_dir="$(dirname "$0")"
  135. # Source the test captured from the master branch.
  136. echo "Sourcing test driver install captured from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}"
  137. source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")"
  138. activate_gke_cluster GKE_CLUSTER_PSM_LB
  139. activate_secondary_gke_cluster GKE_CLUSTER_PSM_LB
  140. set -x
  141. if [[ -n "${KOKORO_ARTIFACTS_DIR}" ]]; then
  142. kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}"
  143. else
  144. local_setup_test_driver "${script_dir}"
  145. fi
  146. build_docker_images_if_needed
  147. # Run tests
  148. cd "${TEST_DRIVER_FULL_DIR}"
  149. local failed_tests=0
  150. run_alpha_test subsetting_test || (( failed_tests++ ))
  151. test_suites=("api_listener_test" "change_backend_service_test" "failover_test" "remove_neg_test" "round_robin_test" "affinity_test")
  152. for test in "${test_suites[@]}"; do
  153. run_test $test || (( failed_tests++ ))
  154. done
  155. echo "Failed test suites: ${failed_tests}"
  156. if (( failed_tests > 0 )); then
  157. exit 1
  158. fi
  159. }
  160. main "$@"