diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 0000000..a28fdeb --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,32 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: bug +assignees: '' + +--- + +**Describe the bug** + +A clear and concise description of what the bug is. + +**To Reproduce** + +Steps to reproduce the behavior. + +**Version** + +What is the version of pgmoneta_ext ? + +**PostgreSQL** + +What is the version of PostgreSQL ? + +**Debug logs** + +Can you provide any debug logs (`log_level = debug5`) of the issue ? + +**Tip** + +Use \`\`\` before and after the text to keep the output as is. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 0000000..2fd92b9 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,20 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: feature +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** + +A clear and concise description of what the problem is. + +**Describe the solution you'd like** + +A clear and concise description of what you want to happen. + +**Additional information** + +Any additional information you can provide, like an overall design description diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca6e3ee..57c2b64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,49 +10,311 @@ on: - main jobs: - build-linux: + test-postgresql-13: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Update system - run: sudo apt update -y + run: sudo apt-get update -y - name: Install dependencies - run: sudo apt install -y git gcc cmake make postgresql postgresql-server-dev-all - - name: Init database + run: sudo apt-get install -y --no-install-recommends git gcc cmake make wget build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libxml2-utils xsltproc ccache pkg-config check + - name: Test PostgreSQL 13 run: | - version=$(pg_config --version | grep -Eo "[0-9]{1,2}" | head -1) - sudo -u postgres /usr/lib/postgresql/${version}/bin/initdb /tmp/pgsql - - name: Config postgres + set -e + install_postgresql() { + version=${1:-13} + git_repo="https://github.com/postgres/postgres.git" + cd /tmp + git clone --branch "REL_${version}_STABLE" --single-branch --depth 1 "$git_repo" + cd /tmp/postgres + ./configure --prefix=/usr/local/pgsql + make + sudo make install + sudo -u postgres /usr/local/pgsql/bin/initdb -D /tmp/pgsql-${version} + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile start + export PATH=/usr/local/pgsql/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/pgsql/lib + } + + config_postgresql(){ + version=${1:-13} + sudo -u postgres sed -i 's/^#\s*password_encryption\s*=\s*\(md5\|scram-sha-256\)/password_encryption = scram-sha-256/' /tmp/pgsql-${version}/postgresql.conf + sudo -u postgres sed -i '/^host/d' /tmp/pgsql-${version}/pg_hba.conf + sudo -u postgres bash -c "cat <> /tmp/pgsql-${version}/pg_hba.conf + host postgres repl 127.0.0.1/32 scram-sha-256 + host postgres repl ::1/128 scram-sha-256 + host replication repl 127.0.0.1/32 scram-sha-256 + host replication repl ::1/128 scram-sha-256 + EOF" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile restart + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" + } + + install_pgmoneta_ext() { + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/ + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/build/ + cmake .. + make + sudo make install + } + + create_pgmoneta_ext(){ + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP EXTENSION IF EXISTS pgmoneta_ext;" + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE EXTENSION pgmoneta_ext;" + } + + test_pgmoneta_ext(){ + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test/build + cmake .. + make + export PGPASSWORD='secretpassword' + ./pgmoneta_ext_test + unset PGPASSWORD + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP ROLE repl;" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile stop + } + + echo "******************* START PostgreSQL 13 ******************" + install_postgresql 13 + config_postgresql 13 + install_pgmoneta_ext + create_pgmoneta_ext + test_pgmoneta_ext + echo "******************* PostgreSQL 13 Success ******************" + + test-postgresql-14: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Update system + run: sudo apt-get update -y + - name: Install dependencies + run: sudo apt-get install -y --no-install-recommends git gcc cmake make wget build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libxml2-utils xsltproc ccache pkg-config check + - name: Test PostgreSQL 14 run: | - sudo -u postgres sed -i 's/#password_encryption = scram-sha-256/password_encryption = scram-sha-256/' /tmp/pgsql/postgresql.conf - sudo -u postgres sed -i '/^host/d' /tmp/pgsql/pg_hba.conf - echo 'cat <> /tmp/pgsql/pg_hba.conf + set -e + + install_postgresql() { + version=${1:-13} + git_repo="https://github.com/postgres/postgres.git" + cd /tmp + git clone --branch "REL_${version}_STABLE" --single-branch --depth 1 "$git_repo" + cd /tmp/postgres + ./configure --prefix=/usr/local/pgsql + make + sudo make install + sudo -u postgres /usr/local/pgsql/bin/initdb -D /tmp/pgsql-${version} + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile start + export PATH=/usr/local/pgsql/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/pgsql/lib + } + + config_postgresql(){ + version=${1:-13} + sudo -u postgres sed -i 's/^#\s*password_encryption\s*=\s*\(md5\|scram-sha-256\)/password_encryption = scram-sha-256/' /tmp/pgsql-${version}/postgresql.conf + sudo -u postgres sed -i '/^host/d' /tmp/pgsql-${version}/pg_hba.conf + sudo -u postgres bash -c "cat <> /tmp/pgsql-${version}/pg_hba.conf host postgres repl 127.0.0.1/32 scram-sha-256 host postgres repl ::1/128 scram-sha-256 host replication repl 127.0.0.1/32 scram-sha-256 host replication repl ::1/128 scram-sha-256 - EOF - '| sudo -u postgres -s - - name: Setup postgres + EOF" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile restart + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" + } + + install_pgmoneta_ext() { + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/ + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/build/ + cmake .. + make + sudo make install + } + + create_pgmoneta_ext(){ + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP EXTENSION IF EXISTS pgmoneta_ext;" + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE EXTENSION pgmoneta_ext;" + } + + test_pgmoneta_ext(){ + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test/build + cmake .. + make + export PGPASSWORD='secretpassword' + ./pgmoneta_ext_test + unset PGPASSWORD + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP ROLE repl;" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile stop + } + + echo "******************* START PostgreSQL 14 ******************" + install_postgresql 14 + config_postgresql 14 + install_pgmoneta_ext + create_pgmoneta_ext + test_pgmoneta_ext + echo "******************* PostgreSQL 14 Success ******************" + + test-postgresql-15: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Update system + run: sudo apt-get update -y + - name: Install dependencies + run: sudo apt-get install -y --no-install-recommends git gcc cmake make wget build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libxml2-utils xsltproc ccache pkg-config check + - name: Test PostgreSQL 15 run: | - version=$(pg_config --version | grep -Eo "[0-9]{1,2}" | head -1) - sudo -u postgres /usr/lib/postgresql/${version}/bin/pg_ctl start -D /tmp/pgsql - sudo -u postgres /usr/lib/postgresql/${version}/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" - - name: GCC/mkdir - run: mkdir build - working-directory: /home/runner/work/pgmoneta/pgmoneta_ext/ - - name: GCC/cmake - run: export CC=/usr/bin/gcc && cmake -DCMAKE_BUILD_TYPE=Debug .. - working-directory: /home/runner/work/pgmoneta/pgmoneta_ext/build/ - - name: GCC/make - run: make - working-directory: /home/runner/work/pgmoneta/pgmoneta_ext/build/ - - name: rm -Rf - run: rm -Rf build/ - working-directory: /home/runner/work/pgmoneta/pgmoneta_ext/ - - name: Cleanup + set -e + install_postgresql() { + version=${1:-13} + git_repo="https://github.com/postgres/postgres.git" + cd /tmp + git clone --branch "REL_${version}_STABLE" --single-branch --depth 1 "$git_repo" + cd /tmp/postgres + ./configure --prefix=/usr/local/pgsql + make + sudo make install + sudo -u postgres /usr/local/pgsql/bin/initdb -D /tmp/pgsql-${version} + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile start + export PATH=/usr/local/pgsql/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/pgsql/lib + } + + config_postgresql(){ + version=${1:-13} + sudo -u postgres sed -i 's/^#\s*password_encryption\s*=\s*\(md5\|scram-sha-256\)/password_encryption = scram-sha-256/' /tmp/pgsql-${version}/postgresql.conf + sudo -u postgres sed -i '/^host/d' /tmp/pgsql-${version}/pg_hba.conf + sudo -u postgres bash -c "cat <> /tmp/pgsql-${version}/pg_hba.conf + host postgres repl 127.0.0.1/32 scram-sha-256 + host postgres repl ::1/128 scram-sha-256 + host replication repl 127.0.0.1/32 scram-sha-256 + host replication repl ::1/128 scram-sha-256 + EOF" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile restart + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" + } + + install_pgmoneta_ext() { + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/ + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/build/ + cmake .. + make + sudo make install + } + + create_pgmoneta_ext(){ + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP EXTENSION IF EXISTS pgmoneta_ext;" + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE EXTENSION pgmoneta_ext;" + } + + test_pgmoneta_ext(){ + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test/build + cmake .. + make + export PGPASSWORD='secretpassword' + ./pgmoneta_ext_test + unset PGPASSWORD + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP ROLE repl;" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile stop + } + + echo "******************* START PostgreSQL 15 ******************" + install_postgresql 15 + config_postgresql 15 + install_pgmoneta_ext + create_pgmoneta_ext + test_pgmoneta_ext + echo "******************* PostgreSQL 15 Success ******************" + + test-postgresql-16: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Update system + run: sudo apt-get update -y + - name: Install dependencies + run: sudo apt-get install -y --no-install-recommends git gcc cmake make wget build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libxml2-utils xsltproc ccache pkg-config check + - name: Test PostgreSQL 16 run: | - version=$(pg_config --version | grep -Eo "[0-9]{1,2}" | head -1) - sudo -u postgres /usr/lib/postgresql/${version}/bin/pg_ctl stop -D /tmp/pgsql \ No newline at end of file + set -e + install_postgresql() { + version=${1:-13} + git_repo="https://github.com/postgres/postgres.git" + cd /tmp + git clone --branch "REL_${version}_STABLE" --single-branch --depth 1 "$git_repo" + cd /tmp/postgres + ./configure --prefix=/usr/local/pgsql + make + sudo make install + sudo -u postgres /usr/local/pgsql/bin/initdb -D /tmp/pgsql-${version} + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile start + export PATH=/usr/local/pgsql/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/pgsql/lib + } + + config_postgresql(){ + version=${1:-13} + sudo -u postgres sed -i 's/^#\s*password_encryption\s*=\s*\(md5\|scram-sha-256\)/password_encryption = scram-sha-256/' /tmp/pgsql-${version}/postgresql.conf + sudo -u postgres sed -i '/^host/d' /tmp/pgsql-${version}/pg_hba.conf + sudo -u postgres bash -c "cat <> /tmp/pgsql-${version}/pg_hba.conf + host postgres repl 127.0.0.1/32 scram-sha-256 + host postgres repl ::1/128 scram-sha-256 + host replication repl 127.0.0.1/32 scram-sha-256 + host replication repl ::1/128 scram-sha-256 + EOF" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile restart + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" + } + + install_pgmoneta_ext() { + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/ + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/build/ + cmake .. + make + sudo make install + } + + create_pgmoneta_ext(){ + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP EXTENSION IF EXISTS pgmoneta_ext;" + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE EXTENSION pgmoneta_ext;" + } + + test_pgmoneta_ext(){ + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test + mkdir build + cd /home/runner/work/pgmoneta_ext/pgmoneta_ext/test/build + cmake .. + make + export PGPASSWORD='secretpassword' + ./pgmoneta_ext_test + unset PGPASSWORD + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP ROLE repl;" + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile stop + } + + echo "******************* START PostgreSQL 16 ******************" + install_postgresql 16 + config_postgresql 16 + install_pgmoneta_ext + create_pgmoneta_ext + test_pgmoneta_ext + echo "******************* PostgreSQL 16 Success ******************" diff --git a/doc/TEST.md b/doc/TEST.md new file mode 100644 index 0000000..e0f7e38 --- /dev/null +++ b/doc/TEST.md @@ -0,0 +1,71 @@ +# Test + +## Installation + +Since we are using source code to install, please first create a new user `postgres` for a normal installation. + +``` sh +sudo adduser postgres +``` + +Enter into the `/test` directory. + +``` sh +chmod +x installation.sh +``` + +Run `installation.sh` to install any necessary dependencies and configuration. + +``` sh +./installation.sh +``` + +The script will install PostgreSQL 13 by default. If you prefer to install a specific version, please input the version as shown below. It will automatically download and use the latest patch release number. You can find the source versions at [PostgreSQL GIT repository](https://github.com/postgres/postgres). + +``` sh +./installation.sh 14 +``` + +> IMPORTANT: +> If you want to test different versions, please make sure you have deleted all previous version data, removed the role 'repl', and stopped the PostgreSQL server. Otherwise, it will raise some compatibility errors when you install and configure a new version. It is better to kill all current PostgreSQL processes. + +If you encounter the following error when creating the extension, you may need to inform the system where to find the `libpq.so.5` library. Use the command `echo "/usr/local/pgsql/lib" | sudo tee -a /etc/ld.so.conf` and then update the cache using `sudo ldconfig`. After this, you need to create the extension manually. + +``` console +ERROR: could not load library "/usr/local/pgsql/lib/pgmoneta_ext.so": libpq.so.5: cannot open shared object file: No such file or directory +``` + +If you see the error `could not change directory to "/home/pgmoneta/pgmoneta_ext/test/build": Permission denied`, it doesn't matter. This will not affect the execution of the command; it is just because you used `sudo`, which tries to change the current working directory. + +## Test functions + +If you have already installed `pgmoneta_ext` successfully and completed all configurations described in [DEVELOPERS.md](https://github.com/pgmoneta/pgmoneta_ext/blob/main/doc/DEVELOPERS.md#developer-guide), you can skip the **Installation** step above and directly follow the steps below to test the functions in the API extension. + +download dependencies + +``` sh +dnf install check check-devel +``` + +Enter into the `/test` directory. + +``` sh +mkdir build +cd build +cmake .. +make +``` + +Before running the `pgmoneta_ext_test` executable, you need to set the `PGPASSWORD` environment variable for the `repl` role. For our setup, the password is `secretpassword`. Follow these steps: + +Set the `PGPASSWORD` environment variable: + +``` sh +export PGPASSWORD='secretpassword' +``` + +Run the `pgmoneta_ext_test` executable. + +``` sh +./pgmoneta_ext_test +``` diff --git a/doc/manual/99-references.md b/doc/manual/99-references.md index eaf9932..15149dd 100644 --- a/doc/manual/99-references.md +++ b/doc/manual/99-references.md @@ -13,6 +13,7 @@ [make]: https://www.gnu.org/software/make/ [clang]: https://clang.llvm.org/ [pandoctemplate]: https://github.com/Wandmalfarbe/pandoc-latex-template + [postgresqlrepo]: https://github.com/postgres/postgres [ask]: https://github.com/pgmoneta/pgmoneta_ext/discussions @@ -23,3 +24,6 @@ [star]: https://github.com/pgmoneta/pgmoneta_ext/stargazers [twitter]: https://twitter.com/pgmoneta/ [license]: https://opensource.org/licenses/BSD-3-Clause + + + [developers]: https://github.com/pgmoneta/pgmoneta_ext/blob/main/doc/DEVELOPERS.md#developer-guide diff --git a/doc/manual/dev-03-test.md b/doc/manual/dev-03-test.md new file mode 100644 index 0000000..bbfbc4b --- /dev/null +++ b/doc/manual/dev-03-test.md @@ -0,0 +1,73 @@ +\newpage + +# Test suite + +## Installation + +Since we are using source code to install, please first create a new user `postgres` for a normal installation. + +``` sh +sudo adduser postgres +``` + +Enter into the `/test` directory. + +``` sh +chmod +x installation.sh +``` + +Run `installation.sh` to install any necessary dependencies and configuration. + +``` sh +./installation.sh +``` + +The script will install PostgreSQL 13 by default. If you prefer to install a specific version, please input the version as shown below. It will automatically download and use the latest patch release number. You can find the source versions at [PostgreSQL GIT repository][postgresqlrepo]. + +``` sh +./installation.sh 14 +``` + +> IMPORTANT: +> If you want to test different versions, please make sure you have deleted all previous version data, removed the role 'repl', and stopped the PostgreSQL server. Otherwise, it will raise some compatibility errors when you install and configure a new version. It is better to kill all current PostgreSQL processes. + +If you encounter the following error when creating the extension, you may need to inform the system where to find the `libpq.so.5` library. Use the command `echo "/usr/local/pgsql/lib" | sudo tee -a /etc/ld.so.conf` and then update the cache using `sudo ldconfig`. After this, you need to create the extension manually. + +``` console +ERROR: could not load library "/usr/local/pgsql/lib/pgmoneta_ext.so": libpq.so.5: cannot open shared object file: No such file or directory +``` + +If you see the error `could not change directory to "/home/pgmoneta/pgmoneta_ext/test/build": Permission denied`, it doesn't matter. This will not affect the execution of the command; it is just because you used `sudo`, which tries to change the current working directory. + +## Test functions + +If you have already installed `pgmoneta_ext` successfully and completed all configurations described in [DEVELOPERS.md][developers], you can skip the **Installation** step above and directly follow the steps below to test the functions in the API extension. + +download dependencies + +``` sh +dnf install check check-devel +``` + +Enter into the `/test` directory. + +``` sh +mkdir build +cd build +cmake .. +make +``` + +Before running the `pgmoneta_ext_test` executable, you need to set the `PGPASSWORD` environment variable for the `repl` role. For our setup, the password is `secretpassword`. Follow these steps: + +Set the `PGPASSWORD` environment variable: + +``` sh +export PGPASSWORD='secretpassword' +``` + +Run the `pgmoneta_ext_test` executable. + +``` sh +./pgmoneta_ext_test +``` diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..c8ede32 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,69 @@ +# +# Copyright (C) 2024 The pgmoneta community +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list +# of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this +# list of conditions and the following disclaimer in the documentation and/or other +# materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may +# be used to endorse or promote products derived from this software without specific +# prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +cmake_minimum_required(VERSION 3.14) + +project(pgmoneta_ext_test C) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") + +find_library(CHECK_LIBRARY check PATHS /usr/lib64) +find_path(CHECK_INCLUDE_DIR check.h PATHS /usr/include) + +if(CHECK_LIBRARY AND CHECK_INCLUDE_DIR) + add_library(Check::check SHARED IMPORTED) + set_target_properties(Check::check PROPERTIES + IMPORTED_LOCATION ${CHECK_LIBRARY} + INTERFACE_INCLUDE_DIRECTORIES ${CHECK_INCLUDE_DIR}) +else() + message(FATAL_ERROR "Check library not found") +endif() + +if(EXISTS "/etc/debian_version") + include_directories(/usr/include/subunit) +endif() + +set(SOURCES + pgmoneta_ext_test.c + runner.c +) + +add_executable(pgmoneta_ext_test ${SOURCES}) + +if(EXISTS "/etc/debian_version") + target_link_libraries(pgmoneta_ext_test Check::check subunit pthread rt m) +else() + target_link_libraries(pgmoneta_ext_test Check::check pthread rt m) +endif() + + +add_custom_target(custom_clean + COMMAND ${CMAKE_COMMAND} -E remove -f *.o pgmoneta_ext_test + COMMENT "Cleaning up..." +) \ No newline at end of file diff --git a/test/installation.sh b/test/installation.sh new file mode 100644 index 0000000..7f4243a --- /dev/null +++ b/test/installation.sh @@ -0,0 +1,177 @@ +: ' + * Copyright (C) 2024 The pgmoneta community + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may + * be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + *' + +#!/bin/bash + +install_dependencies_debian() { + export DEBIAN_FRONTEND=noninteractive + sudo apt-get update + sudo apt-get install -y --no-install-recommends git gcc cmake make wget build-essential libreadline-dev zlib1g-dev flex bison libxml2-dev libxslt1-dev libssl-dev libxml2-utils xsltproc ccache pkg-config +} + +install_dependencies_redhat() { + sudo yum install -y git gcc cmake make bison-devel readline-devel zlib-devel openssl-devel wget ccache libicu-devel flex libxml2-devel libxslt-devel perl + sudo yum groupinstall -y 'Development Tools' +} + +check_postgresql_installed() { + if command -v pg_config >/dev/null 2>&1; then + return 0 + else + return 1 + fi +} + +install_postgresql() { + version=${1:-13} + git_repo="https://github.com/postgres/postgres.git" + + if [ -f /etc/debian_version ] || [ -f /etc/redhat-release ]; then + cd /tmp + git clone --branch "REL_${version}_STABLE" --single-branch --depth 1 "$git_repo" + cd postgres + ./configure --prefix=/usr/local/pgsql + make + sudo make install + sudo -u postgres /usr/local/pgsql/bin/initdb -D /tmp/pgsql-${version} + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile start + export PATH=/usr/local/pgsql/bin:$PATH + export LD_LIBRARY_PATH=/usr/local/pgsql/lib + else + echo "Unsupported OS" + exit 1 + fi + + check_postgresql_installed + if [ $? -eq 0 ]; then + echo "PostgreSQL installed successfully." + else + echo "Failed to install PostgreSQL." + exit 1 + fi +} + +install_pgmoneta_ext() { + cd /tmp + git clone https://github.com/pgmoneta/pgmoneta_ext + cd pgmoneta_ext + mkdir build + cd build + cmake .. + make + sudo make install +} + +config_postgresql(){ + version=${1:-13} + if [ -f /etc/debian_version ] || [ -f /etc/redhat-release ]; then + sudo -u postgres sed -i 's/^#\s*password_encryption\s*=\s*\(md5\|scram-sha-256\)/password_encryption = scram-sha-256/' /tmp/pgsql-${version}/postgresql.conf + sudo -u postgres sed -i '/^host/d' /tmp/pgsql-${version}/pg_hba.conf + + sudo -u postgres bash -c "cat <> /tmp/pgsql-${version}/pg_hba.conf +host postgres repl 127.0.0.1/32 scram-sha-256 +host postgres repl ::1/128 scram-sha-256 +host replication repl 127.0.0.1/32 scram-sha-256 +host replication repl ::1/128 scram-sha-256 +EOF" + + sudo -u postgres /usr/local/pgsql/bin/pg_ctl -D /tmp/pgsql-${version} -l /tmp/logfile restart + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE ROLE repl WITH LOGIN REPLICATION PASSWORD 'secretpassword';" + else + echo "Unsupported OS" + exit 1 + fi +} + +create_pgmoneta_ext(){ + if [ -f /etc/debian_version ] || [ -f /etc/redhat-release ]; then + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "DROP EXTENSION IF EXISTS pgmoneta_ext;" + sudo -u postgres /usr/local/pgsql/bin/psql -U postgres -c "CREATE EXTENSION pgmoneta_ext;" + else + echo "Unsupported OS" + exit 1 + fi +} + +run_tests() { + local version=$1 + + valid_versions=("13" "14" "15" "16") + if [[ ! " ${valid_versions[@]} " =~ " ${version} " ]]; then + echo "Invalid version. Please provide a version of 13, 14, 15, or 16." + exit 1 + fi + + if [ -f /etc/debian_version ]; then + install_dependencies_debian + elif [ -f /etc/redhat-release ]; then + install_dependencies_redhat + else + echo "Unsupported OS for installing dependencies." + exit 1 + fi + + check_postgresql_installed + if [ $? -eq 0 ]; then + echo "Skipping PostgreSQL installation." + else + install_postgresql "$version" + fi + + echo "Configuring PostgreSQL..." + config_postgresql "$version" + if [ $? -ne 0 ]; then + echo "PostgreSQL configuration failed." + fi + + echo "Installing pgmoneta_ext..." + install_pgmoneta_ext + if [ $? -eq 0 ]; then + echo "pgmoneta_ext installation successful." + else + echo "pgmoneta_ext installation failed." + exit 1 + fi + + echo "Running PostgreSQL..." + pg_isready + if [ $? -eq 0 ]; then + echo "PostgreSQL is ready to accept connections." + else + echo "PostgreSQL is not ready." + exit 1 + fi + + echo "Create pgmoneta_ext..." + create_pgmoneta_ext + +} + +version=${1:-13} + +run_tests "$version" diff --git a/test/pgmoneta_ext_test.c b/test/pgmoneta_ext_test.c new file mode 100644 index 0000000..1dae440 --- /dev/null +++ b/test/pgmoneta_ext_test.c @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2024 The pgmoneta community + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may + * be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "pgmoneta_ext_test.h" + +#define BUFFER_SIZE 8192 + +#define PGMONETA_EXT_VERSION_RESULT "0.1.0" +#define PGMONETA_EXT_SWITCH_WAL_RESULT "(f,)" +#define PGMONETA_EXT_CHECKPOINT_RESULT "(f,)" + +int +execute_command(const char* command, char* output, size_t output_size) +{ + FILE* fp; + fp = popen(command, "r"); + if (fp == NULL) + { + return -1; + } + + if (fgets(output, output_size, fp) == NULL) + { + pclose(fp); + return -1; + } + + pclose(fp); + return 0; +} + +START_TEST(test_pgmoneta_ext_version) +{ + char output[BUFFER_SIZE]; + int result = execute_command("psql -h localhost -p 5432 -U repl -d postgres -t -c 'SELECT pgmoneta_ext_version();'", output, BUFFER_SIZE); + ck_assert_int_eq(result, 0); + ck_assert_msg(strstr(output, PGMONETA_EXT_VERSION_RESULT) != NULL, "Expected version string not found in output: %s", output); +} +END_TEST + +START_TEST(test_pgmoneta_ext_switch_wal) +{ + char output[BUFFER_SIZE]; + int result = execute_command("psql -h localhost -p 5432 -U repl -d postgres -t -c 'SELECT pgmoneta_ext_switch_wal();'", output, BUFFER_SIZE); + ck_assert_int_eq(result, 0); + ck_assert_msg(strstr(output, PGMONETA_EXT_SWITCH_WAL_RESULT) != NULL, "Expected WAL switch result not found in output: %s", output); +} +END_TEST + +START_TEST(test_pgmoneta_ext_checkpoint) +{ + char output[BUFFER_SIZE]; + int result = execute_command("psql -h localhost -p 5432 -U repl -d postgres -t -c 'SELECT pgmoneta_ext_checkpoint();'", output, BUFFER_SIZE); + ck_assert_int_eq(result, 0); + ck_assert_msg(strstr(output, PGMONETA_EXT_CHECKPOINT_RESULT) != NULL, "Expected checkpoint result not found in output: %s", output); +} +END_TEST + +Suite* +pgmoneta_ext_suite(void) +{ + Suite* s; + TCase* tc_core; + + s = suite_create("pgmoneta_ext"); + + tc_core = tcase_create("Core"); + + tcase_add_test(tc_core, test_pgmoneta_ext_version); + tcase_add_test(tc_core, test_pgmoneta_ext_switch_wal); + tcase_add_test(tc_core, test_pgmoneta_ext_checkpoint); + suite_add_tcase(s, tc_core); + + return s; +} diff --git a/test/pgmoneta_ext_test.h b/test/pgmoneta_ext_test.h new file mode 100644 index 0000000..05b86ad --- /dev/null +++ b/test/pgmoneta_ext_test.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2024 The pgmoneta community + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may + * be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef PGMONETA_EXT_TEST_H +#define PGMONETA_EXT_TEST_H + +#include +#include +#include +#include + +/** + * Execute a shell command and capture its output + * @param command The shell command to execute + * @param output A buffer to store the command's output + * @param output_size The size of the output buffer + * @return 0 on success, -1 on error + */ +int +execute_command(const char* command, char* output, size_t output_size); + +/** + * Set up a suite of test cases for pgmoneta_ext + * @return The result + */ +Suite* +pgmoneta_ext_suite(void); + +#endif // PGMONETA_EXT_TEST_H diff --git a/test/runner.c b/test/runner.c new file mode 100644 index 0000000..7589d55 --- /dev/null +++ b/test/runner.c @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2024 The pgmoneta community + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may + * be used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR + * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "pgmoneta_ext_test.h" + +int +main(void) +{ + int number_failed; + Suite* s; + SRunner* sr; + + s = pgmoneta_ext_suite(); + sr = srunner_create(s); + + // Run the tests in verbose mode + srunner_run_all(sr, CK_VERBOSE); + number_failed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} \ No newline at end of file