-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTest
executable file
·317 lines (279 loc) · 11.4 KB
/
Test
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env bash
set -e -o pipefail
trap 'echo 1>&2 FAILED; exit 1' 0
die() { echo 1>&2 -e "FAILURE:" "$@"; exit 1; }
errorlist=()
adderror() {
echo 2>&1 '***** ERROR:' "$@"
errorlist+=("$*");
}
setup_docker() {
declare -g docker=docker
if ! $docker --version; then
die "Cannot run '$docker' command. Check path?"
elif ! $docker info >/dev/null 2>&1; then
docker='sudo docker'
sudo -v || die "Cannot sudo to run '$docker'; start proxy?"
fi
}
header() {
echo '┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'
[[ -z "$@" ]] || echo "┃ " "$@"
}
type_check() {
echo '───── Type check'
local args=()
$verbose && args+=('-v')
mypy "${args[@]}" # default args in pyproject.toml.
}
unit_tests() {
echo '───── Unit tests'
pytest #"$@" # default args in pyproject.toml.
}
dryrun_tests() {
echo '───── Dry run tests'
local contname=$RANDOM$RANDOM-delete-me # name that must not exist
# * We do not use -q here because we want to test that stdout/stderr
# are properly interleaved.
# * As well as --dry-run, this also confirms that various parameters
# of the `docker` commands, particularly `docker run`, are correct.
output=$(2>&1 dent --dry-run -R -B drytest:xx \
--run-opt=-P -r=--cpu-count=1 \
$contname true) \
|| die "exitcode=$? output='$output'"
expected=$(echo '
.* -----.Removing.image
.* docker.rmi.-f
.* ---.Building.image.
.* docker.build.--no-cache
.* ---.Creating.new.container
.* docker.run.--name=[[:digit:]]*-delete-me
.* --rm=false.*--detach=true.*--tty=false
.* -P.*--cpu-count=1
.* /bin/sleep.2147483647
' | tr -d '\n ' )
[[ $output =~ $expected ]] || die "dryrun bad output:\n$output"
echo 'dryrun ok'
$verbose && {
echo '──── Dryrun Output ────'
echo "$output"
echo '───────────────────────'
}
return 0 # Avoid error if $verbose is false.
}
nonbuild_tests() {
# We are probably ok using a single image name for multiple
# (even simultaneous) test runs because the image never changes.
etest_image=dent/test/nonbuild
etest_container=dent-test-nonbuild.$$
header "Non-build tests"
echo '≡≡≡≡≡ Setup'
echo "Test image: $etest_image"
echo "Test container: $etest_container"
etest_image_sha=$(
echo '
FROM alpine:latest
CMD ["/bin/sleep", "10"]
' | $docker build -q -t $etest_image -)
echo "Built test image as $etest_image_sha"
echo '≡≡≡≡≡ Pre-existing container tests'
# The test container made from this image is run with --rm so it will
# automatically clean itself up shortly, regardless of test status.
echo -n "docker container rm -f $etest_container: "
$docker container rm -f $etest_container || true
echo -n "docker run --name $etest_container $etest_image: "
$docker run --rm --detach --name $etest_container $etest_image
echo '===== Exec in running container (stdin is terminal if Test is)'
output=$(dent -q $etest_container /bin/echo -n etest 0100 </dev/tty) \
|| die "exitcode=$? output='$output'"
[[ $output = 'etest 0100' ]] || die "bad output='$output'"
echo "ok output='$output'"
echo '===== Exec in running container (stdin not terminal)'
output=$(dent -q $etest_container /bin/echo -n etest 0110 </dev/null) \
|| die "exitcode=$? output='$output'"
[[ $output = 'etest 0110' ]] || die "bad output='$output'"
echo "ok output='$output'"
echo '===== Starts stopped container before exec'
echo -n "docker stop $etest_container: "
$docker stop -t 0 $etest_container # Removes itself when stopped
echo -n "docker create $etest_container: "
$docker create --rm --name $etest_container $etest_image
output=$(dent -q $etest_container /bin/echo -n etest 0120) \
|| die "exitcode=$? output='$output'"
[[ $output = 'etest 0120' ]] || die "bad output='$output'"
echo "ok output='$output'"
$keep_images || {
echo -n "docker image rm -f $etest_image: "
$docker image rm -f "$etest_image" || true
}
echo '≡≡≡≡≡ Container creation from missing pre-existing image '
# We need to confirm that dent tries to download the image, rather than
# trying to build it locally. This means the image must not already
# be on the existing system, but we can't delete local images because
# they might be in use by other images or containers. So the best option
# is to use an image that doesn't exist at all, and simply confirm the
# attempt to download and its failure.
#
# XXX Unfortunately this test actually tries to download the image
# (in order to fail doing it), and so it's slow. There seems to be no
# way to tell `docker run` not to pull the image but just fail
# immediately.
badname='k7k22zhtj6bv:nonexistimage'
echo "docker container rm -f $etest_container"
$docker container rm -f $etest_container >/dev/null
echo "dent -q -i $badname $etest_container /bin/echo -n etest 0220"
exitcode=0; output=$(
dent -q -i "$badname" $etest_container /bin/echo -n etest 0220
) || exitcode=$?
[[ $exitcode -eq 1 ]] || die "exitcode=$? ≠ 1; output='$output'"
[[ $output = '' ]] || die "bad output='$output'"
echo "ok output='$output'"
# XXX This assumes the presence of `alpine:latest`, which needs to
# be maually downloaded to make the test pass. We should probably
# be creating our own local (and tiny) image to use instead.
echo '≡≡≡≡≡ Container creation from present pre-existing image'
echo "docker container rm -f $etest_container"
$docker container rm -f $etest_container >/dev/null
echo "dent -q -i alpine:latest $etest_container /bin/echo -n etest 0200"
exitcode=0; output=$(
dent -q -i alpine:latest $etest_container /bin/echo -n etest 0200
) || exitcode=$?
echo "docker container rm -f $etest_container"
$docker container rm -f $etest_container >/dev/null 2>&1 || true
[[ $exitcode -eq 0 ]] || die "exitcode=$? output='$output'"
[[ $output = 'etest 0200' ]] || die "bad output='$output'"
echo "ok output='$output'"
}
build_tests() {
# Unless arguments are passed in to test specific base images, all
# base images known to dent and viable on this platform are tested.
header 'Builds from base images'
# Certain very old images don't work on very new kernels.
kern_release=$(uname -r)
skip_images=()
if [[ $kern_release > 4.18 ]]; then
# On ≤ centos:6, bash core dumps on 4.19, though works on 4.4.
skip_images+=(centos:5 centos:6)
echo "Kernel $kern_release: skipping images ${skip_images[@]}"
fi
[[ ${#baseimages} = 0 ]] && baseimages=($(dent -L))
for baseimage in "${baseimages[@]}"; do
for skip in "${skip_images[@]}"; do
[[ $baseimage = $skip ]] && {
echo "SKIP: $baseimage broken on $kern_release"
continue 2
}
done
tag=dent-test-$$
tag=dent-test # DEBUG
image="dent/${baseimage/:/.}:$tag" # duplicates code in dent
container="dent-test-${baseimage/:/.}"
header "$baseimage"
echo " Image: $image"
echo "Container: $container"
# Also removes any containers based on this image, running or not.
echo -n "docker image rm -f $image: "
$docker image rm -f "$image" || true
if ! dent $force_rebuild -B $baseimage -t $tag $container true
then
adderror $baseimage
else
# Tests on container now that we know it comes up.
testvar() {
VAR="$1"; shift
VALUE="$1"; shift
if ! dent $container bash -c "echo $VAR=\$$VAR" \
| grep -q "$VAR=$VALUE"
then
adderror "$baseimage: expected $VAR='$VALUE'"
dent 1>&2 $container bash -c "echo $VAR=\$$VAR"
fi
}
# Confirm that we're ensuring $USER and $LOGNAME are set (even
# for non-login shells: no `-l` here!) because this is supposed
# to be emulating a user environment, even though login(1) or
# similar is not being run.
testvar USER "$USER"
testvar LOGNAME "$LOGNAME"
testvar HOST_HOSTNAME "$HOSTNAME"
# Confirm that user can use sudo.
dent $container sudo -v -S </dev/null \
|| adderror "$baseimage: user cannot sudo"
fi
echo -n "docker container rm -f $container: "
$docker container rm -f "$container" || true
$keep_images || {
echo -n "docker image rm -f $image: "
$docker image rm -f "$image" || true
}
done
errcount=${#errorlist[@]}
if [[ $errcount -gt 0 ]]; then
echo "===== ${errcount} Errors:"
for e in "${errorlist[@]}"; do echo " $e"; done
exit 1
fi
}
skip() {
# This differs from calling "nothing at all" with an empty
# variable in that it accepts and ignores any arguments.
:
}
usage() {
[[ "$@" ]] && echo 1>&2 "$@"
sed -e 's/^ //' <<____ 1>&2
Usage: $0 [OPTS …] [-- [PYTEST-ARGS]]
OPTS (see doc/DEVEL.md for more details):
-T, --skip-typecheck
--skip-nonbuild
--skip-build
-B IMGNAME
--keep-images
--no-force-rebuild
____
exit 2
}
####################################################################
# Main
base=$(command cd "$(dirname "$0")" && pwd -P)
command cd "$base"
verbose=false
small_clean=false
skip_typecheck=false
nonbuild_tests=nonbuild_tests
build_tests=build_tests
baseimages=() # base images to test; default `dent -L`
keep_images=false
force_rebuild=--force-rebuild
while true; do case "$1" in
-h|--help) usage;;
-v|--verbose) shift; verbose=true;; # does not affect pytest
--skip-nonbuild) shift; nonbuild_tests=skip;;
--skip-build) shift; build_tests=skip;;
-B) shift; baseimages+=("$1"); shift;;
--keep-images) shift; keep_images=true;;
-f|--no-force-rebuild) shift; force_rebuild=;; # ("fast" mode)
-c|--small-clean) shift; small_clean=true;;
-T|--skip-typechck) shift; skip_typecheck=true;;
--) shift; break;; # remaining args for pytest
-*) usage "Unknown option '$1'";;
*) break;;
esac; done
$small_clean && {
# Even an editable package needs to be re-installed to update certain
# things, such as pyproject.toml metadata (e.g. the version number)
# and the non-Python scripts installed by setup.cfg.
rm -rf .build/virtualenv/
}
# You can select another version of Python by using a .python link, e.g.:
# rm -rf .build && ln -s $(pythonz locate 3.5.10) .python
. ./pactivate -q
setup_docker
header
$skip_typecheck || type_check
unit_tests "$@"
dryrun_tests
$nonbuild_tests
$build_tests "${baseimages[@]}"
trap '' 0
echo OK