2 # Copyright 2020 gRPC authors.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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 # TODO(sergiitk): move to grpc/grpc when implementing support of other languages
19 readonly PYTHON_VERSION="3.6"
21 readonly TEST_DRIVER_REPO_NAME="grpc"
22 readonly TEST_DRIVER_REPO_URL="https://github.com/${TEST_DRIVER_REPO_OWNER:-grpc}/grpc.git"
23 readonly TEST_DRIVER_BRANCH="${TEST_DRIVER_BRANCH:-master}"
24 readonly TEST_DRIVER_PATH="tools/run_tests/xds_k8s_test_driver"
25 readonly TEST_DRIVER_PROTOS_PATH="src/proto/grpc/testing"
27 #######################################
28 # Run command end report its exit code. Doesn't exit on non-zero exit code.
34 # Writes the output of given command to stdout, stderr
35 #######################################
36 run_ignore_exit_code() {
39 echo "Exit code: ${exit_code}"
42 #######################################
43 # Parses information about git repository at given path to global variables.
45 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
46 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
47 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
50 #######################################
51 parse_src_repo_git_info() {
52 local src_dir="${SRC_DIR:?SRC_DIR must be set}"
53 readonly GIT_ORIGIN_URL=$(git -C "${src_dir}" remote get-url origin)
54 readonly GIT_COMMIT=$(git -C "${src_dir}" rev-parse HEAD)
55 readonly GIT_COMMIT_SHORT=$(git -C "${src_dir}" rev-parse --short HEAD)
58 #######################################
59 # List GCR image tags matching given tag name.
64 # Writes the table with the list of found tags to stdout.
65 # If no tags found, the output is an empty string.
66 #######################################
67 gcloud_gcr_list_image_tags() {
68 gcloud container images list-tags --format="table[box](tags,digest,timestamp.date())" --filter="tags:$2" "$1"
71 #######################################
72 # A helper to execute `gcloud -q components update`.
76 # Writes the output of `gcloud` command to stdout, stderr
77 #######################################
79 echo "Update gcloud components:"
80 gcloud -q components update
83 #######################################
84 # Create kube context authenticated with GKE cluster, saves context name.
89 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
90 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
94 # Writes the output of `gcloud` command to stdout, stderr
95 # Writes authorization info $HOME/.kube/config
96 #######################################
97 gcloud_get_cluster_credentials() {
98 if [[ -n "${SECONDARY_GKE_CLUSTER_NAME}" && -n "${SECONDARY_GKE_CLUSTER_ZONE}" ]]; then
99 gcloud container clusters get-credentials "${SECONDARY_GKE_CLUSTER_NAME}" --zone "${SECONDARY_GKE_CLUSTER_ZONE}"
100 readonly SECONDARY_KUBE_CONTEXT="$(kubectl config current-context)"
102 readonly SECONDARY_KUBE_CONTEXT=""
104 gcloud container clusters get-credentials "${GKE_CLUSTER_NAME}" --zone "${GKE_CLUSTER_ZONE}"
105 readonly KUBE_CONTEXT="$(kubectl config current-context)"
108 #######################################
109 # Clone the source code of the test driver to $TEST_DRIVER_REPO_DIR, unless
110 # given folder exists.
112 # TEST_DRIVER_REPO_URL
114 # TEST_DRIVER_REPO_DIR: path to the repo containing the test driver
115 # TEST_DRIVER_REPO_DIR_USE_EXISTING: set non-empty value to use exiting
116 # clone of the driver repo located at $TEST_DRIVER_REPO_DIR.
117 # Useful for debugging the build script locally.
121 # Writes the output of `git` command to stdout, stderr
122 # Writes driver source code to $TEST_DRIVER_REPO_DIR
123 #######################################
124 test_driver_get_source() {
125 if [[ -n "${TEST_DRIVER_REPO_DIR_USE_EXISTING}" && -d "${TEST_DRIVER_REPO_DIR}" ]]; then
126 echo "Using exiting driver directory: ${TEST_DRIVER_REPO_DIR}."
128 echo "Cloning driver to ${TEST_DRIVER_REPO_URL} branch ${TEST_DRIVER_BRANCH} to ${TEST_DRIVER_REPO_DIR}"
129 git clone -b "${TEST_DRIVER_BRANCH}" --depth=1 "${TEST_DRIVER_REPO_URL}" "${TEST_DRIVER_REPO_DIR}"
133 #######################################
134 # Install Python modules from required in $TEST_DRIVER_FULL_DIR/requirements.txt
135 # to Python virtual environment. Creates and activates Python venv if necessary.
137 # TEST_DRIVER_FULL_DIR
142 # Writes the output of `python`, `pip` commands to stdout, stderr
143 # Writes the list of installed modules to stdout
144 #######################################
145 test_driver_pip_install() {
146 echo "Install python dependencies"
147 cd "${TEST_DRIVER_FULL_DIR}"
149 # Create and activate virtual environment unless already using one
150 if [[ -z "${VIRTUAL_ENV}" ]]; then
151 local venv_dir="${TEST_DRIVER_FULL_DIR}/venv"
152 if [[ -d "${venv_dir}" ]]; then
153 echo "Found python virtual environment directory: ${venv_dir}"
155 echo "Creating python virtual environment: ${venv_dir}"
156 "python${PYTHON_VERSION} -m venv ${venv_dir}"
158 # Intentional: No need to check python venv activate script.
159 # shellcheck source=/dev/null
160 source "${venv_dir}/bin/activate"
163 pip install -r requirements.txt
164 echo "Installed Python packages:"
168 #######################################
169 # Compile proto-files needed for the test driver
171 # TEST_DRIVER_REPO_DIR
172 # TEST_DRIVER_FULL_DIR
173 # TEST_DRIVER_PROTOS_PATH
177 # Writes the output of `python -m grpc_tools.protoc` to stdout, stderr
178 # Writes the list if compiled python code to stdout
179 # Writes compiled python code with proto messages and grpc services to
180 # $TEST_DRIVER_FULL_DIR/src/proto
181 #######################################
182 test_driver_compile_protos() {
185 "${TEST_DRIVER_PROTOS_PATH}/test.proto"
186 "${TEST_DRIVER_PROTOS_PATH}/messages.proto"
187 "${TEST_DRIVER_PROTOS_PATH}/empty.proto"
189 echo "Generate python code from grpc.testing protos: ${protos[*]}"
190 cd "${TEST_DRIVER_REPO_DIR}"
191 python -m grpc_tools.protoc \
193 --python_out="${TEST_DRIVER_FULL_DIR}" \
194 --grpc_python_out="${TEST_DRIVER_FULL_DIR}" \
196 local protos_out_dir="${TEST_DRIVER_FULL_DIR}/${TEST_DRIVER_PROTOS_PATH}"
197 echo "Generated files ${protos_out_dir}:"
198 ls -Fl "${protos_out_dir}"
201 #######################################
202 # Installs the test driver and it's requirements.
203 # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#installation
205 # TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
207 # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
209 # The directory for test driver's source code
211 # Writes the output to stdout, stderr
212 #######################################
213 test_driver_install() {
214 readonly TEST_DRIVER_REPO_DIR="${1:?Usage test_driver_install TEST_DRIVER_REPO_DIR}"
215 readonly TEST_DRIVER_FULL_DIR="${TEST_DRIVER_REPO_DIR}/${TEST_DRIVER_PATH}"
216 test_driver_get_source
217 test_driver_pip_install
218 test_driver_compile_protos
221 #######################################
222 # Outputs Kokoro image version and Ubuntu's lsb_release
226 # Writes the output to stdout
227 #######################################
228 kokoro_print_version() {
229 echo "Kokoro VM version:"
230 if [[ -f /VERSION ]]; then
233 run_ignore_exit_code lsb_release -a
236 #######################################
237 # Report extra information about the job via sponge properties.
239 # KOKORO_ARTIFACTS_DIR
246 # Writes the output to stdout
247 # Writes job properties to $KOKORO_ARTIFACTS_DIR/custom_sponge_config.csv
248 #######################################
249 kokoro_write_sponge_properties() {
250 # CSV format: "property_name","property_value"
251 # Bump TESTS_FORMAT_VERSION when reported test name changed enough to when it
252 # makes more sense to discard previous test results from a testgrid board.
253 # Use GIT_ORIGIN_URL to exclude test runs executed against repo forks from
255 cat >"${KOKORO_ARTIFACTS_DIR}/custom_sponge_config.csv" <<EOF
256 TESTS_FORMAT_VERSION,2
257 TESTGRID_EXCLUDE,${TESTGRID_EXCLUDE:-0}
258 GIT_ORIGIN_URL,${GIT_ORIGIN_URL:?GIT_ORIGIN_URL must be set}
259 GIT_COMMIT_SHORT,${GIT_COMMIT_SHORT:?GIT_COMMIT_SHORT must be set}
261 echo "Sponge properties:"
262 cat "${KOKORO_ARTIFACTS_DIR}/custom_sponge_config.csv"
265 #######################################
266 # Configure Python virtual environment on Kokoro VM.
270 # Writes the output of `pyenv` commands to stdout
271 #######################################
272 kokoro_setup_python_virtual_environment() {
273 # Kokoro provides pyenv, so use it instead of `python -m venv`
274 echo "Setup pyenv environment"
275 eval "$(pyenv init -)"
276 eval "$(pyenv virtualenv-init -)"
277 py_latest_patch="$(pyenv versions --bare --skip-aliases | grep -E "^${PYTHON_VERSION}\.[0-9]{1,2}$" | sort --version-sort | tail -n 1)"
278 echo "Activating python ${py_latest_patch} virtual environment"
279 pyenv virtualenv --no-pip "${py_latest_patch}" k8s_xds_test_runner
280 pyenv local k8s_xds_test_runner
281 pyenv activate k8s_xds_test_runner
283 # pip is fixed to 21.0.1 due to issue https://github.com/pypa/pip/pull/9835
284 # internal details: b/186411224
285 # TODO(sergiitk): revert https://github.com/grpc/grpc/pull/26087 when 21.1.1 released
286 python -m pip install -U pip==21.0.1
290 #######################################
291 # Installs and configures the test driver on Kokoro VM.
293 # KOKORO_ARTIFACTS_DIR
294 # TEST_DRIVER_REPO_NAME
295 # SRC_DIR: Populated with absolute path to the source repo on Kokoro VM
296 # TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
298 # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
299 # TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
300 # TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
301 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
302 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
303 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
304 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
305 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
307 # The name of github repository being built
309 # Writes the output to stdout, stderr, files
310 #######################################
311 kokoro_setup_test_driver() {
312 local src_repository_name="${1:?Usage kokoro_setup_test_driver GITHUB_REPOSITORY_NAME}"
313 # Capture Kokoro VM version info in the log.
316 # Kokoro clones repo to ${KOKORO_ARTIFACTS_DIR}/github/${GITHUB_REPOSITORY}
317 local github_root="${KOKORO_ARTIFACTS_DIR}/github"
318 readonly SRC_DIR="${github_root}/${src_repository_name}"
319 local test_driver_repo_dir
320 test_driver_repo_dir="${TEST_DRIVER_REPO_DIR:-$(mktemp -d)/${TEST_DRIVER_REPO_NAME}}"
321 parse_src_repo_git_info SRC_DIR
322 kokoro_write_sponge_properties
323 kokoro_setup_python_virtual_environment
325 # gcloud requires python, so this should be executed after pyenv setup
327 gcloud_get_cluster_credentials
328 test_driver_install "${test_driver_repo_dir}"
329 # shellcheck disable=SC2034 # Used in the main script
330 readonly TEST_DRIVER_FLAGFILE="config/grpc-testing.cfg"
331 # Test artifacts dir: xml reports, logs, etc.
332 local artifacts_dir="${KOKORO_ARTIFACTS_DIR}/artifacts"
333 # Folders after $artifacts_dir reported as target name
334 readonly TEST_XML_OUTPUT_DIR="${artifacts_dir}/${KOKORO_JOB_NAME}"
335 mkdir -p "${artifacts_dir}" "${TEST_XML_OUTPUT_DIR}"
338 #######################################
339 # Installs and configures the test driver for testing build script locally.
341 # TEST_DRIVER_REPO_NAME
342 # TEST_DRIVER_REPO_DIR: Unless provided, populated with a temporary dir with
343 # the path to the test driver repo
344 # SRC_DIR: Populated with absolute path to the source repo
345 # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
346 # TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
347 # TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
348 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
349 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
350 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
351 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
352 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
354 # The path to the folder containing the build script
356 # Writes the output to stdout, stderr, files
357 #######################################
358 local_setup_test_driver() {
359 local script_dir="${1:?Usage: local_setup_test_driver SCRIPT_DIR}"
360 readonly SRC_DIR="$(git -C "${script_dir}" rev-parse --show-toplevel)"
361 parse_src_repo_git_info SRC_DIR
362 readonly KUBE_CONTEXT="${KUBE_CONTEXT:-$(kubectl config current-context)}"
363 readonly SECONDARY_KUBE_CONTEXT="${SECONDARY_KUBE_CONTEXT}"
364 local test_driver_repo_dir
365 test_driver_repo_dir="${TEST_DRIVER_REPO_DIR:-$(mktemp -d)/${TEST_DRIVER_REPO_NAME}}"
366 test_driver_install "${test_driver_repo_dir}"
367 # shellcheck disable=SC2034 # Used in the main script
368 readonly TEST_DRIVER_FLAGFILE="config/local-dev.cfg"
370 readonly TEST_XML_OUTPUT_DIR="${TEST_DRIVER_FULL_DIR}/out"
371 mkdir -p "${TEST_XML_OUTPUT_DIR}"
374 #######################################
375 # Tag and push the given Docker image
377 # The Docker image name
378 # The Docker image original tag name
379 # The Docker image new tag name
381 # Writes the output to stdout, stderr, files
382 #######################################
383 tag_and_push_docker_image() {
384 local image_name="$1"
388 docker tag "${image_name}:${from_tag}" "${image_name}:${to_tag}"
389 docker push "${image_name}:${to_tag}"