Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / tools / internal_ci / linux / grpc_xds_k8s_install_test_driver.sh
1 #!/usr/bin/env bash
2 # Copyright 2020 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 # TODO(sergiitk): move to grpc/grpc when implementing support of other languages
16 set -eo pipefail
17
18 # Constants
19 readonly PYTHON_VERSION="3.6"
20 # Test driver
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"
26
27 #######################################
28 # Run command end report its exit code. Doesn't exit on non-zero exit code.
29 # Globals:
30 #   None
31 # Arguments:
32 #   Command to execute
33 # Outputs:
34 #   Writes the output of given command to stdout, stderr
35 #######################################
36 run_ignore_exit_code() {
37   local exit_code=-1
38   "$@" || exit_code=$?
39   echo "Exit code: ${exit_code}"
40 }
41
42 #######################################
43 # Parses information about git repository at given path to global variables.
44 # Globals:
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
48 # Arguments:
49 #   Git source dir
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)
56 }
57
58 #######################################
59 # List GCR image tags matching given tag name.
60 # Arguments:
61 #   Image name
62 #   Tag name
63 # Outputs:
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"
69 }
70
71 #######################################
72 # A helper to execute `gcloud -q components update`.
73 # Arguments:
74 #   None
75 # Outputs:
76 #   Writes the output of `gcloud` command to stdout, stderr
77 #######################################
78 gcloud_update() {
79   echo "Update gcloud components:"
80   gcloud -q components update
81 }
82
83 #######################################
84 # Create kube context authenticated with GKE cluster, saves context name.
85 # to KUBE_CONTEXT
86 # Globals:
87 #   GKE_CLUSTER_NAME
88 #   GKE_CLUSTER_ZONE
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
91 # Arguments:
92 #   None
93 # Outputs:
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)"
101   else
102     readonly SECONDARY_KUBE_CONTEXT=""
103   fi
104   gcloud container clusters get-credentials "${GKE_CLUSTER_NAME}" --zone "${GKE_CLUSTER_ZONE}"
105   readonly KUBE_CONTEXT="$(kubectl config current-context)"
106 }
107
108 #######################################
109 # Clone the source code of the test driver to $TEST_DRIVER_REPO_DIR, unless
110 # given folder exists.
111 # Globals:
112 #   TEST_DRIVER_REPO_URL
113 #   TEST_DRIVER_BRANCH
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.
118 # Arguments:
119 #   None
120 # Outputs:
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}."
127   else
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}"
130   fi
131 }
132
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.
136 # Globals:
137 #   TEST_DRIVER_FULL_DIR
138 #   PYTHON_VERSION
139 # Arguments:
140 #   None
141 # Outputs:
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}"
148
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}"
154     else
155       echo "Creating python virtual environment: ${venv_dir}"
156       "python${PYTHON_VERSION} -m venv ${venv_dir}"
157     fi
158     # Intentional: No need to check python venv activate script.
159     # shellcheck source=/dev/null
160     source "${venv_dir}/bin/activate"
161   fi
162
163   pip install -r requirements.txt
164   echo "Installed Python packages:"
165   pip list
166 }
167
168 #######################################
169 # Compile proto-files needed for the test driver
170 # Globals:
171 #   TEST_DRIVER_REPO_DIR
172 #   TEST_DRIVER_FULL_DIR
173 #   TEST_DRIVER_PROTOS_PATH
174 # Arguments:
175 #   None
176 # Outputs:
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() {
183   declare -a protos
184   protos=(
185     "${TEST_DRIVER_PROTOS_PATH}/test.proto"
186     "${TEST_DRIVER_PROTOS_PATH}/messages.proto"
187     "${TEST_DRIVER_PROTOS_PATH}/empty.proto"
188   )
189   echo "Generate python code from grpc.testing protos: ${protos[*]}"
190   cd "${TEST_DRIVER_REPO_DIR}"
191   python -m grpc_tools.protoc \
192     --proto_path=. \
193     --python_out="${TEST_DRIVER_FULL_DIR}" \
194     --grpc_python_out="${TEST_DRIVER_FULL_DIR}" \
195     "${protos[@]}"
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}"
199 }
200
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
204 # Globals:
205 #   TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
206 #                         the test driver
207 #   TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
208 # Arguments:
209 #   The directory for test driver's source code
210 # Outputs:
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
219 }
220
221 #######################################
222 # Outputs Kokoro image version and Ubuntu's lsb_release
223 # Arguments:
224 #   None
225 # Outputs:
226 #   Writes the output to stdout
227 #######################################
228 kokoro_print_version() {
229   echo "Kokoro VM version:"
230   if [[ -f /VERSION ]]; then
231     cat /VERSION
232   fi
233   run_ignore_exit_code lsb_release -a
234 }
235
236 #######################################
237 # Report extra information about the job via sponge properties.
238 # Globals:
239 #   KOKORO_ARTIFACTS_DIR
240 #   GIT_ORIGIN_URL
241 #   GIT_COMMIT_SHORT
242 #   TESTGRID_EXCLUDE
243 # Arguments:
244 #   None
245 # Outputs:
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
254   # testgrid reports.
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}
260 EOF
261   echo "Sponge properties:"
262   cat "${KOKORO_ARTIFACTS_DIR}/custom_sponge_config.csv"
263 }
264
265 #######################################
266 # Configure Python virtual environment on Kokoro VM.
267 # Arguments:
268 #   None
269 # Outputs:
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
282   python -m ensurepip
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
287   pip --version
288 }
289
290 #######################################
291 # Installs and configures the test driver on Kokoro VM.
292 # Globals:
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
297 #                         the test driver
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
306 # Arguments:
307 #   The name of github repository being built
308 # Outputs:
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.
314   kokoro_print_version
315
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
324
325   # gcloud requires python, so this should be executed after pyenv setup
326   gcloud_update
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}"
336 }
337
338 #######################################
339 # Installs and configures the test driver for testing build script locally.
340 # Globals:
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
353 # Arguments:
354 #   The path to the folder containing the build script
355 # Outputs:
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"
369   # Test out
370   readonly TEST_XML_OUTPUT_DIR="${TEST_DRIVER_FULL_DIR}/out"
371   mkdir -p "${TEST_XML_OUTPUT_DIR}"
372 }
373
374 #######################################
375 # Tag and push the given Docker image
376 # Arguments:
377 #   The Docker image name
378 #   The Docker image original tag name
379 #   The Docker image new tag name
380 # Outputs:
381 #   Writes the output to stdout, stderr, files
382 #######################################
383 tag_and_push_docker_image() {
384   local image_name="$1"
385   local from_tag="$2"
386   local to_tag="$3"
387
388   docker tag "${image_name}:${from_tag}" "${image_name}:${to_tag}"
389   docker push "${image_name}:${to_tag}"
390 }