@@ -26,38 +26,121 @@ echo -e "${CYAN} Module: ${MODULE}${NC}"
2626echo -e " ${CYAN} ======================================${NC} "
2727echo " "
2828
29+ # ── Cleanup control ───────────────────────────────────────────────────────────
30+ CLEANUP=true
31+ [[ " ${1:- } " == " --no-cleanup" ]] && CLEANUP=false
32+
33+ cleanup () {
34+ if [[ " ${CLEANUP} " == " true" ]]; then
35+ info " Phase 4: Cleanup"
36+ docker compose -f " ${COMPOSE_FILE} " down -v --remove-orphans 2> /dev/null || true
37+ info " Cleanup complete"
38+ else
39+ info " Skipping cleanup (--no-cleanup)"
40+ fi
41+ }
42+ trap cleanup EXIT
43+
2944# ── PHASE 1: Setup ────────────────────────────────────────────────────────────
3045info " Phase 1: Setup"
3146docker compose -f " ${COMPOSE_FILE} " up -d
32- info " Waiting 30s for ${MODULE} to initialize..."
33- sleep 30
3447
3548# ── PHASE 2: Health Checks ────────────────────────────────────────────────────
3649info " Phase 2: Health Checks"
3750
38- if docker compose -f " ${COMPOSE_FILE} " ps | grep -q " running\|Up" ; then
39- pass " Container is running"
51+ info " Waiting for external MariaDB (snipeit-l02-db, up to 90s)..."
52+ for i in $( seq 1 18) ; do
53+ if docker exec snipeit-l02-db mysqladmin ping -uroot -pRootLab02! --silent 2> /dev/null; then
54+ pass " External MariaDB healthy"
55+ break
56+ fi
57+ [[ $i -eq 18 ]] && fail " External MariaDB timed out after 90s"
58+ sleep 5
59+ done
60+
61+ info " Waiting for Mailhog (snipeit-l02-mail, up to 60s)..."
62+ for i in $( seq 1 12) ; do
63+ if curl -sf http://localhost:8711/api/v2/messages > /dev/null 2>&1 ; then
64+ pass " Mailhog API reachable"
65+ break
66+ fi
67+ [[ $i -eq 12 ]] && fail " Mailhog timed out after 60s"
68+ sleep 5
69+ done
70+
71+ info " Waiting for Snipe-IT web (snipeit-l02-app, up to 300s)..."
72+ for i in $( seq 1 30) ; do
73+ http_code=$( curl -o /dev/null -sw ' %{http_code}' http://localhost:8411/ 2> /dev/null || echo " 000" )
74+ if [[ " ${http_code} " =~ ^[234] ]]; then
75+ pass " Snipe-IT web responding (HTTP ${http_code} )"
76+ break
77+ fi
78+ [[ $i -eq 30 ]] && fail " Snipe-IT web timed out after 300s"
79+ sleep 10
80+ done
81+
82+ # ── PHASE 3: Functional Tests ─────────────────────────────────────────────────
83+ info " Phase 3: Functional Tests (Lab 16-02 — External Dependencies)"
84+
85+ # Container states
86+ for svc in snipeit-l02-db snipeit-l02-mail snipeit-l02-app; do
87+ state=$( docker inspect --format=' {{.State.Status}}' " ${svc} " 2> /dev/null || echo " missing" )
88+ if [[ " ${state} " == " running" ]]; then
89+ pass " Container ${svc} is running"
90+ else
91+ fail " Container ${svc} state: ${state} "
92+ fi
93+ done
94+
95+ # DB connectivity from app container
96+ table_count=$( docker exec snipeit-l02-db \
97+ mysql -usnipeit -pSnipeLab02! snipeit -e ' SHOW TABLES;' 2> /dev/null | wc -l | tr -d ' ' )
98+ if [[ " ${table_count} " -gt 5 ]]; then
99+ pass " Snipe-IT database has ${table_count} tables (migrations ran)"
40100else
41- fail " Container is not running"
101+ warn " Snipe-IT database tables: ${table_count} (migrations may still be running) "
42102fi
43103
44- # ── PHASE 3: Functional Tests ─────────────────────────────────────────────────
45- info " Phase 3: Functional Tests (Lab 02 — External Dependencies)"
46-
47- # TODO: Add module-specific functional tests here
48- # Example:
49- # if curl -sf http://localhost:80/health > /dev/null 2>&1; then
50- # pass "Health endpoint responds"
51- # else
52- # fail "Health endpoint not reachable"
53- # fi
54-
55- warn " Functional tests for Lab 16-02 pending implementation"
56-
57- # ── PHASE 4: Cleanup ──────────────────────────────────────────────────────────
58- info " Phase 4: Cleanup"
59- docker compose -f " ${COMPOSE_FILE} " down -v --remove-orphans
60- info " Cleanup complete"
104+ # Mailhog API format check
105+ mailhog_resp=$( curl -sf http://localhost:8711/api/v2/messages 2> /dev/null || echo " {}" )
106+ if echo " ${mailhog_resp} " | grep -q ' total\|items\|count' ; then
107+ pass " Mailhog API returns valid JSON message list"
108+ else
109+ fail " Mailhog API response unexpected: ${mailhog_resp} "
110+ fi
111+
112+ # HTTP status check
113+ http_code=$( curl -o /dev/null -sw ' %{http_code}' -L http://localhost:8411/ 2> /dev/null || echo " 000" )
114+ if [[ " ${http_code} " =~ ^[234] ]]; then
115+ pass " Snipe-IT HTTP GET / -> ${http_code} "
116+ else
117+ fail " Snipe-IT HTTP GET / -> ${http_code} "
118+ fi
119+
120+ # Login page present
121+ if curl -sf -L http://localhost:8411/ 2> /dev/null | grep -qi ' snipe\|login\|email\|password' ; then
122+ pass " Snipe-IT login page rendered"
123+ else
124+ warn " Snipe-IT login page check inconclusive"
125+ fi
126+
127+ # Key env vars present in app container
128+ for var in DB_HOST DB_DATABASE APP_KEY APP_URL MAIL_HOST; do
129+ if docker exec snipeit-l02-app printenv " ${var} " 2> /dev/null | grep -q ' .' ; then
130+ pass " Env var ${var} set in snipeit-l02-app"
131+ else
132+ fail " Env var ${var} missing in snipeit-l02-app"
133+ fi
134+ done
135+
136+ # Volume existence
137+ for vol in snipeit-l02-db-data snipeit-l02-data; do
138+ if docker volume ls --format ' {{.Name}}' | grep -q " ${vol} " ; then
139+ pass " Volume ${vol} exists"
140+ else
141+ fail " Volume ${vol} missing"
142+ fi
143+ done
61144
62145# ── Results ───────────────────────────────────────────────────────────────────
63146echo " "
0 commit comments