Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 1 | |
| 2 | # check dependencies |
| 3 | ( |
| 4 | type docker &>/dev/null || ( echo "docker is not available"; exit 1 ) |
| 5 | type curl &>/dev/null || ( echo "curl is not available"; exit 1 ) |
| 6 | )>&2 |
| 7 | |
| 8 | # Assert that $1 is the outputof a command $2 |
| 9 | function assert { |
| 10 | local expected_output=$1 |
| 11 | shift |
Carlos Sanchez | 5fc9cd0 | 2016-02-28 15:25:18 +0100 | [diff] [blame] | 12 | local actual_output=$("$@") |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 13 | if ! [ "$actual_output" = "$expected_output" ]; then |
Carlos Sanchez | c24ef82 | 2016-02-29 12:10:31 +0100 | [diff] [blame] | 14 | echo "expected: \"$expected_output\"" |
| 15 | echo "actual: \"$actual_output\"" |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 16 | false |
| 17 | fi |
| 18 | } |
| 19 | |
| 20 | # Retry a command $1 times until it succeeds. Wait $2 seconds between retries. |
| 21 | function retry { |
| 22 | local attempts=$1 |
| 23 | shift |
| 24 | local delay=$1 |
| 25 | shift |
| 26 | local i |
| 27 | |
| 28 | for ((i=0; i < attempts; i++)); do |
| 29 | run "$@" |
| 30 | if [ "$status" -eq 0 ]; then |
| 31 | return 0 |
| 32 | fi |
| 33 | sleep $delay |
| 34 | done |
| 35 | |
| 36 | echo "Command \"$@\" failed $attempts times. Status: $status. Output: $output" >&2 |
| 37 | false |
| 38 | } |
| 39 | |
| 40 | function get_jenkins_url { |
Carlos Sanchez | c24ef82 | 2016-02-29 12:10:31 +0100 | [diff] [blame] | 41 | if [ -z "${DOCKER_HOST}" ]; then |
Carlos Sanchez | d1f2665 | 2015-09-11 18:54:47 +0200 | [diff] [blame] | 42 | DOCKER_IP=localhost |
| 43 | else |
| 44 | DOCKER_IP=$(echo $DOCKER_HOST | sed -e 's|tcp://\(.*\):[0-9]*|\1|') |
| 45 | fi |
| 46 | echo "http://$DOCKER_IP:$(docker port $SUT_CONTAINER 8080 | cut -d: -f2)" |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | function test_url { |
| 50 | run curl --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 $(get_jenkins_url)$1 |
| 51 | if [ "$status" -eq 0 ]; then |
| 52 | true |
| 53 | else |
| 54 | echo "URL $(get_jenkins_url)$1 failed" >&2 |
| 55 | echo "output: $output" >&2 |
| 56 | false |
| 57 | fi |
| 58 | } |
Carlos Sanchez | 5fc9cd0 | 2016-02-28 15:25:18 +0100 | [diff] [blame] | 59 | |
| 60 | function cleanup { |
| 61 | docker kill $1 &>/dev/null ||: |
| 62 | docker rm -fv $1 &>/dev/null ||: |
| 63 | } |