run.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. [ "$DEBUG" ] && set -x
  3. set -eo pipefail
  4. dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
  5. image="$1"
  6. # our image may not have "curl" (Alpine variants, for example)
  7. clientImage='buildpack-deps:stretch-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" curl -fs -X"$method" "$@" "http://es:9200/$url"
  22. }
  23. _trimmed() {
  24. _request "$@" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g'
  25. }
  26. _req-comp() {
  27. local method="$1"; shift
  28. local url="$1"; shift
  29. local expected="$1"; shift
  30. response="$(_trimmed "$method" "$url")"
  31. [ "$response" = "$expected" ]
  32. }
  33. _req-exit() {
  34. local method="$1"; shift
  35. local url="$1"; shift
  36. local expectedRet="$1"; shift
  37. [ "$(_request "$method" "$url" --output /dev/null || echo "$?")" = "$expectedRet" ]
  38. }
  39. # Make sure our container is listening
  40. . "$dir/../../retry.sh" '! _req-exit GET / 7' # "Failed to connect to host."
  41. # Perform simple health check
  42. _req-comp GET '/_cat/health?h=status' 'green'
  43. # should be green because it's empty and fresh
  44. _req-exit GET '/_cat/indices/test1?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  45. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  46. doc='{"a":"b","c":{"d":"e"}}'
  47. _request POST '/test1/test/1?refresh=true' --data "$doc" --header 'Content-Type: application/json' -o /dev/null
  48. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  49. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  50. _request POST '/test2/test/1?refresh=true' --data "$doc" --header 'Content-Type: application/json' -o /dev/null
  51. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  52. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  53. _req-comp GET '/test1/test/1/_source' "$doc"
  54. _req-comp GET '/test2/test/1/_source' "$doc"
  55. _request DELETE '/test1/test/1?refresh=true' -o /dev/null
  56. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  57. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  58. _request DELETE '/test2/test/1?refresh=true' -o /dev/null
  59. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  60. _req-comp GET '/_cat/indices/test2?h=docs.count' 0