# 测试环境文档 ## 概述 测试环境是用于执行自动化测试的独立环境,与生产环境隔离,确保测试的稳定性和可重复性。 ## 架构 测试环境采用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+(用于测试数据管理) ### 安装 ```bash # 克隆项目 git clone cd everything-is-suitable # 配置环境变量 cp .env.test.example .env.test # 根据需要修改 .env.test 中的配置 # 部署测试环境 ./scripts/deploy-test-env.sh ``` ### 验证部署 ```bash # 检查服务状态 ./scripts/setup-test-env.sh status # 检查服务健康 ./scripts/setup-test-env.sh health ``` ## 使用指南 ### 启动测试环境 ```bash ./scripts/setup-test-env.sh start ``` ### 停止测试环境 ```bash ./scripts/setup-test-env.sh stop ``` ### 重启测试环境 ```bash ./scripts/setup-test-env.sh restart ``` ### 查看服务状态 ```bash ./scripts/setup-test-env.sh status ``` ### 查看服务日志 ```bash # 查看所有服务日志 ./scripts/setup-test-env.sh logs # 查看特定服务日志 ./scripts/setup-test-env.sh logs test-api-gateway ``` ### 清理测试环境 ```bash # 停止并删除容器 ./scripts/setup-test-env.sh clean # 清理所有数据(包括数据卷) docker-compose -f docker-compose.test-new.yml --env-file .env.test down -v ``` ## 测试数据管理 ### 生成测试数据 ```bash python3 scripts/generate-test-data.py ``` ### 清理测试数据 ```bash python3 scripts/clean-test-data.py ``` ### 重置测试数据 ```bash python3 scripts/reset-test-data.py ``` ### 查看测试数据状态 ```bash 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 连接示例: ```bash psql -h localhost -p 5433 -U test_user -d everything_test ``` ### Redis - **Host**: localhost - **Port**: 6380 连接示例: ```bash redis-cli -p 6380 ``` ### Prometheus - **URL**: http://localhost:9091 - **目标**: http://localhost:9091/targets ### 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` **访问**: http://localhost:3001 ### 告警 测试环境配置了以下告警: - **服务宕机告警**: API Gateway、Admin Backend、PostgreSQL、Redis - **错误率告警**: 5xx错误率超过10% - **延迟告警**: P95延迟超过2秒 - **资源告警**: PostgreSQL连接数、Redis内存使用率 ## CI/CD集成 ### Woodpecker CI 测试环境已集成到Woodpecker CI中,包括以下步骤: 1. **test-env-setup**: 启动测试环境 2. **integration-test**: 运行集成测试 3. **test-env-cleanup**: 清理测试环境 **配置文件**: `.woodpecker.yml` ### GitHub Actions 测试环境已集成到GitHub Actions中,包括以下任务: 1. **setup-test-env**: 设置测试环境 2. **integration-test**: 运行集成测试 3. **cleanup-test-env**: 清理测试环境 **配置文件**: `.github/workflows/test-env-ci.yml` ## 故障排查 ### 服务无法启动 1. 检查端口是否被占用: ```bash lsof -i :5433 lsof -i :6380 lsof -i :8081 lsof -i :8082 ``` 2. 查看服务日志: ```bash ./scripts/setup-test-env logs ``` 3. 检查Docker资源: ```bash docker system df docker system prune -f ``` ### 数据库连接失败 1. 检查数据库是否启动: ```bash docker-compose -f docker-compose.test-new.yml --env-file .env.test ps test-postgres ``` 2. 检查数据库健康状态: ```bash docker-compose -f docker-compose.test-new.yml --env-file .env.test exec test-postgres pg_isready -U test_user ``` 3. 查看数据库日志: ```bash ./scripts/setup-test-env logs test-postgres ``` ### 测试数据生成失败 1. 检查数据库连接: ```bash python3 test-data-manager/main.py status ``` 2. 检查数据库表结构: ```bash 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" ``` 3. 查看错误日志: ```bash python3 scripts/generate-test-data.py ``` ## 最佳实践 ### 1. 环境隔离 - 测试环境与生产环境完全隔离 - 使用独立的数据库和缓存 - 使用独立的端口和配置 ### 2. 数据管理 - 每次测试前重置测试数据 - 使用固定的测试数据集 - 避免硬编码测试数据 ### 3. 资源清理 - 测试完成后及时清理测试数据 - 定期清理Docker资源 - 监控磁盘空间使用 ### 4. 监控告警 - 配置合理的告警阈值 - 及时响应告警信息 - 定期检查监控数据 ### 5. CI/CD集成 - 在CI/CD中自动启动和清理测试环境 - 确保测试环境的稳定性 - 记录测试环境日志 ## 配置说明 ### 环境变量 测试环境配置文件:`.env.test` ```bash # 数据库配置 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可视化 ## 扩展阅读 - [Docker Compose文档](https://docs.docker.com/compose/) - [PostgreSQL文档](https://www.postgresql.org/docs/) - [Redis文档](https://redis.io/documentation) - [Prometheus文档](https://prometheus.io/docs/) - [Grafana文档](https://grafana.com/docs/) ## 联系方式 如有问题或建议,请联系开发团队。