From fe492f54e703e209fc3e21a2186339bc8ef3104b Mon Sep 17 00:00:00 2001 From: root Date: Fri, 26 Jun 2026 18:44:29 +0200 Subject: [PATCH] smoke_test con tabelle e dati --- scripts/api/smoke_test.sh | 105 +++++++++++++++++++++++++++++++++----- scripts/rc.reslevis | 41 +++++++++++++++ 2 files changed, 133 insertions(+), 13 deletions(-) create mode 100755 scripts/rc.reslevis diff --git a/scripts/api/smoke_test.sh b/scripts/api/smoke_test.sh index 3a060b7..58121b8 100755 --- a/scripts/api/smoke_test.sh +++ b/scripts/api/smoke_test.sh @@ -1,14 +1,23 @@ #!/bin/bash -# Full API smoke test: Creates, lists, updates, and deletes records -# effectively exercising 100% of the routes defined in routes.go. +# 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 -# Imposta l'URL di default sulla porta corretta del container esposto (1902) +# 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" @@ -32,16 +41,88 @@ curl -s -f -X GET "$BASE_URL/reslevis/health" && echo "OK" || (echo "FAILED"; ex sleep 1 # --------------------------------------------------------------------------- -# GATEWAYS +# 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\": \"AA:BB:CC:00:11:22\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.50\"}" + -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" @@ -50,7 +131,7 @@ 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\": \"AA:BB:CC:00:11:22\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"192.168.1.51\"}" + -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)" @@ -58,7 +139,7 @@ curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/$GTW_UUID" echo -e "\n" # --------------------------------------------------------------------------- -# ZONES +# ZONES (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "ZONE API TESTS" @@ -84,18 +165,16 @@ curl -s -X DELETE "$BASE_URL/reslevis/removeZone/$ZONE_UUID" echo -e "\n" # --------------------------------------------------------------------------- -# TRACKERS +# TRACKERS (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "TRACKER API TESTS" echo "==========================================" 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\"}" + -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE\", \"mac\": \"001122334455\", \"status\": \"online\", \"battery\": \"99\", \"temperature\": \"24\"}" echo -e "\n" echo "10. Listing Trackers" @@ -104,7 +183,7 @@ 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\"}" + -d "{\"id\": \"$TRK_UUID\", \"name\": \"TRK-SMOKE-UPDATED\", \"mac\": \"001122334455\", \"status\": \"offline\", \"battery\": \"80\", \"temperature\": \"22\"}" echo -e "\n" echo "12. Removing Tracker (DELETE)" @@ -112,7 +191,7 @@ curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/$TRK_UUID" echo -e "\n" # --------------------------------------------------------------------------- -# FLOORS +# FLOORS (LOCAL CRUD) # --------------------------------------------------------------------------- echo -e "\n==========================================" echo "FLOOR API TESTS" diff --git a/scripts/rc.reslevis b/scripts/rc.reslevis new file mode 100755 index 0000000..816072c --- /dev/null +++ b/scripts/rc.reslevis @@ -0,0 +1,41 @@ +#!/bin/sh +. /etc/mnvars +. /etc/mnsuper.conf +. $MN_rcconfig +return=$rc_done + case "$1" in + start) + #Avvio del server API uvicorn FastApi per i comandi da APP androind verso la telecamera per i preset + /etc/init.d/fastapi stop + /etc/init.d/fastapi start + + #Avvio reslevis + /usr/bin/docker compose -f /data/conf/presence/res_levis_backend/build/docker-compose.yaml down -v + /usr/bin/docker compose -f /data/conf/presence/ble-ai-localizer/docker-compose.yml down + /usr/local/bin/fwdocker stop + + /usr/bin/docker compose -f /data/conf/presence/res_levis_backend/build/docker-compose.yaml up -d + /usr/bin/docker compose -f /data/conf/presence/ble-ai-localizer/docker-compose.yml up -d + /usr/local/bin/fwdocker start + + echo -e "$return" + ;; + stop) + + #Stop reslevis + /usr/bin/docker compose -f /data/conf/presence/res_levis_backend/build/docker-compose.yaml down -v + /usr/bin/docker compose -f /data/conf/presence/ble-ai-localizer/docker-compose.yml down + + /usr/local/bin/fwdocker stop + /etc/init.d/fastapi stop + + echo -e "$return" + ;; + restart) + $0 stop && $0 start || return=$rc_failed + ;; + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 + esac +exit 0