name: Regression Tests
description: "Run regression tests"

inputs:
  dfly-executable:
    required: true
    type: string
  gspace-secret:
    required: false
    type: string
  run-only-on-ubuntu-latest:
    # 'true' or 'false' cause boolean
    # is not supported in composite actions
    required: true
    type: string
  build-folder-name:
    required: true
    type: string
  filter:
    required: false
    type: string
  aws-access-key-id:
    required: true
    type: string
  aws-secret-access-key:
    required: true
    type: string
  s3-bucket:
    required: true
    type: string
  epoll:
    required: false
    type: string

runs:
  using: "composite"
  # bring back timeouts once composite actions start supporting them
  # timeout-minutes: 20
  steps:
    - name: Run PyTests
      id: main
      shell: bash
      run: |
        ls -l ${GITHUB_WORKSPACE}/
        cd ${GITHUB_WORKSPACE}/tests
        echo "Current commit is ${{github.sha}}"
        pip3 install -r dragonfly/requirements.txt
        # used by PyTests
        export DRAGONFLY_PATH="${GITHUB_WORKSPACE}/${{inputs.build-folder-name}}/${{inputs.dfly-executable}}"
        export UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 # to crash on errors

        if [[ "${{inputs.epoll}}" == 'true' ]]; then
          export FILTER="${{inputs.filter}} and not exclude_epoll"
          # Run only replication tests with epoll
          timeout 50m pytest -m "$FILTER" --durations=10 --timeout=300 --color=yes --json-report --json-report-file=report.json dragonfly --df force_epoll=true --log-cli-level=INFO || code=$?
        else
          export FILTER="${{inputs.filter}}"
          # Run only replication tests with epoll
          timeout 50m pytest -m "$FILTER" --durations=10 --timeout=300 --color=yes --json-report --json-report-file=report.json dragonfly --log-cli-level=INFO || code=$?
        fi

        # timeout returns 124 if we exceeded the timeout duration
        if [[ $code -eq 124 ]]; then
          # Add an extra new line here because when tests timeout the first line below continues from the test failure name
          echo "\n"
          echo "🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑"
          echo "🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 TESTS TIMEDOUT 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑"
          echo "🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑 🛑"
          # Copy the last log file because we timedout and pytest did not copy it over
          # the /tmp/failed/ folder
          cat /tmp/last_test_log_dir.txt | xargs -I {} mv {}/ /tmp/failed/
          exit 1
        fi

        # when a test fails in pytest it returns 1 but there are other return codes as well so we just check if the code is non zero
        if [[ $code -ne 0 ]]; then
          exit 1
        fi
      env:
        # Add environment variables to enable the S3 snapshot test.
        DRAGONFLY_S3_BUCKET: ${{ inputs.s3-bucket }}
        AWS_ACCESS_KEY_ID: ${{ inputs.aws-access-key-id }}
        AWS_SECRET_ACCESS_KEY: ${{ inputs.aws-secret-access-key }}
        AWS_REGION: us-east-1

    - name: Send notification on failure
      if: failure() && github.ref == 'refs/heads/main'
      shell: bash
      run: |
        get_failed_tests() {
          local report_file=$1
          echo $(jq -r '.tests[] | select(.outcome == "failed") | .nodeid' "$report_file")
        }
        cd ${GITHUB_WORKSPACE}/tests
        failed_tests=""
        if [ -f report.json ]; then
          failed_tests=$(get_failed_tests report.json)
        fi

        job_link="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
        message="Regression tests failed.\\n The commit is: ${{github.sha}}.\\n $failed_tests \\n Job Link: ${job_link}\\n"

        curl -s \
          -X POST \
          -H 'Content-Type: application/json' \
          '${{ inputs.gspace-secret }}' \
          -d '{"text": "'"${message}"'"}'
