-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrun.sh
executable file
·278 lines (248 loc) · 6.15 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env bash
set -Eeuo pipefail
dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
self="$(basename "$0")"
usage() {
cat <<EOUSAGE
usage: $self [-t test ...] image:tag [...]
ie: $self plone/plone-backend:6.0.0a1
$self -t utc plone/plone-backend:6.0.0a1
$self -t utc plone/plone-backend:6.0.0a1 -t plone-zeoclient
This script processes the specified Docker images to test their running
environments.
EOUSAGE
}
# arg handling
opts="$(getopt -o 'ht:c:?' --long 'dry-run,help,test:,config:,keep-namespace' -- "$@" || { usage >&2 && false; })"
eval set -- "$opts"
declare -A argTests=()
declare -a configs=()
dryRun=
keepNamespace=
while true; do
flag=$1
shift
case "$flag" in
--dry-run) dryRun=1 ;;
--help|-h|'-?') usage && exit 0 ;;
--test|-t) argTests["$1"]=1 && shift ;;
--config|-c) configs+=("$(readlink -f "$1")") && shift ;;
--keep-namespace) keepNamespace=1 ;;
--) break ;;
*)
{
echo "error: unknown flag: $flag"
usage
} >&2
exit 1
;;
esac
done
if [ "$#" -eq 0 ]; then
usage >&2
exit 1
fi
# declare configuration variables
declare -a globalTests=()
declare -A testAlias=()
declare -A imageTests=()
declare -A globalExcludeTests=()
declare -A explicitTests=()
# if there are no user-specified configs, use the default config
if [ "${#configs[@]}" -eq 0 ]; then
configs+=( "$dir/config.sh" )
fi
case "$(uname -o)" in
Msys)
# https://stackoverflow.com/a/34386471/433558
# https://github.com/docker/toolbox/blob/6960f28d5b01857d69b2095a02949264f09d3e57/windows/start.sh#L104-L107
docker() {
MSYS_NO_PATHCONV=1 command docker "$@"
}
export -f docker
;;
esac
# load the configs
declare -A testPaths=()
for conf in "${configs[@]}"; do
. "$conf"
# Determine the full path to any newly-declared tests
confDir="$(dirname "$conf")"
for testName in ${globalTests[@]} ${imageTests[@]}; do
[ -n "${testPaths[$testName]:-}" ] && continue
if [ -d "$confDir/tests/$testName" ]; then
# Test directory found relative to the conf file
testPaths[$testName]="$confDir/tests/$testName"
elif [ -d "$dir/tests/$testName" ]; then
# Test directory found in the main tests/ directory
testPaths[$testName]="$dir/tests/$testName"
fi
done
done
didFail=
for dockerImage in "$@"; do
echo "testing $dockerImage"
repo="${dockerImage%:*}"
tagVar="${dockerImage#*:}"
#version="${tagVar%-*}"
variant="${tagVar##*-}"
case "$tagVar" in
*onbuild*)
# "maven:onbuild-alpine" is still onbuild
# ONCE ONBUILD, ALWAYS ONBUILD
variant='onbuild'
;;
*apache-*)
# lolPHP
variant='apache'
;;
*fpm-*)
# lolPHP
variant='fpm'
;;
*alpine*)
# "alpine3.4", "alpine3.6", "nginx:alpine-perl", etc are still "alpine" variants
variant='alpine'
;;
slim-*|*-slim-*)
# "slim-jessie" is still "slim"
variant='slim'
;;
psmdb-*)
# Percona Server for MongoDB is still "mongo"
variant='psmdb'
;;
*nanoserver*)
# all nanoserver variants are windows
variant='nanoserver'
;;
*windowsservercore*)
# all servercore variants are windows
variant='windowsservercore'
;;
esac
testRepo="$repo"
if [ -z "$keepNamespace" ]; then
testRepo="${testRepo##*/}"
fi
for possibleAlias in \
"${testAlias[$repo:$variant]:-}" \
"${testAlias[$repo]:-}" \
"${testAlias[$testRepo:$variant]:-}" \
"${testAlias[$testRepo]:-}" \
; do
if [ -n "$possibleAlias" ]; then
testRepo="$possibleAlias"
break
fi
done
explicitVariant=
for possibleExplicit in \
"${explicitTests[:$variant]:-}" \
"${explicitTests[$repo:$variant]:-}" \
"${explicitTests[$repo]:-}" \
"${explicitTests[$testRepo:$variant]:-}" \
"${explicitTests[$testRepo]:-}" \
; do
if [ -n "$possibleExplicit" ]; then
explicitVariant=1
break
fi
done
testCandidates=()
if [ -z "$explicitVariant" ]; then
testCandidates+=( "${globalTests[@]}" )
fi
testCandidates+=(
${imageTests[:$variant]:-}
)
if [ -z "$explicitVariant" ]; then
testCandidates+=(
${imageTests[$testRepo]:-}
)
fi
testCandidates+=(
${imageTests[$testRepo:$variant]:-}
)
if [ "$testRepo" != "$repo" ]; then
if [ -z "$explicitVariant" ]; then
testCandidates+=(
${imageTests[$repo]:-}
)
fi
testCandidates+=(
${imageTests[$repo:$variant]:-}
)
fi
tests=()
for t in "${testCandidates[@]}"; do
if [ "${#argTests[@]}" -gt 0 ] && [ -z "${argTests[$t]:-}" ]; then
# skipping due to -t
continue
fi
if [ \
! -z "${globalExcludeTests[${testRepo}_$t]:-}" \
-o ! -z "${globalExcludeTests[${testRepo}:${variant}_$t]:-}" \
-o ! -z "${globalExcludeTests[:${variant}_$t]:-}" \
-o ! -z "${globalExcludeTests[${repo}_$t]:-}" \
-o ! -z "${globalExcludeTests[${repo}:${variant}_$t]:-}" \
-o ! -z "${globalExcludeTests[:${variant}_$t]:-}" \
]; then
# skipping due to exclude
continue
fi
tests+=( "$t" )
done
# check for zero tests before checking for existing image
# this will make windows variants no longer fail
if [ "${#tests[@]}" -eq '0' ]; then
echo $'\timage has no tests...skipping'
continue
fi
if ! docker inspect "$dockerImage" &> /dev/null; then
echo $'\timage does not exist!'
didFail=1
continue
fi
currentTest=0
totalTest="${#tests[@]}"
for t in "${tests[@]}"; do
(( currentTest+=1 ))
echo -ne "\t'$t' [$currentTest/$totalTest]..."
# run test against dockerImage here
# find the script for the test
scriptDir="${testPaths[$t]}"
if [ -d "$scriptDir" ]; then
script="$scriptDir/run.sh"
if [ -x "$script" ] && [ ! -d "$script" ]; then
# TODO dryRun logic
if output="$("$script" "$dockerImage")"; then
output="$(tr -d '\r' <<<"$output")" # Windows gives us \r\n ... :D
if [ -f "$scriptDir/expected-std-out.txt" ] && ! d="$(diff -u "$scriptDir/expected-std-out.txt" - <<<"$output" 2>/dev/null)"; then
echo 'failed; unexpected output:'
echo "$d"
didFail=1
else
echo 'passed'
fi
else
echo 'failed'
didFail=1
fi
else
echo "skipping"
echo >&2 "error: $script missing, not executable or is a directory"
didFail=1
continue
fi
else
echo "skipping"
echo >&2 "error: unable to locate test '$t'"
didFail=1
continue
fi
done
done
if [ -n "$didFail" ]; then
exit 1
fi