test_helpers.bash 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  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. # Assert that $1 is the outputof a command $2
  8. function assert {
  9. local expected_output=$1
  10. shift
  11. local actual_output
  12. actual_output=$("$@")
  13. actual_output="${actual_output//[$'\t\r\n']}" # remove newlines
  14. if ! [ "$actual_output" = "$expected_output" ]; then
  15. echo "expected: \"$expected_output\""
  16. echo "actual: \"$actual_output\""
  17. false
  18. fi
  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. for ((i=0; i < attempts; i++)); do
  28. run "$@"
  29. if [ "$status" -eq 0 ]; then
  30. return 0
  31. fi
  32. sleep $delay
  33. done
  34. echo "Command \"$*\" failed $attempts times. Status: $status. Output: $output" >&2
  35. false
  36. }
  37. function docker_build {
  38. if [ -n "$JENKINS_VERSION" ]; then
  39. docker build --build-arg JENKINS_VERSION=$JENKINS_VERSION --build-arg JENKINS_SHA=$JENKINS_SHA "$@"
  40. else
  41. docker build "$@"
  42. fi
  43. }
  44. function get_jenkins_url {
  45. if [ -z "${DOCKER_HOST}" ]; then
  46. DOCKER_IP=localhost
  47. else
  48. DOCKER_IP=$(echo "$DOCKER_HOST" | sed -e 's|tcp://\(.*\):[0-9]*|\1|')
  49. fi
  50. echo "http://$DOCKER_IP:$(docker port "$SUT_CONTAINER" 8080 | cut -d: -f2)"
  51. }
  52. function get_jenkins_password {
  53. docker logs "$SUT_CONTAINER" 2>&1 | grep -A 2 "Please use the following password to proceed to installation" | tail -n 1
  54. }
  55. function test_url {
  56. run curl --user "admin:$(get_jenkins_password)" --output /dev/null --silent --head --fail --connect-timeout 30 --max-time 60 "$(get_jenkins_url)$1"
  57. if [ "$status" -eq 0 ]; then
  58. true
  59. else
  60. echo "URL $(get_jenkins_url)$1 failed" >&2
  61. echo "output: $output" >&2
  62. false
  63. fi
  64. }
  65. function cleanup {
  66. docker kill "$1" &>/dev/null ||:
  67. docker rm -fv "$1" &>/dev/null ||:
  68. }
  69. function unzip_manifest {
  70. local plugin=$1
  71. local work=$2
  72. bash -c "docker run --rm -v $work:/var/jenkins_home --entrypoint unzip $SUT_IMAGE -p /var/jenkins_home/plugins/$plugin META-INF/MANIFEST.MF | tr -d '\r'"
  73. }