08ea5fbe98
添加用户管理视图、API和状态管理文件
6.9 KiB
6.9 KiB
测试环境文档
概述
测试环境是用于执行自动化测试的独立环境,与生产环境隔离,确保测试的稳定性和可重复性。
架构
测试环境采用Docker容器化技术,包含以下服务:
- PostgreSQL: 测试数据库(端口:5433)
- Redis: 测试缓存(端口:6380)
- API Gateway: API网关服务(端口:8081)
- Admin Backend: Admin后端服务(端口:8082)
- Test Data Manager: 测试数据管理工具
- Prometheus: 监控指标收集(端口:9091)
- Grafana: 监控可视化(端口:3001)
快速开始
前置要求
- Docker 20.10+
- Docker Compose 2.0+
- Python 3.11+(用于测试数据管理)
安装
# 克隆项目
git clone <repository-url>
cd everything-is-suitable
# 配置环境变量
cp .env.test.example .env.test
# 根据需要修改 .env.test 中的配置
# 部署测试环境
./scripts/deploy-test-env.sh
验证部署
# 检查服务状态
./scripts/setup-test-env.sh status
# 检查服务健康
./scripts/setup-test-env.sh health
使用指南
启动测试环境
./scripts/setup-test-env.sh start
停止测试环境
./scripts/setup-test-env.sh stop
重启测试环境
./scripts/setup-test-env.sh restart
查看服务状态
./scripts/setup-test-env.sh status
查看服务日志
# 查看所有服务日志
./scripts/setup-test-env.sh logs
# 查看特定服务日志
./scripts/setup-test-env.sh logs test-api-gateway
清理测试环境
# 停止并删除容器
./scripts/setup-test-env.sh clean
# 清理所有数据(包括数据卷)
docker-compose -f docker-compose.test-new.yml --env-file .env.test down -v
测试数据管理
生成测试数据
python3 scripts/generate-test-data.py
清理测试数据
python3 scripts/clean-test-data.py
重置测试数据
python3 scripts/reset-test-data.py
查看测试数据状态
python3 test-data-manager/main.py status
服务访问
API Gateway
- URL: http://localhost:8081
- 健康检查: http://localhost:8081/actuator/health
- Prometheus指标: http://localhost:8081/actuator/prometheus
Admin Backend
- URL: http://localhost:8082
- 健康检查: http://localhost:8082/actuator/health
- Prometheus指标: http://localhost:8082/actuator/prometheus
PostgreSQL
- Host: localhost
- Port: 5433
- Database: everything_test
- Username: test_user
- Password: test_password
连接示例:
psql -h localhost -p 5433 -U test_user -d everything_test
Redis
- Host: localhost
- Port: 6380
连接示例:
redis-cli -p 6380
Prometheus
Grafana
- URL: http://localhost:3001
- 用户名: admin
- 密码: admin
监控
Prometheus
Prometheus用于收集测试环境的监控指标。
配置文件: test-monitoring/prometheus/prometheus.yml
告警规则: test-monitoring/prometheus/alerting_rules.yml
Grafana
Grafana用于可视化监控数据。
数据源配置: test-monitoring/grafana/provisioning/datasources/prometheus.yml
告警
测试环境配置了以下告警:
- 服务宕机告警: API Gateway、Admin Backend、PostgreSQL、Redis
- 错误率告警: 5xx错误率超过10%
- 延迟告警: P95延迟超过2秒
- 资源告警: PostgreSQL连接数、Redis内存使用率
CI/CD集成
Woodpecker CI
测试环境已集成到Woodpecker CI中,包括以下步骤:
- test-env-setup: 启动测试环境
- integration-test: 运行集成测试
- test-env-cleanup: 清理测试环境
配置文件: .woodpecker.yml
GitHub Actions
测试环境已集成到GitHub Actions中,包括以下任务:
- setup-test-env: 设置测试环境
- integration-test: 运行集成测试
- cleanup-test-env: 清理测试环境
配置文件: .github/workflows/test-env-ci.yml
故障排查
服务无法启动
- 检查端口是否被占用:
lsof -i :5433
lsof -i :6380
lsof -i :8081
lsof -i :8082
- 查看服务日志:
./scripts/setup-test-env logs <service-name>
- 检查Docker资源:
docker system df
docker system prune -f
数据库连接失败
- 检查数据库是否启动:
docker-compose -f docker-compose.test-new.yml --env-file .env.test ps test-postgres
- 检查数据库健康状态:
docker-compose -f docker-compose.test-new.yml --env-file .env.test exec test-postgres pg_isready -U test_user
- 查看数据库日志:
./scripts/setup-test-env logs test-postgres
测试数据生成失败
- 检查数据库连接:
python3 test-data-manager/main.py status
- 检查数据库表结构:
docker-compose -f docker-compose.test-new.yml --env-file .env.test exec -T test-postgres psql -U test_user -d everything_test -c "\dt"
- 查看错误日志:
python3 scripts/generate-test-data.py
最佳实践
1. 环境隔离
- 测试环境与生产环境完全隔离
- 使用独立的数据库和缓存
- 使用独立的端口和配置
2. 数据管理
- 每次测试前重置测试数据
- 使用固定的测试数据集
- 避免硬编码测试数据
3. 资源清理
- 测试完成后及时清理测试数据
- 定期清理Docker资源
- 监控磁盘空间使用
4. 监控告警
- 配置合理的告警阈值
- 及时响应告警信息
- 定期检查监控数据
5. CI/CD集成
- 在CI/CD中自动启动和清理测试环境
- 确保测试环境的稳定性
- 记录测试环境日志
配置说明
环境变量
测试环境配置文件:.env.test
# 数据库配置
TEST_DB_NAME=everything_test
TEST_DB_USER=test_user
TEST_DB_PASSWORD=test_password
TEST_DB_PORT=5433
# Redis配置
TEST_REDIS_PORT=6380
# API服务配置
TEST_API_PORT=8081
TEST_ADMIN_PORT=8082
# 监控配置
TEST_PROMETHEUS_PORT=9091
TEST_GRAFANA_PORT=3001
# Grafana配置
GRAFANA_ADMIN_USER=admin
GRAFANA_ADMIN_PASSWORD=admin
Docker Compose配置
测试环境配置文件:docker-compose.test-new.yml
主要服务:
test-postgres: PostgreSQL数据库test-redis: Redis缓存test-api-gateway: API网关服务test-admin-backend: Admin后端服务test-data-manager: 测试数据管理工具test-prometheus: Prometheus监控test-grafana: Grafana可视化
扩展阅读
联系方式
如有问题或建议,请联系开发团队。