
name: CI-taptests

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

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

jobs:
  build:
    runs-on: ubuntu-22.04
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:

    - name: Install dependencies
      run: |
        sudo apt-get update -y
        sudo apt-get full-upgrade -y
        sudo apt-get install -y python3-pymysql python3-structlog sysbench mycli
        sudo pip3 install fastcov
        
        wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-client_3.2.6_amd64.deb
        sudo dpkg -i orchestrator-client_3.2.6_amd64.deb
        
        sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
        wget https://repo.mysql.com/mysql-apt-config_0.8.29-1_all.deb
        sudo dpkg -i mysql-apt-config_0.8.29-1_all.deb
        # mysql-apt-config.postinst writes an expired signing key into the
        # keyring referenced by sources.list.d/mysql.list (signed-by=...).
        # Overwrite with the renewed key (same fingerprint, valid through
        # 2027-10-23) so apt-get update trusts the InRelease signature.
        wget -qO - https://repo.mysql.com/RPM-GPG-KEY-mysql-2025 | sudo gpg --dearmor --yes -o /usr/share/keyrings/mysql-apt-config.gpg
        sudo apt-get update -y
        sudo apt-cache policy mysql-shell
        sudo apt-get install -y mysql-shell
        
        sudo sed -i 's/8.0/5.7/' /etc/apt/sources.list.d/mysql.list
        sudo sed -i 's/jammy/bionic/' /etc/apt/sources.list.d/mysql.list
        sudo apt-get update -y
        sudo apt-cache policy libmysqlclient-dev
        sudo apt-get install -y --allow-downgrades libmysqlclient-dev=5.7*

    - name: Checkout proxysql
      uses: actions/checkout@v4
      with:
        repository: 'sysown/proxysql'
        ref: ${{ env.SHA }}
        fetch-depth: 0
        path: 'proxysql'

    - name: Set GIT_VERSION
      run: |
        cd proxysql/
        git fetch --tags --force
        echo "GIT_VERSION=$(git describe --long --abbrev=7)" >> $GITHUB_ENV

    - name: Checkout jenkins_build_scripts
      uses: actions/checkout@v4
      with:
        repository: 'proxysql/jenkins-build-scripts'
        ref: 'kubernetes'
