1#!/bin/bash
2# Copyright 2015 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
19set -ex
20
21cd "$(dirname "$0")/../../.."
22git_root=$(pwd)
23cd -
24
25# Ensure existence of ccache directory
26mkdir -p /tmp/ccache
27
28# Inputs
29# DOCKERFILE_DIR - Directory in which Dockerfile file is located.
30# DOCKER_RUN_SCRIPT - Script to run under docker (relative to grpc repo root)
31# DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org.
32
33# Use image name based on Dockerfile location checksum
34DOCKER_IMAGE_NAME=$(basename "$DOCKERFILE_DIR")_$(sha1sum "$DOCKERFILE_DIR/Dockerfile" | cut -f1 -d\ )
35
36if [ "$DOCKERHUB_ORGANIZATION" != "" ]
37then
38  DOCKER_IMAGE_NAME=$DOCKERHUB_ORGANIZATION/$DOCKER_IMAGE_NAME
39  time docker pull "$DOCKER_IMAGE_NAME"
40else
41  # Make sure docker image has been built. Should be instantaneous if so.
42  docker build -t "$DOCKER_IMAGE_NAME" "$DOCKERFILE_DIR"
43fi
44
45# Choose random name for docker container
46CONTAINER_NAME="run_tests_$(uuidgen)"
47
48# Git root as seen by the docker instance
49docker_instance_git_root=/var/local/jenkins/grpc
50
51# Run tests inside docker
52DOCKER_EXIT_CODE=0
53# TODO: silence complaint about $TTY_FLAG expansion in some other way
54# shellcheck disable=SC2086
55docker run \
56  --cap-add SYS_PTRACE \
57  -e "RUN_TESTS_COMMAND=$RUN_TESTS_COMMAND" \
58  -e "config=$config" \
59  -e "arch=$arch" \
60  -e CCACHE_DIR=/tmp/ccache \
61  -e THIS_IS_REALLY_NEEDED='see https://github.com/docker/docker/issues/14203 for why docker is awful' \
62  -e HOST_GIT_ROOT="$git_root" \
63  -e LOCAL_GIT_ROOT=$docker_instance_git_root \
64  -e "BUILD_ID=$BUILD_ID" \
65  -e "BUILD_URL=$BUILD_URL" \
66  -e "JOB_BASE_NAME=$JOB_BASE_NAME" \
67  -e "KOKORO_BUILD_ID=$KOKORO_BUILD_ID" \
68  -e "KOKORO_BUILD_NUMBER=$KOKORO_BUILD_NUMBER" \
69  -e "KOKORO_BUILD_URL=$KOKORO_BUILD_URL" \
70  -e "KOKORO_JOB_NAME=$KOKORO_JOB_NAME" \
71  -i \
72  $TTY_FLAG \
73  --sysctl net.ipv6.conf.all.disable_ipv6=0 \
74  -v ~/.config/gcloud:/root/.config/gcloud \
75  -v "$git_root:$docker_instance_git_root" \
76  -v /tmp/ccache:/tmp/ccache \
77  -v /tmp/npm-cache:/tmp/npm-cache \
78  -w /var/local/git/grpc \
79  --name="$CONTAINER_NAME" \
80  "$DOCKER_IMAGE_NAME" \
81  bash -l "/var/local/jenkins/grpc/$DOCKER_RUN_SCRIPT" || DOCKER_EXIT_CODE=$?
82
83# use unique name for reports.zip to prevent clash between concurrent
84# run_tests.py runs
85TEMP_REPORTS_ZIP=$(mktemp)
86docker cp "$CONTAINER_NAME:/var/local/git/grpc/reports.zip" "${TEMP_REPORTS_ZIP}" || true
87unzip -o "${TEMP_REPORTS_ZIP}" -d "$git_root" || true
88rm -f "${TEMP_REPORTS_ZIP}"
89
90# remove the container, possibly killing it first
91docker rm -f "$CONTAINER_NAME" || true
92
93exit $DOCKER_EXIT_CODE
94