emqx-smoke-test.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. [ $# -ne 2 ] && { echo "Usage: $0 host port"; exit 1; }
  4. HOST=$1
  5. PORT=$2
  6. BASE_URL="http://$HOST:$PORT"
  7. ## Check if EMQX is responding
  8. wait_for_emqx() {
  9. local attempts=10
  10. local url="$BASE_URL"/status
  11. while ! curl "$url" >/dev/null 2>&1; do
  12. if [ $attempts -eq 0 ]; then
  13. echo "emqx is not responding on $url"
  14. exit 1
  15. fi
  16. sleep 5
  17. attempts=$((attempts-1))
  18. done
  19. }
  20. ## Get the JSON format status which is jq friendly and includes a version string
  21. json_status() {
  22. local url="${BASE_URL}/status?format=json"
  23. local resp
  24. resp="$(curl -s "$url")"
  25. if (echo "$resp" | jq . >/dev/null 2>&1); then
  26. echo "$resp"
  27. else
  28. echo 'NOT_JSON'
  29. fi
  30. }
  31. ## Check if the API docs are available
  32. check_api_docs() {
  33. local url="$BASE_URL/api-docs/index.html"
  34. local status
  35. status="$(curl -s -o /dev/null -w "%{http_code}" "$url")"
  36. if [ "$status" != "200" ]; then
  37. echo "emqx return non-200 responses($status) on $url"
  38. exit 1
  39. fi
  40. }
  41. ## Check if the swagger.json contains hidden fields
  42. ## fail if it does
  43. check_swagger_json() {
  44. local url="$BASE_URL/api-docs/swagger.json"
  45. ## assert swagger.json is valid json
  46. JSON="$(curl -s "$url")"
  47. echo "$JSON" | jq . >/dev/null
  48. ## assert swagger.json does not contain trie_compaction (which is a hidden field)
  49. if echo "$JSON" | grep -q trie_compaction; then
  50. echo "swagger.json contains hidden fields"
  51. exit 1
  52. fi
  53. }
  54. check_schema_json() {
  55. local name="$1"
  56. local expected_title="$2"
  57. local url="$BASE_URL/api/v5/schemas/$name"
  58. local json
  59. json="$(curl -s "$url" | jq .)"
  60. title="$(echo "$json" | jq -r '.info.title')"
  61. if [[ "$title" != "$expected_title" ]]; then
  62. echo "unexpected value from GET $url"
  63. echo "expected: $expected_title"
  64. echo "got : $title"
  65. exit 1
  66. fi
  67. }
  68. main() {
  69. wait_for_emqx
  70. local JSON_STATUS
  71. JSON_STATUS="$(json_status)"
  72. check_api_docs
  73. ## The json status feature was added after hotconf and bridges schema API
  74. if [ "$JSON_STATUS" != 'NOT_JSON' ]; then
  75. check_swagger_json
  76. check_schema_json hotconf "EMQX Hot Conf API Schema"
  77. check_schema_json bridges "EMQX Data Bridge API Schema"
  78. fi
  79. }
  80. main