# Woodpecker CI/CD - 测试套件专用流水线
# 用途: 执行系统性的测试套件(E2E、UAT、性能、安全测试)

pipeline:
  # 环境准备阶段
  prepare:
    image: python:3.11-slim
    commands:
      - echo "准备测试环境..."
      - cd test-suite
      - pip install -r requirements.txt
      - echo "✅ 测试环境准备完成"
    when:
      event: [push, pull_request]
    
  # 集成测试阶段
  test-integration:
    image: python:3.11-slim
    commands:
      - echo "开始集成测试..."
      - cd test-suite
      - pytest tests/integration/ -v --tb=short --cov=. --cov-report=xml --alluredir=allure-results/integration
      - echo "✅ 集成测试完成"
    when:
      event: [push, pull_request]
    
  # E2E测试阶段
  test-e2e:
    image: python:3.11-slim
    commands:
      - echo "开始E2E测试..."
      - cd test-suite
      - pytest tests/e2e/ -v --tb=short --cov=. --cov-report=xml --alluredir=allure-results/e2e -m "e2e"
      - echo "✅ E2E测试完成"
    when:
      event: [push, pull_request]
    
  # UAT验收测试阶段
  test-uat:
    image: python:3.11-slim
    commands:
      - echo "开始UAT验收测试..."
      - cd test-suite
      - pytest tests/uat/ -v --tb=short --cov=. --cov-report=xml --alluredir=allure-results/uat -m "uat"
      - echo "✅ UAT测试完成"
    when:
      event: [push, pull_request]
    
  # 性能测试阶段
  test-performance:
    image: python:3.11-slim
    commands:
      - echo "开始性能测试..."
      - cd test-suite
      - pytest tests/performance/ -v --tb=short --cov=. --cov-report=xml --alluredir=allure-results/performance -m "performance"
      - echo "✅ 性能测试完成"
    when:
      event: [push]
      branch: [main, develop]
    
  # 安全测试阶段
  test-security:
    image: python:3.11-slim
    commands:
      - echo "开始安全测试..."
      - cd test-suite
      - pytest tests/security/ -v --tb=short --cov=. --cov-report=xml --alluredir=allure-results/security -m "security"
      - echo "✅ 安全测试完成"
    when:
      event: [pull_request]
    
  # 测试报告生成
  generate-reports:
    image: python:3.11-slim
    commands:
      - echo "生成测试报告..."
      - cd test-suite
      - mkdir -p reports
      - cp -r htmlcov reports/
      - cp -r allure-results reports/
      - echo "✅ 测试报告生成完成"
    when:
      event: [push, pull_request]
      status: [success, failure]
    
  # 质量门禁检查
  quality-gates:
    image: python:3.11-slim
    commands:
      - echo "开始质量门禁检查..."
      - cd test-suite
      - |
        # 检查测试覆盖率
        if [ -f coverage.xml ]; then
          coverage_percent=$(python -c "import xml.etree.ElementTree as ET; tree = ET.parse('coverage.xml'); root = tree.getroot(); print(float(root.attrib['line-rate']) * 100)")
          echo "测试覆盖率: ${coverage_percent}%"
          if (( $(echo "$coverage_percent < 80" | bc -l) )); then
            echo "❌ 测试覆盖率不足80%"
            exit 1
          fi
        fi
      - echo "✅ 测试覆盖率检查通过"
      - echo "✅ 所有测试用例通过"
      - echo "✅ 质量门禁检查通过"
    when:
      event: [pull_request]

# 工作流配置
workflows:
  # 完整测试工作流(主分支)
  full-test:
    when:
      event: [push]
      branch: [main, develop]
    steps:
      - prepare
      - test-integration
      - test-e2e
      - test-uat
      - test-performance
      - generate-reports
  
  # 快速测试工作流(Pull Request)
  quick-test:
    when:
      event: [pull_request]
    steps:
      - prepare
      - test-integration
      - test-e2e
      - test-uat
      - test-security
      - quality-gates
      - generate-reports

# 通知配置
notifications:
  slack:
    webhook: ${SLACK_WEBHOOK_URL}
    channel: '#test-reports'
    on_success: true
    on_failure: true
    on_start: false

# 环境变量
environment:
  - PYTHONPATH=/woodpecker/src/github.com/novalon/novalon-manage-system/test-suite
  - TEST_ENV=ci

# 缓存配置
cache:
  paths:
    - test-suite/.pytest_cache
    - test-suite/htmlcov
    - test-suite/allure-results
