forked from exivity/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
75 lines (57 loc) · 1.89 KB
/
test.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
#!/usr/bin/env bash
# Fail script in case of unset variables - see more here:
# http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2577541.
set -o nounset
# Fail scripts in case a command fails - see more here:
# http://web.archive.org/web/20110314180918/http://www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2577574.
set -o errexit
OS_PREFIX_FOR_LINUX='ubuntu'
OS_PREFIX_FOR_WINDOWS='windows'
if [[ $CI_PLATFORM == "$OS_PREFIX_FOR_LINUX"* ]]; then
DOCKER_PLATFORM='linux'
echo 'About to test Linux Docker container ...'
elif [[ $CI_PLATFORM == "$OS_PREFIX_FOR_WINDOWS"* ]]; then
DOCKER_PLATFORM='windows'
echo 'About to test Windows Docker container ...'
fi
LATEST_TAG=latest-$DOCKER_PLATFORM
# https://gist.github.com/sj26/88e1c6584397bb7c13bd11108a579746
function retry {
local retries=$1
shift
local count=0
until "$@"; do
exit=$?
wait=$((2 ** $count))
count=$(($count + 1))
if [ $count -lt $retries ]; then
echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
sleep $wait
else
echo "Retry $count/$retries exited $exit, no more retries left."
return $exit
fi
done
return 0
}
function get_health_status {
docker inspect --format="{{json .State.Health.Status}}" test
}
function check_if_healthy {
status=`get_health_status`
echo "Got status: $status"
[[ $status == "\"healthy\"" ]]
}
set -e
echo "Running Docker image $DOCKER_IMAGE:$LATEST_TAG in a container named 'test' ..."
docker container run \
--rm \
--detach \
--name test \
$DOCKER_IMAGE:$LATEST_TAG
echo "Running health check ..."
# retry 6 means we will wait max 1+2+4+8+16 seconds
retry 6 check_if_healthy || (docker inspect test && exit 1)
echo "Stopping Docker container 'test' ..."
docker stop test
echo 'Docker container has been successfully stopped'