#        fetch-depth: 0
        path: 'jenkins-build-scripts'
        token: ${{ secrets.GH_TOKEN_PROXYSQL }}
        submodules: 'false'

    - name: Patch TAP-tests
      run: |
        # apply patches
        #for PATCH in $(cd jenkins-build-scripts/test-scripts/patches; find . -type f); do
        #  if [[ $PATCH =~ \.patch$ ]]; then
        #    patch proxysql/${PATCH%.patch} jenkins-build-scripts/test-scripts/patches/${PATCH} || true
        #  elif [[ ! -f jenkins-build-scripts/test-scripts/patches/${PATCH#./}.patch ]]; then
        #    cp jenkins-build-scripts/test-scripts/patches/${PATCH#./} proxysql/${PATCH#./} || true
        #  fi
        #done
        # patch multi port listening
        cd proxysql/
        sed -i '61 s/"0.0.0.0:6032"/ current.c_str()/' test/tap/tests/admin-listen_on_unix-t.cpp
        sed -i 's/:6035/:6036/' test/tap/tests/test_sqlite3_server-t.cpp

    - name: Build
      run: |
        cd proxysql/
        # fix compile issues
        #grep -rl "<curl/curl.h>" test/ | xargs sed -i 's|<curl/curl.h>|"curl/curl.h"|'
        #sed -i 's|-I/usr/include/mysql |-I/usr/include/mysql -I$(CURL_IDIR) |' test/tap/tests/Makefile
        
        # build proxysql
        sed -i "/command/c \    command: bash -l -c 'cd /opt/proxysql && make -j$(nproc) debug_clickhouse WITHGCOV=1'" docker-compose.yml
        #cat docker-compose.yml
        make ubuntu22-dbg
        
        # build tap tests
        sed -i "/command/c \    command: bash -l -c 'cd /opt/proxysql && make -j$(nproc) build_tap_test_debug WITHGCOV=1'" docker-compose.yml
        #cat docker-compose.yml
        make ubuntu22-dbg
        
        # remove irrelevant tests (missing infra)
        cd test/tap/tests
        rm -f *clickhouse*-t
        #rm -f *cluster*-t
        rm -f *binlog*-t
        rm -f *mariadb*-t
        rm -f reg_test_3992_fast_forward_malformed_packet*-t
        rm -f test_cluster_sync-t
        rm -f test_cluster_sync_mysql_servers-t
        rm -f test_read_only_actions_offline_hard_servers-t
        rm -f test_auth_methods-t
        rm -f test_sqlite3_pass_exts-t
        
        # remove always failing tests
        rm -f test_rw_binary_data-t
        rm -f reg_test_3603-stmt_metadata-t
        rm -f test_log_last_insert_id-t
        rm -f test_session_status_flags-t
        rm -f set_testing-240-t
        rm -f reg_test_fast_forward_split_packet-t
        rm -f test_com_reset_connection_com_change_user-t
        rm -f test_cluster1-t
        rm -f test_server_sess_status-t
        
        # remove occasionaly failing tests
        rm -f max_connections_ff-t
        rm -f test_backend_conn_ping-t
        
    - name: Prepare tests
      id: set-matrix
      if: ${{ always() }}
      run: |
        find proxysql/src -type f -executable -print > proxysql/executable.txt
        find proxysql/test -type f -executable -print >> proxysql/executable.txt
        MATRIX="[ "$(find proxysql/test/tap/tests/ -type f -name '*-t' -executable -printf "'%f', ")" ]"
        #echo "matrix=${MATRIX}"
        echo "matrix=${MATRIX}" >> $GITHUB_OUTPUT
        
    - name: Archive artifacts
      if: ${{ always() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-run#${{ github.run_number }}
        path: |
          proxysql/src/
          proxysql/test/
          proxysql/deps/libssl/openssl-*/include/openssl
          proxysql/executable.txt
          

###############################################################################################################


  test:
    runs-on: ubuntu-22.04
    needs: [ build ]
    strategy:
      fail-fast: false
      matrix:
#        testgroup: [ '.*admin.*-t|.*basic.*-t|.*charset.*-t|.*firewall.*-t|.*kill.*-t|.*max.*-t', 'multiple.*-t', '.*mysql.*-t', '.*prepare.*-t', 'reg_test.*-t', 'save.*-t', '.*set.*-t', '.*sqlite.*-t', '.*ssl.*-t', 'test_.*-t' ]
#        testgroup: [ '' ]
#        testgroup: [ 'kill_connection3-t', 'reg_test_3847_admin_lock-t', 'set_testing-240-t', 'max_connections_ff-t', 'test_server_sess_status-t' ]
        testgroup: ${{ fromJson(needs.build.outputs.matrix) }}
    env:
      TESTGROUP: ${{ matrix.testgroup }}
      GIT_VERSION:

    steps:
    
    - name: Install dependencies
      run: |
        sudo apt-get update -y
        sudo apt-get install -y python3-pymysql python3-structlog sysbench mycli
        sudo pip3 install fastcov
        
        wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-client_3.2.6_amd64.deb
        sudo dpkg -i orchestrator-client_3.2.6_amd64.deb
        
        sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 467B942D3A79BD29
        wget https://repo.mysql.com/mysql-apt-config_0.8.24-1_all.deb
        sudo dpkg -i mysql-apt-config_0.8.24-1_all.deb
        sudo apt-get update -y
        sudo apt-cache policy mysql-shell
        sudo apt-get install -y mysql-shell

        sudo sed -i 's/8.0/5.7/' /etc/apt/sources.list.d/mysql.list
        sudo sed -i 's/jammy/bionic/' /etc/apt/sources.list.d/mysql.list
        sudo apt-get update -y
        sudo apt-cache policy libmysqlclient-dev
        sudo apt-get install -y --allow-downgrades libmysqlclient-dev=5.7*
        
    - name: Checkout proxysql
      uses: actions/checkout@v4
      with:
        repository: ${{ github.repository }}
        ref: ${{ env.SHA }}
        fetch-depth: 0
        path: 'proxysql'
        
    - name: Set GIT_VERSION
      run: |
        cd proxysql/
        git fetch --tags --force
        echo "GIT_VERSION=$(git describe --long --abbrev=7)" >> $GITHUB_ENV
        
    - name: Download build
      uses: actions/download-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.GIT_VERSION }}-run#${{ github.run_number }}
        path: proxysql
        
    - name: Fix build
      run: |
        cat proxysql/executable.txt | xargs -n1 chmod +x
        sudo ln -s ${{ github.workspace }}/proxysql /opt/proxysql
        
    - name: Checkout jenkins_build_scripts
      uses: actions/checkout@v4
      with:
        repository: 'proxysql/jenkins-build-scripts'
        ref: 'kubernetes'
