Files
EventHubBack/test/scripts/test_runner.sh
2026-04-20 21:04:16 +03:00

228 lines
6.3 KiB
Bash

#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
SCRIPTS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPTS_DIR/../.." && pwd)"
BASE_URL="http://localhost:8080"
SERVER_STARTED=false
SERVER_PID=""
# ============================================================================
# Функции
# ============================================================================
cleanup() {
if [ "$SERVER_STARTED" = true ] && [ -n "$SERVER_PID" ]; then
echo ""
echo -e "${BLUE}[INFO]${NC} Stopping server..."
kill "$SERVER_PID" 2>/dev/null
wait "$SERVER_PID" 2>/dev/null
pkill -f "beam.*eventhub_test" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
usage() {
echo "Usage: $0 [options] [pattern]"
echo ""
echo "Options:"
echo " -h, --help Show this help"
echo " -l, --list List available test scripts"
echo " -v, --verbose Verbose output"
echo " -s, --server Use existing server (don't start/stop)"
echo ""
echo "Examples:"
echo " $0 Run all tests"
echo " $0 auth Run tests matching 'auth'"
echo " $0 booking Run tests matching 'booking'"
echo " $0 -l List all test scripts"
echo " $0 -s Use already running server"
}
list_scripts() {
echo "Available test scripts:"
find "$SCRIPTS_DIR" -maxdepth 1 -name "test_*.sh" ! -name "test_all.sh" ! -name "test_runner.sh" -type f | sort | while read script; do
name=$(basename "$script")
echo " - $name"
done
}
start_server() {
echo -e "${CYAN}[STEP]${NC} Starting EventHub server..."
# Переходим в корень проекта
cd "$PROJECT_ROOT"
# Проверяем, что мы в правильной директории
if [ ! -f "rebar.config" ]; then
echo -e "${RED}[ERROR]${NC} rebar.config not found in $(pwd)"
return 1
fi
echo -e "${BLUE}[INFO]${NC} Project root: $(pwd)"
# Компилируем если нужно
if [ ! -d "_build" ]; then
echo -e "${BLUE}[INFO]${NC} Compiling project..."
rebar3 compile
fi
# Запускаем сервер
LOG_FILE="/tmp/eventhub_test_server.log"
echo -e "${BLUE}[INFO]${NC} Starting server, log: $LOG_FILE"
rebar3 shell --sname eventhub_test > "$LOG_FILE" 2>&1 &
SERVER_PID=$!
echo -e "${BLUE}[INFO]${NC} Server PID: $SERVER_PID"
# Ждём готовности
for i in {1..30}; do
echo -n "."
if curl -s "http://localhost:8080/health" 2>/dev/null | grep -q "ok"; then
echo ""
echo -e "${GREEN}[SUCCESS]${NC} Server ready (took $i seconds)"
return 0
fi
# Проверяем, не умер ли процесс
if ! kill -0 "$SERVER_PID" 2>/dev/null; then
echo ""
echo -e "${RED}[ERROR]${NC} Server process died"
echo -e "${YELLOW}[INFO]${NC} Last 20 lines of log:"
tail -20 "$LOG_FILE"
return 1
fi
sleep 1
done
echo ""
echo -e "${RED}[ERROR]${NC} Server failed to start within 30 seconds"
echo -e "${YELLOW}[INFO]${NC} Last 20 lines of log:"
tail -20 "$LOG_FILE"
return 1
}
# ============================================================================
# Парсинг аргументов
# ============================================================================
VERBOSE=false
USE_EXISTING=false
PATTERN=""
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
exit 0
;;
-l|--list)
list_scripts
exit 0
;;
-v|--verbose)
VERBOSE=true
shift
;;
-s|--server)
USE_EXISTING=true
shift
;;
*)
PATTERN="$1"
shift
;;
esac
done
# ============================================================================
# Главная логика
# ============================================================================
echo "============================================================"
echo " EVENTHUB TEST RUNNER"
echo "============================================================"
echo ""
if [ "$USE_EXISTING" = false ]; then
if ! start_server; then
exit 1
fi
SERVER_STARTED=true
else
if ! curl -s "$BASE_URL/health" | grep -q "ok"; then
echo -e "${RED}[ERROR]${NC} Server is not running"
exit 1
fi
echo -e "${GREEN}[SUCCESS]${NC} Using existing server"
fi
echo ""
# Находим тесты
if [ -n "$PATTERN" ]; then
TEST_SCRIPTS=$(find "$SCRIPTS_DIR" -maxdepth 1 -name "test_*${PATTERN}*.sh" ! -name "test_all.sh" ! -name "test_runner.sh" -type f | sort)
else
TEST_SCRIPTS=$(find "$SCRIPTS_DIR" -maxdepth 1 -name "test_*.sh" ! -name "test_all.sh" ! -name "test_runner.sh" -type f | sort)
fi
if [ -z "$TEST_SCRIPTS" ]; then
echo -e "${YELLOW}[WARNING]${NC} No test scripts found"
exit 0
fi
echo -e "${BLUE}[INFO]${NC} Running tests:"
for script in $TEST_SCRIPTS; do
echo " - $(basename "$script")"
done
echo ""
PASSED=0
FAILED=0
for script in $TEST_SCRIPTS; do
script_name=$(basename "$script")
echo "============================================================"
echo -e "${CYAN}[RUNNING]${NC} $script_name"
echo "============================================================"
if $VERBOSE; then
bash "$script"
EXIT_CODE=$?
else
bash "$script"
EXIT_CODE=$?
fi
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}[PASSED]${NC} $script_name"
((PASSED++))
else
echo -e "${RED}[FAILED]${NC} $script_name"
((FAILED++))
fi
echo ""
done
echo "============================================================"
echo " TEST SUMMARY"
echo "============================================================"
echo -e "Scripts run: $((PASSED + FAILED))"
echo -e "${GREEN}Passed: $PASSED${NC}"
echo -e "${RED}Failed: $FAILED${NC}"
echo "============================================================"
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}🎉 ALL TESTS PASSED!${NC}"
exit 0
else
echo -e "${RED}❌ SOME TESTS FAILED${NC}"
exit 1
fi