2
0

test_helpers.bash 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. $options \
  108. -e PYTHON_PORTS="$ports" \
  109. python:3 bash -c "
  110. trap '[ \${#PIDS[@]} -gt 0 ] && kill -TERM \${PIDS[@]}' TERM
  111. declare -a PIDS
  112. for port in \$PYTHON_PORTS; do
  113. echo starting a web server listening on port \$port;
  114. mkdir /var/www/\$port
  115. cd /var/www/\$port
  116. echo \"answer from port \$port\" > data
  117. python -m http.server \$port &
  118. PIDS+=(\$!)
  119. done
  120. wait \${PIDS[@]}
  121. trap - TERM
  122. wait \${PIDS[@]}
  123. "
  124. assert_success
  125. # THEN querying directly port works
  126. IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
  127. for port in $ports; do
  128. run retry 5 1s docker run --label bats-type="curl" appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data
  129. assert_output "answer from port $port"
  130. done
  131. }
  132. # stop all containers with the "bats-type" label (matching the optionally supplied value)
  133. #
  134. # $1 optional label value
  135. function stop_bats_containers {
  136. local -r value=$1
  137. if [ -z "$value" ]; then
  138. CIDS=( $(docker ps -q --filter "label=bats-type") )
  139. else
  140. CIDS=( $(docker ps -q --filter "label=bats-type=$value") )
  141. fi
  142. if [ ${#CIDS[@]} -gt 0 ]; then
  143. docker stop ${CIDS[@]} >&2
  144. fi
  145. }
  146. # wait for a docker-gen container to receive a specified event from a
  147. # container with the specified ID/name
  148. #
  149. # $1 docker-gen container name
  150. # $2 event
  151. # $3 ID/name of container to receive event from
  152. function dockergen_wait_for_event {
  153. local -r container=$1
  154. local -r event=$2
  155. local -r other=$3
  156. local -r did=$(docker_id "$other")
  157. docker_wait_for_log "$container" 9 "Received event $event for container ${did:0:12}"
  158. }