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