4NK_node/scripts/test_ui.sh
Your Name 42564cf558
Some checks failed
CI - 4NK_node / Code Quality (push) Failing after 38s
CI - 4NK_node / Unit Tests (push) Failing after 36s
CI - 4NK_node / Integration Tests (push) Successful in 33s
CI - 4NK_node / Security Tests (push) Failing after 33s
CI - 4NK_node / Docker Build & Test (push) Failing after 15s
CI - 4NK_node / Documentation Tests (push) Successful in 12s
CI - 4NK_node / Release Guard (push) Has been skipped
CI - 4NK_node / Performance Tests (push) Successful in 35s
CI - 4NK_node / Notify (push) Failing after 2s
chore(refine): adapter .gitea/docs/scripts au projet 4NK_node
2025-08-27 11:56:17 +02:00

100 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Test de déploiement et vérification de l'UI (ihm_client)
# Usage:
# HOST=localhost PORT=8080 ./scripts/test_ui.sh
# HOST=VM_IP PORT=8080 ./scripts/test_ui.sh
HOST="${HOST:-localhost}"
PORT="${PORT:-8080}"
SERVICE="ihm_client"
LOG_DIR="tests/logs"
TS="$(date +%Y%m%d_%H%M%S)"
LOG_FILE="$LOG_DIR/ui_test_${TS}.log"
mkdir -p "$LOG_DIR"
log() {
printf "%s %s\n" "[$(date +%H:%M:%S)]" "$*" | tee -a "$LOG_FILE"
}
run() {
log "+ $*"
# shellcheck disable=SC2068
"$@" 2>&1 | tee -a "$LOG_FILE"
}
log "Démarrage du test UI (hôte=${HOST}, port=${PORT})"
log "Étape 1/5: build de l'image ${SERVICE}"
run sudo docker compose build --pull ${SERVICE}
log "Étape 2/5: démarrage du service ${SERVICE}"
run sudo docker compose up -d ${SERVICE}
# Attendre l'état healthy si un healthcheck est défini
log "Attente de l'état healthy du conteneur ${SERVICE} (timeout 120s)"
CID=$(sudo docker compose ps -q ${SERVICE} || true)
DEADLINE=$(( $(date +%s) + 120 ))
if [ -n "${CID}" ]; then
while [ $(date +%s) -lt ${DEADLINE} ]; do
STATUS=$(sudo docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}unknown{{end}}' "${CID}" 2>/dev/null || echo unknown)
log "health=${STATUS}"
if [ "${STATUS}" = "healthy" ]; then
break
fi
sleep 2
done
else
log "Avertissement: CID introuvable pour ${SERVICE}"
fi
log "Étape 3/5: vérification HTTP en-têtes sur http://${HOST}:${PORT}"
HTTP_HEADERS=$(curl -sS -I --max-time 10 "http://${HOST}:${PORT}" || true)
printf "%s\n" "$HTTP_HEADERS" | tee -a "$LOG_FILE"
STATUS_CODE=$(printf "%s\n" "$HTTP_HEADERS" | awk 'NR==1{print $2}')
CONTENT_TYPE=$(printf "%s\n" "$HTTP_HEADERS" | awk 'BEGIN{IGNORECASE=1}/^Content-Type:/{sub(/^[^:]*: */,"",$0);print;exit}')
log "Statut HTTP: ${STATUS_CODE:-inconnu} | Content-Type: ${CONTENT_TYPE:-inconnu}"
log "Étape 4/5: vérification du corps de la page d'accueil"
TMP_HTML="/tmp/ui_index_${TS}.html"
curl -sS --max-time 15 "http://${HOST}:${PORT}" -o "$TMP_HTML" || true
if [ -s "$TMP_HTML" ]; then
log "Premières lignes de la réponse HTML:"
head -n 10 "$TMP_HTML" | tee -a "$LOG_FILE"
if head -n1 "$TMP_HTML" | grep -qi "<!doctype html>"; then
log "Signature HTML détectée (<!doctype html>)"
else
log "ATTENTION: pas de doctype détecté en première ligne"
fi
else
log "ATTENTION: réponse vide ou indisponible (aucun contenu)."
fi
log "Vérification du module index.js"
HTTP_JS=$(curl -sS -I --max-time 10 "http://${HOST}:${PORT}/index.js" || true)
printf "%s\n" "$HTTP_JS" | tee -a "$LOG_FILE"
log "Étape 5/5: collecte des logs docker (dernier 1 min)"
run sudo docker compose ps
run sudo docker compose logs --since=1m ${SERVICE} || true
log "Résumé: statut=${STATUS_CODE:-inconnu}, content-type=${CONTENT_TYPE:-inconnu}, log=${LOG_FILE}"
case "${STATUS_CODE:-}" in
200|204)
log "Succès HTTP côté UI."
;;
*)
log "Échec ou statut inattendu. Voir ${LOG_FILE} pour le détail."
exit 1
;;
esac
exit 0