您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

229 行
8.0 KiB

  1. #!/bin/bash
  2. # Full API smoke test: Verifies DB status, displays active tables,
  3. # and exercises 100% of the local routes.
  4. #
  5. # Requires: jq
  6. # Usage: ./smoke_test.sh
  7. set -e
  8. # Configurazione percorsi e URL
  9. BASE_URL=${BASE_URL:-"http://127.0.0.1:1902"}
  10. ENV_FILE="/data/conf/presence/res_levis_backend/build/env/db.env"
  11. # Estrazione dinamica della password per i test del database
  12. if [ -f "$ENV_FILE" ]; then
  13. DB_PASSWORD=$(grep -E "^POSTGRES_PASSWORD=" "$ENV_FILE" | cut -d'=' -f2)
  14. else
  15. echo "Errore: File d'ambiente non trovato in $ENV_FILE"
  16. exit 1
  17. fi
  18. # Generiamo degli UUID statici di test validi per la sessione dello script
  19. GTW_UUID="11111111-2222-3333-4444-555555555555"
  20. ZONE_UUID="22222222-3333-4444-5555-666666666666"
  21. TRK_UUID="33333333-4444-5555-6666-777777777777"
  22. FLR_UUID="44444444-5555-6666-7777-888888888888"
  23. echo "=========================================="
  24. echo "STARTING SYSTEM HEALTH CHECKS"
  25. echo "=========================================="
  26. echo -n "Checking basic /health... "
  27. curl -s -f -X GET "$BASE_URL/health" && echo "OK" || (echo "FAILED"; exit 1)
  28. echo -n "Checking database readiness /ready... "
  29. curl -s -f -X GET "$BASE_URL/ready" && echo "OK" || (echo "FAILED"; exit 1)
  30. echo -n "Checking application state /reslevis/health... "
  31. curl -s -f -X GET "$BASE_URL/reslevis/health" && echo "OK" || (echo "FAILED"; exit 1)
  32. sleep 1
  33. # ---------------------------------------------------------------------------
  34. # VERIFICA PERSISTENZA E STAMPA CONTENUTO DB POSTGRES
  35. # ---------------------------------------------------------------------------
  36. echo -e "\n=========================================="
  37. echo "DATABASE REPLICATION & CONTENT CHECKS"
  38. echo "=========================================="
  39. # Rimosso il flag -t per evitare i blocchi di cattura dell'output in Bash
  40. check_table_count() {
  41. local table=$1
  42. local count=$(docker exec -i -e PGPASSWORD="$DB_PASSWORD" db psql -U postgres -d postgres -t -c "SELECT COUNT(*) FROM public.$table;" | tr -d '[:space:]')
  43. echo "$count"
  44. }
  45. print_table_content() {
  46. local table=$1
  47. local columns=$2
  48. echo -e "\n--- [ CONTENUTO TABELLA: $table ] ---"
  49. docker exec -i -e PGPASSWORD="$DB_PASSWORD" db psql -U postgres -d postgres -c "SELECT $columns FROM public.$table;"
  50. }
  51. # 1. GATEWAYS
  52. echo -n "Checking table 'gateways' populations... "
  53. GTW_COUNT=$(check_table_count "gateways")
  54. if [ "$GTW_COUNT" -gt 0 ]; then
  55. echo "OK ($GTW_COUNT gateways found)"
  56. print_table_content "gateways" "name, mac, ip, x, y"
  57. else
  58. echo "FAILED (0 records in table 'gateways')"
  59. exit 1
  60. fi
  61. echo -e "\n"
  62. # 2. TRACKERS
  63. echo -n "Checking table 'trackers' populations... "
  64. TRK_COUNT=$(check_table_count "trackers")
  65. if [ "$TRK_COUNT" -gt 0 ]; then
  66. echo "OK ($TRK_COUNT trackers found)"
  67. print_table_content "trackers" "name, mac, battery, temperature, status"
  68. else
  69. echo "FAILED (0 records in table 'trackers' - Sincronizzazione API o parsing JSON fallito)"
  70. exit 1
  71. fi
  72. echo -e "\n"
  73. # 3. FLOORS
  74. echo -n "Checking table 'floors' populations... "
  75. FLR_COUNT=$(check_table_count "floors")
  76. if [ "$FLR_COUNT" -gt 0 ]; then
  77. echo "OK ($FLR_COUNT floors found)"
  78. print_table_content "floors" "id, name, floor_number, building"
  79. else
  80. echo "INFO (0 records in table 'floors')"
  81. fi
  82. echo -e "\n"
  83. # 4. ZONES
  84. echo -n "Checking table 'zones' populations... "
  85. ZN_COUNT=$(check_table_count "zones")
  86. if [ "$ZN_COUNT" -gt 0 ]; then
  87. echo "OK ($ZN_COUNT zones found)"
  88. print_table_content "zones" "id, name, groups, floor"
  89. else
  90. echo "INFO (0 records in table 'zones')"
  91. fi
  92. echo -e "\n"
  93. sleep 1
  94. # ---------------------------------------------------------------------------
  95. # GATEWAYS (LOCAL CRUD)
  96. # ---------------------------------------------------------------------------
  97. echo "=========================================="
  98. echo "GATEWAY API TESTS"
  99. echo "=========================================="
  100. echo "1. Creating a test Gateway (POST)"
  101. curl -s -X POST "$BASE_URL/reslevis/postGateway" \
  102. -H "Content-Type: application/json" \
  103. -d "{\"id\": \"$GTW_UUID\", \"name\": \"GTW-SMOKE-01\", \"mac\": \"AABBCC001122\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.50\"}"
  104. echo -e "\n"
  105. echo "2. Listing Gateways"
  106. curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq '.'
  107. echo -e "\n3. Updating Gateway (PUT)"
  108. curl -s -X PUT "$BASE_URL/reslevis/updateGateway/$GTW_UUID" \
  109. -H "Content-Type: application/json" \
  110. -d "{\"id\": \"$GTW_UUID\", \"name\": \"GTW-SMOKE-UPDATED\", \"mac\": \"AABBCC001122\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.51\"}"
  111. echo -e "\n"
  112. echo "4. Removing Gateway (DELETE)"
  113. curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/$GTW_UUID"
  114. echo -e "\n"
  115. # ---------------------------------------------------------------------------
  116. # ZONES (LOCAL CRUD)
  117. # ---------------------------------------------------------------------------
  118. echo -e "\n=========================================="
  119. echo "ZONE API TESTS"
  120. echo "=========================================="
  121. echo "5. Creating a test Zone (POST)"
  122. curl -s -X POST "$BASE_URL/reslevis/postZone" \
  123. -H "Content-Type: application/json" \
  124. -d "{\"id\": \"$ZONE_UUID\", \"name\": \"Zone-Smoke-Test\", \"groups\": [\"test\"]}"
  125. echo -e "\n"
  126. echo "6. Listing Zones"
  127. curl -s -X GET "$BASE_URL/reslevis/getZones" | jq '.'
  128. echo -e "\n7. Updating Zone (PUT)"
  129. curl -s -X PUT "$BASE_URL/reslevis/updateZone" \
  130. -H "Content-Type: application/json" \
  131. -d "{\"id\": \"$ZONE_UUID\", \"name\": \"Zone-Smoke-Updated\", \"groups\": [\"test\", \"updated\"]}"
  132. echo -e "\n"
  133. echo "8. Removing Zone (DELETE)"
  134. curl -s -X DELETE "$BASE_URL/reslevis/removeZone/$ZONE_UUID"
  135. echo -e "\n"
  136. # ---------------------------------------------------------------------------
  137. # TRACKERS (LOCAL CRUD)
  138. # ---------------------------------------------------------------------------
  139. echo -e "\n=========================================="
  140. echo "TRACKER API TESTS"
  141. echo "=========================================="
  142. echo "9. Creating a test Tracker (POST)"
  143. curl -s -X POST "$BASE_URL/reslevis/postTracker" \
  144. -H "Content-Type: application/json" \
  145. -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE\", \"mac\": \"001122334455\", \"status\": \"online\", \"battery\": \"99\", \"temperature\": \"24\"}"
  146. echo -e "\n"
  147. echo "10. Listing Trackers"
  148. curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq '.'
  149. echo -e "\n11. Updating Tracker (PUT)"
  150. curl -s -X PUT "$BASE_URL/reslevis/updateTracker" \
  151. -H "Content-Type: application/json" \
  152. -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE-UPDATED\", \"mac\": \"001122334455\", \"status\": \"offline\", \"battery\": \"80\", \"temperature\": \"22\"}"
  153. echo -e "\n"
  154. echo "12. Removing Tracker (DELETE)"
  155. curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/$TRK_UUID"
  156. echo -e "\n"
  157. # ---------------------------------------------------------------------------
  158. # FLOORS (LOCAL CRUD)
  159. # ---------------------------------------------------------------------------
  160. echo -e "\n=========================================="
  161. echo "FLOOR API TESTS"
  162. echo "=========================================="
  163. echo "13. Creating a test Floor (POST)"
  164. curl -s -X POST "$BASE_URL/reslevis/postFloor" \
  165. -H "Content-Type: application/json" \
  166. -d "{\"id\": \"$FLR_UUID\", \"name\": \"Floor 1\", \"floornumber\": 1, \"image\": \"\", \"description\": \"Test\", \"scale\": 1, \"building\": \"Main\"}"
  167. echo -e "\n"
  168. echo "14. Listing Floors"
  169. curl -s -X GET "$BASE_URL/reslevis/getFloors" | jq '.'
  170. echo -e "\n15. Removing Floor (DELETE)"
  171. curl -s -X DELETE "$BASE_URL/reslevis/removeFloor/$FLR_UUID"
  172. echo -e "\n"
  173. # ---------------------------------------------------------------------------
  174. # PARSER CONFIGS & SETTINGS
  175. # ---------------------------------------------------------------------------
  176. echo "=========================================="
  177. echo "PARSER CONFIGS & SETTINGS TESTS"
  178. echo "=========================================="
  179. echo "16. Listing Parser Beacons configurations"
  180. curl -s -X GET "$BASE_URL/configs/beacons" | jq '.'
  181. echo -e "\n17. Testing Settings GET"
  182. curl -s -X GET "$BASE_URL/reslevis/settings" | jq '.'
  183. echo -e "\n=========================================="
  184. echo "ALL TESTS COMPLETED"
  185. echo "=========================================="