name: rspamd_test

on:
  workflow_call:
    inputs:
      image:
        required: true
        type: string
      name:
        required: true
        type: string
      runner:
        type: string
        default: 'ubuntu-24.04'
      enable_luajit:
        type: string
        default: 'ON'

env:
  CTEST_OUTPUT_ON_FAILURE: 1
  RSPAMD_LUA_EXPENSIVE_TESTS: 1

jobs:
  test:
    runs-on: "${{ inputs.runner }}"
    container:
      image: ${{ inputs.image }}
      options: --user root
    steps:
      - name: Create directories
        run: |
          sudo mkdir -p ${GITHUB_WORKSPACE}
          sudo chown -R build:build ${GITHUB_WORKSPACE}

      - name: Check out source code
        uses: actions/checkout@v4
        with:
          path: src

      - name: Set variables on ARM64
        if: runner.arch == 'ARM64'
        run: echo "HYPERSCAN_ALTROOT=-DHYPERSCAN_ROOT_DIR=/vectorscan" >> "$GITHUB_ENV"

      - name: Install Lua FFI for non-LuaJIT builds
        if: inputs.enable_luajit == 'OFF'
        run: |
          sudo dnf install -y luarocks lua-devel libffi-devel meson pkgconf gcc-c++
          cd /tmp
          curl -sL https://github.com/q66/cffi-lua/archive/refs/tags/v0.2.3.tar.gz | tar xz
          cd cffi-lua-0.2.3
          find . -exec touch {} +
          meson setup build -Dlua_version=5.4 -Dbuildtype=release -Dtests=false --prefix=/usr --libdir=lib64
          ninja -C build
          sudo ninja -C build install

      - name: Run cmake
        run: |
          mkdir ${GITHUB_WORKSPACE}/build
          cd ${GITHUB_WORKSPACE}/build
          cmake -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/install -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_VERBOSE_MAKEFILE=ON -DENABLE_COVERAGE=ON -DENABLE_LIBUNWIND=ON -DENABLE_HYPERSCAN=ON -DENABLE_LUAJIT=${{ inputs.enable_luajit }} ${{ env.HYPERSCAN_ALTROOT }} -GNinja ${GITHUB_WORKSPACE}/src

      - name: Build rspamd
        run: |
          cd ${GITHUB_WORKSPACE}/build
          ncpu=$(getconf _NPROCESSORS_ONLN)
          ninja -j $ncpu install
          ninja -j $ncpu rspamd-test
          ninja -j $ncpu rspamd-test-cxx

      - name: Run unit tests
        if: "!(inputs.name == 'ubuntu-ci' && runner.arch == 'ARM64')"
        run: |
          cd ${GITHUB_WORKSPACE}/build
          ninja test

      - name: Apply Fedora specifics
        if: inputs.name == 'fedora-ci'
        run: |
          sudo mv /usr/bin/miltertest /usr/bin/miltertest.is.broken.on.fedora || true

      - name: Run functional tests
        run: |
          cd ${GITHUB_WORKSPACE}/build
          ulimit -c unlimited
          ulimit -s unlimited
          set +e
          RSPAMD_INSTALLROOT=${GITHUB_WORKSPACE}/install robot  -v RSPAMD_USER:root -v RSPAMD_GROUP:root --removekeywords wuks --exclude isbroken ${GITHUB_WORKSPACE}/src/test/functional/cases; EXIT_CODE=$?
          set -e
          core_files=$(find /var/tmp/ -name '*.core')
          for core in $core_files; do exe=$(gdb --batch -ex 'info proc mappings' -c $core | tail -1 | awk '{print $5}'); gdb --batch -ex 'bt' -c $core $exe; echo '---'; done
          exit $EXIT_CODE

      - name: Save workspace directory
        if: (success() || failure())
        run: echo "CONTAINER_WORKSPACE=${GITHUB_WORKSPACE}" >> "$GITHUB_ENV"

      - name: Upload robot logs
        if: (success() || failure())
        uses: actions/upload-artifact@v4
        with:
          name: robotlog-${{ inputs.name }}
          path: |
            ${{ env.CONTAINER_WORKSPACE }}/build/*.*ml
          retention-days: 1

      - name: Upload rspamd logs
        if: (success() || failure())
        uses: actions/upload-artifact@v4
        with:
          name: rspamdlog-${{ inputs.name }}
          path: ${{ env.CONTAINER_WORKSPACE }}/build/robot-save
          retention-days: 1

      - name: Upload built rspamd
        if: inputs.name == 'ubuntu-ci-amd64'
        uses: actions/upload-artifact@v4
        with:
          name: rspamd-binary-${{ inputs.name }}
          path: ${{ github.workspace }}/install
          retention-days: 1
