name: CI-selftests

on:
  workflow_dispatch:
  workflow_call:
    inputs:
      trigger:
        type: string

env:
  SHA: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}

jobs:
  tests:
    runs-on: ubuntu-22.04
    strategy:
      fail-fast: false
      matrix:
        testdist: [ 'ubuntu22-tap' ]
#        infradb: [ 'mysql57', 'mysql80', 'mysql81', 'mysql82', 'mariadb10', 'mariadb11' ]
        infradb: [ 'mysql57' ]
    env:
      TESTDIST: ${{ matrix.testdist }}
      INFRADB:  ${{ matrix.infradb }}
      BLDCACHE: ${{ inputs.trigger && fromJson(inputs.trigger).event.workflow_run.head_sha || github.sha }}_${{ matrix.testdist }}_src
      MATRIX: '(${{ matrix.infradb }})'

    steps:

    - uses: LouisBrunner/checks-action@v2.0.0
      id: checks
      if: always()
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        name: '${{ github.workflow }} / ${{ github.job }} ${{ env.MATRIX }}'
        repo: ${{ github.repository }}
        sha: ${{ env.SHA }}
        status: 'in_progress'
#        action_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        details_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
    
#    - name: Wait for cache
#      if: ${{ env.BRANCH != 'none' }}
#      env:
#        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#      run: |
#        echo "Wating for cache '${BLDCACHE}' ..."
#        sleep $(( $RANDOM / 1024 ))
#        PROBE="$(gh cache list --limit 300 --repo ${{ github.repository }} | grep -i ${BLDCACHE} || echo '')"
#        while [[ -z ${PROBE} ]]; do
#          echo "Wating for cache '${BLDCACHE}' ..."
#          sleep 30
#          PROBE="$(gh cache list --limit 300 --repo ${{ github.repository }} | grep -i ${BLDCACHE} || echo '')"
#        done
#        echo "Cache available '${BLDCACHE}'"

    - name: Cache restore src
      id: cache-src
      uses: actions/cache/restore@v4
      with:
        key: ${{ env.BLDCACHE }}
        enableCrossOsArchive: true
        fail-on-cache-miss: true
        path: |
          proxysql/src/

    - name: Run proxysql
      run: |
        #set -x
        set +e
        ulimit -c unlimited
        
        cd ${{ github.workspace }}/proxysql
        mkdir -p ci_infra_logs/regular_infra/proxysql
        cd src
        mkdir coverage_reports

        #(./proxysql --clickhouse-server --sqlite3-server --idle-threads -f -c "$DOCKER_SCRIPT_PATH/conf/proxysql/proxysql.cnf" -D $REGULAR_INFRA_DATADIR &> $REGULAR_INFRA_DATADIR/proxysql.log) &
        (./proxysql -f --initial --clickhouse --idle-threads -D . &>proxysql.log &)
        sleep 10
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -e "SELECT version();" 2>&1 | grep -v 'Using a password'

    - name: Self-tests
      run: |
        #set -x
        set +e
        set -o pipefail
        ulimit -c unlimited
        cd ${{ github.workspace }}/proxysql/src

        mysql -V -vvv
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvv -e "SELECT version();" 2>&1 | grep -v 'Using a password'
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvv -e "SET mysql-verbose_query_error=true;" 2>&1 | grep -v 'Using a password'
        
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvv -e "PROXYSQL COREDUMP;" 2>&1 | grep -v 'Using a password'
        (ls -l ./core* && file ./core* && rm ./core*) || exit 1
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvv -e "PROXYSQL COMPRESSEDCOREDUMP;" 2>&1 | grep -v 'Using a password'
        (ls -l ./core* && file ./core* && rm ./core*) || exit 1
        
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -vvv 2>&1 << EOF | grep -v 'Using a password'
        SELECT COUNT(*) FROM stats_mysql_query_digest;
        SELECT COUNT(*) FROM stats_mysql_query_digest_reset;
        SELECT schemaname, COUNT(*) FROM stats_mysql_query_digest GROUP BY schemaname;
        SELECT schemaname, COUNT(*) FROM stats_mysql_query_digest_reset GROUP BY schemaname;
        TRUNCATE TABLE stats.stats_mysql_query_digest_reset;
        TRUNCATE TABLE stats.stats_mysql_query_digest;
        PROXYSQLTEST 1 1000;
        PROXYSQLTEST 2 1000;
        PROXYSQLTEST 3 1000;
        PROXYSQLTEST 4 1000;
        PROXYSQLTEST 5 1000;
        PROXYSQLTEST 6 1000;
        EOF
        RC=$?
        
        echo 'DONE'
        exit $RC

    - name: Cleanup
      if: ${{ always() }}
      run: |
        set +e
        
        mysql -h127.0.0.1 -P6032 -uadmin -padmin -e "PROXYSQL SHUTDOWN SLOW" &> /dev/null
        sleep 10
        killall proxysql
        
        sudo chmod -R 777 ${{ github.workspace }}/*

    - name: Fix artifact permissions
      if: ${{ failure() && !cancelled() }}
      run: |
        # actions/upload-artifact dies with EACCES when it scandirs into
        # directories under ci_*_logs/ that were created inside docker
        # build containers (root-owned). Make everything readable by the
        # runner user before upload. sudo required because files are
        # root-owned; 2>/dev/null + || true because the path may not
        # exist on all failure paths (e.g. a cache-restore failure before
        # any test even runs).
        sudo chmod -R a+rX proxysql/ci_*_logs/ 2>/dev/null || true

    - name: Archive artifacts logs
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-logs-run#${{ github.run_number }}
        path: |
          proxysql/ci_*_logs/
          
    - name: Archive artifacts bin
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-bin-run#${{ github.run_number }}
        path: |
          proxysql/binaries/
          proxysql/src/
          proxysql/libs/
#          proxysql/deps/
          
    - uses: LouisBrunner/checks-action@v2.0.0
      if: always()
      with:
        token: ${{ secrets.GITHUB_TOKEN }}
        check_id: ${{ steps.checks.outputs.check_id }}
        repo: ${{ github.repository }}
        sha: ${{ env.SHA }}
        conclusion: ${{ job.status }}
#        action_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
        details_url: 'https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
