run.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. cid="$(docker run -d -e ES_JAVA_OPTS='-Xms32m -Xmx32m' "$image")"
  11. trap "docker rm -vf $cid > /dev/null" EXIT
  12. _request() {
  13. local method="$1"
  14. shift
  15. local url="${1#/}"
  16. shift
  17. # https://github.com/docker/docker/issues/14203#issuecomment-129865960 (DOCKER_FIX)
  18. docker run --rm --link "$cid":es \
  19. -e DOCKER_FIX=' ' \
  20. "$clientImage" curl -fs -X"$method" "$@" "http://es:9200/$url"
  21. }
  22. _trimmed() {
  23. _request "$@" | sed -r 's/^[[:space:]]+|[[:space:]]+$//g'
  24. }
  25. _req-comp() {
  26. local method="$1"; shift
  27. local url="$1"; shift
  28. local expected="$1"; shift
  29. response="$(_trimmed "$method" "$url")"
  30. [ "$response" = "$expected" ]
  31. }
  32. _req-exit() {
  33. local method="$1"; shift
  34. local url="$1"; shift
  35. local expectedRet="$1"; shift
  36. [ "$(_request "$method" "$url" --output /dev/null || echo "$?")" = "$expectedRet" ]
  37. }
  38. # Make sure our container is listening
  39. . "$dir/../../retry.sh" '! _req-exit GET / 7' # "Failed to connect to host."
  40. # Perform simple health check
  41. _req-comp GET '/_cat/health?h=status' 'green'
  42. # should be green because it's empty and fresh
  43. _req-exit GET '/_cat/indices/test1?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  44. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  45. doc='{"a":"b","c":{"d":"e"}}'
  46. _request POST '/test1/test/1?refresh=true' --data "$doc" -o /dev/null
  47. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  48. _req-exit GET '/_cat/indices/test2?h=docs.count' 22 # "HTTP page not retrieved. 4xx"
  49. _request POST '/test2/test/1?refresh=true' --data "$doc" -o /dev/null
  50. _req-comp GET '/_cat/indices/test1?h=docs.count' 1
  51. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  52. _req-comp GET '/test1/test/1/_source' "$doc"
  53. _req-comp GET '/test2/test/1/_source' "$doc"
  54. _request DELETE '/test1/test/1?refresh=true' -o /dev/null
  55. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  56. _req-comp GET '/_cat/indices/test2?h=docs.count' 1
  57. _request DELETE '/test2/test/1?refresh=true' -o /dev/null
  58. _req-comp GET '/_cat/indices/test1?h=docs.count' 0
  59. _req-comp GET '/_cat/indices/test2?h=docs.count' 0