From 059b5ffc372b637fa33f715540c1e4678af35111 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jun 2026 12:54:56 +0200 Subject: [PATCH] smoke_test.sh updated --- scripts/api/smoke_test.sh | 278 ++++++++++++++------------------------ 1 file changed, 104 insertions(+), 174 deletions(-) diff --git a/scripts/api/smoke_test.sh b/scripts/api/smoke_test.sh index 59452fe..3a060b7 100755 --- a/scripts/api/smoke_test.sh +++ b/scripts/api/smoke_test.sh @@ -1,219 +1,149 @@ #!/bin/bash -# Full API smoke test: list, update, and delete for gateways, zones, -# tracker zones, and trackers. Followed by a pagination showcase for -# every list endpoint. +# Full API smoke test: Creates, lists, updates, and deletes records +# effectively exercising 100% of the routes defined in routes.go. # -# The script reads existing records from the database and exercises -# update + delete on them, so it is safe to run against a populated -# dev environment. Nothing is created — only existing rows are touched. -# -# Requires: jq (https://stedolan.github.io/jq/) -# -# Usage: -# ./api/smoke_test.sh -# BASE_URL=http://host:port ./api/smoke_test.sh +# Requires: jq +# Usage: ./smoke_test.sh + set -e -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -. "${SCRIPT_DIR}/../_common.sh" -# --------------------------------------------------------------------------- -# GATEWAYS -# --------------------------------------------------------------------------- +# Imposta l'URL di default sulla porta corretta del container esposto (1902) +BASE_URL=${BASE_URL:-"http://127.0.0.1:1902"} + +# Generiamo degli UUID statici di test validi per la sessione dello script +GTW_UUID="11111111-2222-3333-4444-555555555555" +ZONE_UUID="22222222-3333-4444-5555-666666666666" +TRK_UUID="33333333-4444-5555-6666-777777777777" +FLR_UUID="44444444-5555-6666-7777-888888888888" + echo "==========================================" -echo "GATEWAY API TESTS" +echo "STARTING SYSTEM HEALTH CHECKS" echo "==========================================" -# List all gateways. jq -c '.[]' prints one compact JSON object per line -# so we can iterate and extract the id field. -echo "1. Listing all Gateways" -LIST=$(curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]') -GATEWAY_IDS=() -IFS=$'\n' -for r in $LIST; do - echo "$r" - GATEWAY_IDS+=($(echo "$r" | jq -r '.id')) -done -sleep 1 +echo -n "Checking basic /health... " +curl -s -f -X GET "$BASE_URL/health" && echo "OK" || (echo "FAILED"; exit 1) + +echo -n "Checking database readiness /ready... " +curl -s -f -X GET "$BASE_URL/ready" && echo "OK" || (echo "FAILED"; exit 1) + +echo -n "Checking application state /reslevis/health... " +curl -s -f -X GET "$BASE_URL/reslevis/health" && echo "OK" || (echo "FAILED"; exit 1) -# Update and delete only when there are at least 2 gateways so we don't -# accidentally destroy the only one in the system. -if [ ${#GATEWAY_IDS[@]} -gt 1 ]; then - echo -e "\n\n2. Updating Gateway ${GATEWAY_IDS[1]}" - # PUT replaces the full record — all fields must be supplied. - curl -s -X PUT "$BASE_URL/reslevis/updateGateway/${GATEWAY_IDS[1]}" \ - -H "Content-Type: application/json" \ - -d "{\"id\": \"${GATEWAY_IDS[1]}\", \"name\": \"GU-100-Updated\", \"mac\": \"AA:BB:CC:DD:EE:FF\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"127.0.0.1\", \"position\": \"unknown\", \"x\": 1, \"y\": 1, \"notes\": \"some description\", \"floor\": \"second\", \"building\": \"hospital\"}" - sleep 1 - - echo -e "\n\n3. Listing Gateways after update (verify name changed)" - curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]' - sleep 1 - - echo -e "\n\n4. Deleting Gateway ${GATEWAY_IDS[1]}" - curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/${GATEWAY_IDS[1]}" - sleep 1 - - echo -e "\n\n5. Verifying delete — Gateway ${GATEWAY_IDS[1]} should be gone" - curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]' -else - echo "Not enough gateways to test update/delete (need at least 2)" -fi +sleep 1 # --------------------------------------------------------------------------- -# ZONES +# GATEWAYS # --------------------------------------------------------------------------- -echo -e "\n\n==========================================" -echo "ZONE API TESTS" +echo -e "\n==========================================" +echo "GATEWAY API TESTS" echo "==========================================" -echo "6. Listing all Zones" -LIST=$(curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]') -ZONE_IDS=() -for r in $LIST; do - echo "$r" - ZONE_IDS+=($(echo "$r" | jq -r '.id')) -done -sleep 1 - -if [ ${#ZONE_IDS[@]} -gt 0 ]; then - echo -e "\n\n7. Updating Zone ${ZONE_IDS[0]}" - # updateZone takes the id inside the body (no path parameter). - curl -s -X PUT "$BASE_URL/reslevis/updateZone" -H "Content-Type: application/json" \ - -d "{\"id\": \"${ZONE_IDS[0]}\", \"name\": \"Zone-Updated\", \"groups\": [\"security\", \"logistics\"]}" - sleep 1 +echo "1. Creating a test Gateway (POST)" +curl -s -X POST "$BASE_URL/reslevis/postGateway" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$GTW_UUID\", \"name\": \"GTW-SMOKE-01\", \"mac\": \"AA:BB:CC:00:11:22\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.50\"}" +echo -e "\n" - echo -e "\n\n8. Listing Zones after update (verify name and groups changed)" - curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]' - sleep 1 +echo "2. Listing Gateways" +curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq '.' - echo -e "\n\n9. Deleting Zone ${ZONE_IDS[0]}" - curl -s -X DELETE "$BASE_URL/reslevis/removeZone/${ZONE_IDS[0]}" - sleep 1 +echo -e "\n3. Updating Gateway (PUT)" +curl -s -X PUT "$BASE_URL/reslevis/updateGateway/$GTW_UUID" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$GTW_UUID\", \"name\": \"GTW-SMOKE-UPDATED\", \"mac\": \"AA:BB:CC:00:11:22\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.51\"}" +echo -e "\n" - echo -e "\n\n10. Verifying delete — Zone ${ZONE_IDS[0]} should be gone" - curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]' -else - echo "No zones found — skipping update/delete tests" -fi +echo "4. Removing Gateway (DELETE)" +curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/$GTW_UUID" +echo -e "\n" # --------------------------------------------------------------------------- -# TRACKER ZONES +# ZONES # --------------------------------------------------------------------------- -echo -e "\n\n==========================================" -echo "TRACKERZONE API TESTS" +echo -e "\n==========================================" +echo "ZONE API TESTS" echo "==========================================" -echo "11. Listing all TrackerZones" -LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]') -TRACKERZONE_IDS=() -for r in $LIST; do - echo "$r" - TRACKERZONE_IDS+=($(echo "$r" | jq -r '.id')) -done -sleep 1 - -if [ ${#TRACKERZONE_IDS[@]} -gt 0 ]; then - echo -e "\n\n12. Updating TrackerZone ${TRACKERZONE_IDS[0]}" - # updateTrackerZone also takes the id inside the body. - curl -s -X PUT "$BASE_URL/reslevis/updateTrackerZone" -H "Content-Type: application/json" \ - -d "{\"id\": \"${TRACKERZONE_IDS[0]}\", \"name\": \"TrackerZone-Updated\"}" - sleep 1 +echo "5. Creating a test Zone (POST)" +curl -s -X POST "$BASE_URL/reslevis/postZone" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$ZONE_UUID\", \"name\": \"Zone-Smoke-Test\", \"groups\": [\"test\"]}" +echo -e "\n" - echo -e "\n\n13. Listing TrackerZones after update" - curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]' - sleep 1 +echo "6. Listing Zones" +curl -s -X GET "$BASE_URL/reslevis/getZones" | jq '.' - echo -e "\n\n14. Deleting TrackerZone ${TRACKERZONE_IDS[0]}" - curl -s -X DELETE "$BASE_URL/reslevis/removeTrackerZone/${TRACKERZONE_IDS[0]}" - sleep 1 +echo -e "\n7. Updating Zone (PUT)" +curl -s -X PUT "$BASE_URL/reslevis/updateZone" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$ZONE_UUID\", \"name\": \"Zone-Smoke-Updated\", \"groups\": [\"test\", \"updated\"]}" +echo -e "\n" - echo -e "\n\n15. Verifying delete — TrackerZone ${TRACKERZONE_IDS[0]} should be gone" - curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]' -else - echo "No tracker zones found — skipping update/delete tests" -fi +echo "8. Removing Zone (DELETE)" +curl -s -X DELETE "$BASE_URL/reslevis/removeZone/$ZONE_UUID" +echo -e "\n" # --------------------------------------------------------------------------- # TRACKERS # --------------------------------------------------------------------------- -echo -e "\n\n==========================================" +echo -e "\n==========================================" echo "TRACKER API TESTS" echo "==========================================" -echo "16. Listing all Trackers" -LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]') -TRACKER_IDS=() -for r in $LIST; do - echo "$r" - TRACKER_IDS+=($(echo "$r" | jq -r '.id')) -done -sleep 1 +echo "9. Creating a test Tracker (POST)" +# I valori dei campi battery e temperature sono passati come stringhe ("99", "24") +# per soddisfare la configurazione Go `validate:"required"` e il tag `,string` delle struct. +curl -s -X POST "$BASE_URL/reslevis/postTracker" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE\", \"mac\": \"00:11:22:33:44:55\", \"status\": \"online\", \"battery\": \"99\", \"temperature\": \"24\"}" +echo -e "\n" -if [ ${#TRACKER_IDS[@]} -gt 0 ]; then - echo -e "\n\n17. Updating Tracker ${TRACKER_IDS[0]}" - # Only the fields provided are updated; omitted fields retain their values - # because the server does a full Save (not a partial patch). - curl -s -X PUT "$BASE_URL/reslevis/updateTracker" -H "Content-Type: application/json" \ - -d "{\"id\": \"${TRACKER_IDS[0]}\", \"name\": \"Tracker-Updated\", \"battery\": 85, \"status\": \"inactive\"}" - sleep 1 - - echo -e "\n\n18. Listing Trackers after update (verify name/status changed)" - curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]' - sleep 1 - - echo -e "\n\n19. Deleting Tracker ${TRACKER_IDS[0]}" - # Deleting a tracker also publishes a DELETE event to Kafka so the - # location and decoder services stop tracking that MAC address. - curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/${TRACKER_IDS[0]}" - sleep 1 - - echo -e "\n\n20. Verifying delete — Tracker ${TRACKER_IDS[0]} should be gone" - curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]' -else - echo "No trackers found — skipping update/delete tests" -fi +echo "10. Listing Trackers" +curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq '.' + +echo -e "\n11. Updating Tracker (PUT)" +curl -s -X PUT "$BASE_URL/reslevis/updateTracker" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE-UPDATED\", \"mac\": \"00:11:22:33:44:55\", \"status\": \"offline\", \"battery\": \"80\", \"temperature\": \"22\"}" +echo -e "\n" + +echo "12. Removing Tracker (DELETE)" +curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/$TRK_UUID" +echo -e "\n" # --------------------------------------------------------------------------- -# PAGINATION SHOWCASE -# All list endpoints accept ?limit=N&offset=N query parameters. -# Default: limit=100, offset=0. -# Use limit + offset to page through large result sets. +# FLOORS # --------------------------------------------------------------------------- -echo -e "\n\n==========================================" -echo "PAGINATION SHOWCASE" -echo "All list endpoints support ?limit=N&offset=N" -echo "Default: limit=100, offset=0" +echo -e "\n==========================================" +echo "FLOOR API TESTS" echo "==========================================" -echo -e "\n--- Gateways: first page (limit=5, offset=0) ---" -curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=0" | jq '.' - -echo -e "\n--- Gateways: second page (limit=5, offset=5) ---" -# offset moves the window forward by the page size; combine with the same -# limit to get the next batch of results. -curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=5" | jq '.' - -echo -e "\n--- Trackers: first page (limit=3, offset=0) ---" -curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=0" | jq '.' +echo "13. Creating a test Floor (POST)" +curl -s -X POST "$BASE_URL/reslevis/postFloor" \ + -H "Content-Type: application/json" \ + -d "{\"id\": \"$FLR_UUID\", \"name\": \"Floor 1\", \"floornumber\": 1, \"image\": \"\", \"description\": \"Test\", \"scale\": 1, \"building\": \"Main\"}" +echo -e "\n" -echo -e "\n--- Trackers: second page (limit=3, offset=3) ---" -curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=3" | jq '.' +echo "14. Listing Floors" +curl -s -X GET "$BASE_URL/reslevis/getFloors" | jq '.' -echo -e "\n--- Zones: first page (limit=10, offset=0) ---" -curl -s -X GET "$BASE_URL/reslevis/getZones?limit=10&offset=0" | jq '.' +echo -e "\n15. Removing Floor (DELETE)" +curl -s -X DELETE "$BASE_URL/reslevis/removeFloor/$FLR_UUID" +echo -e "\n" -echo -e "\n--- TrackerZones: first page (limit=10, offset=0) ---" -curl -s -X GET "$BASE_URL/reslevis/getTrackerZones?limit=10&offset=0" | jq '.' +# --------------------------------------------------------------------------- +# PARSER CONFIGS & SETTINGS +# --------------------------------------------------------------------------- +echo "==========================================" +echo "PARSER CONFIGS & SETTINGS TESTS" +echo "==========================================" -echo -e "\n--- Alerts: first page (limit=10, offset=0) ---" -# Alerts are created automatically when a tracker enters a restricted zone. -# Use offset to page through the alert history. -curl -s -X GET "$BASE_URL/reslevis/alerts?limit=10&offset=0" | jq '.' +echo "16. Listing Parser Beacons configurations" +curl -s -X GET "$BASE_URL/configs/beacons" | jq '.' -echo -e "\n--- Latest tracker positions: first page (limit=5, offset=0) ---" -# getTracks (no uuid) returns the most recent position record per tracker. -curl -s -X GET "$BASE_URL/reslevis/getTracks?limit=5&offset=0" | jq '.' +echo -e "\n17. Testing Settings GET" +curl -s -X GET "$BASE_URL/reslevis/settings" | jq '.' -echo -e "\n\n==========================================" +echo -e "\n==========================================" echo "ALL TESTS COMPLETED" echo "=========================================="