name: Test component

on:
  workflow_call:
    inputs:
      artifact_run_id:
        type: string
        default: ""
      amdgpu_families:
        type: string
      test_runs_on:
        type: string
      platform:
        type: string
      component:
        type: string


permissions:
  contents: read

jobs:
  test_component:
    name: 'Test ${{ fromJSON(inputs.component).job_name }} (shard ${{ matrix.shard }} of ${{ fromJSON(inputs.component).total_shards }})'
    runs-on: ${{ inputs.test_runs_on }}
    container:
      image: ${{ inputs.platform == 'linux' && 'ghcr.io/rocm/no_rocm_image_ubuntu24_04@sha256:4150afe4759d14822f0e3f8930e1124f26e11f68b5c7b91ec9a02b20b1ebbb98' || null }}
      options: --ipc host
        --group-add video
        --device /dev/kfd
        --device /dev/dri
        --group-add 110
        --env-file /etc/podinfo/gha-gpu-isolation-settings
    strategy:
      fail-fast: false
      matrix:
        # The shard array is based on "total_shards" from "fetch_test_configurations.py"
        # The test executable will shard based on the array. (ex: [1, 2, 3, 4] = four test shards)
        shard: ${{ fromJSON(inputs.component).shard_arr }}
    defaults:
      run:
        shell: bash
    env:
      VENV_DIR: ${{ github.workspace }}/.venv
      ARTIFACT_RUN_ID: "${{ inputs.artifact_run_id != '' && inputs.artifact_run_id || github.run_id }}"
      OUTPUT_ARTIFACTS_DIR: "./build"
      THEROCK_BIN_DIR: "./build/bin"
      AMDGPU_FAMILIES: ${{ inputs.amdgpu_families }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          repository: "ROCm/TheRock"

      - name: Run setup test environment workflow
        uses: './.github/actions/setup_test_environment'
        with:
          ARTIFACT_RUN_ID: ${{ env.ARTIFACT_RUN_ID }}
          AMDGPU_FAMILIES: ${{ inputs.amdgpu_families }}
          OUTPUT_ARTIFACTS_DIR: ${{ env.OUTPUT_ARTIFACTS_DIR }}
          VENV_DIR: ${{ env.VENV_DIR }}
          FETCH_ARTIFACT_ARGS: ${{ fromJSON(inputs.component).fetch_artifact_args }}
          IS_PR_FROM_FORK: ${{ github.event.pull_request.head.repo.fork }}

      - name: Test
        timeout-minutes: ${{ fromJSON(inputs.component).timeout_minutes }}
        env:
          SHARD_INDEX: ${{ matrix.shard }}
          TOTAL_SHARDS: ${{ fromJSON(inputs.component).total_shards }}
        run: |
          ${{ fromJSON(inputs.component).test_script }}
