Files
xiaoxia-saas/scripts/ci/run_integration_tests.sh
T
CI Bot 8b66169137
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 / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (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 / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (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 13s
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 19s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 4m12s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 4m18s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 4m40s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 4m41s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m45s
CI/CD Pipeline / Integration Tests (pull_request) Failing after 20s
AI Code Review / AI Code Review (pull_request) Successful in 5m4s
fix(ci): 多系统兼容的PG/Redis安装方式(apk/apt/yum/dnf),本地优先Docker fallback
runner容器是Alpine系统,没有apt-get,导致127错误。
改为多包管理器兼容:apk(Alpine) / apt-get(Debian/Ubuntu) / yum/dnf(RHEL/CentOS)
优先本地安装,失败则fallback到Docker方式。

这样在任何runner环境下都能最大限度保证PG和Redis可用。
2026-07-19 23:21:00 +08:00

199 lines
7.4 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 ==="
# 兼容多种包管理器
install_redis() {
if command -v apk > /dev/null 2>&1; then
apk add --no-cache redis > /dev/null 2>&1
elif command -v apt-get > /dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq > /dev/null 2>&1
apt-get install -y -qq redis-server > /dev/null 2>&1
elif command -v yum > /dev/null 2>&1; then
yum install -y -q redis > /dev/null 2>&1
elif command -v dnf > /dev/null 2>&1; then
dnf install -y -q redis > /dev/null 2>&1
else
return 1
fi
}
# 如果redis-cli不存在才安装
if ! command -v redis-cli > /dev/null 2>&1; then
install_redis || echo "Warning: Redis installation failed, trying Docker..."
fi
# 优先用本地redis-server,失败再fallback
if command -v redis-server > /dev/null 2>&1; then
redis-server --daemonize yes --port 6379 2>/dev/null || true
sleep 1
for i in $(seq 1 10); do
if redis-cli ping 2>/dev/null | grep -q PONG; then
echo "Redis is ready on 127.0.0.1:6379"
break
fi
sleep 1
done
fi
# 如果本地redis连不上,fallback到Docker
if ! redis-cli ping 2>/dev/null | grep -q PONG; then
echo "Local Redis not available, falling back to Docker..."
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
for i in $(seq 1 20); do
if docker inspect --format='{{.State.Health.Status}}' "$REDIS_CONTAINER" 2>/dev/null | grep -q healthy; then
break
fi
sleep 2
done
REDIS_PORT=$(docker port "$REDIS_CONTAINER" 6379/tcp | cut -d: -f2)
export REDIS_URL="redis://127.0.0.1:$REDIS_PORT/0"
echo "Redis (Docker) ready on 127.0.0.1:$REDIS_PORT"
else
export REDIS_URL="redis://127.0.0.1:6379/0"
fi
# --- 安装并启动 PostgreSQL ---
echo ""
echo "=== 安装并启动 PostgreSQL ==="
install_pg_local() {
if command -v pg_isready > /dev/null 2>&1; then
return 0
fi
if command -v apk > /dev/null 2>&1; then
apk add --no-cache postgresql postgresql-client > /dev/null 2>&1
# Alpine需要手动初始化
mkdir -p /var/lib/postgresql/data
chown postgres:postgres /var/lib/postgresql/data
su - postgres -c "initdb -D /var/lib/postgresql/data" > /dev/null 2>&1
su - postgres -c "pg_ctl -D /var/lib/postgresql/data -l /var/lib/postgresql/logfile start" > /dev/null 2>&1
sleep 2
su - postgres -c "psql -c "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';"" > /dev/null 2>&1
su - postgres -c "psql -c "CREATE DATABASE xiaoxia_saas OWNER postgres;"" > /dev/null 2>&1
elif command -v apt-get > /dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq > /dev/null 2>&1
apt-get install -y -qq postgresql postgresql-client > /dev/null 2>&1
PG_VERSION=$(pg_lsclusters -h | head -1 | awk '{print $1}')
PG_CLUSTER=$(pg_lsclusters -h | head -1 | awk '{print $2}')
PG_HBA="/etc/postgresql/$PG_VERSION/$PG_CLUSTER/pg_hba.conf"
sed -i "s/local.*all.*all.*peer/local all all trust/" "$PG_HBA" 2>/dev/null || true
sed -i "s/host.*all.*all.*127.0.0.1.*scram-sha-256/host all all 127.0.0.1/32 trust/" "$PG_HBA" 2>/dev/null || true
pg_ctlcluster "$PG_VERSION" "$PG_CLUSTER" start 2>/dev/null || true
sleep 2
su - postgres -c "psql -c "CREATE USER postgres WITH SUPERUSER PASSWORD 'postgres';"" 2>/dev/null || true
su - postgres -c "psql -c "CREATE DATABASE xiaoxia_saas OWNER postgres;"" 2>/dev/null || true
else
return 1
fi
}
PG_LOCAL_OK=0
if install_pg_local; then
for i in $(seq 1 15); do
if pg_isready -U postgres -h 127.0.0.1 -p 5432 2>/dev/null | grep -q "accepting connections"; then
echo "PostgreSQL is ready on 127.0.0.1:5432 (local install)"
PG_LOCAL_OK=1
break
fi
sleep 2
done
fi
# 本地安装失败则fallback到Docker
if [ "$PG_LOCAL_OK" != "1" ]; then
echo "Local PostgreSQL not available, falling back to Docker..."
PG_CONTAINER="ci-pg-${GITHUB_RUN_ID:-$$}"
docker rm -f "$PG_CONTAINER" 2>/dev/null || true
docker run -d --name "$PG_CONTAINER" -P --shm-size=256m -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=xiaoxia_saas --health-cmd "pg_isready -U postgres" --health-interval 3s --health-timeout 3s --health-retries 20 postgres:16-alpine
for i in $(seq 1 30); do
if docker inspect --format='{{.State.Health.Status}}' "$PG_CONTAINER" 2>/dev/null | grep -q healthy; then
break
fi
sleep 2
done
PG_PORT=$(docker port "$PG_CONTAINER" 5432/tcp | cut -d: -f2)
echo "PostgreSQL (Docker) ready on 127.0.0.1:$PG_PORT"
export DATABASE_URL="postgresql+psycopg://postgres:postgres@127.0.0.1:$PG_PORT/xiaoxia_saas"
else
export DATABASE_URL="postgresql+psycopg://postgres:postgres@127.0.0.1:5432/xiaoxia_saas"
fi
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 "=== 停止服务 ==="
pg_ctlcluster $PG_VERSION $PG_CLUSTER stop 2>/dev/null || true
redis-cli shutdown 2>/dev/null || true
echo "✅ 清理完成"
# --- 覆盖率汇总 ---
echo ""
echo "=== 覆盖率汇总 ==="
set +e
python3 scripts/ci_coverage_summary.py
set -e
echo ""
echo "=== CI Integration Tests 全部通过 ✅ ==="