Debian Codegraph @

tools/internal_ci/linux/grpc_xds_k8s_lb_python.sh

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