#!/bin/bash # DMS合规性测试工具 Docker测试脚本 set -e # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color IMAGE_NAME="dms-compliance-tool" CONTAINER_NAME="dms-compliance-tool-test" print_message() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_step() { echo -e "${BLUE}[STEP]${NC} $1" } # 清理测试环境 cleanup() { print_step "清理测试环境..." docker stop $CONTAINER_NAME 2>/dev/null || true docker rm $CONTAINER_NAME 2>/dev/null || true } # 构建测试镜像 build_test_image() { print_step "构建测试镜像..." docker build -f Dockerfile.service -t $IMAGE_NAME:test . if [ $? -eq 0 ]; then print_message "测试镜像构建成功" else print_error "测试镜像构建失败" exit 1 fi } # 启动测试容器 start_test_container() { print_step "启动测试容器..." docker run -d \ --name $CONTAINER_NAME \ -p 5052:5050 \ -p 5053:5051 \ -e FLASK_ENV=development \ $IMAGE_NAME:test if [ $? -eq 0 ]; then print_message "测试容器启动成功" else print_error "测试容器启动失败" exit 1 fi } # 等待服务启动 wait_for_service() { print_step "等待服务启动..." for i in {1..30}; do if curl -s http://localhost:5052/ > /dev/null 2>&1; then print_message "API服务已启动" break fi echo -n "." sleep 2 done for i in {1..30}; do if curl -s http://localhost:5053/ > /dev/null 2>&1; then print_message "历史查看器服务已启动" return 0 fi echo -n "." sleep 2 done print_error "服务启动超时" docker logs $CONTAINER_NAME exit 1 } # 测试基本功能 test_basic_functionality() { print_step "测试基本功能..." # 测试API服务首页 if curl -s http://localhost:5052/ | grep -q "DMS"; then print_message "✅ API服务首页访问正常" else print_error "❌ API服务首页访问失败" return 1 fi # 测试历史查看器首页 if curl -s http://localhost:5053/ > /dev/null 2>&1; then print_message "✅ 历史查看器服务访问正常" else print_error "❌ 历史查看器服务访问失败" return 1 fi # 测试健康检查(如果有的话) if curl -s http://localhost:5052/health > /dev/null 2>&1; then print_message "✅ API服务健康检查正常" else print_warning "⚠️ API服务健康检查端点不存在或失败" fi } # 测试容器内部功能 test_internal_functionality() { print_step "测试容器内部功能..." # 测试Python环境 if docker exec $CONTAINER_NAME python --version; then print_message "✅ Python环境正常" else print_error "❌ Python环境异常" return 1 fi # 测试依赖包 if docker exec $CONTAINER_NAME python -c "import flask, pydantic, reportlab; print('Dependencies OK')"; then print_message "✅ 依赖包正常" else print_error "❌ 依赖包异常" return 1 fi # 测试目录结构 if docker exec $CONTAINER_NAME ls -la /app/test_reports /app/uploads > /dev/null 2>&1; then print_message "✅ 目录结构正常" else print_error "❌ 目录结构异常" return 1 fi } # 测试文件权限 test_file_permissions() { print_step "测试文件权限..." # 测试写入权限 if docker exec $CONTAINER_NAME touch /app/test_reports/test_file.txt; then print_message "✅ 文件写入权限正常" docker exec $CONTAINER_NAME rm /app/test_reports/test_file.txt else print_error "❌ 文件写入权限异常" return 1 fi } # 显示容器信息 show_container_info() { print_step "显示容器信息..." echo "容器状态:" docker ps -f name=$CONTAINER_NAME echo -e "\n容器资源使用:" docker stats $CONTAINER_NAME --no-stream echo -e "\n容器日志 (最后10行):" docker logs --tail 10 $CONTAINER_NAME } # 主测试函数 run_tests() { print_message "开始Docker测试..." cleanup build_test_image start_test_container wait_for_service # 运行测试 local test_passed=0 local test_total=0 # 基本功能测试 ((test_total++)) if test_basic_functionality; then ((test_passed++)) fi # 内部功能测试 ((test_total++)) if test_internal_functionality; then ((test_passed++)) fi # 文件权限测试 ((test_total++)) if test_file_permissions; then ((test_passed++)) fi # 显示测试结果 echo -e "\n${BLUE}=== 测试结果 ===${NC}" echo -e "通过: ${GREEN}$test_passed${NC}/$test_total" if [ $test_passed -eq $test_total ]; then print_message "🎉 所有测试通过!" show_container_info return 0 else print_error "❌ 部分测试失败" show_container_info return 1 fi } # 显示帮助 show_help() { echo "用法: $0 [选项]" echo "" echo "选项:" echo " --cleanup-only 仅清理测试环境" echo " --build-only 仅构建测试镜像" echo " --help 显示帮助信息" echo "" } # 主函数 main() { case "$1" in --cleanup-only) cleanup print_message "清理完成" ;; --build-only) build_test_image print_message "构建完成" ;; --help) show_help ;; "") if run_tests; then print_message "测试完成,容器仍在运行" print_message "API服务: http://localhost:5052" print_message "历史查看器: http://localhost:5053" print_message "使用 'docker stop $CONTAINER_NAME' 停止测试容器" exit 0 else cleanup exit 1 fi ;; *) print_error "未知选项: $1" show_help exit 1 ;; esac } # 捕获退出信号,确保清理 trap cleanup EXIT # 执行主函数 main "$@"