2
0

test_helpers.bash 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. --name $container_name \
  28. "$@" \
  29. $SUT_IMAGE \
  30. && wait_for_nginxproxy_container_to_start $container_name \
  31. && docker logs $container_name
  32. }
  33. # wait until the nginx-proxy container is ready to operate
  34. #
  35. # $1 container name
  36. function wait_for_nginxproxy_container_to_start {
  37. local -r container_name=$1
  38. sleep .5s # give time to eventually fail to initialize
  39. function is_running {
  40. run docker_running_state $container_name
  41. assert_output "true"
  42. }
  43. retry 3 1 is_running
  44. }
  45. # Send a HTTP request to container $1 for path $2 and
  46. # Additional curl options can be passed as $@
  47. #
  48. # $1 container name
  49. # $2 HTTP path to query
  50. # $@ additional options to pass to the curl command
  51. function curl_container {
  52. local -r container=$1
  53. local -r path=$2
  54. shift 2
  55. docker run appropriate/curl --silent \
  56. --connect-timeout 5 \
  57. --max-time 20 \
  58. "$@" \
  59. http://$(docker_ip $container)${path}
  60. }
  61. # start a container running (one or multiple) webservers listening on given ports
  62. #
  63. # $1 container name
  64. # $2 container port(s). If multiple ports, provide them as a string: "80 90" with a space as a separator
  65. # $@ `docker run` additional options
  66. function prepare_web_container {
  67. local -r container_name=$1
  68. local -r ports=$2
  69. shift 2
  70. local -r options="$@"
  71. local expose_option=""
  72. IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
  73. for port in $ports; do
  74. expose_option="${expose_option}--expose=$port "
  75. done
  76. ( # used for debugging purpose. Will be display if test fails
  77. echo "container_name: $container_name"
  78. echo "ports: $ports"
  79. echo "options: $options"
  80. echo "expose_option: $expose_option"
  81. )>&2
  82. docker_clean $container_name
  83. # GIVEN a container exposing 1 webserver on ports 1234
  84. run docker run -d \
  85. --label bats-type="web" \
  86. --name $container_name \
  87. $expose_option \
  88. -w /var/www/ \
  89. $options \
  90. -e PYTHON_PORTS="$ports" \
  91. python:3 bash -c "
  92. trap '[ \${#PIDS[@]} -gt 0 ] && kill -TERM \${PIDS[@]}' TERM
  93. declare -a PIDS
  94. for port in \$PYTHON_PORTS; do
  95. echo starting a web server listening on port \$port;
  96. mkdir /var/www/\$port
  97. cd /var/www/\$port
  98. echo \"answer from port \$port\" > data
  99. python -m http.server \$port &
  100. PIDS+=(\$!)
  101. done
  102. wait \${PIDS[@]}
  103. trap - TERM
  104. wait \${PIDS[@]}
  105. "
  106. assert_success
  107. # THEN querying directly port works
  108. IFS=$' \t\n' # See https://github.com/sstephenson/bats/issues/89
  109. for port in $ports; do
  110. run retry 5 1s docker run appropriate/curl --silent --fail http://$(docker_ip $container_name):$port/data
  111. assert_output "answer from port $port"
  112. done
  113. }