Skip to content

Commit 0bd960a

Browse files
committed
Add Docker test image and documentation
Based on PR pgpartman#279 by @jcoleman, this includes a Dockerfile for a PG17 based image, scripts to automate re/building the extension and re/creating the test DB, and notes on how to run the tests the same way as the CI pipeline.
1 parent 949bf56 commit 0bd960a

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

docker/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM pgxn/pgxn-tools:latest
2+
3+
RUN NO_CLUSTER=1 pg-start 17 postgresql-17-pgtap
4+
5+
ADD entrypoint.sh /usr/local/bin/
6+
7+
WORKDIR /pg_partman
8+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
9+
CMD ["/bin/bash"]

docker/README_docker.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Docker Environment
2+
3+
If you don't have a build environment setup on your local machine, the project provides a docker container setup for your convenience. As long as Docker is installed, you can use the following commands to run tests:
4+
5+
```sh
6+
# Build the image:
7+
docker build -t partman_test_image ./docker
8+
9+
# Start the container. This will initialize and start Postgres, then drop to a shell.
10+
docker run -it --name partman_test -v "$(pwd):/pg_partman" partman_test_image
11+
```
12+
13+
The source code for pg_partman is mounted inside the container rather than included in the build, so any changes can be tested without rebuilding the container.
14+
15+
Example commands to run inside the container:
16+
17+
```sh
18+
# Convenience script to build and install pg_partman, then create a database named partman_test.
19+
# This does a fresh build+install each time, and the database is recreated if it already exists.
20+
docker/build_for_tests.sh
21+
22+
# Run a specific test
23+
pg_prove -ovf -U postgres -d partman_test test/<test_file_path>.sql
24+
25+
# Build, install, create DB and run all tests
26+
docker/build_and_test.sh
27+
28+
# The script can run specific tests as well (relative to test/)
29+
docker/build_and_test.sh <test_file_path>.sql
30+
```
31+
32+
You can also start the container and run the tests in one command with:
33+
34+
```sh
35+
# All tests
36+
docker run --rm -it -v "$(pwd):/pg_partman" partman_test_image docker/build_and_test.sh
37+
38+
# Specific tests, again relative to test/
39+
docker run --rm -it -v "$(pwd):/pg_partman" partman_test_image docker/build_and_test.sh <test_file_path>.sql
40+
```
41+
42+
When finished, stop and optionally remove the container.
43+
44+
```sh
45+
docker stop partman_test
46+
docker rm partman_test
47+
```
48+
49+
### Replicating the CI environment
50+
51+
Both the above image and the GitHub CI pipeline use the [pgxn-tools](https://github.com/pgxn/docker-pgxn-tools) image. The Dockerfile and entrypoint in this folder are meant to ease iterative development, but you can use the standalone entrypoint to run the same steps that CI does.
52+
53+
```sh
54+
# Install PostgreSQL 17 and run all tests, discarding the container afterwards
55+
docker run -it --rm -w /pg_partman --volume "$(pwd):/pg_partman" pgxn/pgxn-tools docker/pgxn_standalone_entrypoint.sh 17
56+
```

docker/build_and_test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
7+
cd "$SCRIPT_DIR/.."
8+
9+
docker/build_for_tests.sh
10+
11+
cd test
12+
13+
pg_prove --dbname partman_test --username postgres --ext .sql --comments --verbose --failures ${@:-.}

docker/build_for_tests.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
7+
cd "$SCRIPT_DIR/.."
8+
9+
make && make install
10+
11+
psql -U postgres -c "DROP DATABASE IF EXISTS partman_test"
12+
psql -U postgres -c "CREATE DATABASE partman_test"
13+
psql -U postgres -d partman_test -c "CREATE EXTENSION pgtap"
14+
psql -U postgres -d partman_test -c "CREATE SCHEMA partman"
15+
psql -U postgres -d partman_test -c "CREATE EXTENSION pg_partman SCHEMA partman"

docker/entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
pg_createcluster 17 test --start -p 5432 --pgoption max_locks_per_transaction=128 -- -A trust
5+
6+
exec $@
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [ ! -n $1 ] ; then
5+
echo "Usage: $0 <PG_VERSION> [test_to_run.sql ...]" >&2
6+
exit 1
7+
elif ! [[ $1 =~ ^[0-9]+$ ]]; then
8+
echo "PG_VERSION must be a positive integer." >&2
9+
exit 1
10+
else
11+
PG_VERSION=$1
12+
shift
13+
fi
14+
15+
CREATE_OPTIONS="--pgoption max_locks_per_transaction=128" pg-start $PG_VERSION postgresql-$PG_VERSION-pgtap
16+
17+
exec /pg_partman/docker/build_and_test.sh $@

test/README_test.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ Tests for the Background Worker can be found in the test/test_bgw folder. Please
3939
Tests for procedures are in their own folders. These must be run in stages with manual commands between them since pgTap cannot handle distinct commits within a single test run.
4040
4141
Tests for the reindexing script are in development and will return once the reindexing feature has been updated. They can be found in the test/test_reindex folder. These tests cannot just be run all at once and are not run within rolled back transactions. They must be run in order, one at a time, and there are explicit instructions at the end of each test for what to do next.
42+
43+
### Running tests in Docker
44+
See [README_docker.md](../docker/README_docker.md) for instructions on testing inside a persistent container running PostgreSQL 17.
45+
46+
To run the same tests as GitHub CI (on PostgreSQL 17):
47+
```sh
48+
docker run -it --rm -w /pg_partman --volume "$(pwd):/pg_partman" pgxn/pgxn-tools docker/pgxn_standalone_entrypoint.sh 17
49+
```

0 commit comments

Comments
 (0)