name: 'Dynamo Graph Deployment Test'
description: 'Deploy a DynamoGraphDeployment to Kubernetes, validate it serves requests, and cleanup'

inputs:
  # Kubernetes Configuration
  kubeconfig_base64:
    description: 'Base64-encoded kubeconfig for cluster access'
    required: true
  namespace:
    description: 'Kubernetes namespace for deployment'
    required: true
  registry:
    description: 'Container registry hostname'
    required: true
  operator_tag:
    description: 'Operator image tag (default: main-operator)'
    required: false
    default: 'main-operator'
  hf_token:
    description: 'HuggingFace token for model access'
    required: false
    default: ''
  dockerhub_username:
    description: 'Docker Hub username for image pull secrets. Required for rerun self-bootstrap if private images are used.'
    required: false
    default: ''
  dockerhub_password:
    description: 'Docker Hub password for image pull secrets. Required for rerun self-bootstrap if private images are used.'
    required: false
    default: ''

  framework:
    description: 'Framework name (vllm, sglang, trtllm)'
    required: true
  profile:
    description: 'Deployment profile (e.g., disagg_router, agg)'
    required: true
  image:
    description: 'Full container image reference for the framework runtime'
    required: true
  platform_arch:
    description: 'Platform architecture (amd64, arm64)'
    required: false
    default: 'amd64'

runs:
  using: "composite"
  steps:
    - name: Setup Kubeconfig
      id: setup-kubeconfig
      shell: bash
      env:
        NAMESPACE: ${{ inputs.namespace }}
      run: |
        echo "${{ inputs.kubeconfig_base64 }}" | base64 -d > ${{ github.workspace }}/.kubeconfig
        chmod 600 ${{ github.workspace }}/.kubeconfig
        echo "KUBECONFIG=${{ github.workspace }}/.kubeconfig" >> $GITHUB_ENV

        export KUBECONFIG=${{ github.workspace }}/.kubeconfig
        kubectl config set-context --current --namespace=${NAMESPACE}
        kubectl config get-contexts

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.12'
        cache: 'pip'
        cache-dependency-path: 'container/deps/requirements.test.txt'
    - name: Install test dependencies
      shell: bash
      run: |
        python -m pip install --upgrade pip
        pip install -r container/deps/requirements.test.txt

    - name: Deploy and Test
      id: deploy
      shell: bash
      env:
        KUBECONFIG: ${{ github.workspace }}/.kubeconfig
        NAMESPACE: ${{ inputs.namespace }}
        FRAMEWORK: ${{ inputs.framework }}
        PROFILE: ${{ inputs.profile }}
        IMAGE: ${{ inputs.image }}
      run: |
        mkdir -p test-results
        pytest tests/deploy/test_deploy.py \
          --framework="${FRAMEWORK}" \
          --profile="${PROFILE}" \
          --image="${IMAGE}" \
          --namespace="${NAMESPACE}" \
          -v -s \
          --durations=10 \
          --junitxml=test-results/pytest_deploy_${FRAMEWORK}_${PROFILE}_${{ inputs.platform_arch }}_${{ github.run_id }}_${{ job.check_run_id }}.xml \
          --log-cli-level=INFO

    - name: Cleanup Deployment
      if: always()
      shell: bash
      env:
        NAMESPACE: ${{ inputs.namespace }}
        GRAPH_NAME: ${{ steps.deploy.outputs.graph_name }}
      run: |
        echo "::group::Cleanup Deployment"
        set -x
        export KUBECONFIG=${{ github.workspace }}/.kubeconfig

        echo "=== PRE-CLEANUP STATUS ==="
        kubectl get dynamographdeployments -n $NAMESPACE || true
        kubectl get pods -n $NAMESPACE || true

        kubectl get dynamographdeployments -n $NAMESPACE --no-headers 2>/dev/null \
          | awk '$2 == "False" {print $1}' \
          | while read -r dep_name; do
              echo ">>> DETAILED DESCRIPTION FOR FAILED DEPLOYMENT: $dep_name"
              kubectl describe dynamographdeployments "$dep_name" -n $NAMESPACE
          done || true

        if kubectl get dynamographdeployments "${GRAPH_NAME}" -n $NAMESPACE &>/dev/null; then
          echo "DGD ${GRAPH_NAME} still exists after test, deleting..."
          kubectl delete dynamographdeployments ${GRAPH_NAME} -n $NAMESPACE --timeout=60s
        else
          echo "DGD ${GRAPH_NAME} already cleaned up by test"
        fi
        echo "::endgroup::"

    - name: Upload Test Results
      uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f #v6
      if: always()
      with:
        name: test-results-${{ inputs.framework }}-${{ inputs.profile }}-${{ inputs.platform_arch }}-${{ github.run_id }}-${{ job.check_run_id }}
        path: test-results/pytest_deploy_${{ inputs.framework }}_${{ inputs.profile }}_${{ inputs.platform_arch }}_${{ github.run_id }}_${{ job.check_run_id }}.xml
        retention-days: 7
