Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PostgreSQL test DB setup documentation and script #45413

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 15 additions & 8 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ machine does not have an X server. In that case you need to run the test under
Make sure that you have enabled building of postgres test in CMake.
`cmake -DENABLE_TESTS=ON -DENABLE_PGTEST=ON ..`

To test the postgres provider you will need to have a database available to
which the postgres provider can connect. The server will need to have PostGIS
support enabled.
To test the postgres provider you will need to setup a test database
to which the postgres provider can connect.

By default the tests use the following connection string:
By default the tests use the following connection string to access
the test database:

service=qgis_test

Expand All @@ -66,19 +66,26 @@ If these do not match your setup you can set the environment variable
rely on standard libpq environment variables to tweak host, port user
and password (PGHOST, PGPORT, PGUSER, PGPASSWORD).

Please note that the test database needs to be initialized using
the sql-scripts:
The test database must be initialized using the sql-scripts:

tests/testdata/provider/testdata_pg*.sql

They take care of activating PostGIS for the test database and
create some tables containing test data.
They take care of activating PostGIS and creating some tables containing
test data.

For convenience, a shell script is provided to create the database
and initialize it as needed:

tests/testdata/provider/testdata_pg.sh

Some tests will attempt to create database users too and expect them to
be allowed to connect using a password.
Most PostgreSQL installation by default use "ident" authentication
model when connecting via unix sockets. Make sure the `qgis_test`
service (or your `QGIS_PGTEST_DB` connection string) uses a connection
method which allows passing username/password pair to connect (and
which allows creating users)

# Write tests

Instructions about writing tests for the processing framework
Expand Down
68 changes: 43 additions & 25 deletions tests/testdata/provider/testdata_pg.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/sh

DB=${DB:-qgis_test}
DB=${1:-qgis_test}

cd $(dirname $0)/../../../ || exit 1
cd "$(dirname "$0")"/../../../ || exit 1

SCRIPTS="
tests/testdata/provider/testdata_pg.sql
Expand All @@ -26,41 +26,59 @@ SCRIPTS12="
tests/testdata/provider/testdata_pg_12_generated.sql
"

dropdb --if-exists $DB
createdb $DB -E UTF8 -T template0 || exit 1
echo "Dropping DB $DB"
dropdb --if-exists "${DB}"
echo "Creating DB $DB"
# TODO: use service=qgis_test to connect to "template1" and use SQL ?
createdb "${DB}" -E UTF8 -T template0 || exit 1
for f in ${SCRIPTS}; do
echo "Restoring $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f $f $DB -v ON_ERROR_STOP=1 || exit 1
echo "Loading $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f "${f}" "${DB}" -v ON_ERROR_STOP=1 || exit 1
done

PGSERVERVERSION=$(psql -XtA -c 'SHOW server_version_num' $DB)
if test $PGSERVERVERSION -gt 120000; then
PGSERVERVERSION=$(psql -XtA -c 'SHOW server_version_num' "${DB}")
if test "${PGSERVERVERSION}" -gt 120000; then
for f in ${SCRIPTS12}; do
echo "Restoring $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f $f $DB -v ON_ERROR_STOP=1 || exit 1
echo "Loading $f"
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f "${f}" "${DB}" -v ON_ERROR_STOP=1 || exit 1
done
fi

# Test existence of qgis_test service, and recommend how to set it up
# otherwise
TESTDB=$(psql -XtA 'service=qgis_test' -c "select current_database()")
if test "${TESTDB}" != "${DB}"; then
FINGERPRINT="${DB}-$(date +%s)"
echo "Storing fingerprint ${FINGERPRINT} in $DB"
psql -q --echo-errors "${DB}" -v ON_ERROR_STOP=1 <<EOF || exit 1
CREATE TABLE qgis_test.schema_info AS SELECT
'fingerprint' var, '${FINGERPRINT}' val;
EOF

# Test service=qgis_test connects to the just-created database
CHECK=$(psql -XtA 'service=qgis_test' -c "select val from qgis_test.schema_info where var='fingerprint'")
if test "${CHECK}" != "${FINGERPRINT}"; then
exec >&2
if test -n "${TESTDB}"; then
echo "WARNING: [qgis_test] service section points to db '${TESTDB}'" \
"but we populated db '${DB}' instead"
else
echo "ERROR: [qgis_test] service not found in ~/.pg_service.conf"
echo "HINT: create a section like the following:"
cat <<EOF
[qgis_test]
echo "ERROR: Could not access the just created test database ${DB} via service=qgis_test"
echo "HINT: create a section like the following in ~/.pg_service.conf"
cat <<EOF
[qgis_test]
host=localhost
port=5432
dbname=${DB}
user=USERNAME
password=PASSWORD
EOF
fi
else
echo "Database ${DB} populated and ready for use with 'service=qgis_test'"
exit 1
fi

# TODO: Test service=qgis_test connects via a method which accepts
# username/password
CHECK=$(psql -XtA 'service=qgis_test user=qgis_test_user password=qgis_test_user_password' -c "select version()")
if test -z "${CHECK}"; then
exec >&2
echo "ERROR: Cannot service=qgis_test via username/password"
echo "HINT: make sure MD5 method is accepted in pg_hba.conf "
echo "(specifying host=localhost in the [qgis_test] section of "
echo "'~/.pg_service.conf' often does help)"
exit 1
fi


echo "Database ${DB} populated and ready for use with 'service=qgis_test'"