1#!/bin/bash 2# Copyright 2016 The 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# This script is invoked by run_tests.py to accommodate "test under docker" 17# scenario. You should never need to call this script on your own. 18 19# shellcheck disable=SC2103 20 21set -ex 22 23cd "$(dirname "$0")/../../.." 24git_root=$(pwd) 25cd - 26 27# Inputs 28# DOCKERFILE_DIR - Directory in which Dockerfile file is located. 29# OUTPUT_DIR - (optional) Directory under the git repo root that will be copied from inside docker container after finishing. 30# DOCKER_RUN_SCRIPT - (optional) Script to run under docker (relative to grpc repo root). If specified, the cmdline args 31# passed on the commadline (a.k.a. $@) will be interpreted as extra args to pass to the "docker run" command. 32# If DOCKER_RUN_SCRIPT is not set, $@ will be interpreted as the command and args to run under the docker container. 33 34# The exact docker image to use and its version is determined by the corresponding .current_version file 35DOCKER_IMAGE_NAME="$(cat "${DOCKERFILE_DIR}.current_version")" 36 37# If TTY is available, the running container can be conveniently terminated with Ctrl+C. 38if [[ -t 0 ]]; then 39 DOCKER_TTY_ARGS=("-it") 40else 41 # The input device on kokoro is not a TTY, so -it does not work. 42 DOCKER_TTY_ARGS=() 43fi 44 45if [ "${DOCKER_RUN_SCRIPT}" != "" ] 46then 47 # Execute a well-known script inside the docker container. 48 # The script will act as a "wrapper" for the actual test command we want to run. 49 # TODO(jtattermusch): is the -l flag still necessary? 50 DOCKER_CMD_AND_ARGS=( bash -l "/var/local/jenkins/grpc/${DOCKER_RUN_SCRIPT}" ) 51 DOCKER_RUN_SCRIPT_ARGS=( 52 # propagate the variable with command to be run by the DOCKER_RUN_SCRIPT 53 "-e=DOCKER_RUN_SCRIPT_COMMAND=${DOCKER_RUN_SCRIPT_COMMAND}" 54 # Interpret any cmdline args as extra docker args. 55 # TODO(jtattermusch): remove this hack once there are no places where build_and_run_docker.sh is invoked 56 # with args. 57 "$@" 58 ) 59else 60 # Interpret any cmdline args as the command to be run inside the docker container. 61 DOCKER_CMD_AND_ARGS=( "$@" ) 62 DOCKER_RUN_SCRIPT_ARGS=( 63 # TODO(jtattermusch): remove the hack 64 "-w=/var/local/jenkins/grpc" 65 ) 66fi 67 68# If available, make KOKORO_KEYSTORE_DIR accessible from the container (as readonly) 69if [ "${KOKORO_KEYSTORE_DIR}" != "" ] 70then 71 MOUNT_KEYSTORE_DIR_ARGS=( 72 "-v=${KOKORO_KEYSTORE_DIR}:/kokoro_keystore:ro" 73 "-e=KOKORO_KEYSTORE_DIR=/kokoro_keystore" 74 ) 75else 76 MOUNT_KEYSTORE_DIR_ARGS=() 77fi 78 79# If available, make KOKORO_GFILE_DIR accessible from the container (as readonly) 80if [ "${KOKORO_GFILE_DIR}" != "" ] 81then 82 MOUNT_GFILE_DIR_ARGS=( 83 "-v=${KOKORO_GFILE_DIR}:/kokoro_gfile:ro" 84 "-e=KOKORO_GFILE_DIR=/kokoro_gfile" 85 ) 86else 87 MOUNT_GFILE_DIR_ARGS=() 88fi 89 90# If available, make KOKORO_ARTIFACTS_DIR accessible from the container 91if [ "${KOKORO_ARTIFACTS_DIR}" != "" ] 92then 93 MOUNT_ARTIFACTS_DIR_ARGS=( 94 "-v=${KOKORO_ARTIFACTS_DIR}:/kokoro_artifacts" 95 "-e=KOKORO_ARTIFACTS_DIR=/kokoro_artifacts" 96 ) 97else 98 MOUNT_ARTIFACTS_DIR_ARGS=() 99fi 100 101# args required to be able to run gdb/strace under the docker container 102DOCKER_PRIVILEGED_ARGS=( 103 # TODO(jtattermusch): document why exactly is this option needed. 104 "--cap-add=SYS_PTRACE" 105) 106 107# propagate the well known variables to the docker container 108DOCKER_PROPAGATE_ENV_ARGS=( 109 "--env-file=tools/run_tests/dockerize/docker_propagate_env.list" 110) 111 112DOCKER_CLEANUP_ARGS=( 113 # delete the container when the containers exits 114 # (otherwise the container will not release the disk space it used) 115 "--rm=true" 116) 117 118DOCKER_NETWORK_ARGS=( 119 # enable IPv6 120 "--sysctl=net.ipv6.conf.all.disable_ipv6=0" 121) 122 123DOCKER_IMAGE_IDENTITY_ARGS=( 124 # make the info about docker image used available from inside the docker container 125 "-e=GRPC_TEST_DOCKER_IMAGE_IDENTITY=${DOCKER_IMAGE_NAME}" 126) 127 128# TODO: silence complaint about lack of quotes in some other way 129# shellcheck disable=SC2206 130DOCKER_EXTRA_ARGS_FROM_ENV=( 131 # Not quoting the variable is intentional, since the env variable may contain 132 # multiple arguments and we want to interpret it as such. 133 # TODO: get rid of EXTRA_DOCKER_ARGS occurrences and replace with DOCKER_EXTRA_ARGS 134 ${EXTRA_DOCKER_ARGS} 135 ${DOCKER_EXTRA_ARGS} 136) 137 138# Git root as seen by the docker instance 139# TODO(jtattermusch): rename to a more descriptive directory name 140# currently that's nontrivial because the name is hardcoded in many places. 141EXTERNAL_GIT_ROOT=/var/local/jenkins/grpc 142 143MOUNT_GIT_ROOT_ARGS=( 144 "-v=${git_root}:${EXTERNAL_GIT_ROOT}" 145 "-e=EXTERNAL_GIT_ROOT=${EXTERNAL_GIT_ROOT}" 146) 147 148# temporary directory that will be mounted to the docker container 149# as a way to persist report files. 150TEMP_REPORT_DIR="$(mktemp -d)" 151# make sure the "reports" dir exists and is owned by current user. 152mkdir -p "${TEMP_REPORT_DIR}/reports" 153mkdir -p "${git_root}/reports" 154 155MOUNT_REPORT_DIR_ARGS=( 156 # mount the temporary directory as the "report dir". 157 "-v=${TEMP_REPORT_DIR}:/var/local/report_dir" 158 # make the reports/ directory show up under the mounted git root 159 "-v=${TEMP_REPORT_DIR}/reports:${EXTERNAL_GIT_ROOT}/reports" 160 # set GRPC_TEST_REPORT_BASE_DIR to make sure that when XML test reports 161 # are generated, they will be stored in the report dir. 162 "-e=GRPC_TEST_REPORT_BASE_DIR=/var/local/report_dir" 163) 164 165if [ "${OUTPUT_DIR}" != "" ] 166then 167 # temporary directory that will be mounted to the docker container 168 # as a way to persist output files. 169 # use unique name for the output directory to prevent clash between concurrent 170 # runs of multiple docker containers 171 TEMP_OUTPUT_DIR="$(mktemp -d)" 172 173 # make sure the "${OUTPUT_DIR}" dir exists and is owned by current user. 174 mkdir -p "${TEMP_OUTPUT_DIR}/${OUTPUT_DIR}" 175 mkdir -p "${git_root}/${OUTPUT_DIR}" 176 177 MOUNT_OUTPUT_DIR_ARGS=( 178 # the OUTPUT_DIR refers to a subdirectory of the git root. 179 "-v=${TEMP_OUTPUT_DIR}/${OUTPUT_DIR}:${EXTERNAL_GIT_ROOT}/${OUTPUT_DIR}" 180 "-e=OUTPUT_DIR=${OUTPUT_DIR}" 181 ) 182else 183 MOUNT_OUTPUT_DIR_ARGS=() 184fi 185 186# Run tests inside docker 187DOCKER_EXIT_CODE=0 188 189docker run \ 190 "${DOCKER_TTY_ARGS[@]}" \ 191 "${DOCKER_RUN_SCRIPT_ARGS[@]}" \ 192 "${MOUNT_KEYSTORE_DIR_ARGS[@]}" \ 193 "${MOUNT_GFILE_DIR_ARGS[@]}" \ 194 "${MOUNT_ARTIFACTS_DIR_ARGS[@]}" \ 195 "${DOCKER_PRIVILEGED_ARGS[@]}" \ 196 "${DOCKER_PROPAGATE_ENV_ARGS[@]}" \ 197 "${DOCKER_CLEANUP_ARGS[@]}" \ 198 "${DOCKER_NETWORK_ARGS[@]}" \ 199 "${DOCKER_IMAGE_IDENTITY_ARGS[@]}" \ 200 "${MOUNT_GIT_ROOT_ARGS[@]}" \ 201 "${MOUNT_REPORT_DIR_ARGS[@]}" \ 202 "${MOUNT_OUTPUT_DIR_ARGS[@]}" \ 203 "${DOCKER_EXTRA_ARGS_FROM_ENV[@]}" \ 204 "${DOCKER_IMAGE_NAME}" \ 205 "${DOCKER_CMD_AND_ARGS[@]}" || DOCKER_EXIT_CODE=$? 206 207# Copy reports stored by the container (if any) 208if [ "${GRPC_TEST_REPORT_BASE_DIR}" != "" ] 209then 210 mkdir -p "${GRPC_TEST_REPORT_BASE_DIR}" 211 cp -r "${TEMP_REPORT_DIR}"/* "${GRPC_TEST_REPORT_BASE_DIR}" || true 212else 213 cp -r "${TEMP_REPORT_DIR}"/* "${git_root}" || true 214fi 215 216# Copy contents of OUTPUT_DIR back under the git repo root 217if [ "${OUTPUT_DIR}" != "" ] 218then 219 cp -r "${TEMP_OUTPUT_DIR}/${OUTPUT_DIR}" "${git_root}" || DOCKER_EXIT_CODE=$? 220fi 221 222exit $DOCKER_EXIT_CODE 223