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