---
name: Reusable Robustness Workflow
on:
  workflow_call:
    inputs:
      etcdBranch:
        required: true
        type: string
      count:
        required: true
        type: number
      testTimeout:
        required: false
        type: string
        default: '30m'
      artifactName:
        required: true
        type: string
      runs-on:
        required: false
        type: string
        default: "['ubuntu-latest']"
permissions: read-all

jobs:
  test:
    timeout-minutes: 210
    runs-on: ${{ fromJson(inputs.runs-on) }}
    container:
      image: golang:1.21-bookworm
      # Required for mounting fuse lazyfs
      options: --privileged
    defaults:
      run:
        shell: bash
    steps:
      - uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
      # https://github.com/actions/checkout/issues/1169
      - run: git config --system --add safe.directory '*'
      - id: goversion
        run: echo "goversion=$(cat .go-version)" >> "$GITHUB_OUTPUT"
      - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
        with:
          go-version: ${{ steps.goversion.outputs.goversion }}
      - name: test-robustness
        env:
          ETCD_BRANCH: "${{ inputs.etcdBranch }}"
        run: |
          set -euo pipefail
          go clean -testcache

          # Build LazyFS
          apt update && apt-get --yes install cmake libfuse3-dev libfuse3-3 fuse3
          make install-lazyfs
          # Use --failfast to avoid overriding report generated by failed test
          GO_TEST_FLAGS="-v --count ${{ inputs.count }} --timeout ${{ inputs.testTimeout }} --failfast --run TestRobustness"
          case "${ETCD_BRANCH}" in
            release-3.5)
              EXPECT_DEBUG=true GO_TEST_FLAGS=${GO_TEST_FLAGS} RESULTS_DIR=/tmp/results make test-robustness-release-3.5
              ;;
            release-3.4)
              EXPECT_DEBUG=true GO_TEST_FLAGS=${GO_TEST_FLAGS} RESULTS_DIR=/tmp/results make test-robustness-release-3.4
              ;;
            main)
              make gofail-enable
              make build
              EXPECT_DEBUG=true GO_TEST_FLAGS=${GO_TEST_FLAGS} RESULTS_DIR=/tmp/results make test-robustness
              ;;
            *)
              echo "Failed to find target ${ETCD_BRANCH}"
              exit 1
              ;;
          esac
      - uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
        if: always()
        with:
          name: ${{ inputs.artifactName }}
          path: /tmp/results/*
