From abc739b1be75d424cdf17c4b1bfbdf0470114a97 Mon Sep 17 00:00:00 2001 From: "xiuting.xu" Date: Mon, 13 Oct 2025 16:48:05 +0800 Subject: [PATCH] =?UTF-8?q?[#19]=20alert=E5=92=8Cweb=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tests/data/alertmanager/alertmanager.yml | 19 ++++ src/alert/tests/data/alertmanager/nflog | 0 src/alert/tests/data/alertmanager/silences | 0 .../data/etc/alertmanager.alert.argus.com | 1 + src/alert/tests/docker-compose.yml | 37 ++++++ src/alert/tests/scripts/01_bootstrap.sh | 19 ++++ src/alert/tests/scripts/02_up.sh | 10 ++ .../scripts/03_alertmanager_add_alert.sh | 106 ++++++++++++++++++ src/alert/tests/scripts/04_query_alerts.sh | 71 ++++++++++++ src/alert/tests/scripts/05_down.sh | 21 ++++ src/alert/tests/scripts/e2e_test.sh | 105 +++++++++++++++++ src/web/build_tools/frontend/health-check.sh | 6 +- src/web/build_tools/frontend/supervisord.conf | 16 ++- src/web/tests/data/etc/web.argus.com | 1 + src/web/tests/docker-compose.yml | 63 +++++++++++ src/web/tests/scripts/01_bootstrap.sh | 19 ++++ src/web/tests/scripts/02_up.sh | 10 ++ src/web/tests/scripts/03_web_health_check.sh | 93 +++++++++++++++ src/web/tests/scripts/04_down.sh | 21 ++++ src/web/tests/scripts/e2e_test.sh | 85 ++++++++++++++ 20 files changed, 698 insertions(+), 5 deletions(-) create mode 100644 src/alert/tests/data/alertmanager/alertmanager.yml create mode 100644 src/alert/tests/data/alertmanager/nflog create mode 100644 src/alert/tests/data/alertmanager/silences create mode 100644 src/alert/tests/data/etc/alertmanager.alert.argus.com create mode 100644 src/alert/tests/docker-compose.yml create mode 100644 src/alert/tests/scripts/01_bootstrap.sh create mode 100644 src/alert/tests/scripts/02_up.sh create mode 100644 src/alert/tests/scripts/03_alertmanager_add_alert.sh create mode 100644 src/alert/tests/scripts/04_query_alerts.sh create mode 100644 src/alert/tests/scripts/05_down.sh create mode 100644 src/alert/tests/scripts/e2e_test.sh create mode 100644 src/web/tests/data/etc/web.argus.com create mode 100644 src/web/tests/docker-compose.yml create mode 100644 src/web/tests/scripts/01_bootstrap.sh create mode 100644 src/web/tests/scripts/02_up.sh create mode 100644 src/web/tests/scripts/03_web_health_check.sh create mode 100644 src/web/tests/scripts/04_down.sh create mode 100644 src/web/tests/scripts/e2e_test.sh diff --git a/src/alert/tests/data/alertmanager/alertmanager.yml b/src/alert/tests/data/alertmanager/alertmanager.yml new file mode 100644 index 0000000..26060aa --- /dev/null +++ b/src/alert/tests/data/alertmanager/alertmanager.yml @@ -0,0 +1,19 @@ +global: + resolve_timeout: 5m + +route: + group_by: ['alertname', 'instance'] # 分组:相同 alertname + instance 的告警合并 + group_wait: 30s # 第一个告警后,等 30s 看是否有同组告警一起发 + group_interval: 5m # 同组告警变化后,至少 5 分钟再发一次 + repeat_interval: 3h # 相同告警,3 小时重复提醒一次 + receiver: 'null' + +receivers: + - name: 'null' + +inhibit_rules: + - source_match: + severity: 'critical' # critical 告警存在时 + target_match: + severity: 'warning' # 抑制相同 instance 的 warning 告警 + equal: ['instance'] diff --git a/src/alert/tests/data/alertmanager/nflog b/src/alert/tests/data/alertmanager/nflog new file mode 100644 index 0000000..e69de29 diff --git a/src/alert/tests/data/alertmanager/silences b/src/alert/tests/data/alertmanager/silences new file mode 100644 index 0000000..e69de29 diff --git a/src/alert/tests/data/etc/alertmanager.alert.argus.com b/src/alert/tests/data/etc/alertmanager.alert.argus.com new file mode 100644 index 0000000..cd339f9 --- /dev/null +++ b/src/alert/tests/data/etc/alertmanager.alert.argus.com @@ -0,0 +1 @@ +172.18.0.2 diff --git a/src/alert/tests/docker-compose.yml b/src/alert/tests/docker-compose.yml new file mode 100644 index 0000000..cef0204 --- /dev/null +++ b/src/alert/tests/docker-compose.yml @@ -0,0 +1,37 @@ +version: '3.8' +services: + alertmanager: + build: + context: ../../../ + dockerfile: src/alert/alertmanager/build/Dockerfile + args: + ARGUS_UID: ${ARGUS_UID:-2133} + ARGUS_GID: ${ARGUS_GID:-2015} + USE_INTRANET: ${USE_INTRANET:-false} + image: argus-alertmanager:latest + container_name: argus-alertmanager + environment: + - ALERTMANAGER_BASE_PATH=/private/argus/alert/alertmanager + - ARGUS_UID=${ARGUS_UID:-2133} + - ARGUS_GID=${ARGUS_GID:-2015} + ports: + - "${ARGUS_PORT:-9093}:9093" + volumes: + - ${DATA_ROOT:-./data}/alertmanager:/private/argus/alert/alertmanager + - ${DATA_ROOT:-./data}/etc:/private/argus/etc + networks: + - argus-network + restart: unless-stopped + logging: + driver: "json-file" + options: + max-size: "10m" + max-file: "3" +networks: + argus-network: + driver: bridge + name: argus-network + +volumes: + alertmanager_data: + driver: local diff --git a/src/alert/tests/scripts/01_bootstrap.sh b/src/alert/tests/scripts/01_bootstrap.sh new file mode 100644 index 0000000..3862c7e --- /dev/null +++ b/src/alert/tests/scripts/01_bootstrap.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)" +project_root="$(cd "$root/../../.." && pwd)" + +source "$project_root/scripts/common/build_user.sh" +load_build_user + +# 创建新的private目录结构 (基于argus目录结构) +echo "[INFO] Creating private directory structure for supervisor-based containers..." +mkdir -p "$root/private/argus/alert/alertmanager" +mkdir -p "$root/private/argus/etc/" + +# 设置数据目录权限 +echo "[INFO] Setting permissions for data directories..." +chown -R "${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID}" "$root/private/argus/alert/alertmanager" 2>/dev/null || true +chown -R "${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID}" "$root/private/argus/etc" 2>/dev/null || true + +echo "[INFO] Supervisor-based containers will manage their own scripts and configurations" diff --git a/src/alert/tests/scripts/02_up.sh b/src/alert/tests/scripts/02_up.sh new file mode 100644 index 0000000..27e9020 --- /dev/null +++ b/src/alert/tests/scripts/02_up.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +compose_cmd="docker compose" +if ! $compose_cmd version >/dev/null 2>&1; then + if command -v docker-compose >/dev/null 2>&1; then compose_cmd="docker-compose"; else + echo "需要 Docker Compose,请安装后重试" >&2; exit 1; fi +fi +$compose_cmd -p alert-mvp up -d --remove-orphans +echo "[OK] 服务已启动:Alertmanager http://localhost:9093" diff --git a/src/alert/tests/scripts/03_alertmanager_add_alert.sh b/src/alert/tests/scripts/03_alertmanager_add_alert.sh new file mode 100644 index 0000000..d65da79 --- /dev/null +++ b/src/alert/tests/scripts/03_alertmanager_add_alert.sh @@ -0,0 +1,106 @@ +#!/bin/bash +set -euo pipefail + +# ========================================================== +# Alertmanager 测试脚本 +# ========================================================== + +ALERTMANAGER_URL="http://localhost:9093" +TEST_ALERT_NAME_CRITICAL="NodeDown" +TEST_ALERT_NAME_WARNING="HighCPU" +TMP_LOG="/tmp/test-alertmanager.log" + +# 等待参数 +am_wait_attempts=30 +am_wait_interval=2 + +GREEN="\033[1;32m" +RED="\033[1;31m" +YELLOW="\033[1;33m" +RESET="\033[0m" + +# ========================================================== +# 函数定义 +# ========================================================== + +wait_for_alertmanager() { + local attempt=1 + echo "[INFO] 等待 Alertmanager 启动中..." + while (( attempt <= am_wait_attempts )); do + if curl -fsS "${ALERTMANAGER_URL}/api/v2/status" >/dev/null 2>&1; then + echo -e "${GREEN}[OK] Alertmanager 已就绪 (attempt=${attempt}/${am_wait_attempts})${RESET}" + return 0 + fi + echo "[..] Alertmanager 尚未就绪 (${attempt}/${am_wait_attempts})" + sleep "${am_wait_interval}" + (( attempt++ )) + done + echo -e "${RED}[ERROR] Alertmanager 在 ${am_wait_attempts} 次尝试后仍未就绪${RESET}" + return 1 +} + +log_step() { + echo -e "${YELLOW}==== $1 ====${RESET}" +} + +# ========================================================== +# 主流程 +# ========================================================== + +log_step "测试 Alertmanager 开始" +echo "[INFO] Alertmanager 地址: $ALERTMANAGER_URL" + +# Step 1: 等待 Alertmanager 启动 +wait_for_alertmanager + +# Step 2: 触发一个critical测试告警 +echo "[INFO] 发送critical测试告警..." +curl -fsS -X POST "${ALERTMANAGER_URL}/api/v2/alerts" \ + -H "Content-Type: application/json" \ + -d '[ + { + "labels": { + "alertname": "'"${TEST_ALERT_NAME_CRITICAL}"'", + "instance": "node-1", + "severity": "critical" + }, + "annotations": { + "summary": "节点 node-1 宕机" + } + } + ]' \ + -o "$TMP_LOG" + +if [ $? -eq 0 ]; then + echo -e "${GREEN}[OK] 已成功发送critical测试告警${RESET}" +else + echo -e "${RED}[ERROR] critical告警发送失败!${RESET}" + cat "$TMP_LOG" + exit 1 +fi + +# Step 3: 触发一个warning测试告警 +echo "[INFO] 发送warning测试告警..." +curl -fsS -X POST "${ALERTMANAGER_URL}/api/v2/alerts" \ + -H "Content-Type: application/json" \ + -d '[ + { + "labels": { + "alertname": "'"${TEST_ALERT_NAME_WARNING}"'", + "instance": "node-1", + "severity": "warning" + }, + "annotations": { + "summary": "节点 node-1 CPU 使用率过高" + } + } + ]' \ + -o "$TMP_LOG" + +if [ $? -eq 0 ]; then + echo -e "${GREEN}[OK] 已成功发送warning测试告警${RESET}" +else + echo -e "${RED}[ERROR] warning告警发送失败!${RESET}" + cat "$TMP_LOG" + exit 1 +fi \ No newline at end of file diff --git a/src/alert/tests/scripts/04_query_alerts.sh b/src/alert/tests/scripts/04_query_alerts.sh new file mode 100644 index 0000000..05c616e --- /dev/null +++ b/src/alert/tests/scripts/04_query_alerts.sh @@ -0,0 +1,71 @@ +#!/bin/bash +set -euo pipefail + +# ========================================================== +# Alertmanager 测试脚本(含启动等待) +# ========================================================== + +ALERTMANAGER_URL="http://localhost:9093" +TEST_ALERT_NAME_CRITICAL="NodeDown" +TEST_ALERT_NAME_WARNING="HighCPU" +TMP_LOG="/tmp/test-alertmanager.log" + +# 等待参数 +am_wait_attempts=30 +am_wait_interval=2 + +GREEN="\033[1;32m" +RED="\033[1;31m" +YELLOW="\033[1;33m" +RESET="\033[0m" + +# ========================================================== +# 函数定义 +# ========================================================== + +wait_for_alertmanager() { + local attempt=1 + echo "[INFO] 等待 Alertmanager 启动中..." + while (( attempt <= am_wait_attempts )); do + if curl -fsS "${ALERTMANAGER_URL}/api/v2/status" >/dev/null 2>&1; then + echo -e "${GREEN}[OK] Alertmanager 已就绪 (attempt=${attempt}/${am_wait_attempts})${RESET}" + return 0 + fi + echo "[..] Alertmanager 尚未就绪 (${attempt}/${am_wait_attempts})" + sleep "${am_wait_interval}" + (( attempt++ )) + done + echo -e "${RED}[ERROR] Alertmanager 在 ${am_wait_attempts} 次尝试后仍未就绪${RESET}" + return 1 +} + +log_step() { + echo -e "${YELLOW}==== $1 ====${RESET}" +} + +# ========================================================== +# 主流程 +# ========================================================== + +log_step "查询 Alertmanager 当前告警列表开始" +echo "[INFO] Alertmanager 地址: $ALERTMANAGER_URL" + +# Step 1: 等待 Alertmanager 启动 +wait_for_alertmanager + +# Step 2: 查询当前告警列表 +echo "[INFO] 查询当前告警..." +sleep 1 +curl -fsS "${ALERTMANAGER_URL}/api/v2/alerts" | jq '.' || { + echo -e "${RED}[WARN] 无法解析返回 JSON,请检查 jq 是否安装${RESET}" + curl -s "${ALERTMANAGER_URL}/api/v2/alerts" +} + +# Step 3: 检查告警是否包含 NodeDown +if curl -fsS "${ALERTMANAGER_URL}/api/v2/alerts" | grep -q "${TEST_ALERT_NAME_CRITICAL}"; then + echo -e "${GREEN}✅ 测试通过:Alertmanager 已成功接收告警 ${TEST_ALERT_NAME_CRITICAL}${RESET}" +else + echo -e "${RED}❌ 测试失败:未检测到告警 ${TEST_ALERT_NAME_CRITICAL}${RESET}" +fi + +log_step "测试结束" diff --git a/src/alert/tests/scripts/05_down.sh b/src/alert/tests/scripts/05_down.sh new file mode 100644 index 0000000..a1aab8e --- /dev/null +++ b/src/alert/tests/scripts/05_down.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +compose_cmd="docker compose" +if ! $compose_cmd version >/dev/null 2>&1; then + if command -v docker-compose >/dev/null 2>&1; then compose_cmd="docker-compose"; else + echo "需要 Docker Compose,请安装后重试" >&2; exit 1; fi +fi +$compose_cmd -p alert-mvp down +echo "[OK] 已停止所有容器" + +# 清理private目录内容 +echo "[INFO] 清理private目录内容..." +cd "$(dirname "$0")/.." +if [ -d "private" ]; then + # 删除private目录及其所有内容 + rm -rf private + echo "[OK] 已清理private目录" +else + echo "[INFO] private目录不存在,无需清理" +fi diff --git a/src/alert/tests/scripts/e2e_test.sh b/src/alert/tests/scripts/e2e_test.sh new file mode 100644 index 0000000..3798b57 --- /dev/null +++ b/src/alert/tests/scripts/e2e_test.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "=======================================" +echo "ARGUS Alert System End-to-End Test" +echo "=======================================" +echo "" + +# 记录测试开始时间 +test_start_time=$(date +%s) + +# 函数:等待服务就绪 +wait_for_services() { + echo "[INFO] Waiting for all services to be ready..." + local max_attempts=${SERVICE_WAIT_ATTEMPTS:-120} + local attempt=1 + + while [ $attempt -le $max_attempts ]; do + if curl -fs http://localhost:9093/api/v2/status >/dev/null 2>&1; then + echo "[OK] All services are ready!" + return 0 + fi + echo " Waiting for services... ($attempt/$max_attempts)" + sleep 5 + ((attempt++)) + done + + echo "[ERROR] Services not ready after $max_attempts attempts" + return 1 +} + +# 函数:显示测试步骤 +show_step() { + echo "" + echo "🔄 Step $1: $2" + echo "----------------------------------------" +} + +# 函数:验证步骤结果 +verify_step() { + if [ $? -eq 0 ]; then + echo "✅ $1 - SUCCESS" + else + echo "❌ $1 - FAILED" + exit 1 + fi +} + +# 开始端到端测试 +show_step "1" "Bootstrap - Initialize environment" +./scripts/01_bootstrap.sh +verify_step "Bootstrap" + +show_step "2" "Startup - Start all services" +./scripts/02_up.sh +verify_step "Service startup" + +# 等待服务完全就绪 +wait_for_services || exit 1 + +# 发送告警数据 +show_step "3" "Add alerts - Send test alerts to Alertmanager" +./scripts/03_alertmanager_add_alert.sh +verify_step "Send test alerts" + +# 查询告警数据 +show_step "4" "Verify data - Query Alertmanager" +./scripts/04_query_alerts.sh +verify_step "Data verification" + + +# 检查服务健康状态 +show_step "Health" "Check service health" +echo "[INFO] Checking service health..." + +# 检查 Alertmanager 状态 +if curl -fs "http://localhost:9093/api/v2/status" >/dev/null 2>&1; then + am_status="available" + echo "✅ Alertmanager status: $am_status" +else + am_status="unavailable" + echo "⚠️ Alertmanager status: $am_status" +fi +verify_step "Service health check" + +# 清理环境 +show_step "5" "Cleanup - Stop all services" +./scripts/05_down.sh +verify_step "Service cleanup" + +# 计算总测试时间 +test_end_time=$(date +%s) +total_time=$((test_end_time - test_start_time)) + +echo "" +echo "=======================================" +echo "🎉 END-TO-END TEST COMPLETED SUCCESSFULLY!" +echo "=======================================" +echo "📊 Test Summary:" +echo " • Total time: ${total_time}s" +echo " • Alertmanager status: $am_status" +echo " • All services started and stopped successfully" +echo "" +echo "✅ The ARGUS Alert system is working correctly!" +echo "" diff --git a/src/web/build_tools/frontend/health-check.sh b/src/web/build_tools/frontend/health-check.sh index 849e1b4..e993031 100644 --- a/src/web/build_tools/frontend/health-check.sh +++ b/src/web/build_tools/frontend/health-check.sh @@ -3,13 +3,13 @@ set -euo pipefail URL="http://127.0.0.1:80" -echo "[INFO] Starting frontend health check loop for $URL..." +echo "[INFO] Starting Argus web health check loop for $URL..." while true; do if curl -s --max-time 5 "$URL" > /dev/null; then - echo "[OK] $(date '+%Y-%m-%d %H:%M:%S') Frontend is healthy" + echo "[OK] $(date '+%Y-%m-%d %H:%M:%S') Argus web is healthy" else - echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') Frontend health check failed" + echo "[ERROR] $(date '+%Y-%m-%d %H:%M:%S') Argus web health check failed" exit 1 fi sleep 10 diff --git a/src/web/build_tools/frontend/supervisord.conf b/src/web/build_tools/frontend/supervisord.conf index 7e08949..ee7c3b3 100644 --- a/src/web/build_tools/frontend/supervisord.conf +++ b/src/web/build_tools/frontend/supervisord.conf @@ -7,8 +7,8 @@ user=root [program:web] command=/usr/local/bin/start-web-supervised.sh user=root -stdout_logfile=/var/log/supervisor/web.log -stderr_logfile=/var/log/supervisor/web_error.log +stdout_logfile=/var/log/supervisor/web-frontend.log +stderr_logfile=/var/log/supervisor/web-frontend_error.log autorestart=true startretries=3 startsecs=5 @@ -28,6 +28,18 @@ stopwaitsecs=10 killasgroup=true stopasgroup=true +[program:dns-monitor] +command=/usr/local/bin/dns-monitor.sh +user=root +stdout_logfile=/var/log/supervisor/dns-monitor.log +stderr_logfile=/var/log/supervisor/dns-monitor_error.log +autorestart=true +startretries=3 +startsecs=5 +stopwaitsecs=10 +killasgroup=true +stopasgroup=true + [unix_http_server] file=/var/run/supervisor.sock chmod=0700 diff --git a/src/web/tests/data/etc/web.argus.com b/src/web/tests/data/etc/web.argus.com new file mode 100644 index 0000000..f7395bb --- /dev/null +++ b/src/web/tests/data/etc/web.argus.com @@ -0,0 +1 @@ +172.18.0.3 diff --git a/src/web/tests/docker-compose.yml b/src/web/tests/docker-compose.yml new file mode 100644 index 0000000..7212466 --- /dev/null +++ b/src/web/tests/docker-compose.yml @@ -0,0 +1,63 @@ +version: '3.8' +services: + web-frontend: + build: + context: ../../../ + dockerfile: src/web/build_tools/frontend/Dockerfile + args: + ARGUS_UID: ${ARGUS_UID:-2133} + ARGUS_GID: ${ARGUS_GID:-2015} + USE_INTRANET: ${USE_INTRANET:-false} + image: argus-web-frontend:latest + container_name: argus-web-frontend + environment: + - ALERTMANAGER_BASE_PATH=/private/argus/web/frontend + - ARGUS_UID=${ARGUS_UID:-2133} + - ARGUS_GID=${ARGUS_GID:-2015} + ports: + - "${ARGUS_WEB_PORT:-8080}:80" + volumes: + - ${DATA_ROOT:-./data}/web:/private/argus/web/frontend + - ${DATA_ROOT:-./data}/etc:/private/argus/etc + networks: + - argus-network + restart: unless-stopped + logging: + driver: "json-file" + options: + max-size: "10m" + max-file: "3" + web-proxy: + build: + context: ../../../ + dockerfile: src/web/build_tools/proxy/Dockerfile + args: + ARGUS_UID: ${ARGUS_UID:-2133} + ARGUS_GID: ${ARGUS_GID:-2015} + USE_INTRANET: ${USE_INTRANET:-false} + image: argus-web-proxy:latest + container_name: argus-web-proxy + environment: + - ARGUS_UID=${ARGUS_UID:-2133} + - ARGUS_GID=${ARGUS_GID:-2015} + ports: + - "8088:80" + volumes: + - ${DATA_ROOT:-./data}/etc:/private/argus/etc + networks: + - argus-network + restart: unless-stopped + logging: + driver: "json-file" + options: + max-size: "10m" + max-file: "3" + +networks: + argus-network: + driver: bridge + name: argus-network + +volumes: + web-frontend_data: + driver: local diff --git a/src/web/tests/scripts/01_bootstrap.sh b/src/web/tests/scripts/01_bootstrap.sh new file mode 100644 index 0000000..2815fbb --- /dev/null +++ b/src/web/tests/scripts/01_bootstrap.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail +root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../" && pwd)" +project_root="$(cd "$root/../../.." && pwd)" + +source "$project_root/scripts/common/build_user.sh" +load_build_user + +# 创建新的private目录结构 (基于argus目录结构) +echo "[INFO] Creating private directory structure for supervisor-based containers..." +mkdir -p "$root/private/argus/web" +mkdir -p "$root/private/argus/etc/" + +# 设置数据目录权限 +echo "[INFO] Setting permissions for data directories..." +chown -R "${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID}" "$root/private/argus/web" 2>/dev/null || true +chown -R "${ARGUS_BUILD_UID}:${ARGUS_BUILD_GID}" "$root/private/argus/etc" 2>/dev/null || true + +echo "[INFO] Supervisor-based containers will manage their own scripts and configurations" diff --git a/src/web/tests/scripts/02_up.sh b/src/web/tests/scripts/02_up.sh new file mode 100644 index 0000000..51b7387 --- /dev/null +++ b/src/web/tests/scripts/02_up.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +compose_cmd="docker compose" +if ! $compose_cmd version >/dev/null 2>&1; then + if command -v docker-compose >/dev/null 2>&1; then compose_cmd="docker-compose"; else + echo "需要 Docker Compose,请安装后重试" >&2; exit 1; fi +fi +$compose_cmd -p alert-mvp up -d --remove-orphans +echo "[OK] 服务已启动:Web Frontend http://localhost:8080" diff --git a/src/web/tests/scripts/03_web_health_check.sh b/src/web/tests/scripts/03_web_health_check.sh new file mode 100644 index 0000000..17dfb07 --- /dev/null +++ b/src/web/tests/scripts/03_web_health_check.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash +set -euo pipefail + +WEB_URL=${WEB_URL:-"http://localhost:8080"} +API_URL=${API_URL:-"http://master.argus.com/api/v1/master/nodes"} +TIMEOUT=10 + +GREEN="\033[1;32m" +RED="\033[1;31m" +YELLOW="\033[1;33m" +RESET="\033[0m" + +echo "[info] 测试 Argus Web 前端启动状态..." +echo "--------------------------------------------------" + +# 等待 web 前端可用 +attempt=1 +while (( attempt <= 10 )); do + if curl -fsS -m "$TIMEOUT" -o /dev/null "$WEB_URL"; then + echo "[ok] Web 前端已启动 (${attempt}/10)" + break + fi + echo "[..] 等待 Web 前端启动中 (${attempt}/10)" + sleep 3 + (( attempt++ )) +done + +if (( attempt > 10 )); then + echo "[err] Web 前端在 30 秒内未启动" + exit 1 +fi + +# 1. 检查首页可访问性 +echo "[test] 检查首页访问..." +if curl -fsS "$WEB_URL" -m "$TIMEOUT" | grep -q ""; then + echo -e "[${GREEN}ok${RESET}] 首页可访问" +else + echo -e "[${RED}err${RESET}] 首页访问失败" + exit 1 +fi + +# 2. 检查静态资源加载 +echo "[test] 检查静态资源..." +if curl -fsS "$WEB_URL/static/js" -m "$TIMEOUT" | grep -q "Cannot GET"; then + echo -e "[${YELLOW}warn${RESET}] 静态资源路径可能未正确配置" +else + echo -e "[${GREEN}ok${RESET}] 静态资源服务正常" +fi + +# 3. 检查前端路由兼容 +echo "[test] 检查 React Router 路由兼容..." +if curl -fsS "$WEB_URL/dashboard" -m "$TIMEOUT" | grep -q "<title>"; then + echo -e "[${GREEN}ok${RESET}] React Router 路由兼容正常" +else + echo -e "[${YELLOW}warn${RESET}] /dashboard 路由未正确返回 index.html" +fi + +# 4. 测试 API 代理访问 +echo "[test] 检查 API 代理..." +if curl -fsS "$API_URL" -m "$TIMEOUT" | grep -q "nodes"; then + echo -e "[${GREEN}ok${RESET}] API 代理成功" +else + echo -e "[${YELLOW}warn${RESET}] API 代理请求失败,请检查 Nginx proxy_pass" +fi + +# 5. 页面关键字验证 +echo "[test] 检查关键内容..." +if curl -fsS "$WEB_URL" | grep -q "Argus"; then + echo -e "[${GREEN}ok${RESET}] 页面包含关键字 'Argus'" +else + echo -e "[${YELLOW}warn${RESET}] 页面内容中未找到 'Argus'" +fi + +# 6. DNS 检查 +echo "[test] 检查 DNS 解析..." +if dig +short web.argus.com >/dev/null; then + echo -e "[${GREEN}ok${RESET}] 域名 web.argus.com 解析正常" +else + echo -e "[${YELLOW}warn${RESET}] 域名 web.argus.com 解析失败" +fi + +# 7. 响应时间测试 +echo "[test] 检查响应时间..." +response_time=$(curl -o /dev/null -s -w "%{time_total}\n" "$WEB_URL") +echo "[info] 响应时间: ${response_time}s" +if (( $(echo "$response_time > 2.0" | bc -l) )); then + echo -e "[${YELLOW}warn${RESET}] 响应时间较慢(>2s)" +else + echo -e "[${GREEN}ok${RESET}] 响应时间正常" +fi + +echo "--------------------------------------------------" +echo "[done] Web 前端测试完成 ✅" diff --git a/src/web/tests/scripts/04_down.sh b/src/web/tests/scripts/04_down.sh new file mode 100644 index 0000000..a1aab8e --- /dev/null +++ b/src/web/tests/scripts/04_down.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")/.." +compose_cmd="docker compose" +if ! $compose_cmd version >/dev/null 2>&1; then + if command -v docker-compose >/dev/null 2>&1; then compose_cmd="docker-compose"; else + echo "需要 Docker Compose,请安装后重试" >&2; exit 1; fi +fi +$compose_cmd -p alert-mvp down +echo "[OK] 已停止所有容器" + +# 清理private目录内容 +echo "[INFO] 清理private目录内容..." +cd "$(dirname "$0")/.." +if [ -d "private" ]; then + # 删除private目录及其所有内容 + rm -rf private + echo "[OK] 已清理private目录" +else + echo "[INFO] private目录不存在,无需清理" +fi diff --git a/src/web/tests/scripts/e2e_test.sh b/src/web/tests/scripts/e2e_test.sh new file mode 100644 index 0000000..b7f6cdf --- /dev/null +++ b/src/web/tests/scripts/e2e_test.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +set -euo pipefail + +echo "=======================================" +echo "ARGUS Web System End-to-End Test" +echo "=======================================" +echo "" + +# 记录测试开始时间 +test_start_time=$(date +%s) + +# 函数:等待服务就绪 +wait_for_services() { + echo "[INFO] Waiting for all services to be ready..." + local max_attempts=${SERVICE_WAIT_ATTEMPTS:-120} + local attempt=1 + + while [ $attempt -le $max_attempts ]; do + if curl -fs http://localhost:8080 >/dev/null 2>&1; then + echo "[OK] All services are ready!" + return 0 + fi + echo " Waiting for services... ($attempt/$max_attempts)" + sleep 5 + ((attempt++)) + done + + echo "[ERROR] Services not ready after $max_attempts attempts" + return 1 +} + +# 函数:显示测试步骤 +show_step() { + echo "" + echo "🔄 Step $1: $2" + echo "----------------------------------------" +} + +# 函数:验证步骤结果 +verify_step() { + if [ $? -eq 0 ]; then + echo "✅ $1 - SUCCESS" + else + echo "❌ $1 - FAILED" + exit 1 + fi +} + +# 开始端到端测试 +show_step "1" "Bootstrap - Initialize environment" +./scripts/01_bootstrap.sh +verify_step "Bootstrap" + +show_step "2" "Startup - Start all services" +./scripts/02_up.sh +verify_step "Service startup" + +# 等待服务完全就绪 +wait_for_services || exit 1 + +# 测试前端页面 +show_step "3" "Web - Check frontend availability" +./scripts/03_web_health_check.sh +verify_step "Web frontend availability" + +# 清理环境 +show_step "4" "Cleanup - Stop all services" +./scripts/04_down.sh +verify_step "Service cleanup" + +# 计算总测试时间 +test_end_time=$(date +%s) +total_time=$((test_end_time - test_start_time)) + +echo "" +echo "=======================================" +echo "🎉 END-TO-END TEST COMPLETED SUCCESSFULLY!" +echo "=======================================" +echo "📊 Test Summary:" +echo " • Total time: ${total_time}s" +echo " • Alertmanager status: $am_status" +echo " • All services started and stopped successfully" +echo "" +echo "✅ The ARGUS Web system is working correctly!" +echo ""