# Currently, Lima job test only for EL, though in the future it could be used to also test FreeBSD or other linux-es
name: job-test-in-lima

on:
  workflow_call:
    inputs:
      timeout:
        required: true
        type: number
      runner:
        required: true
        type: string
      target:
        required: true
        type: string
      guest:
        required: true
        type: string
      skip-flaky:
        required: false
        default: false
        type: boolean

jobs:
  test:
    name: "${{ inputs.guest }} ${{ inputs.target }}"
    timeout-minutes: ${{ inputs.timeout }}
    runs-on: "${{ inputs.runner }}"
    env:
      TARGET: ${{ inputs.target }}
    steps:
      - name: "Init: checkout"
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2
        with:
          fetch-depth: 1

      - name: "Init: lima"
        uses: lima-vm/lima-actions/setup@55627e31b78637bf254a8b2a14da8ea7d12564e5  # v1.1.0
        id: lima-actions-setup

      - name: "Init: Cache"
        uses: actions/cache@cdf6c1fa76f9f475f3d7449005a359c84ca0f306  # v5.0.3
        with:
          path: ~/.cache/lima
          key: lima-${{ steps.lima-actions-setup.outputs.version }}

      - name: "Init: start the guest VM"
        run: |
          set -eux
          # containerd=none is set because the built-in containerd support conflicts with Docker
          limactl start \
            --name=default \
            --cpus=4 \
            --memory=12 \
            --containerd=none \
            --set '.mounts=null | .portForwards=[{"guestSocket":"/var/run/docker.sock","hostSocket":"{{.Dir}}/sock/docker.sock"}]' \
            template://${{ inputs.guest }}

      # FIXME: the tests should be directly executed in the VM without nesting Docker inside it
      # https://github.com/containerd/nerdctl/issues/3858
      - name: "Init: install dockerd in the guest VM"
        run: |
          set -eux
          lima sudo mkdir -p /etc/systemd/system/docker.socket.d
          cat <<-EOF | lima sudo tee /etc/systemd/system/docker.socket.d/override.conf
          [Socket]
          SocketUser=$(whoami)
          EOF
          lima sudo dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo
          lima sudo dnf -q -y install docker-ce --nobest
          lima sudo systemctl enable --now docker

      - name: "Init: configure the host to use dockerd in the guest VM"
        run: |
          set -eux
          sudo systemctl disable --now docker.service docker.socket
          export DOCKER_HOST="unix://$(limactl ls --format '{{.Dir}}/sock/docker.sock' default)"
          echo "DOCKER_HOST=${DOCKER_HOST}" >>$GITHUB_ENV
          docker info
          docker version

      - name: "Init: install br-netfilter in the guest VM"
        run: |
          lima sudo modprobe br-netfilter

      - name: "Init: expose GitHub Runtime variables for gha"
        uses: crazy-max/ghaction-github-runtime@04d248b84655b509d8c44dc1d6f990c879747487  # v4.0.0

      - name: "Init: prepare integration tests"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          set -eux

          sudo losetup -Dv
          sudo losetup -lv

          [ "$TARGET" = "rootless" ] && TARGET=test-integration-rootless || TARGET=test-integration
          docker buildx create --name with-gha --use
          docker buildx build \
            --secret id=github_token,env=GITHUB_TOKEN \
            --output=type=docker \
            --cache-from type=gha,scope=test-integration-dependencies-amd64 \
            -t test-integration --target "${TARGET}" \
            .

      - name: "Run integration tests"
        # Presumably, something is broken with the way docker exposes /dev to the container, as it appears to only
        # randomly work. Mounting /dev does workaround the issue.
        # This might be due to the old kernel shipped with Alma (4.18), or something else between centos/docker.
        run: |
          set -eux
          if [ "$TARGET" = "rootless" ]; then
            echo "rootless"
            docker run -t -v /dev:/dev --rm --privileged test-integration /test-integration-rootless.sh ./hack/test-integration.sh -test.only-flaky=false
          else
            echo "rootful"
            docker run -t -v /dev:/dev --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=false
          fi
      - name: "Run: integration tests (flaky)"
        if: ${{ !fromJSON(inputs.skip-flaky) }}
        run: |
          set -eux
          if [ "$TARGET" = "rootless" ]; then
            echo "rootless"
            docker run -t -v /dev:/dev --rm --privileged test-integration /test-integration-rootless.sh ./hack/test-integration.sh -test.only-flaky=true
          else
            echo "rootful"
            docker run -t -v /dev:/dev --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=true
          fi
