# Note: freebsd tests are not ran here (see integration instead)
name: job-test-unit

on:
  workflow_call:
    inputs:
      timeout:
        required: true
        type: number
      go-version:
        required: true
        type: string
      runner:
        required: true
        type: string
      canary:
        required: false
        default: false
        type: boolean
      windows-cni-version:
        required: true
        type: string
      linux-cni-version:
        required: true
        type: string
      linux-cni-sha:
        required: true
        type: string

env:
  GOTOOLCHAIN: local
  # Windows fails without this
  CGO_ENABLED: 0

jobs:
  test-unit:
    name: ${{ format('{0}{1}', inputs.runner, inputs.canary && ' (go canary)' || '') }}
    timeout-minutes: ${{ inputs.timeout }}
    runs-on: "${{ inputs.runner }}"
    defaults:
      run:
        shell: bash

    env:
      GO_VERSION: ${{ inputs.go-version }}

    steps:
      - name: "Init: checkout"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
        with:
          fetch-depth: 1

      # If canary is requested, check for the latest unstable release
      - if: ${{ inputs.canary }}
        name: "Init (canary): retrieve GO_VERSION"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          latest_go="$(. ./hack/provisioning/version/fetch.sh; go::canary::for::go-setup)"
          printf "GO_VERSION=%s\n" "$latest_go" >> "$GITHUB_ENV"
          [ "$latest_go" != "" ] || \
            echo "::warning title=No canary go::There is currently no canary go version to test. Following steps will not run."

      - if: ${{ env.GO_VERSION != '' }}
        name: "Init: install go"
        uses: actions/setup-go@4b73464bb391d4059bd26b0524d20df3927bd417  # v6.3.0
        with:
          go-version: ${{ env.GO_VERSION }}
          check-latest: true

      # Install CNI and CRIU
      - if: ${{ env.GO_VERSION != '' }}
        name: "Init: set up CNI and CRIU"
        run: |
          if [ "$RUNNER_OS" == "Windows" ]; then
            GOPATH=$(go env GOPATH) WINCNI_VERSION=${{ inputs.windows-cni-version }} ./hack/provisioning/windows/cni.sh
          elif [ "$RUNNER_OS" == "Linux" ]; then
            ./hack/provisioning/linux/cni.sh install "${{ inputs.linux-cni-version }}" "amd64" "${{ inputs.linux-cni-sha }}"
            sudo apt-get update -qq
            sudo add-apt-repository ppa:criu/ppa -y
            sudo apt-get install -qq criu
          fi

      - if: ${{ env.GO_VERSION != '' }}
        name: "Run"
        run: |
          make test-unit

      # On linux, also run with root
      - if: ${{ env.GO_VERSION != '' && env.RUNNER_OS == 'Linux' }}
        name: "Run: with root"
        run: |
          sudo make test-unit