#        fetch-depth: 0
        path: 'jenkins-build-scripts'
        token: ${{ secrets.GH_TOKEN_PROXYSQL }}
        submodules: 'false'
        
    - name: Configure env.sh
      run: |
        cd jenkins-build-scripts
        # configure paths
        sed -i "s|JENKINS_SCRIPTS_PATH=.*|JENKINS_SCRIPTS_PATH=${{ github.workspace }}/jenkins-build-scripts|" env.sh
        sed -i "s|WORKSPACE=.*|WORKSPACE=${{ github.workspace }}/proxysql|" env.sh
        
    - name: Configure test/env.sh
      run: |
        cd proxysql
        # select tests
        echo "export TEST_PY_INTERNAL=0" >> test/env.sh
        echo "export TEST_PY_BENCHMARK=0" >> test/env.sh
        echo "export TEST_PY_CHUSER=0" >> test/env.sh
        echo "export TEST_PY_STATS=0" >> test/env.sh
        echo "export TEST_PY_TAP=1" >> test/env.sh
        echo "export TEST_PY_TAPINT=0" >> test/env.sh
        echo "export TEST_PY_FAILOVER=0" >> test/env.sh
        echo 'export TEST_PY_TAP_INCL="${{ matrix.testgroup }}"' >> test/env.sh
        #echo 'export TEST_PY_TAP_EXCL=""' >> test/env.sh
        echo 'export MALLOC_CONF="retain:false"' >> test/env.sh
        echo 'export' >> test/env.sh
        cat test/env.sh
        
    - name: Docker-hoster
      run: |
        cd ${{ github.workspace }}/jenkins-build-scripts/infra-docker-hoster
        sed -i 's|image: .*|image: ghcr.io/mrmohebi/docker-hoster|' docker-compose.yml
        docker compose up -d
        
    - name: Run proxysql
      run: |
        set -x
        set +e
        cd ${{ github.workspace }}/jenkins-build-scripts
        source ./env.sh
        ./cluster_start.bash
        sleep 10
        
        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) &
        sleep 10
        mysql -uadmin -padmin -h127.0.0.1 -P6032 -e "SELECT version();"
        
        cd ${{ github.workspace }}/jenkins-build-scripts
        ./cluster_init.bash
        sleep 10        
        
    - name: Infra
      run: |
        cd ${{ github.workspace }}/jenkins-build-scripts
        source ./env.sh
        cd ${{ github.workspace }}/jenkins-build-scripts/infra-mysql57
        
        sed -i "s/\${INFRA}-\${CONTAINER}-1/\${INFRA}_\${CONTAINER}_1/" docker-compose-init.bash
        ./docker-compose-init.bash
        docker ps -a
        cat /etc/hosts
        
    - name: TAP-tests
      run: |
        set +e
        cd ${{ github.workspace }}/jenkins-build-scripts
        
        source ./env.sh
        env | sort
        sudo -E ./test-scripts/bin/proxysql-tester.py
        RC=$?
        
        SUMMARY="$(grep -Po "SUMMARY: .*" ${{ github.workspace }}/proxysql/ci_tests_logs/proxysql-tester.log)"
        echo "$SUMMARY" >> $GITHUB_STEP_SUMMARY
        
        exit $RC
        
    - name: Fix premissions
      if: ${{ failure() }}
      run: |
        sudo chmod -R 777 ${{ github.workspace }}/*
        echo "${{ matrix.testgroup }}" | tr -d '*|' | xargs -0 printf 'TESTGROUP=%s' >> $GITHUB_ENV
        
    - 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 }}-${{ env.TESTGROUP }}
        path: |
          proxysql/ci_*_logs/

    - name: Archive artifacts bins
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-bins-run#${{ github.run_number }}-${{ env.TESTGROUP }}
        path: |
          proxysql/binaries/
          proxysql/src/
          proxysql/libs/
#          proxysql/deps/

    - name: Archive artifacts tests
      if: ${{ failure() && !cancelled() }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ github.workflow }}-${{ env.SHA }}-tests-run#${{ github.run_number }}-${{ env.TESTGROUP }}
        path: |
          proxysql/test/tap/tests*
          
