psm-security.sh 6.6 KB

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