4NK_node/tests/scripts/test_manage_services.sh
Debian 49b30a7ef1
Some checks failed
CI - 4NK_node / Code Quality (push) Failing after 50s
CI - 4NK_node / Unit Tests (push) Failing after 36s
CI - 4NK_node / Integration Tests (push) Successful in 34s
CI - 4NK_node / Security Tests (push) Failing after 32s
CI - 4NK_node / Docker Build & Test (push) Failing after 15s
CI - 4NK_node / Documentation Tests (push) Successful in 11s
CI - 4NK_node / Security Audit (push) Successful in 9s
CI - 4NK_node / Release Guard (push) Has been skipped
CI - 4NK_node / Performance Tests (push) Successful in 34s
CI - 4NK_node / Notify (push) Failing after 2s
CI - 4NK_node / Publish Release (push) Has been skipped
feat: Ajout du script de gestion des services et documentation
- Ajout du script manage_services.sh pour arrêter, nettoyer et redémarrer les services
- Documentation complète du script dans docs/scripts/
- Tests unitaires pour le script dans tests/scripts/
- Correction des contextes de build dans docker-compose.yml
2025-08-29 15:37:32 +00:00

329 lines
8.5 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# =============================================================================
# Tests pour le Script de Gestion des Services 4NK Node
# =============================================================================
set -e
# =============================================================================
# CONFIGURATION
# =============================================================================
# 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
# Configuration des tests
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../scripts" && pwd)/manage_services.sh"
TEST_DIR="/tmp/.4nk/test_manage_services_$(date +%s)"
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# =============================================================================
# FONCTIONS UTILITAIRES
# =============================================================================
print_header() {
echo -e "${BLUE}=============================================================================${NC}"
echo -e "${BLUE}$1${NC}"
echo -e "${BLUE}=============================================================================${NC}"
}
print_test() {
echo -e "${YELLOW}🧪 Test: $1${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
print_info() {
echo -e "${BLUE} $1${NC}"
}
# =============================================================================
# FONCTIONS DE TEST
# =============================================================================
test_script_exists() {
print_test "Vérification de l'existence du script"
if [ -f "$SCRIPT_PATH" ]; then
print_success "Le script existe"
return 0
else
print_error "Le script n'existe pas: $SCRIPT_PATH"
return 1
fi
}
test_script_executable() {
print_test "Vérification des permissions d'exécution"
if [ -x "$SCRIPT_PATH" ]; then
print_success "Le script est exécutable"
return 0
else
print_error "Le script n'est pas exécutable"
return 1
fi
}
test_help_command() {
print_test "Test de la commande help"
if output=$(bash "$SCRIPT_PATH" help 2>&1); then
if echo "$output" | grep -q "Usage:"; then
print_success "Commande help fonctionne"
return 0
else
print_error "Commande help ne retourne pas l'usage attendu"
return 1
fi
else
print_error "Commande help échoue"
return 1
fi
}
test_invalid_command() {
print_test "Test d'une commande invalide"
if output=$(bash "$SCRIPT_PATH" invalid_command 2>&1); then
print_error "Commande invalide devrait échouer"
return 1
else
if echo "$output" | grep -q "Commande inconnue"; then
print_success "Commande invalide correctement rejetée"
return 0
else
print_error "Message d'erreur inattendu pour commande invalide"
return 1
fi
fi
}
test_docker_requirements() {
print_test "Vérification des prérequis Docker"
# Test Docker
if ! command -v docker &> /dev/null; then
print_error "Docker n'est pas installé"
return 1
fi
# Test Docker daemon
if ! docker info &> /dev/null; then
print_error "Docker daemon n'est pas en cours d'exécution"
return 1
fi
# Test Docker Compose
if ! docker compose version &> /dev/null; then
print_error "Docker Compose n'est pas disponible"
return 1
fi
print_success "Tous les prérequis Docker sont satisfaits"
return 0
}
test_compose_file_exists() {
print_test "Vérification de l'existence du fichier docker-compose.yml"
local compose_file="$PROJECT_DIR/docker-compose.yml"
if [ -f "$compose_file" ]; then
print_success "Fichier docker-compose.yml trouvé"
return 0
else
print_error "Fichier docker-compose.yml manquant: $compose_file"
return 1
fi
}
test_status_command() {
print_test "Test de la commande status"
if output=$(bash "$SCRIPT_PATH" status 2>&1); then
if echo "$output" | grep -q "STATUT DES SERVICES"; then
print_success "Commande status fonctionne"
return 0
else
print_error "Commande status ne retourne pas le format attendu"
return 1
fi
else
print_error "Commande status échoue"
return 1
fi
}
test_stop_command() {
print_test "Test de la commande stop"
# Vérifier d'abord s'il y a des services en cours
if ! docker compose -f "$PROJECT_DIR/docker-compose.yml" ps | grep -q "Up"; then
print_info "Aucun service en cours, test de stop ignoré"
return 0
fi
if output=$(bash "$SCRIPT_PATH" stop 2>&1); then
if echo "$output" | grep -q "Services Docker Compose arrêtés"; then
print_success "Commande stop fonctionne"
return 0
else
print_error "Commande stop ne retourne pas le message attendu"
return 1
fi
else
print_error "Commande stop échoue"
return 1
fi
}
test_clean_command() {
print_test "Test de la commande clean"
if output=$(bash "$SCRIPT_PATH" clean 2>&1); then
if echo "$output" | grep -q "Nettoyage des conteneurs"; then
print_success "Commande clean fonctionne"
return 0
else
print_error "Commande clean ne retourne pas le message attendu"
return 1
fi
else
print_error "Commande clean échoue"
return 1
fi
}
# =============================================================================
# FONCTION PRINCIPALE
# =============================================================================
run_tests() {
print_header "TESTS DU SCRIPT DE GESTION DES SERVICES 4NK NODE"
print_info "Script: $SCRIPT_PATH"
print_info "Projet: $PROJECT_DIR"
print_info "Date: $(date)"
local total_tests=0
local passed_tests=0
local failed_tests=0
# Créer le répertoire de test
mkdir -p "$TEST_DIR"
# Tests de base
((total_tests++))
if test_script_exists; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_script_executable; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_help_command; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_invalid_command; then
((passed_tests++))
else
((failed_tests++))
fi
# Tests des prérequis
((total_tests++))
if test_docker_requirements; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_compose_file_exists; then
((passed_tests++))
else
((failed_tests++))
fi
# Tests des commandes (si Docker est disponible)
if command -v docker &> /dev/null && docker info &> /dev/null; then
((total_tests++))
if test_status_command; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_stop_command; then
((passed_tests++))
else
((failed_tests++))
fi
((total_tests++))
if test_clean_command; then
((passed_tests++))
else
((failed_tests++))
fi
else
print_info "Tests des commandes Docker ignorés (Docker non disponible)"
fi
# Résumé
print_header "RÉSUMÉ DES TESTS"
echo -e "${GREEN}Tests réussis: $passed_tests${NC}"
echo -e "${RED}Tests échoués: $failed_tests${NC}"
echo -e "${BLUE}Total: $total_tests${NC}"
# Nettoyage
rm -rf "$TEST_DIR"
if [ $failed_tests -eq 0 ]; then
print_success "Tous les tests sont passés !"
exit 0
else
print_error "$failed_tests test(s) ont échoué"
exit 1
fi
}
# =============================================================================
# EXÉCUTION
# =============================================================================
if [ "${1:-}" = "help" ] || [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
echo "Usage: $0 [help]"
echo ""
echo "Tests pour le script de gestion des services 4NK Node"
echo ""
echo "Options:"
echo " help, -h, --help Afficher cette aide"
exit 0
fi
run_tests