26
26
27
27
# Inputs
28
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
- # OUTPUT_DIR - Directory (relatively to git repo root) that will be copied from inside docker container after finishing.
31
- # DOCKERHUB_ORGANIZATION - If set, pull a prebuilt image from given dockerhub org.
32
- # $@ - Extra args to pass to the "docker run" command.
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.
33
34
34
35
# Use image name based on Dockerfile location checksum
35
36
DOCKER_IMAGE_NAME=$( basename " $DOCKERFILE_DIR " ) :$( sha1sum " $DOCKERFILE_DIR /Dockerfile" | cut -f1 -d\ )
@@ -43,45 +44,175 @@ else
43
44
docker build -t " $DOCKER_IMAGE_NAME " " $DOCKERFILE_DIR "
44
45
fi
45
46
47
+ # If TTY is available, the running container can be conveniently terminated with Ctrl+C.
46
48
if [[ -t 0 ]]; then
47
- DOCKER_TTY_ARGS=" -it"
49
+ DOCKER_TTY_ARGS=( " -it" )
48
50
else
49
51
# The input device on kokoro is not a TTY, so -it does not work.
50
- DOCKER_TTY_ARGS=
52
+ DOCKER_TTY_ARGS=()
51
53
fi
52
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
+
53
148
# Git root as seen by the docker instance
54
149
# TODO(jtattermusch): rename to a more descriptive directory name
55
150
# currently that's nontrivial because the name is hardcoded in many places.
56
151
EXTERNAL_GIT_ROOT=/var/local/jenkins/grpc
57
152
153
+ MOUNT_GIT_ROOT_ARGS=(
154
+ " -v=${git_root} :${EXTERNAL_GIT_ROOT} "
155
+ " -e=EXTERNAL_GIT_ROOT=${EXTERNAL_GIT_ROOT} "
156
+ )
157
+
58
158
# temporary directory that will be mounted to the docker container
59
- # as a way to persist output files.
60
- # use unique name for the output directory to prevent clash between concurrent
61
- # runs of multiple docker containers
159
+ # as a way to persist report files.
62
160
TEMP_REPORT_DIR=" $( mktemp -d) "
63
- TEMP_OUTPUT_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
64
195
65
196
# Run tests inside docker
66
197
DOCKER_EXIT_CODE=0
67
- # TODO: silence complaint about $DOCKER_TTY_ARGS expansion in some other way
68
- # shellcheck disable=SC2086,SC2154
198
+
69
199
docker run \
70
- " $@ " \
71
- ${DOCKER_TTY_ARGS} \
72
- ${EXTRA_DOCKER_ARGS} \
73
- --cap-add SYS_PTRACE \
74
- -e " DOCKER_RUN_SCRIPT_COMMAND=${DOCKER_RUN_SCRIPT_COMMAND} " \
75
- -e " EXTERNAL_GIT_ROOT=${EXTERNAL_GIT_ROOT} " \
76
- -e " OUTPUT_DIR=${OUTPUT_DIR} " \
77
- --env-file tools/run_tests/dockerize/docker_propagate_env.list \
78
- --rm \
79
- --sysctl net.ipv6.conf.all.disable_ipv6=0 \
80
- -v " ${git_root} :${EXTERNAL_GIT_ROOT} " \
81
- -v " ${TEMP_REPORT_DIR} :/var/local/report_dir" \
82
- -v " ${TEMP_OUTPUT_DIR} :/var/local/output_dir" \
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[@]} " \
83
214
" ${DOCKER_IMAGE_NAME} " \
84
- bash -l " /var/local/jenkins/grpc/ ${DOCKER_RUN_SCRIPT }" || DOCKER_EXIT_CODE=$?
215
+ " ${DOCKER_CMD_AND_ARGS[@] }" || DOCKER_EXIT_CODE=$?
85
216
86
217
# Copy reports stored by the container (if any)
87
218
if [ " ${GRPC_TEST_REPORT_BASE_DIR} " != " " ]
95
226
# Copy contents of OUTPUT_DIR back under the git repo root
96
227
if [ " ${OUTPUT_DIR} " != " " ]
97
228
then
98
- # create the directory if it doesn't exist yet.
99
- mkdir -p " ${TEMP_OUTPUT_DIR} /${OUTPUT_DIR} "
100
229
cp -r " ${TEMP_OUTPUT_DIR} /${OUTPUT_DIR} " " ${git_root} " || DOCKER_EXIT_CODE=$?
101
230
fi
102
231
0 commit comments