run.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. [ "$DEBUG" ] && set -x
  3. set -eo pipefail
  4. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  5. image="$1"
  6. # Use a client image with curl for testing
  7. clientImage='buildpack-deps:buster-curl'
  8. # Create an instance of the container-under-test
  9. # (explicitly setting a low memory limit since the image defaults to 2GB)
  10. # (disable "bootstrap checks" by setting discovery.type option)
  11. cid="$(docker run -d -e ES_JAVA_OPTS='-Xms128m -Xmx128m' -e discovery.type=single-node "$image")"
  12. trap "docker rm -vf $cid > /dev/null" EXIT
  13. _request() {
  14. local method="$1"
  15. shift
  16. local url="${1#/}"
  17. shift
  18. # https://github.com/docker/docker/issues/14203#issuecomment-129865960 (DOCKER_FIX)
  19. docker run --rm --link "$cid":es \
  20. -e DOCKER_FIX=' ' \
  21. "$clientImage" \
  22. curl -fs -X"$method" "$@" "http://es:9200/$url"
  23. }
  24. _trimmed() {
  25. _request "$@" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g'
  26. }
  27. _req-comp() {
  28. local method="$1"; shift
  29. local url="$1"; shift
  30. local expected="$1"; shift
  31. response="$(_trimmed "$method" "$url")"
  32. [ "$response" = "$expected" ]
  33. }
  34. _req-exit() {
  35. local method="$1"; shift
  36. local url="$1"; shift
  37. local expectedRet="$1"; shift
  38. [ "$(_request "$method" "$url" --output /dev/null || echo "$?")" = "$expectedRet" ]
  39. }
  40. # Make sure our container is listening
  41. . "$dir/../../retry.sh" '! _req-exit GET / 7' # "Failed to connect to host."
  42. # Perform simple health check
  43. _req-comp GET '/_cat/health?h=status' 'green'
  44. # should be green because it's empty and fresh
  45. _req-exit GET '/_cat/indices/test1?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  46. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  47. doc='{"a":"b","c":{"d":"e"}}'
  48. _request POST '/test1/test/1?refresh=true' --data "$doc" --header 'Content-Type: application/json' -o /dev/null
  49. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  50. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  51. _request POST '/test2/test/1?refresh=true' --data "$doc" --header 'Content-Type: application/json' -o /dev/null
  52. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  53. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  54. _req-comp GET '/test1/test/1/_source' "$doc"
  55. _req-comp GET '/test2/test/1/_source' "$doc"
  56. _request DELETE '/test1/test/1?refresh=true' -o /dev/null
  57. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  58. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  59. _request DELETE '/test2/test/1?refresh=true' -o /dev/null
  60. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  61. _req-comp GET '/_cat/indices/test2?h=docs.count' 0