veza/dev-environment/testing/test-framework.sh
2025-12-03 22:56:50 +01:00

342 lines
11 KiB
Bash
Raw Permalink 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
# 🧪 FRAMEWORK DE TESTS UNIFIÉ - VEZA
# Script pour exécuter tous les tests de manière cohérente
set -e
# Couleurs
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Configuration
TEST_RESULTS_DIR="test-results"
COVERAGE_DIR="coverage"
TIMEOUT=300 # 5 minutes
# Fonction pour afficher les messages
log_info() {
echo -e "${BLUE} $1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# Fonction pour exécuter les tests Go
run_go_tests() {
local service=$1
local test_dir=$2
log_info "Exécution des tests Go pour $service..."
if [ ! -d "$test_dir" ]; then
log_warning "Répertoire $test_dir non trouvé, ignoré"
return 0
fi
cd "$test_dir"
# Tests unitaires
log_info "Tests unitaires..."
go test -v -race -timeout=${TIMEOUT}s -coverprofile=coverage.out ./... 2>&1 | tee "$TEST_RESULTS_DIR/${service}_unit.log"
# Tests d'intégration
log_info "Tests d'intégration..."
go test -v -tags=integration -timeout=${TIMEOUT}s ./... 2>&1 | tee "$TEST_RESULTS_DIR/${service}_integration.log"
# Génération du rapport de couverture
if [ -f "coverage.out" ]; then
go tool cover -html=coverage.out -o "$COVERAGE_DIR/${service}_coverage.html"
go tool cover -func=coverage.out > "$COVERAGE_DIR/${service}_coverage.txt"
fi
cd - > /dev/null
log_success "Tests Go pour $service terminés"
}
# Fonction pour exécuter les tests Rust
run_rust_tests() {
local service=$1
local test_dir=$2
log_info "Exécution des tests Rust pour $service..."
if [ ! -d "$test_dir" ]; then
log_warning "Répertoire $test_dir non trouvé, ignoré"
return 0
fi
cd "$test_dir"
# Tests unitaires et d'intégration
cargo test --verbose --all-features 2>&1 | tee "$TEST_RESULTS_DIR/${service}_tests.log"
# Tests de performance
cargo test --release --verbose --all-features 2>&1 | tee "$TEST_RESULTS_DIR/${service}_perf.log"
# Génération du rapport de couverture (si tarpaulin est installé)
if command -v cargo-tarpaulin >/dev/null 2>&1; then
cargo tarpaulin --out Html --output-dir "$COVERAGE_DIR" --name "${service}_coverage"
fi
cd - > /dev/null
log_success "Tests Rust pour $service terminés"
}
# Fonction pour exécuter les tests JavaScript/TypeScript
run_js_tests() {
local service=$1
local test_dir=$2
log_info "Exécution des tests JS/TS pour $service..."
if [ ! -d "$test_dir" ]; then
log_warning "Répertoire $test_dir non trouvé, ignoré"
return 0
fi
cd "$test_dir"
# Vérifier si Jest est configuré
if [ -f "package.json" ] && grep -q "jest" package.json; then
# Tests unitaires
npm test -- --coverage --watchAll=false 2>&1 | tee "$TEST_RESULTS_DIR/${service}_tests.log"
# Tests E2E si configurés
if [ -f "e2e.config.js" ] || [ -f "cypress.config.js" ]; then
npm run test:e2e 2>&1 | tee "$TEST_RESULTS_DIR/${service}_e2e.log"
fi
else
log_warning "Jest non configuré pour $service, ignoré"
fi
cd - > /dev/null
log_success "Tests JS/TS pour $service terminés"
}
# Fonction pour exécuter les tests de performance
run_performance_tests() {
log_info "Exécution des tests de performance..."
# Tests de charge pour l'API
if command -v k6 >/dev/null 2>&1; then
log_info "Tests de charge avec k6..."
k6 run --out json="$TEST_RESULTS_DIR/load_test_results.json" tests/performance/load_test.js 2>&1 | tee "$TEST_RESULTS_DIR/load_test.log"
fi
# Tests de stress
if command -v wrk >/dev/null 2>&1; then
log_info "Tests de stress avec wrk..."
wrk -t12 -c400 -d30s http://localhost:8080/health 2>&1 | tee "$TEST_RESULTS_DIR/stress_test.log"
fi
log_success "Tests de performance terminés"
}
# Fonction pour exécuter les tests de sécurité
run_security_tests() {
log_info "Exécution des tests de sécurité..."
# Scan de vulnérabilités Go
if command -v gosec >/dev/null 2>&1; then
log_info "Scan de sécurité Go..."
gosec -fmt json -out "$TEST_RESULTS_DIR/gosec_report.json" ./... 2>&1 | tee "$TEST_RESULTS_DIR/gosec.log"
fi
# Scan de vulnérabilités Rust
if command -v cargo-audit >/dev/null 2>&1; then
log_info "Scan de sécurité Rust..."
cargo audit --json > "$TEST_RESULTS_DIR/cargo_audit.json" 2>&1 | tee "$TEST_RESULTS_DIR/cargo_audit.log"
fi
# Scan de vulnérabilités npm
if command -v npm-audit >/dev/null 2>&1; then
log_info "Scan de sécurité npm..."
npm audit --json > "$TEST_RESULTS_DIR/npm_audit.json" 2>&1 | tee "$TEST_RESULTS_DIR/npm_audit.log"
fi
log_success "Tests de sécurité terminés"
}
# Fonction pour générer le rapport global
generate_report() {
log_info "Génération du rapport global..."
local report_file="$TEST_RESULTS_DIR/test_report.html"
cat > "$report_file" << 'HTMLEOF'
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rapport de Tests - Veza Platform</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }
.container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.header { text-align: center; margin-bottom: 30px; }
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-bottom: 30px; }
.card { background: #f8f9fa; padding: 20px; border-radius: 8px; border-left: 4px solid #007bff; }
.card h3 { margin: 0 0 10px 0; color: #333; }
.card .value { font-size: 2em; font-weight: bold; color: #007bff; }
.section { margin-bottom: 30px; }
.section h2 { color: #333; border-bottom: 2px solid #007bff; padding-bottom: 10px; }
.test-results { background: #f8f9fa; padding: 15px; border-radius: 8px; margin: 10px 0; }
.success { color: #28a745; }
.error { color: #dc3545; }
.warning { color: #ffc107; }
.coverage { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px; }
.coverage-item { text-align: center; padding: 15px; background: #e9ecef; border-radius: 8px; }
.coverage-value { font-size: 1.5em; font-weight: bold; }
.coverage-90 { color: #28a745; }
.coverage-70 { color: #ffc107; }
.coverage-50 { color: #dc3545; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🧪 Rapport de Tests - Veza Platform</h1>
<p>Généré le $(date)</p>
</div>
<div class="summary">
<div class="card">
<h3>Tests Exécutés</h3>
<div class="value" id="total-tests">-</div>
</div>
<div class="card">
<h3>Tests Réussis</h3>
<div class="value success" id="passed-tests">-</div>
</div>
<div class="card">
<h3>Tests Échoués</h3>
<div class="value error" id="failed-tests">-</div>
</div>
<div class="card">
<h3>Couverture Moyenne</h3>
<div class="value" id="coverage">-</div>
</div>
</div>
<div class="section">
<h2>📊 Couverture de Code</h2>
<div class="coverage" id="coverage-details">
<!-- Les détails de couverture seront ajoutés ici -->
</div>
</div>
<div class="section">
<h2>🔍 Résultats par Service</h2>
<div id="service-results">
<!-- Les résultats par service seront ajoutés ici -->
</div>
</div>
<div class="section">
<h2>⚡ Tests de Performance</h2>
<div id="performance-results">
<!-- Les résultats de performance seront ajoutés ici -->
</div>
</div>
<div class="section">
<h2>🔒 Tests de Sécurité</h2>
<div id="security-results">
<!-- Les résultats de sécurité seront ajoutés ici -->
</div>
</div>
</div>
<script>
// Script pour analyser les résultats et mettre à jour le rapport
// Ce script sera exécuté côté client pour dynamiser le rapport
</script>
</body>
</html>
HTMLEOF
log_success "Rapport généré: $report_file"
}
# Fonction principale
main() {
local test_type=${1:-"all"}
log_info "Démarrage du framework de tests Veza"
log_info "Type de tests: $test_type"
# Création des répertoires de résultats
mkdir -p "$TEST_RESULTS_DIR" "$COVERAGE_DIR"
# Nettoyage des résultats précédents
rm -rf "$TEST_RESULTS_DIR"/* "$COVERAGE_DIR"/*
case $test_type in
"unit")
log_info "Exécution des tests unitaires uniquement"
run_go_tests "backend-api" "veza-backend-api"
run_rust_tests "chat-server" "veza-chat-server"
run_rust_tests "stream-server" "veza-stream-server"
run_js_tests "frontend" "veza-frontend"
run_js_tests "mobile" "veza-mobile"
;;
"integration")
log_info "Exécution des tests d'intégration uniquement"
run_go_tests "backend-api" "veza-backend-api"
run_rust_tests "chat-server" "veza-chat-server"
run_rust_tests "stream-server" "veza-stream-server"
;;
"performance")
log_info "Exécution des tests de performance uniquement"
run_performance_tests
;;
"security")
log_info "Exécution des tests de sécurité uniquement"
run_security_tests
;;
"all"|*)
log_info "Exécution de tous les tests"
run_go_tests "backend-api" "veza-backend-api"
run_rust_tests "chat-server" "veza-chat-server"
run_rust_tests "stream-server" "veza-stream-server"
run_js_tests "frontend" "veza-frontend"
run_js_tests "mobile" "veza-mobile"
run_performance_tests
run_security_tests
;;
esac
# Génération du rapport
generate_report
log_success "Framework de tests terminé!"
log_info "Résultats disponibles dans: $TEST_RESULTS_DIR"
log_info "Couverture disponible dans: $COVERAGE_DIR"
}
# Vérification des arguments
if [ $# -gt 1 ]; then
echo "Usage: $0 [unit|integration|performance|security|all]"
echo " unit - Tests unitaires uniquement"
echo " integration - Tests d'intégration uniquement"
echo " performance - Tests de performance uniquement"
echo " security - Tests de sécurité uniquement"
echo " all - Tous les tests (défaut)"
exit 1
fi
# Exécution
main "$@"