name: Cloud Hypervisor Tests (MSHV) (x86_64)
on: [pull_request_target, merge_group]
permissions: {}

jobs:
  infra-setup:
    name: MSHV Infra Setup (x86_64)
    uses: ./.github/workflows/mshv-infra.yaml
    with:
      ARCH: x86_64
      KEY: azure_key_${{ github.run_id }}
      OS_DISK_SIZE: 512
      RG: MSHV-INTEGRATION-${{ github.run_id }}
      VM_SKU: Standard_D16s_v5
    secrets:
      MI_CLIENT_ID: ${{ secrets.MSHV_MI_CLIENT_ID }}
      RUNNER_RG: ${{ secrets.MSHV_RUNNER_RG }}
      STORAGE_ACCOUNT_PATHS: ${{ secrets.MSHV_STORAGE_ACCOUNT_PATHS }}
      ARCH_SOURCE_PATH: ${{ secrets.MSHV_X86_SOURCE_PATH }}
      USERNAME: ${{ secrets.MSHV_USERNAME }}

  run-tests:
    name: Integration Tests (x86_64)
    needs: infra-setup
    if: ${{ always() && needs.infra-setup.result == 'success' }}
    runs-on: mshv
    steps:
      - name: Run integration tests
        timeout-minutes: 60
        env:
          KEY: azure_key_${{ github.run_id }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          REPO_URL: https://github.com/cloud-hypervisor/cloud-hypervisor.git
          REPO_DIR: cloud-hypervisor
          PRIVATE_IP: ${{ needs.infra-setup.outputs.PRIVATE_IP }}
          RG: MSHV-${{ github.run_id }}
          USERNAME: ${{ secrets.MSHV_USERNAME }}
        run: |
          set -eufo pipefail
          echo "Connecting to the VM via SSH..."
          ssh -i ~/.ssh/"${KEY}" -o StrictHostKeyChecking=no -- "${USERNAME}@${PRIVATE_IP}" << EOF
            set -e
            echo "Logged in successfully."
            export PATH="\$HOME/.cargo/bin:\$PATH"

            if [[ "${{ github.event_name }}" == "pull_request_target" ]]; then
              git clone --depth 1 "$REPO_URL" "$REPO_DIR"
              cd "$REPO_DIR"
              git fetch origin pull/${{ github.event.pull_request.number }}/merge
              git checkout FETCH_HEAD
            else
              git clone --depth 1 --single-branch --branch "${{ github.ref_name }}" "$REPO_URL" "$REPO_DIR"
              cd "$REPO_DIR"
            fi

            echo "Loading VDPA kernel modules..."
            sudo modprobe vdpa
            sudo modprobe vhost_vdpa
            sudo modprobe vdpa_sim
            sudo modprobe vdpa_sim_blk
            sudo modprobe vdpa_sim_net

            echo "Creating VDPA devices..."
            sudo vdpa dev add name vdpa-blk0 mgmtdev vdpasim_blk
            sudo vdpa dev add name vdpa-blk1 mgmtdev vdpasim_blk
            sudo vdpa dev add name vdpa-blk2 mgmtdev vdpasim_net

            echo "Setting permissions..."
            for i in 0 1 2; do
              dev="/dev/vhost-vdpa-\$i"
              if [ -e "\$dev" ]; then
                sudo chown \$USER:\$USER "\$dev"
                sudo chmod 660 "\$dev"
              else
                echo "Warning: Device \$dev not found"
              fi
            done

            sudo ./scripts/dev_cli.sh tests --hypervisor mshv --integration
          EOF

      - name: Dump dmesg
        if: always()
        continue-on-error: true
        env:
          KEY: azure_key_${{ github.run_id }}
          PRIVATE_IP: ${{ needs.infra-setup.outputs.PRIVATE_IP }}
          USERNAME: ${{ secrets.MSHV_USERNAME }}
        run: |
          ssh -i ~/.ssh/"${KEY}" -o StrictHostKeyChecking=no -- "${USERNAME}@${PRIVATE_IP}" sudo dmesg

      - name: Dump serial console logs
        if: always()
        continue-on-error: true
        env:
          RG_NAME: ${{ needs.infra-setup.outputs.RG_NAME }}
          VM_NAME: ${{ needs.infra-setup.outputs.VM_NAME }}
        run: |
          set -eufo pipefail
          az vm boot-diagnostics get-boot-log --name "${VM_NAME}" --resource-group "${RG_NAME}" | jq -r

  cleanup:
    name: Cleanup
    needs: run-tests
    if: always()
    runs-on: mshv
    steps:
      - name: Delete RG
        env:
          RG: MSHV-INTEGRATION-${{ github.run_id }}
        run: |
          if az group exists --name "${RG}"; then
            az group delete --name "${RG}" --yes --no-wait
          else
            echo "Resource Group ${RG} does not exist. Skipping deletion."
          fi
          echo "Cleanup process completed."

      - name: Delete SSH Key
        env:
          KEY: azure_key_${{ github.run_id }}
        run: |
          if [ -f ~/.ssh/"${KEY}" ]; then
            rm -f ~/.ssh/"${KEY}" ~/.ssh/"${KEY}.pub"
            echo "SSH key deleted successfully."
          else
            echo "SSH key does not exist. Skipping deletion."
          fi
          echo "Cleanup process completed."
