name: 'Kubevela K8s Upgrade Multicluster E2E Test'
description: 'Runs Kubevela Kubernetes upgrade multicluster end-to-end tests, uploads coverage, and collects diagnostics on failure.'
author: 'viskumar_gwre'

inputs:
  codecov-token:
    description: 'Codecov token for uploading coverage reports'
    required: false
    default: ''
  codecov-enable:
    description: 'Enable codecov coverage upload'
    required: false
    default: 'false'

runs:
  using: 'composite'
  steps:
    # ========================================================================
    # Environment Setup
    # ========================================================================
    - name: Configure environment setup
      uses: ./.github/actions/env-setup

    # ========================================================================
    # E2E Test Execution
    # ========================================================================
    - name: Prepare e2e test environment
      shell: bash
      run: |
        # Build CLI tools and prepare test environment
        echo "Building KubeVela CLI..."
        make vela-cli

        echo "Cleaning up previous test artifacts..."
        make e2e-cleanup

        echo "Setting up core authentication for e2e tests..."
        make e2e-setup-core-auth

    - name: Execute multicluster upgrade e2e tests
      shell: bash
      run: |
        # Add built CLI to PATH and run multicluster tests
        export PATH=$(pwd)/bin:$PATH

        echo "Running e2e multicluster upgrade tests..."
        make e2e-multicluster-test

    - name: Upload coverage report
      if: ${{ inputs.codecov-enable == 'true' }}
      uses: codecov/codecov-action@18283e04ce6e62d37312384ff67231eb8fd56d24
      with:
        token: ${{ inputs.codecov-token }}
        files: /tmp/e2e-profile.out,/tmp/e2e_multicluster_test.out
        flags: e2e-multicluster-test
        name: codecov-umbrella

    # ========================================================================
    # Failure Diagnostics
    # ========================================================================
    - name: Collect failure diagnostics
      if: failure()
      shell: bash
      run: |
        echo "=== FAILURE DIAGNOSTICS ==="
        echo "Collecting diagnostic information for debugging..."

        echo "--- Cluster Status ---"
        kubectl get nodes -o wide || true
        kubectl get pods -A || true

        echo "--- KubeVela System Logs ---"
        kubectl logs -n vela-system -l app.kubernetes.io/name=vela-core --tail=100 || true

        echo "--- Recent Events ---"
        kubectl get events -A --sort-by='.lastTimestamp' --field-selector type!=Normal || true