psm-security-python.sh 6.7 KB

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