Carlos Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 1 | #!/bin/bash |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 2 | |
| 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 |
| 10 | function assert { |
| 11 | local expected_output=$1 |
| 12 | shift |
Carlos Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 13 | local actual_output |
| 14 | actual_output=$("$@") |
Carlos Sanchez | 2d68b19 | 2016-04-20 13:26:59 +0200 | [diff] [blame] | 15 | actual_output="${actual_output//[$'\t\r\n']}" # remove newlines |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 16 | if ! [ "$actual_output" = "$expected_output" ]; then |
Carlos Sanchez | c24ef82 | 2016-02-29 12:10:31 +0100 | [diff] [blame] | 17 | echo "expected: \"$expected_output\"" |
| 18 | echo "actual: \"$actual_output\"" |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 19 | false |
| 20 | fi |
| 21 | } |
| 22 | |
| 23 | # Retry a command $1 times until it succeeds. Wait $2 seconds between retries. |
| 24 | function 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 Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 39 | echo "Command \"$*\" failed $attempts times. Status: $status. Output: $output" >&2 |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 40 | false |
| 41 | } |
| 42 | |
| 43 | function get_jenkins_url { |
Carlos Sanchez | c24ef82 | 2016-02-29 12:10:31 +0100 | [diff] [blame] | 44 | if [ -z "${DOCKER_HOST}" ]; then |
Carlos Sanchez | c3e7a0c | 2016-04-20 16:39:04 +0200 | [diff] [blame] | 45 | if [ "$(uname)" == "Darwin" ]; then |
| 46 | DOCKER_IP=docker.local |
| 47 | else |
| 48 | DOCKER_IP=localhost |
| 49 | fi |
Carlos Sanchez | d1f2665 | 2015-09-11 18:54:47 +0200 | [diff] [blame] | 50 | else |
Carlos Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 51 | DOCKER_IP=$(echo "$DOCKER_HOST" | sed -e 's|tcp://\(.*\):[0-9]*|\1|') |
Carlos Sanchez | d1f2665 | 2015-09-11 18:54:47 +0200 | [diff] [blame] | 52 | fi |
Carlos Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 53 | echo "http://$DOCKER_IP:$(docker port "$SUT_CONTAINER" 8080 | cut -d: -f2)" |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Carlos Sanchez | 5a51d0a | 2016-04-21 13:50:25 +0200 | [diff] [blame^] | 56 | function get_jenkins_password { |
| 57 | docker logs "$SUT_CONTAINER" 2>&1 | grep -A 2 "Please use the following password to proceed to installation" | tail -n 1 |
| 58 | } |
| 59 | |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 60 | function test_url { |
Carlos Sanchez | 5a51d0a | 2016-04-21 13:50:25 +0200 | [diff] [blame^] | 61 | run curl --user "admin:$(get_jenkins_password)" --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 "$(get_jenkins_url)$1" |
Thomas LEVEIL | 58695bb | 2015-09-04 21:51:10 +0000 | [diff] [blame] | 62 | if [ "$status" -eq 0 ]; then |
| 63 | true |
| 64 | else |
| 65 | echo "URL $(get_jenkins_url)$1 failed" >&2 |
| 66 | echo "output: $output" >&2 |
| 67 | false |
| 68 | fi |
| 69 | } |
Carlos Sanchez | 5fc9cd0 | 2016-02-28 15:25:18 +0100 | [diff] [blame] | 70 | |
| 71 | function cleanup { |
Carlos Sanchez | 8cb67a0 | 2016-04-20 13:40:44 +0200 | [diff] [blame] | 72 | docker kill "$1" &>/dev/null ||: |
| 73 | docker rm -fv "$1" &>/dev/null ||: |
Carlos Sanchez | 5fc9cd0 | 2016-02-28 15:25:18 +0100 | [diff] [blame] | 74 | } |