Skip to content

Commit

Permalink
[pgmoneta#9] Test suite in container
Browse files Browse the repository at this point in the history
  • Loading branch information
GuChad369 committed Jul 13, 2024
1 parent d833196 commit 33eb760
Show file tree
Hide file tree
Showing 15 changed files with 867 additions and 567 deletions.
564 changes: 272 additions & 292 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

29 changes: 28 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)

project(pgmoneta_ext VERSION ${VERSION_STRING} LANGUAGES C)

include(CTest)
enable_testing()

set(CPACK_PACKAGE_VERSION_MAJOR ${VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${VERSION_PATCH})
Expand Down Expand Up @@ -58,6 +61,29 @@ endif()
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/src/")

set(generation TRUE)
set(check TRUE)
set(container FALSE)

find_package(Check)
if (CHECK_FOUND)
message(STATUS "check found")
add_library(Check::check SHARED IMPORTED)
set_target_properties(Check::check PROPERTIES
IMPORTED_LOCATION ${CHECK_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${CHECK_INCLUDE_DIR})
else ()
set(check FALSE)
message(STATUS "check needed. The test suite process will be skipped.")
endif()

find_package(Docker)
find_package(Podman)
if (DOCKER_FOUND OR PODMAN_FOUND)
set(container TRUE)
message(STATUS "Docker or podman found")
else ()
message(STATUS "Docker or podman needed. The test suite will be skipped.")
endif()

find_package(Pandoc)
if (PANDOC_FOUND)
Expand All @@ -84,4 +110,5 @@ endif()

add_subdirectory(doc)
add_subdirectory(src)
add_subdirectory(sql)
add_subdirectory(sql)
add_subdirectory(test)
29 changes: 29 additions & 0 deletions cmake/FindCheck.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# check support
#

find_package(PkgConfig)
pkg_check_modules(PC_CHECK check)

find_path(CHECK_INCLUDE_DIR
NAMES check.h
PATHS ${PC_CHECK_INCLUDE_DIRS}
PATH_SUFFIXES check
)

find_library(CHECK_LIBRARY
NAMES check
PATHS ${PC_CHECK_LIBRARY_DIRS}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Check DEFAULT_MSG CHECK_INCLUDE_DIR CHECK_LIBRARY)

if (CHECK_FOUND)
set(CHECK_LIBRARIES ${CHECK_LIBRARY})
set(CHECK_INCLUDE_DIRS ${CHECK_INCLUDE_DIR})
else ()
set(check FALSE)
endif ()

mark_as_advanced(CHECK_INCLUDE_DIR CHECK_LIBRARY)
18 changes: 18 additions & 0 deletions cmake/FindDocker.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Check for Docker
#

find_program(DOCKER_EXECUTABLE
NAMES docker
PATHS /usr/local/bin /usr/bin /bin
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Docker DEFAULT_MSG DOCKER_EXECUTABLE)

if (DOCKER_FOUND)
set(container TRUE)
set(DOCKER_EXECUTABLE ${DOCKER_EXECUTABLE})
endif ()

mark_as_advanced(DOCKER_EXECUTABLE)
18 changes: 18 additions & 0 deletions cmake/FindPodman.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Check for Podman
#

find_program(PODMAN_EXECUTABLE
NAMES podman
PATHS /usr/local/bin /usr/bin /bin
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Podman DEFAULT_MSG PODMAN_EXECUTABLE)

if (PODMAN_FOUND)
set(container TRUE)
set(PODMAN_EXECUTABLE ${PODMAN_EXECUTABLE})
endif ()

mark_as_advanced(PODMAN_EXECUTABLE)
100 changes: 68 additions & 32 deletions doc/TEST.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,107 @@
# Test

## Installation
## Container Environment

Since we are using source code to install, please first create a new user `postgres` for a normal installation.
To ensure the test suite works well, please make sure you have installed `Docker` or `podman` on your OS. If neither `Docker` nor `podman` is installed, the test suite compilation will be skipped.

``` sh
sudo adduser postgres
### Docker

First, ensure your system is up to date.

```sh
dnf update
```

Enter into the `/test` directory.
Install the necessary packages for Docker.

``` sh
chmod +x installation.sh
```sh
dnf -y install dnf-plugins-core
```

Run `installation.sh` to install any necessary dependencies and configuration.
Add the Docker repository to your system.

``` sh
./installation.sh
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
```

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).
Install Docker Engine, Docker CLI, and Containerd.

``` sh
./installation.sh 14
```sh
sudo dnf install docker-ce docker-ce-cli containerd.io
```

> 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.
Start the Docker service and enable it to start on boot.

```sh
sudo systemctl start docker
sudo systemctl enable docker
```

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.
Verify that Docker is installed correctly.

``` 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
```sh
docker --version
```

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.
If you see the Docker version, then you have successfully installed Docker on Fedora.

## Test functions
### Podman

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.
Install Podman and the Docker alias package.

download dependencies
```sh
dnf install podman podman-docker.noarch
```

``` sh
dnf install check check-devel
Verify that Podman is installed correctly.

```sh
podman --version
```

Enter into the `/test` directory.
If you see the Podman version, then you have successfully installed Podman on Fedora.

The `podman-docker.noarch` package simplifies the use of `Podman` for users accustomed to Docker.

## Test suite

Before you test, you need to install the `check` library. If there is no package for `check`, the `CMakeLists.txt` will not compile the test suite. Only after you have installed `check` will it compile the test suite.

``` sh
mkdir build
cd build
cmake ..
make
dnf install -y check check-devel check-static
```

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:
You can simply use `CTest` to test all PostgreSQL versions from 13 to 16. It will automatically run `testsuite.sh` to test `pgmoneta_ext` for each version. The script will automatically create the Docker container, run it, and then use the `check` framework to test their functions inside it. After that, it will automatically clean up everything for you.

Set the `PGPASSWORD` environment variable:
After you follow the [DEVELOPERS.md](https://github.com/pgmoneta/pgmoneta_ext/blob/main/doc/DEVELOPERS.md) to install `pgmoneta_ext`, go to the directory `/pgmoneta_ext/build` and run the test.

``` sh
export PGPASSWORD='secretpassword'
make test
```

Run the `pgmoneta_ext_test` executable.
This command will run the tests sequentially. If you want them to run simultaneously, please use the following command:

``` sh
ctest -j 8
```

This will run the tests in parallel using the given number of jobs.

`CTest` will output logs into `/pgmoneta_ext/build/Testing/Temporary/LastTest.log`. If you want to check the specific process, you can review that log file.

`testsuite.sh` accepts three variables. The first one is `dir`, which specifies the `/test` directory location, with a default value of `./`. The second one is `dockerfile`, with a default value of `Dockerfile.rocky8`. The third one is the PostgreSQL `version`, with a default value of `13`.

## Local test

If you just want to test if `pgmoneta_ext` works well on your machine, please follow these steps:

- Install `pgmoneta_ext` and create the extension in PostgreSQL.
- Navigate to the directory `/pgmoneta_ext/build/test`.
- Run the following commands:

```sh
export PGPASSWORD='secretpassword'
./pgmoneta_ext_test
```

The `pgmoneta_ext_test` executable will use `check` to run all the functions in the extension locally and verify if they work correctly.
100 changes: 68 additions & 32 deletions doc/manual/dev-03-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,108 @@

# Test suite

## Installation
## Container Environment

Since we are using source code to install, please first create a new user `postgres` for a normal installation.
To ensure the test suite works well, please make sure you have installed `Docker` or `podman` on your OS. If neither `Docker` nor `podman` is installed, the test suite compilation will be skipped.

``` sh
sudo adduser postgres
### Docker

First, ensure your system is up to date.

```sh
dnf update
```

Enter into the `/test` directory.
Install the necessary packages for Docker.

``` sh
chmod +x installation.sh
```sh
dnf -y install dnf-plugins-core
```

Run `installation.sh` to install any necessary dependencies and configuration.
Add the Docker repository to your system.

``` sh
./installation.sh
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
```

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].
Install Docker Engine, Docker CLI, and Containerd.

``` sh
./installation.sh 14
```sh
sudo dnf install docker-ce docker-ce-cli containerd.io
```

> 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.
Start the Docker service and enable it to start on boot.

```sh
sudo systemctl start docker
sudo systemctl enable docker
```

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.
Verify that Docker is installed correctly.

``` 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
```sh
docker --version
```

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.
If you see the Docker version, then you have successfully installed Docker on Fedora.

## Test functions
### Podman

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.
Install Podman and the Docker alias package.

download dependencies
```sh
dnf install podman podman-docker.noarch
```

``` sh
dnf install check check-devel
Verify that Podman is installed correctly.

```sh
podman --version
```

Enter into the `/test` directory.
If you see the Podman version, then you have successfully installed Podman on Fedora.

The `podman-docker.noarch` package simplifies the use of `Podman` for users accustomed to Docker.

## Test suite

Before you test, you need to install the `check` library. If there is no package for `check`, the `CMakeLists.txt` will not compile the test suite. Only after you have installed `check` will it compile the test suite.

``` sh
mkdir build
cd build
cmake ..
make
dnf install -y check check-devel check-static
```

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:
You can simply use `CTest` to test all PostgreSQL versions from 13 to 16. It will automatically run `testsuite.sh` to test `pgmoneta_ext` for each version. The script will automatically create the Docker container, run it, and then use the `check` framework to test their functions inside it. After that, it will automatically clean up everything for you.

Set the `PGPASSWORD` environment variable:
After you follow the [DEVELOPERS.md][developers] to install `pgmoneta_ext`, go to the directory `/pgmoneta_ext/build` and run the test.

``` sh
export PGPASSWORD='secretpassword'
make test
```

Run the `pgmoneta_ext_test` executable.
This command will run the tests sequentially. If you want them to run simultaneously, please use the following command:

``` sh
ctest -j 8
```

This will run the tests in parallel using the given number of jobs.

`CTest` will output logs into `/pgmoneta_ext/build/Testing/Temporary/LastTest.log`. If you want to check the specific process, you can review that log file.

`testsuite.sh` accepts three variables. The first one is `dir`, which specifies the `/test` directory location, with a default value of `./`. The second one is `dockerfile`, with a default value of `Dockerfile.rocky8`. The third one is the PostgreSQL `version`, with a default value of `13`.

## Local test

If you just want to test if `pgmoneta_ext` works well on your machine, please follow these steps:

- Install `pgmoneta_ext` and create the extension in PostgreSQL.
- Navigate to the directory `/pgmoneta_ext/build/test`.
- Run the following commands:

```sh
export PGPASSWORD='secretpassword'
./pgmoneta_ext_test
```

The `pgmoneta_ext_test` executable will use `check` to run all the functions in the extension locally and verify if they work correctly.
Loading

0 comments on commit 33eb760

Please sign in to comment.