/
gabasoft
/
winpulse
Обзор
Документация
Войти
/
gabasoft
/
winpulse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
scripts/test.sh
396 строк
11 KB
Pavel Kashchenko
change filenames
07 июл 2025, 22:02
07 июл 2025, 22:02
dfae9aa
Код
Авторство
О чём код?
#!/bin/bash # WinPulse Test Script # This script runs all types of tests for the project set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_header() { echo -e "${BLUE}[TEST]${NC} $1" } # Default values TEST_TYPE="all" COVERAGE_THRESHOLD_FRONTEND=90 COVERAGE_THRESHOLD_BACKEND=85 PARALLEL_TESTS=true # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in --type|-t) TEST_TYPE="$2" shift 2 ;; --frontend-coverage|-fc) COVERAGE_THRESHOLD_FRONTEND="$2" shift 2 ;; --backend-coverage|-bc) COVERAGE_THRESHOLD_BACKEND="$2" shift 2 ;; --no-parallel|-np) PARALLEL_TESTS=false shift ;; --help|-h) echo "Usage: $0 [OPTIONS]" echo "Options:" echo " --type, -t Test type (all|unit|integration|e2e|load|security)" echo " --frontend-coverage, -fc Frontend coverage threshold (default: 90)" echo " --backend-coverage, -bc Backend coverage threshold (default: 85)" echo " --no-parallel, -np Disable parallel test execution" echo " --help, -h Show this help message" exit 0 ;; *) print_error "Unknown option: $1" exit 1 ;; esac done # Validate test type valid_types=("all" "unit" "integration" "e2e" "load" "security") if [[ ! " ${valid_types[@]} " =~ " ${TEST_TYPE} " ]]; then print_error "Invalid test type: $TEST_TYPE. Must be one of: ${valid_types[*]}" exit 1 fi print_header "Starting WinPulse test suite" print_status "Test type: $TEST_TYPE" print_status "Frontend coverage threshold: $COVERAGE_THRESHOLD_FRONTEND%" print_status "Backend coverage threshold: $COVERAGE_THRESHOLD_BACKEND%" print_status "Parallel execution: $PARALLEL_TESTS" # Check prerequisites check_prerequisites() { print_status "Checking test prerequisites..." if ! command -v node &> /dev/null; then print_error "Node.js is not installed" exit 1 fi if ! command -v npm &> /dev/null; then print_error "npm is not installed" exit 1 fi if ! command -v python3 &> /dev/null; then print_error "Python 3 is not installed" exit 1 fi if ! command -v pytest &> /dev/null; then print_error "pytest is not installed" exit 1 fi print_status "All test prerequisites are satisfied" } # Run frontend unit tests run_frontend_unit_tests() { print_header "Running frontend unit tests..." cd frontend # Install dependencies if needed if [ ! -d "node_modules" ]; then npm install fi # Run Jest tests with coverage if [ "$PARALLEL_TESTS" = true ]; then npm run test:unit -- --coverage --maxWorkers=4 else npm run test:unit -- --coverage --maxWorkers=1 fi # Check coverage threshold COVERAGE=$(npm run test:coverage --silent | grep -o '[0-9]*\.[0-9]*%' | head -1 | sed 's/%//') if (( $(echo "$COVERAGE < $COVERAGE_THRESHOLD_FRONTEND" | bc -l) )); then print_error "Frontend coverage $COVERAGE% is below threshold $COVERAGE_THRESHOLD_FRONTEND%" exit 1 else print_status "Frontend coverage: $COVERAGE% (threshold: $COVERAGE_THRESHOLD_FRONTEND%)" fi cd .. print_status "Frontend unit tests completed successfully" } # Run backend unit tests run_backend_unit_tests() { print_header "Running backend unit tests..." services=("auth" "data" "llm" "recommendation" "notification" "analytics" "support") total_coverage=0 service_count=0 for service in "${services[@]}"; do print_status "Running unit tests for $service service..." cd "services/$service" # Install dependencies if needed if [ -f "requirements.txt" ]; then pip install -r requirements.txt fi # Run pytest with coverage if [ -d "tests" ]; then if [ "$PARALLEL_TESTS" = true ]; then pytest tests/ -v --cov=src --cov-report=term-missing --tb=short -n auto else pytest tests/ -v --cov=src --cov-report=term-missing --tb=short fi # Get coverage percentage COVERAGE=$(pytest tests/ --cov=src --cov-report=term-missing --tb=no | grep TOTAL | awk '{print $4}' | sed 's/%//') total_coverage=$(echo "$total_coverage + $COVERAGE" | bc -l) service_count=$((service_count + 1)) print_status "$service service coverage: $COVERAGE%" else print_warning "No tests directory found for $service service" fi cd ../.. done # Calculate average coverage if [ $service_count -gt 0 ]; then avg_coverage=$(echo "scale=2; $total_coverage / $service_count" | bc -l) if (( $(echo "$avg_coverage < $COVERAGE_THRESHOLD_BACKEND" | bc -l) )); then print_error "Backend average coverage $avg_coverage% is below threshold $COVERAGE_THRESHOLD_BACKEND%" exit 1 else print_status "Backend average coverage: $avg_coverage% (threshold: $COVERAGE_THRESHOLD_BACKEND%)" fi fi print_status "Backend unit tests completed successfully" } # Run integration tests run_integration_tests() { print_header "Running integration tests..." # Check if test environment is available if ! kubectl cluster-info &> /dev/null; then print_warning "Kubernetes cluster not available, skipping integration tests" return 0 fi # Run integration tests cd tests/integration if [ -f "requirements.txt" ]; then pip install -r requirements.txt fi pytest . -v --tb=short cd ../.. print_status "Integration tests completed successfully" } # Run end-to-end tests run_e2e_tests() { print_header "Running end-to-end tests..." cd frontend # Check if Detox is configured if [ ! -f "package.json" ] || ! grep -q "detox" package.json; then print_warning "Detox not configured, skipping E2E tests" cd .. return 0 fi # Install Detox dependencies npm install # Run Detox tests if [ "$PARALLEL_TESTS" = true ]; then npm run test:e2e -- --configuration=ci else npm run test:e2e -- --configuration=ci --workers=1 fi cd .. print_status "End-to-end tests completed successfully" } # Run load tests run_load_tests() { print_header "Running load tests..." # Check if JMeter is available if ! command -v jmeter &> /dev/null; then print_warning "JMeter not installed, skipping load tests" return 0 fi cd tests/load # Run JMeter load test jmeter -n -t winpulse-load-test.jmx -l results.jtl -e -o report/ # Analyze results python analyze_results.py results.jtl cd ../.. print_status "Load tests completed successfully" } # Run security tests run_security_tests() { print_header "Running security tests..." # Frontend security audit print_status "Running npm audit..." cd frontend npm audit --audit-level=high cd .. # Backend security scan print_status "Running safety check..." if command -v safety &> /dev/null; then safety check else print_warning "safety not installed, skipping Python security check" fi # Run bandit for Python code print_status "Running bandit security scan..." if command -v bandit &> /dev/null; then bandit -r services/ -f json -o bandit-report.json print_status "Bandit report generated: bandit-report.json" else print_warning "bandit not installed, skipping Python security linting" fi # Run OWASP ZAP if available if command -v zap-baseline.py &> /dev/null; then print_status "Running OWASP ZAP baseline scan..." zap-baseline.py -t https://staging.winpulse.app -J zap-report.json print_status "ZAP report generated: zap-report.json" else print_warning "OWASP ZAP not installed, skipping web security scan" fi print_status "Security tests completed successfully" } # Generate test report generate_test_report() { print_header "Generating test report..." REPORT_FILE="test-report-$(date +%Y%m%d-%H%M%S).html" # Create HTML report cat > $REPORT_FILE << EOF <!DOCTYPE html> <html> <head> <title>WinPulse Test Report</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } .header { background-color: #f0f0f0; padding: 20px; border-radius: 5px; } .section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; } .success { background-color: #d4edda; border-color: #c3e6cb; } .warning { background-color: #fff3cd; border-color: #ffeaa7; } .error { background-color: #f8d7da; border-color: #f5c6cb; } </style> </head> <body> <div class="header"> <h1>WinPulse Test Report</h1> <p>Generated on: $(date)</p> <p>Test type: $TEST_TYPE</p> </div> <div class="section success"> <h2>Test Summary</h2> <p>All tests completed successfully!</p> </div> <div class="section"> <h2>Coverage Report</h2> <p>Frontend coverage threshold: $COVERAGE_THRESHOLD_FRONTEND%</p> <p>Backend coverage threshold: $COVERAGE_THRESHOLD_BACKEND%</p> </div> </body> </html> EOF print_status "Test report generated: $REPORT_FILE" } # Main test process main() { print_header "Starting WinPulse test suite" # Check prerequisites check_prerequisites # Run tests based on type case $TEST_TYPE in "all") run_frontend_unit_tests run_backend_unit_tests run_integration_tests run_e2e_tests run_load_tests run_security_tests ;; "unit") run_frontend_unit_tests run_backend_unit_tests ;; "integration") run_integration_tests ;; "e2e") run_e2e_tests ;; "load") run_load_tests ;; "security") run_security_tests ;; esac # Generate test report generate_test_report print_header "🎉 All tests completed successfully!" } # Execute main function main