#!/bin/bash # Full API smoke test: Verifies DB status, displays active tables, # and exercises 100% of the local routes. # # Requires: jq # Usage: ./smoke_test.sh set -e # Configurazione percorsi e URL BASE_URL=${BASE_URL:-"http://127.0.0.1:1902"} ENV_FILE="/data/conf/presence/res_levis_backend/build/env/db.env" # Estrazione dinamica della password per i test del database if [ -f "$ENV_FILE" ]; then DB_PASSWORD=$(grep -E "^POSTGRES_PASSWORD=" "$ENV_FILE" | cut -d'=' -f2) else echo "Errore: File d'ambiente non trovato in $ENV_FILE" exit 1 fi # 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 "STARTING SYSTEM HEALTH CHECKS" echo "==========================================" 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) sleep 1 # --------------------------------------------------------------------------- # VERIFICA PERSISTENZA E STAMPA CONTENUTO DB POSTGRES # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "DATABASE REPLICATION & CONTENT CHECKS" echo "==========================================" # Rimosso il flag -t per evitare i blocchi di cattura dell'output in Bash check_table_count() { local table=$1 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:]') echo "$count" } print_table_content() { local table=$1 local columns=$2 echo -e "\n--- [ CONTENUTO TABELLA: $table ] ---" docker exec -i -e PGPASSWORD="$DB_PASSWORD" db psql -U postgres -d postgres -c "SELECT $columns FROM public.$table;" } # 1. GATEWAYS echo -n "Checking table 'gateways' populations... " GTW_COUNT=$(check_table_count "gateways") if [ "$GTW_COUNT" -gt 0 ]; then echo "OK ($GTW_COUNT gateways found)" print_table_content "gateways" "name, mac, ip, x, y" else echo "FAILED (0 records in table 'gateways')" exit 1 fi echo -e "\n" # 2. TRACKERS echo -n "Checking table 'trackers' populations... " TRK_COUNT=$(check_table_count "trackers") if [ "$TRK_COUNT" -gt 0 ]; then echo "OK ($TRK_COUNT trackers found)" print_table_content "trackers" "name, mac, battery, temperature, status" else echo "FAILED (0 records in table 'trackers' - Sincronizzazione API o parsing JSON fallito)" exit 1 fi echo -e "\n" # 3. FLOORS echo -n "Checking table 'floors' populations... " FLR_COUNT=$(check_table_count "floors") if [ "$FLR_COUNT" -gt 0 ]; then echo "OK ($FLR_COUNT floors found)" print_table_content "floors" "id, name, floor_number, building" else echo "INFO (0 records in table 'floors')" fi echo -e "\n" # 4. ZONES echo -n "Checking table 'zones' populations... " ZN_COUNT=$(check_table_count "zones") if [ "$ZN_COUNT" -gt 0 ]; then echo "OK ($ZN_COUNT zones found)" print_table_content "zones" "id, name, groups, floor" else echo "INFO (0 records in table 'zones')" fi echo -e "\n" sleep 1 # --------------------------------------------------------------------------- # GATEWAYS (LOCAL CRUD) # --------------------------------------------------------------------------- echo "==========================================" echo "GATEWAY API TESTS" echo "==========================================" 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\": \"AABBCC001122\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.50\"}" echo -e "\n" echo "2. Listing Gateways" curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq '.' 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\": \"AABBCC001122\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.51\"}" echo -e "\n" echo "4. Removing Gateway (DELETE)" curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/$GTW_UUID" echo -e "\n" # --------------------------------------------------------------------------- # ZONES (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "ZONE API TESTS" echo "==========================================" 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 "6. Listing Zones" curl -s -X GET "$BASE_URL/reslevis/getZones" | jq '.' 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 "8. Removing Zone (DELETE)" curl -s -X DELETE "$BASE_URL/reslevis/removeZone/$ZONE_UUID" echo -e "\n" # --------------------------------------------------------------------------- # TRACKERS (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "TRACKER API TESTS" echo "==========================================" echo "9. Creating a test Tracker (POST)" curl -s -X POST "$BASE_URL/reslevis/postTracker" \ -H "Content-Type: application/json" \ -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE\", \"mac\": \"001122334455\", \"status\": \"online\", \"battery\": \"99\", \"temperature\": \"24\"}" echo -e "\n" 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\": \"001122334455\", \"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" # --------------------------------------------------------------------------- # FLOORS (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "FLOOR API TESTS" echo "==========================================" 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 "14. Listing Floors" curl -s -X GET "$BASE_URL/reslevis/getFloors" | jq '.' echo -e "\n15. Removing Floor (DELETE)" curl -s -X DELETE "$BASE_URL/reslevis/removeFloor/$FLR_UUID" echo -e "\n" # --------------------------------------------------------------------------- # PARSER CONFIGS & SETTINGS # --------------------------------------------------------------------------- echo "==========================================" echo "PARSER CONFIGS & SETTINGS TESTS" echo "==========================================" echo "16. Listing Parser Beacons configurations" curl -s -X GET "$BASE_URL/configs/beacons" | jq '.' echo -e "\n17. Testing Settings GET" curl -s -X GET "$BASE_URL/reslevis/settings" | jq '.' echo -e "\n==========================================" echo "ALL TESTS COMPLETED" echo "=========================================="