name: CI | Basic amd64 tests
on:
  workflow_call:
    inputs:
      tarball-suffix:
        required: false
        type: string
      commit-hash:
        required: false
        type: string
      target-branch:
        required: false
        type: string
        default: ""

jobs:
  run-cri-containerd:
    strategy:
      # We can set this to true whenever we're 100% sure that
      # the all the tests are not flaky, otherwise we'll fail
      # all the tests due to a single flaky instance.
      fail-fast: false
      matrix:
        containerd_version: ['lts', 'active']
        vmm: ['clh', 'dragonball', 'qemu', 'stratovirt', 'cloud-hypervisor', 'qemu-runtime-rs']
    runs-on: ubuntu-22.04
    env:
      CONTAINERD_VERSION: ${{ matrix.containerd_version }}
      GOPATH: ${{ github.workspace }}
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/integration/cri-containerd/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/cri-containerd/gha-run.sh install-kata kata-artifacts

      - name: Run cri-containerd tests
        timeout-minutes: 10
        run: bash tests/integration/cri-containerd/gha-run.sh run

  run-containerd-sandboxapi:
    strategy:
      # We can set this to true whenever we're 100% sure that
      # the all the tests are not flaky, otherwise we'll fail
      # all the tests due to a single flaky instance.
      fail-fast: false
      matrix:
        containerd_version: ['active']
        vmm: ['dragonball', 'cloud-hypervisor', 'qemu-runtime-rs']
    runs-on: ubuntu-22.04
    env:
      CONTAINERD_VERSION: ${{ matrix.containerd_version }}
      GOPATH: ${{ github.workspace }}
      KATA_HYPERVISOR: ${{ matrix.vmm }}
      SANDBOXER: "shim"
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/integration/cri-containerd/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/cri-containerd/gha-run.sh install-kata kata-artifacts

      - name: Run containerd-sandboxapi tests
        timeout-minutes: 10
        run: bash tests/integration/cri-containerd/gha-run.sh run

  run-containerd-stability:
    strategy:
      fail-fast: false
      matrix:
        containerd_version: ['lts', 'active']
        vmm: ['clh', 'cloud-hypervisor', 'dragonball', 'qemu', 'stratovirt']
    runs-on: ubuntu-22.04
    env:
      CONTAINERD_VERSION: ${{ matrix.containerd_version }}
      GOPATH: ${{ github.workspace }}
      KATA_HYPERVISOR: ${{ matrix.vmm }}
      SANDBOXER: "podsandbox"
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/stability/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/stability/gha-run.sh install-kata kata-artifacts

      - name: Run containerd-stability tests
        timeout-minutes: 15
        run: bash tests/stability/gha-run.sh run

  run-nydus:
    strategy:
      # We can set this to true whenever we're 100% sure that
      # the all the tests are not flaky, otherwise we'll fail
      # all the tests due to a single flaky instance.
      fail-fast: false
      matrix:
        containerd_version: ['lts', 'active']
        vmm: ['clh', 'qemu', 'dragonball', 'stratovirt']
    runs-on: ubuntu-22.04
    env:
      CONTAINERD_VERSION: ${{ matrix.containerd_version }}
      GOPATH: ${{ github.workspace }}
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/integration/nydus/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/nydus/gha-run.sh install-kata kata-artifacts

      - name: Run nydus tests
        timeout-minutes: 10
        run: bash tests/integration/nydus/gha-run.sh run

  run-runk:
    # Skip runk tests as we have no maintainers. TODO: Decide when to remove altogether
    if: false
    runs-on: ubuntu-22.04
    env:
      CONTAINERD_VERSION: lts
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/integration/runk/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/runk/gha-run.sh install-kata kata-artifacts

      - name: Run runk tests
        timeout-minutes: 10
        run: bash tests/integration/runk/gha-run.sh run

  run-tracing:
    strategy:
      fail-fast: false
      matrix:
        vmm:
          - clh # cloud-hypervisor
          - qemu
    # TODO: enable me when https://github.com/kata-containers/kata-containers/issues/9763 is fixed
    # TODO: Transition to free runner (see #9940).
    if: false
    runs-on: garm-ubuntu-2204-smaller
    env:
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/functional/tracing/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/functional/tracing/gha-run.sh install-kata kata-artifacts

      - name: Run tracing tests
        timeout-minutes: 15
        run: bash tests/functional/tracing/gha-run.sh run

  run-vfio:
    strategy:
      fail-fast: false
      matrix:
        vmm:
          - clh
          - qemu
    # TODO: enable with clh when https://github.com/kata-containers/kata-containers/issues/9764 is fixed
    # TODO: enable with qemu when https://github.com/kata-containers/kata-containers/issues/9851 is fixed
    # TODO: Transition to free runner (see #9940).
    if: false
    runs-on: garm-ubuntu-2304
    env:
      GOPATH: ${{ github.workspace }}
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/functional/vfio/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Run vfio tests
        timeout-minutes: 15
        run: bash tests/functional/vfio/gha-run.sh run

  run-docker-tests:
    strategy:
      # We can set this to true whenever we're 100% sure that
      # all the tests are not flaky, otherwise we'll fail them
      # all due to a single flaky instance.
      fail-fast: false
      matrix:
        vmm:
          - clh
          - qemu
          - dragonball
          - cloud-hypervisor
    runs-on: ubuntu-22.04
    env:
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/integration/docker/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/docker/gha-run.sh install-kata kata-artifacts

      - name: Run docker smoke test
        timeout-minutes: 5
        run: bash tests/integration/docker/gha-run.sh run

  run-nerdctl-tests:
    strategy:
      # We can set this to true whenever we're 100% sure that
      # all the tests are not flaky, otherwise we'll fail them
      # all due to a single flaky instance.
      fail-fast: false
      matrix:
        vmm:
          - clh
          - dragonball
          - qemu
          - cloud-hypervisor
    runs-on: ubuntu-22.04
    env:
      KATA_HYPERVISOR: ${{ matrix.vmm }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        env:
          GITHUB_API_TOKEN: ${{ github.token }}
        run: bash tests/integration/nerdctl/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/integration/nerdctl/gha-run.sh install-kata kata-artifacts

      - name: Run nerdctl smoke test
        timeout-minutes: 5
        run: bash tests/integration/nerdctl/gha-run.sh run

      - name: Collect artifacts ${{ matrix.vmm }}
        if: always()
        run: bash tests/integration/nerdctl/gha-run.sh collect-artifacts
        continue-on-error: true

      - name: Archive artifacts ${{ matrix.vmm }}
        uses: actions/upload-artifact@v4
        with:
          name: nerdctl-tests-garm-${{ matrix.vmm }}
          path: /tmp/artifacts
          retention-days: 1

  run-kata-agent-apis:
    strategy:
      fail-fast: false
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.commit-hash }}
          fetch-depth: 0

      - name: Rebase atop of the latest target branch
        run: |
          ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch"
        env:
          TARGET_BRANCH: ${{ inputs.target-branch }}

      - name: Install dependencies
        run: bash tests/functional/kata-agent-apis/gha-run.sh install-dependencies

      - name: get-kata-tarball
        uses: actions/download-artifact@v4
        with:
          name: kata-static-tarball-amd64${{ inputs.tarball-suffix }}
          path: kata-artifacts

      - name: Install kata
        run: bash tests/functional/kata-agent-apis/gha-run.sh install-kata kata-artifacts

      - name: Run kata agent api tests with agent-ctl
        run: bash tests/functional/kata-agent-apis/gha-run.sh run