Files
xiaoxia-saas/scripts/ci/run_integration_tests.sh
T
CI Bot 80c3432c67
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 11s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 17s
AI Code Review / AI Code Review (pull_request) Successful in 2m5s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 4m8s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 4m6s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 6m5s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 6m22s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 6m27s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 1m34s
fix(ci): 修复PG/Redis连接探测 - 等网络就绪再探测,避免空host假阳性
修复两个关键bug:
1. 容器刚healthy时端口映射和IP可能还没就绪,改为循环等待
   healthy + (有映射端口 or 有容器IP) 才进入下一步
2. 容器IP为空时 socket.connect(('', port)) 不报错导致假阳性,
   改为先验证IP/端口格式合法再尝试连接

优化:将healthy检查和网络就绪检查合并到一个循环,
同时展示当前状态,方便调试。
2026-07-19 22:53:00 +08:00

207 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# CI Integration Tests Job 主脚本
# 包含:依赖安装、ffmpeg安装、Redis启动、PG启动、迁移、测试、清理、覆盖率
set -eu
echo "=== CI Integration Tests 开始 ==="
# --- 安装依赖 ---
echo ""
echo "=== 安装 Python 依赖 ==="
python3 -m pip install -q -r requirements-base.txt
python3 -m pip install -q -r requirements.txt
python3 -m pip install -q -r requirements-dev.txt
python3 -m pip install -q pytest-rerunfailures
pytest --version
# --- 安装 ffmpeg ---
echo ""
echo "=== 安装 ffmpeg ==="
bash scripts/ci/step_install_ffmpeg.sh
# --- 启动 Redis ---
echo ""
echo "=== 启动 Redis ==="
REDIS_CONTAINER="ci-redis-${GITHUB_RUN_ID:-$$}"
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
docker run -d --name "$REDIS_CONTAINER" \
-P \
--health-cmd "redis-cli ping" \
--health-interval 2s \
--health-timeout 2s \
--health-retries 10 \
redis:7-alpine
# 等待Redis healthy + 网络就绪
for i in $(seq 1 30); do
HEALTH=$(docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" 2>/dev/null || echo "starting")
MAPPED_PORT=$(docker port "$REDIS_CONTAINER" 6379/tcp 2>/dev/null | cut -d: -f2 | grep -E '^[0-9]+$' || true)
CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$REDIS_CONTAINER" 2>/dev/null | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || true)
if echo "$HEALTH" | grep -q healthy && { [ -n "$MAPPED_PORT" ] || [ -n "$CONTAINER_IP" ]; }; then
echo "Redis ready - health: $HEALTH, mapped port: ${MAPPED_PORT:-N/A}, container IP: ${CONTAINER_IP:-N/A}"
break
fi
echo "Waiting for Redis + network... ($i/30) health=$HEALTH port=${MAPPED_PORT:-none} ip=${CONTAINER_IP:-none}"
sleep 2
done
# 双模式探测
REDIS_HOST=""
REDIS_PORT=""
if [ -n "$MAPPED_PORT" ]; then
echo "Trying Redis mapped port mode (127.0.0.1:$MAPPED_PORT)..."
for i in $(seq 1 15); do
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('127.0.0.1', $MAPPED_PORT)); s.close()" 2>/dev/null; then
REDIS_HOST="127.0.0.1"
REDIS_PORT="$MAPPED_PORT"
echo "✅ Redis mapped port mode works"
break
fi
sleep 2
done
fi
if [ -z "$REDIS_HOST" ] && [ -n "$CONTAINER_IP" ]; then
echo "Trying Redis container IP mode ($CONTAINER_IP:6379)..."
for i in $(seq 1 15); do
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$CONTAINER_IP', 6379)); s.close()" 2>/dev/null; then
REDIS_HOST="$CONTAINER_IP"
REDIS_PORT="6379"
echo "✅ Redis container IP mode works"
break
fi
sleep 2
done
fi
if [ -z "$REDIS_HOST" ]; then
echo "ERROR: 无法连接到Redis容器 (mapped=${MAPPED_PORT:-none}, ip=${CONTAINER_IP:-none})"
exit 1
fi
echo "Using Redis: $REDIS_HOST:$REDIS_PORT"
export REDIS_URL="redis://$REDIS_HOST:$REDIS_PORT/0"
# --- 启动 PostgreSQL ---
echo ""
echo "=== 启动 PostgreSQL ==="
PG_CONTAINER="ci-pg-${GITHUB_RUN_ID:-$$}"
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
docker run -d --name "$PG_CONTAINER" \
--shm-size=256m \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=xiaoxia_saas \
-P \
--health-cmd "pg_isready -U postgres" \
--health-interval 5s \
--health-timeout 5s \
--health-retries 12 \
postgres:16
# 等待PG healthy + 网络就绪
for i in $(seq 1 40); do
HEALTH=$(docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null || echo "starting")
MAPPED_PORT=$(docker port "$PG_CONTAINER" 5432/tcp 2>/dev/null | cut -d: -f2 | grep -E '^[0-9]+$' || true)
CONTAINER_IP=$(docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$PG_CONTAINER" 2>/dev/null | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || true)
if echo "$HEALTH" | grep -q healthy && { [ -n "$MAPPED_PORT" ] || [ -n "$CONTAINER_IP" ]; }; then
echo "PostgreSQL ready - health: $HEALTH, mapped port: ${MAPPED_PORT:-N/A}, container IP: ${CONTAINER_IP:-N/A}"
break
fi
echo "Waiting for PostgreSQL + network... ($i/40) health=$HEALTH port=${MAPPED_PORT:-none} ip=${CONTAINER_IP:-none}"
sleep 2
done
# 双模式探测
PG_HOST=""
PG_PORT=""
if [ -n "$MAPPED_PORT" ]; then
echo "Trying PG mapped port mode (127.0.0.1:$MAPPED_PORT)..."
for i in $(seq 1 15); do
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('127.0.0.1', $MAPPED_PORT)); s.close()" 2>/dev/null; then
PG_HOST="127.0.0.1"
PG_PORT="$MAPPED_PORT"
echo "✅ PG mapped port mode works"
break
fi
sleep 2
done
fi
if [ -z "$PG_HOST" ] && [ -n "$CONTAINER_IP" ]; then
echo "Trying PG container IP mode ($CONTAINER_IP:5432)..."
for i in $(seq 1 15); do
if python3 -c "import socket; s=socket.socket(); s.settimeout(2); s.connect(('$CONTAINER_IP', 5432)); s.close()" 2>/dev/null; then
PG_HOST="$CONTAINER_IP"
PG_PORT="5432"
echo "✅ PG container IP mode works"
break
fi
sleep 2
done
fi
if [ -z "$PG_HOST" ]; then
echo "ERROR: 无法连接到PostgreSQL容器 (mapped=${MAPPED_PORT:-none}, ip=${CONTAINER_IP:-none})"
echo "当前runner的Docker网络可能存在配置异常,请联系运维检查runner环境"
exit 1
fi
echo "Using PostgreSQL: $PG_HOST:$PG_PORT"
export DATABASE_URL="postgresql+psycopg://postgres:postgres@$PG_HOST:$PG_PORT/xiaoxia_saas"
# --- 执行迁移 ---
echo ""
echo "=== 执行 Alembic 迁移 ==="
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m alembic upgrade head
echo "✅ 迁移完成"
# --- 运行集成测试 ---
echo ""
echo "=== 运行集成测试 ==="
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m coverage run \
--source=apps/api/app,packages \
--omit="*/migrations/*,*/tests/*,*/test_*.py,*/site-packages/*" \
--branch \
-m pytest tests/integration -q --timeout=60 -x --reruns 2 --reruns-delay 1 -m "not performance"
python3 -m coverage report --show-missing
python3 -m coverage xml -o coverage.xml
python3 -m coverage report --fail-under=40 > /dev/null
echo "✅ 集成测试通过"
# --- API 性能基线测试(仅告警) ---
echo ""
echo "=== API 性能基线测试(仅告警) ==="
set +e
PERF_OUTPUT=$(mktemp)
PYTHONPATH="$PWD/apps/api:$PWD" python3 -m pytest tests/integration/test_api_performance.py \
-v --timeout=120 -p no:cacheprovider 2>&1 | tee "$PERF_OUTPUT"
echo ""
echo "=== 性能测试摘要 ==="
grep "PERF_STATS:" "$PERF_OUTPUT" || echo "PERF_STATS: 未找到统计数据"
grep "PERF_RESULT:" "$PERF_OUTPUT" || echo "PERF_RESULT: 未找到详细结果"
TOTAL=$(grep -c "PERF_RESULT:" "$PERF_OUTPUT" || echo 0)
PASSED=$(grep "PERF_RESULT: PASS" "$PERF_OUTPUT" | wc -l)
FAILED=$(grep "PERF_RESULT: FAIL" "$PERF_OUTPUT" | wc -l)
echo ""
echo "性能测试结果: $PASSED/$TOTAL 通过, $FAILED 未达标"
if [ "$FAILED" -gt 0 ]; then
echo ""
echo "⚠️ 警告: $FAILED 个接口性能未达标"
fi
rm -f "$PERF_OUTPUT"
set -e
# --- 清理 ---
echo ""
echo "=== 清理容器 ==="
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
docker rm -f "$REDIS_CONTAINER" 2>/dev/null || true
echo "✅ 清理完成"
# --- 覆盖率汇总 ---
echo ""
echo "=== 覆盖率汇总 ==="
set +e
python3 scripts/ci_coverage_summary.py
set -e
echo ""
echo "=== CI Integration Tests 全部通过 ✅ ==="