grpc_xds_url_map_python.sh 5.6 KB

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