blob: 787edd34fb6bf2a8df2f0a39976c6900d0bf63d7 [file] [log] [blame]
Carlos Sanchez8cb67a02016-04-20 13:40:44 +02001#!/bin/bash
Thomas LEVEIL58695bb2015-09-04 21:51:10 +00002
3# check dependencies
4(
5 type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
6 type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
7)>&2
8
9# Assert that $1 is the outputof a command $2
10function assert {
11 local expected_output=$1
12 shift
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020013 local actual_output
14 actual_output=$("$@")
Carlos Sanchez2d68b192016-04-20 13:26:59 +020015 actual_output="${actual_output//[$'\t\r\n']}" # remove newlines
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000016 if ! [ "$actual_output" = "$expected_output" ]; then
Carlos Sanchezc24ef822016-02-29 12:10:31 +010017 echo "expected: \"$expected_output\""
18 echo "actual: \"$actual_output\""
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000019 false
20 fi
21}
22
23# Retry a command $1 times until it succeeds. Wait $2 seconds between retries.
24function retry {
25 local attempts=$1
26 shift
27 local delay=$1
28 shift
29 local i
30
31 for ((i=0; i < attempts; i++)); do
32 run "$@"
33 if [ "$status" -eq 0 ]; then
34 return 0
35 fi
36 sleep $delay
37 done
38
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020039 echo "Command \"$*\" failed $attempts times. Status: $status. Output: $output" >&2
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000040 false
41}
42
43function get_jenkins_url {
Carlos Sanchezc24ef822016-02-29 12:10:31 +010044 if [ -z "${DOCKER_HOST}" ]; then
Carlos Sanchezd1f26652015-09-11 18:54:47 +020045 DOCKER_IP=localhost
46 else
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020047 DOCKER_IP=$(echo "$DOCKER_HOST" | sed -e 's|tcp://\(.*\):[0-9]*|\1|')
Carlos Sanchezd1f26652015-09-11 18:54:47 +020048 fi
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020049 echo "http://$DOCKER_IP:$(docker port "$SUT_CONTAINER" 8080 | cut -d: -f2)"
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000050}
51
52function test_url {
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020053 run curl --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 "$(get_jenkins_url)$1"
Thomas LEVEIL58695bb2015-09-04 21:51:10 +000054 if [ "$status" -eq 0 ]; then
55 true
56 else
57 echo "URL $(get_jenkins_url)$1 failed" >&2
58 echo "output: $output" >&2
59 false
60 fi
61}
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010062
63function cleanup {
Carlos Sanchez8cb67a02016-04-20 13:40:44 +020064 docker kill "$1" &>/dev/null ||:
65 docker rm -fv "$1" &>/dev/null ||:
Carlos Sanchez5fc9cd02016-02-28 15:25:18 +010066}