grpc_xds_url_map.sh 5.6 KB

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