---
# yamllint disable rule:line-length
name: Python Unit Tests
on:  # yamllint disable-line rule:truthy
  pull_request:
  merge_group:
    branches:
      - main
    types:
      - checks_requested
  push:
    branches:
      - main
  workflow_dispatch:
permissions:
  contents: read
jobs:
  python:
    if: |
      !((github.event_name == 'pull_request' && contains(github.event.pull_request.title, '[citest_skip]')) ||
        (github.event_name == 'push' && contains(github.event.head_commit.message, '[citest_skip]')))
    strategy:
      matrix:
        include:
          # pcs_version 0.11.x - ubuntu 22.04 only
          # pcs_version 0.12.x - ubuntu 24.04 only
          # pcs_dir_local:
          #   Since Ubuntu 24.04, python forces pcs being installed into local/
          #   and it cannot be persuaded not to do that. This is covered by
          #   pcs_dir_local variable.
          # pcs_site_packages:
          #   pcs-0.11 was installed by pcs install scripts into site-packages,
          #   while pcs-0.12 is installed into the correct dir dist-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.4
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.5
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.6
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.7
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.8
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.9
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.10
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-22.04
            pcs_version: v0.11.11
            pcs_dir_local: ''
            pcs_site_packages: site-packages
          - os: ubuntu-24.04
            # v0.12.0 contains a bug preventing it being build in CI
            # v0.12.0-lsr-ci fixes that bug while not adding any other changes
            pcs_version: v0.12.0-lsr-ci
            pcs_dir_local: '/local'
            pcs_site_packages: dist-packages
          - os: ubuntu-24.04
            pcs_version: v0.12.1
            pcs_dir_local: '/local'
            pcs_site_packages: dist-packages
          - os: ubuntu-24.04
            pcs_version: v0.12.2
            pcs_dir_local: '/local'
            pcs_site_packages: dist-packages
          - os: ubuntu-24.04
            pcs_version: main
            pcs_dir_local: '/local'
            pcs_site_packages: dist-packages
    runs-on: ${{ matrix.os }}
    steps:
      - name: Update git
        run: |
          set -euxo pipefail
          sudo apt update
          sudo apt install -y git

      - name: checkout PR
        uses: actions/checkout@v6

      - name: Install platform dependencies, python, tox, tox-lsr
        run: |
          set -euxo pipefail
          python -m pip install --upgrade pip
          pip install "git+https://github.com/linux-system-roles/tox-lsr@3.15.0"
          # If you have additional OS dependency packages e.g. libcairo2-dev
          # then put them in .github/config/ubuntu-requirements.txt, one
          # package per line.
          if [ -f .github/config/ubuntu-requirements.txt ]; then
            sudo apt-get install -y $(cat .github/config/ubuntu-requirements.txt)
            # remove pcs which is installed as a dependency of pacemaker (since
            # ubuntu 23.10) and conflicts with pcs built from source
            sudo dpkg --force-depends --purge pcs
          fi

      - name: Clone pcs
        uses: actions/checkout@v6
        with:
          repository: ClusterLabs/pcs
          ref: ${{ matrix.pcs_version }}
          path: pcs-upstream

      - name: Build and install pcs
        run: |
          set -euxo pipefail
          pcs_dir="${RUNNER_TEMP}/pcs"
          mkdir -p "$pcs_dir"
          cd pcs-upstream
          if [ "x${{ matrix.pcs_version }}" == "xmain" ]; then
            echo "0.0.1+ci" > .tarball-version
          fi
          ./autogen.sh && ./configure --prefix "$pcs_dir" && make && make install
          cd ..
          rm -rf pcs-upstream
          site_pkgs_dir=`python -m site --user-site`
          pyver=$(python -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')
          echo "${pcs_dir}${{ matrix.pcs_dir_local }}/lib/python${pyver}/${{ matrix.pcs_site_packages }}/" > "${site_pkgs_dir}/pcs.pth"
          python -c 'from pcs import settings; print(settings.pcs_version)'

      - name: Run unit tests
        run: |
          set -euxo pipefail
          toxpyver=$(python -c 'import sys; print(f"{sys.version_info.major}{sys.version_info.minor}")')
          toxenvs="py${toxpyver}"
          # NOTE: The use of flake8, pylint, black with specific
          # python envs is arbitrary and must be changed in tox-lsr
          case "$toxpyver" in
          311) toxenvs="${toxenvs},coveralls,flake8,pylint,black" ;;
          *) toxenvs="${toxenvs},coveralls" ;;
          esac
          # sitepackages needs to be enabled for the unit tests to be able to
          # find pcs built from upstream sources
          VIRTUALENV_SYSTEM_SITE_PACKAGES=true TOXENV="$toxenvs" lsr_ci_runtox

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v5
