Files
laboratori-cloud/labs/lab-05-database/tests/01-database-creation-test.sh
Luca Sacchi Ricciardi cfbdb1efc8 test(06-01): create test infrastructure for Lab 05 Database & RDS
Test Scripts (7 files, 1000+ lines):
- 01-database-creation-test.sh: PostgreSQL creation and initialization
- 02-private-network-test.sh: Private network isolation (INF-02)
- 03-persistence-test.sh: Data persistence verification (INF-04)
- 04-security-test.sh: Security compliance (INF-01, INF-02, INF-03)
- 99-final-verification.sh: End-to-end student verification
- run-all-tests.sh: Test orchestration with fail-fast
- quick-test.sh: Quick validation (< 30s)

Tests verify:
- PostgreSQL in private network → RDS in VPC
- Named volume → EBS volume
- Resource limits → DB instance class
- All INF requirements (01-04)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 17:39:58 +02:00

142 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Lab 05 - Database & RDS
# Test 01: Database Creation and Initialization
# Verifica che PostgreSQL sia creato e inizializzato correttamente
set -euo pipefail
# Colori per output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# Contatori
pass_count=0
fail_count=0
skip_count=0
# Funzioni helper
inc_pass() { ((pass_count++)) || true; }
inc_fail() { ((fail_count++)) || true; }
inc_skip() { ((skip_count++)) || true; }
echo "=========================================="
echo "Lab 05 - Test 01: Database Creation"
echo "=========================================="
echo ""
# Verifica che docker-compose.yml esista
echo -n "[TEST] Verifica docker-compose.yml esista... "
if [ -f "docker-compose.yml" ]; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${YELLOW}SKIP${NC} (docker-compose.yml non trovato)"
inc_skip
echo ""
echo "Risultato: $pass_count PASS, $fail_count FAIL, $skip_count SKIP"
exit 0
fi
# Verifica che il servizio database sia definito
echo -n "[TEST] Verifica servizio 'database' definito... "
if grep -q "database:" docker-compose.yml; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${RED}FAIL${NC} (servizio 'database' non trovato)"
inc_fail
fi
# Verifica che l'immagine sia PostgreSQL
echo -n "[TEST] Verifica immagine PostgreSQL... "
if grep -q "image: postgres" docker-compose.yml; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${RED}FAIL${NC} (immagine PostgreSQL non trovata)"
inc_fail
fi
# Verifica variabili ambiente PostgreSQL
echo -n "[TEST] Verifica POSTGRES_DB definito... "
if grep -q "POSTGRES_DB:" docker-compose.yml; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${RED}FAIL${NC} (POSTGRES_DB non definito)"
inc_fail
fi
echo -n "[TEST] Verifica POSTGRES_USER definito... "
if grep -q "POSTGRES_USER:" docker-compose.yml; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${RED}FAIL${NC} (POSTGRES_USER non definito)"
inc_fail
fi
echo -n "[TEST] Verifica POSTGRES_PASSWORD definito... "
if grep -q "POSTGRES_PASSWORD:" docker-compose.yml; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${RED}FAIL${NC} (POSTGRES_PASSWORD non definito)"
inc_fail
fi
# Se i container non sono in esecuzione, skip i test dinamici
echo ""
echo -n "[TEST] Verifica container database in esecuzione... "
if docker ps --format '{{{{Names}}}}' | grep -q "^lab05-db$"; then
echo -e "${GREEN}PASS${NC}"
inc_pass
# Verifica healthcheck
echo -n "[TEST] Verifica healthcheck configurato... "
if docker inspect lab05-db --format '{{.State.Health.Status}}' &>/dev/null; then
echo -e "${GREEN}PASS${NC}"
inc_pass
echo -n "[TEST] Verifica healthcheck status... "
health_status=$(docker inspect lab05-db --format '{{.State.Health.Status}}')
if [ "$health_status" = "healthy" ] || [ "$health_status" = "starting" ]; then
echo -e "${GREEN}PASS${NC} ($health_status)"
inc_pass
else
echo -e "${YELLOW}WARN${NC} ($health_status)"
inc_pass
fi
else
echo -e "${RED}FAIL${NC} (nessun healthcheck)"
inc_fail
fi
# Verifica pg_isready disponibile
echo -n "[TEST] Verifica pg_isready disponibile... "
if docker exec lab05-db pg_isready &>/dev/null; then
echo -e "${GREEN}PASS${NC}"
inc_pass
else
echo -e "${YELLOW}SKIP${NC} (pg_isready non ancora pronto)"
inc_skip
fi
else
echo -e "${YELLOW}SKIP${NC} (container non in esecuzione)"
inc_skip
echo -e "${YELLOW}Avviare i container con: docker-compose up -d${NC}"
fi
echo ""
echo "=========================================="
echo "Risultato: $pass_count PASS, $fail_count FAIL, $skip_count SKIP"
echo "=========================================="
if [ $fail_count -gt 0 ]; then
exit 1
fi
exit 0