-
Notifications
You must be signed in to change notification settings - Fork 33
/
test_docker.sh
executable file
·179 lines (145 loc) · 4.57 KB
/
test_docker.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
#!/bin/bash -e
# Test pman in isolation with docker OR podman
#
# Notes:
# - state is dirty if tests fail
# - suggested that you run this script with no existing containers nor volumes
set -o pipefail
HERE=$(dirname "$(readlink -f "$0")")
CONTAINER_RUNTIME=$1
if [ -z "$CONTAINER_RUNTIME" ]; then
echo "usage: $0 [docker|podman]"
exit 1
fi
CR_EXEC=$(which $CONTAINER_RUNTIME)
# shadows the name "podman" so that it calls the specified runtime
# which might be docker OR podman
function podman () {
$CR_EXEC "$@"
}
# find out where the podman/docker socket is
if [ -n "$DOCKER_HOST" ]; then
socket="$DOCKER_HOST"
else
if [[ "$CR_EXEC" = *podman* ]]; then
if [ "$(podman info --format '{{ .Host.RemoteSocket.Exists }}')" != 'true' ]; then
echo "error: podman socket must be active."
echo "try running:"
echo
echo " systemctl --user start podman.service"
echo
exit 1
fi
socket="$(podman info --format '{{ .Host.RemoteSocket.Path }}')"
elif [[ "$CR_EXEC" = *docker* ]]; then
socket=/var/run/docker.sock
fi
fi
set -ex
# pull example ChRIS plugin
SIMPLEDSAPP=docker.io/fnndsc/pl-simpledsapp:2.1.0
podman pull $SIMPLEDSAPP
# build
pman_image="localhost/fnndsc/pman:${0##*/}"
podman build -t $pman_image .
# "storebase" directory where "pfcon" writes data
volume=$(podman volume create --driver local)
storeBase=$(podman volume inspect --format '{{ .Mountpoint }}' $volume)
# simulate action of pfcon receiving data
jid="pmantest-$RANDOM"
podman run --rm \
-v "$HERE:/here:ro" -w /here \
-v "$volume:/var/local/storeBase:rw" \
alpine sh -c "mkdir -vp /var/local/storeBase/key-$jid/incoming /var/local/storeBase/key-$jid/outgoing && cp -v README.md LICENSE /var/local/storeBase/key-$jid/incoming"
# pman identifies the aforementioned volume by inspecting
# its peer pfcon, so run a mock pfcon container
mock_pfcon=$(podman run -d -v "$volume:/var/local/storeBase" -l org.chrisproject.role=pfcon alpine sleep 60)
# run pman in the background
# IGNORE_LIMITS is forwarded to the container as a workaround for a Github Actions bug,
# see .github/workflows/ci.yml
pman=$(
podman run -d --userns host -p 5010:5010 \
-e IGNORE_LIMITS \
-v $socket:/var/run/docker.sock:rw \
-e SECRET_KEY=secret -e CONTAINER_ENV=$CONTAINER_RUNTIME \
$pman_image
)
# wait for pman to be up
set +e
elapsed=0
until [ "$(curl -w '%{http_code}' -o /dev/null -s --head http://localhost:5010/api/v1/)" = "200" ]; do
sleep 1
if [ "$((elapsed++))" -gt "5" ]; then
echo "error: timed out waiting for pman"
exit 1
fi
done
# submit job to pman
set -e
body="$(cat << EOF
{
"jid": "$jid",
"args": [
"--saveinputmeta", "--saveoutputmeta",
"--prefix", "hello_test_"
],
"auid": "cube-test",
"number_of_workers": "1",
"cpu_limit": "1000",
"memory_limit": "200",
"gpu_limit": "0",
"image": "$SIMPLEDSAPP",
"entrypoint": ["simpledsapp"],
"type": "ds",
"input_dir": "key-$jid/incoming",
"output_dir": "key-$jid/outgoing"
}
EOF
)"
curl -H 'Accept: application/json' -H 'Content-Type: application/json' \
--data "$body" -s http://localhost:5010/api/v1/
podman logs $pman # debug output
# assert pman created container, and that STOREBASE was mounted
podman container inspect --format '{{ range .Mounts }}{{ .Source }}{{end}}' $jid \
| grep -Fqm 1 "$storeBase/key-$jid"
# wait for simpledsapp to finish
rc=$(podman wait $jid)
if [ "$rc" != "0" ]; then
echo "failed assertion: $jid exited with code $rc"
exit $rc
fi
podman logs $jid # debug output
# assert simpledsapp worked
podman run --rm -v "$storeBase/key-$jid:/share:ro" alpine diff -rq \
"/share/incoming/README.md" \
"/share/outgoing/hello_test_README.md"
# assert pman reports simpledsapp finishedSuccessfully
status="$(curl -sH 'Accept: application/json' http://localhost:5010/api/v1/$jid/ | jq -r '.status')"
if [ "$status" != "finishedSuccessfully" ]; then
echo "failed assertion: pman reports $jid has status \"$status\""
exit 1
fi
## pause for manual inspection
#echo "press any key to continue..."
#read -n 1 _whatever
# delete job
set +e
curl -sX DELETE http://localhost:5010/api/v1/$jid/
set +o pipefail
podman container inspect $jid 2>&1 | grep -Fqm 1 "no such container" \
|| (echo "assertion failed: container $jid not deleted"; exit 1)
set +x
cat << EOF
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! !!
!! TESTS PASSED !!
!! !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
EOF
# clean up
set +e
set -x
podman kill $pman $mock_pfcon
podman rm $pman $mock_pfcon
podman volume rm $volume
# podman rmi $pman_image # optional