forked from cachethq/Docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helpers.bash
61 lines (53 loc) · 1.43 KB
/
test_helpers.bash
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
# Test if requirements are met
(
type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
)>&2
# ENV vars for tests
export APP_ENV=development
export APP_KEY="base64:v2LwHrdgnE+RavEXdnF8LgWIibjvEcFkU2qaX5Ji708="
TEST_FILE=$(basename $BATS_TEST_FILENAME .bats)
# stop all containers with the "bats-type" label (matching the optionally supplied value)
#
# $1 optional label value
function stop_bats_containers {
docker-compose stop
}
# delete all containers
docker_cleanup() {
docker-compose rm -af
}
# Send a HTTP request to container $1 for path $2 and
# Additional curl options can be passed as $@
#
# $1 container name
# $2 HTTP path to query
# $@ additional options to pass to the curl command
function curl_container {
local -r container=$1
local -r path=$2
shift 2
docker run --rm --net=docker_default --label bats-type="curl" curlimages/curl --silent \
--connect-timeout 5 \
--max-time 20 \
--retry 4 --retry-delay 5 \
"$@" \
http://$(docker_ip $container)${path}
}
# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
function retry {
local attempts=$1
shift
local delay=$1
shift
local i
for ((i=0; i < attempts; i++)); do
run "$@"
if [ "$status" -eq 0 ]; then
echo "$output"
return 0
fi
sleep $delay
done
echo "Command \"$@\" failed $attempts times. Status: $status. Output: $output" >&2
false
}