#!/bin/bash # Script: Exécution des tests de connectivité # Description: Lance les tests de connectivité de l'infrastructure 4NK Node # Auteur: Assistant IA # Date: 2024-12-19 set -e # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" LOG_DIR="$SCRIPT_DIR/logs" TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S) LOG_FILE="$LOG_DIR/connectivity_tests_$TIMESTAMP.log" # Couleurs pour l'affichage RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Variables globales VERBOSE=false DEBUG=false # Fonctions utilitaires log() { local level="$1" shift local message="$*" local timestamp=$(date +%Y-%m-%d\ %H:%M:%S) case "$level" in "INFO") echo -e "${BLUE}[$timestamp] INFO:${NC} $message" | tee -a "$LOG_FILE" ;; "SUCCESS") echo -e "${GREEN}[$timestamp] SUCCESS:${NC} $message" | tee -a "$LOG_FILE" ;; "WARNING") echo -e "${YELLOW}[$timestamp] WARNING:${NC} $message" | tee -a "$LOG_FILE" ;; "ERROR") echo -e "${RED}[$timestamp] ERROR:${NC} $message" | tee -a "$LOG_FILE" ;; "DEBUG") if [ "$DEBUG" = true ]; then echo -e "${YELLOW}[$timestamp] DEBUG:${NC} $message" | tee -a "$LOG_FILE" fi ;; esac } # Afficher l'aide show_help() { cat << EOF Usage: $0 [OPTIONS] Options: -h, --help Afficher cette aide -v, --verbose Mode verbose -d, --debug Mode debug Exemples: $0 # Exécuter les tests de connectivité $0 --verbose # Mode verbose $0 --debug # Mode debug complet EOF } # Vérifier les prérequis check_prerequisites() { log "INFO" "Vérification des prérequis pour les tests de connectivité..." # Vérifier Docker if ! command -v docker &> /dev/null; then log "ERROR" "Docker n'est pas installé" exit 1 fi # Vérifier Python if ! command -v python3 &> /dev/null; then log "ERROR" "Python3 n'est pas installé" exit 1 fi # Vérifier les répertoires mkdir -p "$LOG_DIR" # Vérifier que les services sont démarrés if ! docker ps | grep -q "bitcoin"; then log "ERROR" "Bitcoin Core n'est pas démarré" exit 1 fi if ! docker ps | grep -q "blindbit"; then log "ERROR" "Blindbit n'est pas démarré" exit 1 fi if ! docker ps | grep -q "sdk_relay"; then log "ERROR" "sdk_relay n'est pas démarré" exit 1 fi log "SUCCESS" "Prérequis vérifiés" } # Exécuter les tests de connectivité run_connectivity_tests() { log "INFO" "Début des tests de connectivité..." cd "$SCRIPT_DIR/connectivity" local connectivity_results=() local total_tests=0 local successful_tests=0 local failed_tests=0 # Test de connectivité shell if [ -f "test_connectivity.sh" ]; then total_tests=$((total_tests + 1)) log "INFO" "Exécution de test_connectivity.sh" if [ "$VERBOSE" = true ]; then if ./test_connectivity.sh 2>&1 | tee -a "$LOG_FILE"; then connectivity_results+=("test_connectivity.sh:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_connectivity.sh terminé avec succès" else connectivity_results+=("test_connectivity.sh:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_connectivity.sh a échoué" fi else if ./test_connectivity.sh >> "$LOG_FILE" 2>&1; then connectivity_results+=("test_connectivity.sh:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_connectivity.sh terminé avec succès" else connectivity_results+=("test_connectivity.sh:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_connectivity.sh a échoué" fi fi else log "WARNING" "Test test_connectivity.sh non trouvé" fi # Test WebSocket Python if [ -f "test_websocket_messages.py" ]; then total_tests=$((total_tests + 1)) log "INFO" "Exécution de test_websocket_messages.py" if [ "$VERBOSE" = true ]; then if python3 test_websocket_messages.py 2>&1 | tee -a "$LOG_FILE"; then connectivity_results+=("test_websocket_messages.py:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_websocket_messages.py terminé avec succès" else connectivity_results+=("test_websocket_messages.py:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_websocket_messages.py a échoué" fi else if python3 test_websocket_messages.py >> "$LOG_FILE" 2>&1; then connectivity_results+=("test_websocket_messages.py:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_websocket_messages.py terminé avec succès" else connectivity_results+=("test_websocket_messages.py:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_websocket_messages.py a échoué" fi fi else log "WARNING" "Test test_websocket_messages.py non trouvé" fi # Test storage via reverse proxy if [ -f "test_storage_proxy.sh" ]; then total_tests=$((total_tests + 1)) log "INFO" "Exécution de test_storage_proxy.sh" if [ "$VERBOSE" = true ]; then if HOST=localhost ./test_storage_proxy.sh 2>&1 | tee -a "$LOG_FILE"; then connectivity_results+=("test_storage_proxy.sh:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_storage_proxy.sh terminé avec succès" else connectivity_results+=("test_storage_proxy.sh:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_storage_proxy.sh a échoué" fi else if HOST=localhost ./test_storage_proxy.sh >> "$LOG_FILE" 2>&1; then connectivity_results+=("test_storage_proxy.sh:SUCCESS") successful_tests=$((successful_tests + 1)) log "SUCCESS" "test_storage_proxy.sh terminé avec succès" else connectivity_results+=("test_storage_proxy.sh:FAILED") failed_tests=$((failed_tests + 1)) log "ERROR" "test_storage_proxy.sh a échoué" fi fi else log "WARNING" "Test test_storage_proxy.sh non trouvé" fi # Afficher le résumé des tests de connectivité log "INFO" "=== Résumé des tests de connectivité ===" log "INFO" "Total: $total_tests, Succès: $successful_tests, Échecs: $failed_tests" for result in "${connectivity_results[@]}"; do if [[ "$result" == *":SUCCESS" ]]; then log "SUCCESS" " ✅ ${result%:SUCCESS}" else log "ERROR" " ❌ ${result%:FAILED}" fi done # Retourner le code de sortie approprié if [ $failed_tests -gt 0 ]; then log "ERROR" "Tests de connectivité terminés avec $failed_tests échec(s)" return 1 else log "SUCCESS" "Tous les tests de connectivité ont réussi" return 0 fi } # Fonction principale main() { log "INFO" "=== Début des tests de connectivité 4NK Node ===" log "INFO" "Timestamp: $TIMESTAMP" log "INFO" "Répertoire de travail: $PROJECT_ROOT" # Vérifier les prérequis check_prerequisites # Exécuter les tests de connectivité run_connectivity_tests log "SUCCESS" "=== Tests de connectivité terminés ===" log "INFO" "Logs: $LOG_FILE" } # Traitement des arguments while [[ $# -gt 0 ]]; do case $1 in -h|--help) show_help exit 0 ;; -v|--verbose) VERBOSE=true shift ;; -d|--debug) DEBUG=true VERBOSE=true shift ;; *) log "ERROR" "Option inconnue: $1" show_help exit 1 ;; esac done # Exécution principale main "$@"