run.sh 2.7 KB

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