name: Test target (make)
on:
  workflow_call:
    inputs:
      erlang_version:
        required: true
        type: string
      elixir_version:
        required: true
        type: string
      metadata_store:
        required: true
        type: string
      mixed_clusters:
        required: false
        default: false
        type: boolean
      make_target:
        required: true
        type: string
      plugin:
        required: true
        type: string
jobs:
  test:
    name: ${{ inputs.plugin }} (${{ inputs.make_target }})
    runs-on: ubuntu-latest
    timeout-minutes: 60
    steps:
    - name: CHECKOUT REPOSITORY
      uses: actions/checkout@v4

    - name: FETCH TAGS
      run: git fetch --tags

    - name: SETUP OTP & ELIXIR
      uses: erlef/setup-beam@v1.17
      with:
        otp-version: ${{ inputs.erlang_version }}
        elixir-version: ${{ inputs.elixir_version }}
        hexpm-mirrors: |
          https://builds.hex.pm
          https://cdn.jsdelivr.net/hex
        # This currently only applies to Elixir; and can be safely
        # restricted to the build jobs to avoid duplication in output.
        disable_problem_matchers: true

    - name: MIXED CLUSTERS - FETCH SIGNING KEYS
      uses: dsaltares/fetch-gh-release-asset@master
      if: inputs.mixed_clusters
      with:
        repo: rabbitmq/signing-keys
        file: rabbitmq-release-signing-key.asc

    - name: MIXED CLUSTERS - FETCH PREVIOUS VERSION
      id: fetch_secondary_dist
      uses: dsaltares/fetch-gh-release-asset@master
      if: inputs.mixed_clusters
      with:
        version: 'tags/v4.0.5'
        regex: true
        file: "rabbitmq-server-generic-unix-\\d.+\\.tar\\.xz"
        target: ./

    - name: MIXED CLUSTERS - SETUP SECONDARY_DIST
      if: inputs.mixed_clusters
      run: |
        ls -l rabbitmq-server-generic-unix-*.tar.xz*

        archive_name=$(echo rabbitmq-server-generic-unix-*.tar.xz)
        archive_version=$(echo $archive_name | sed -E -e 's/^rabbitmq-server-generic-unix-//' -e 's/\.tar\.xz$//')

        gpg --import rabbitmq-release-signing-key.asc
        gpg --verify $archive_name.asc $archive_name
        tar xf $archive_name

        echo "SECONDARY_DIST=${GITHUB_WORKSPACE}/rabbitmq_server-$archive_version" >> $GITHUB_ENV

    - name: SETUP SLAPD (rabbitmq_auth_backend_ldap)
      if: inputs.plugin == 'rabbitmq_auth_backend_ldap'
      run: |
        sudo apt-get update && \
        sudo apt-get install -y \
          ldap-utils \
          slapd

        sudo systemctl is-active --quiet apparmor.service && sudo systemctl stop apparmor.service
        sudo systemctl disable apparmor.service

    - name: RUN TESTS
      if: inputs.plugin != 'rabbitmq_cli'
      run: |
        make -C deps/${{ inputs.plugin }} ${{ inputs.make_target }} RABBITMQ_METADATA_STORE=${{ inputs.metadata_store }}

    # rabbitmq_cli needs a correct broker version for two of its tests.
    # But setting PROJECT_VERSION makes other plugins fail.
    - name: RUN TESTS (rabbitmq_cli)
      if: inputs.plugin == 'rabbitmq_cli'
      run: |
        make -C deps/${{ inputs.plugin }} ${{ inputs.make_target }} RABBITMQ_METADATA_STORE=${{ inputs.metadata_store }} PROJECT_VERSION="4.1.0"

    - name: UPLOAD TEST LOGS
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: CT logs (${{ inputs.plugin }} ${{ inputs.make_target }} OTP-${{ inputs.erlang_version }} ${{ inputs.metadata_store }}${{ inputs.mixed_clusters && ' mixed' || '' }})
        path: |
          logs/
#          !logs/**/log_private
        if-no-files-found: ignore
