2
0

test_helpers.bash 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Test if requirements are met
  2. (
  3. type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
  4. )>&2
  5. # set a few global variables
  6. SUT_IMAGE=jwilder/nginx-proxy:bats
  7. TEST_FILE=$(basename $BATS_TEST_FILENAME .bats)
  8. # load the Bats stdlib (see https://github.com/sstephenson/bats/pull/110)
  9. DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  10. export BATS_LIB="${DIR}/lib/bats"
  11. load "${BATS_LIB}/batslib.bash"
  12. # load additional bats helpers
  13. load ${DIR}/lib/helpers.bash
  14. load ${DIR}/lib/docker_helpers.bash
  15. # Define functions specific to our test suite
  16. # run the SUT docker container
  17. # and makes sure it remains started
  18. # and displays the nginx-proxy start logs
  19. #
  20. # $1 container name
  21. # $@ other options for the `docker run` command
  22. function nginxproxy {
  23. local -r container_name=$1
  24. shift
  25. docker_clean $container_name \
  26. && docker run -d \
  27. --label bats-type="nginx-proxy" \
  28. --name $container_name \
  29. "$@" \
  30. $SUT_IMAGE \
  31. && wait_for_nginxproxy_container_to_start $container_name \
  32. && docker logs $container_name
  33. }
  34. # wait until the nginx-proxy container is ready to operate
  35. #
  36. # $1 container name
  37. function wait_for_nginxproxy_container_to_start {
  38. local -r container_name=$1
  39. sleep .5s # give time to eventually fail to initialize
  40. function is_running {
  41. run docker_running_state $container_name
  42. assert_output "true"
  43. }
  44. retry 3 1 is_running
  45. }
  46. # Send a HTTP request to container $1 for path $2 and
  47. # Additional curl options can be passed as $@
  48. #
  49. # $1 container name
  50. # $2 HTTP path to query
  51. # $@ additional options to pass to the curl command
  52. function curl_container {
  53. local -r container=$1
  54. local -r path=$2
  55. shift 2
  56. docker run --label bats-type="curl" appropriate/curl --silent \
  57. --connect-timeout 5 \
  58. --max-time 20 \
  59. "$@" \
  60. http://$(docker_ip $container)${path}
  61. }
  62. # Send a HTTPS request to container $1 for path $2 and
  63. # Additional curl options can be passed as $@
  64. #
  65. # $1 container name
  66. # $2 HTTPS path to query
  67. # $@ additional options to pass to the curl command
  68. function curl_container_https {
  69. local -r container=$1
  70. local -r path=$2
  71. shift 2
  72. docker run --label bats-type="curl" appropriate/curl --silent \
  73. --connect-timeout 5 \
  74. --max-time 20 \
  75. --insecure \
  76. "$@" \
  77. https://$(docker_ip $container)${path}
  78. }
  79. # start a container running (one or multiple) webservers listening on given ports
  80. #
  81. # $1 container name
  82. # $2 container port(s). If multiple ports, provide them as a string: "80 90" with a space as a separator
  83. # $@ `docker run` additional options
  84. function prepare_web_container {
  85. local -r container_name=$1
  86. local -r ports=$2
  87. shift 2
  88. local -r options="$@"
  89. local expose_option=""
  90. IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
  91. for port in $ports; do
  92. expose_option="${expose_option}--expose=$port "
  93. done
  94. ( # used for debugging purpose. Will be display if test fails
  95. echo "container_name: $container_name"
  96. echo "ports: $ports"
  97. echo "options: $options"
  98. echo "expose_option: $expose_option"
  99. )>&2
  100. docker_clean $container_name
  101. # GIVEN a container exposing 1 webserver on ports 1234
  102. run docker run -d \
  103. --label bats-type="web" \
  104. --name $container_name \
  105. $expose_option \
  106. -w /var/www/ \
  107. -v $DIR/web_helpers:/var/www:ro \
  108. $options \
  109. -e PYTHON_PORTS="$ports" \
  110. python:3 bash -c "
  111. trap '[ \${#PIDS[@]} -gt 0 ] && kill -TERM \${PIDS[@]}' TERM
  112. declare -a PIDS
  113. for port in \$PYTHON_PORTS; do
  114. echo starting a web server listening on port \$port;
  115. ./webserver.py \$port &
  116. PIDS+=(\$!)
  117. done
  118. wait \${PIDS[@]}
  119. trap - TERM
  120. wait \${PIDS[@]}
  121. "
  122. assert_success
  123. # THEN querying directly port works
  124. IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
  125. for port in $ports; do
  126. run retry 5 1s docker run --label bats-type="curl" appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/port
  127. assert_output "answer from port $port"
  128. done
  129. }
  130. # stop all containers with the "bats-type" label (matching the optionally supplied value)
  131. #
  132. # $1 optional label value
  133. function stop_bats_containers {
  134. local -r value=$1
  135. if [ -z "$value" ]; then
  136. CIDS=( $(docker ps -q --filter "label=bats-type") )
  137. else
  138. CIDS=( $(docker ps -q --filter "label=bats-type=$value") )
  139. fi
  140. if [ ${#CIDS[@]} -gt 0 ]; then
  141. docker stop ${CIDS[@]} >&2
  142. fi
  143. }
  144. # wait for a docker-gen container to receive a specified event from a
  145. # container with the specified ID/name
  146. #
  147. # $1 docker-gen container name
  148. # $2 event
  149. # $3 ID/name of container to receive event from
  150. function dockergen_wait_for_event {
  151. local -r container=$1
  152. local -r event=$2
  153. local -r other=$3
  154. local -r did=$(docker_id "$other")
  155. docker_wait_for_log "$container" 9 "Received event $event for container ${did:0:12}"
  156. }