2
0

test_helpers.bash 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Test if requirements are met
  2. (
  3. type docker &>/dev/null || ( echo "docker is not available"; exit 1 )
  4. type curl &>/dev/null || ( echo "curl is not available"; exit 1 )
  5. )>&2
  6. # set a few global variables
  7. SUT_IMAGE=jwilder/nginx-proxy:bats
  8. TEST_FILE=$(basename $BATS_TEST_FILENAME .bats)
  9. # load the Bats stdlib (see https://github.com/sstephenson/bats/pull/110)
  10. DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
  11. export BATS_LIB="${DIR}/lib/bats"
  12. load "${BATS_LIB}/batslib.bash"
  13. # load additional bats helpers
  14. load ${DIR}/lib/helpers.bash
  15. load ${DIR}/lib/docker_helpers.bash
  16. # Define functions specific to our test suite
  17. # run the SUT docker container
  18. # and makes sure it remains started
  19. # and displays the nginx-proxy start logs
  20. #
  21. # $1 container name
  22. # $@ other options for the `docker run` command
  23. function nginxproxy {
  24. local -r container_name=$1
  25. shift
  26. docker_clean $container_name \
  27. && docker run -d \
  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. curl --silent \
  57. --connect-timeout 5 \
  58. --max-time 20 \
  59. "$@" \
  60. http://$(docker_ip $container)${path}
  61. }
  62. # start a container running (one or multiple) webservers listening on given ports
  63. #
  64. # $1 container name
  65. # $2 container port(s). If multiple ports, provide them as a string: "80 90" with a space as a separator
  66. # $@ `docker run` additional options
  67. function prepare_web_container {
  68. local -r container_name=$1
  69. local -r ports=$2
  70. shift 2
  71. local -r options="$@"
  72. local expose_option=""
  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 sh -c "
  92. for port in \$PYTHON_PORTS; do
  93. echo starting a web server listening on port \$port;
  94. mkdir /var/www/\$port
  95. cd /var/www/\$port
  96. echo \"answer from port \$port\" > data
  97. python -m http.server \$port &
  98. done
  99. wait
  100. "
  101. assert_success
  102. # THEN querying directly port works
  103. for port in $ports; do
  104. run retry 5 1s curl --silent --fail http://$(docker_ip $container_name):$port/data
  105. assert_output "answer from port $port"
  106. done
  107. }