Skip to content

Commit 257e2fa

Browse files
committed
pkg-testsuite: add testing helpers
This is already used by some Fedora packages, and it is worth maintaining on one place. * Makefile.am (_AX_TEXT_ADDITIONAL_SED_SUBSTITUTIONS): Define new empty global. * lib/rpm/macros.d/macros.in (%pgtests_init, %pgtests_start): New RPM macros. * share/postgresql-setup/Makefile.inc (PKG_TESTS_LIB): Expand new variable in templates. * share/postgresql-setup/postgresql_pkg_tests.sh: New file bringing all the trivial helpers.
1 parent a3b8ebf commit 257e2fa

File tree

5 files changed

+201
-3
lines changed

5 files changed

+201
-3
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ EXTRA_DIST =
2525
INTERMEDIATE_FILES =
2626
GENERATED_FILES =
2727
CLEANFILES =
28+
_AX_TEXT_ADDITIONAL_SED_SUBSTITUTIONS =
2829

2930
# include $(srcdir)/build-helpers/Makefile.inc
3031

NEWS

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Bugfixes in 5.0 version
66

77
* fixed semantics of 'redhat_sockets_hack' configuration option
88

9-
* the RPM macros file is installed automatically
9+
* The RPM macros file is installed automatically, it provides now
10+
%postgresql_major, %pgtests_init and %pgtests_start macros.
1011

1112
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1213

lib/rpm/macros.d/macros.in

+15
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@
55
# *-build subpackage).
66

77
%postgresql_major @PGMAJORVERSION@
8+
9+
%_pgtests_tests_lib "@PKG_TESTS_LIB@"
10+
11+
# Initialize the PostgreSQL tests environment. This is supposed to be invoked
12+
# in prep/build/install (where you plan to run the testsuite), while it defines
13+
# several useful shell variables and provies useful commands. Effect of this
14+
# command end with shell exit.
15+
%pgtests_init . %_pgtests_tests_lib
16+
17+
# Start the testing postgresql server, setting the actual unix user to be the
18+
# PostgreSQL admin. The $PGHOST/$PGPORT are set appropriately, so psql is able
19+
# to run without passwords. This also sets shell exit trap callback so that if
20+
# something goes wrong and package build fails, the temporary database is
21+
# automatically cleaned up.
22+
%pgtests_start pgtests_start

share/postgresql-setup/Makefile.inc

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
lib = %D%/library.sh
22
lib_in = $(srcdir)/$(lib).in
33

4+
pkg_tests_base = postgresql_pkg_tests.sh
5+
pkg_tests = %D%/$(pkg_tests_base)
6+
7+
_AX_TEXT_ADDITIONAL_SED_SUBSTITUTIONS += \
8+
-e 's|@PKG_TESTS_LIB@|$(rawpkgdatadir)/$(pkg_tests_base)|'
9+
410
rawdata_generated_files = \
511
$(lib)
612

713
rawdata_template_files = \
814
$(lib_in)
915

10-
rawdata_static_files =
16+
rawdata_static_files = \
17+
$(pkg_tests)
1118

1219
rawpkgdata_DATA = \
13-
$(rawdata_generated_files)
20+
$(rawdata_generated_files) \
21+
$(rawdata_static_files)
1422

1523
GENERATED_FILES += $(rawdata_generated_files)
1624
EXTRA_DIST += $(rawdata_static_files) $(rawdata_template_files)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
#! /bin/sh
2+
3+
# Do some "integration" testing against running PostgreSQL server.
4+
5+
# This file is to be sourced.
6+
7+
: ${PGTESTS_DATADIR=`pwd`/datadir}
8+
: ${PGTESTS_ADMIN=`id -u -n`}
9+
: ${PGTESTS_ADMINDB=$PGTESTS_ADMIN}
10+
: ${PGTESTS_ADMINPASS=$PGTESTS_ADMIN}
11+
: ${PGTESTS_PORT=54321}
12+
: ${PGTESTS_SOCKETDIR=/tmp}
13+
: ${PGTESTS_USERS=test:test}
14+
: ${PGTESTS_DATABASES=test:test}
15+
16+
# Stop the old cluster and/or remove it's data.
17+
: ${PGTESTS_STARTCLEANUP=:}
18+
19+
# Cleanup once we exit the script.
20+
: ${PGTESTS_CLEANUP=:}
21+
22+
# Cleanup once we exit the script.
23+
: ${PGTESTS_CLEANUP=:}
24+
25+
export PGPORT=$PGTESTS_PORT
26+
export PGHOST=$PGTESTS_SOCKETDIR
27+
28+
warning ()
29+
{
30+
echo >&2 " ! $*"
31+
}
32+
33+
34+
__trap_cb ()
35+
{
36+
IFS=' '
37+
for __func in $__TRAP_ACTIONS
38+
do
39+
$__func
40+
done
41+
}
42+
trap __trap_cb EXIT
43+
44+
45+
__pgtests_initdb ()
46+
{
47+
initdb "$PGTESTS_DATADIR" -U "$PGTESTS_ADMIN" \
48+
--auth-local=peer --auth-host=md5 \
49+
${PGTESTS_LOCALE+--locale="$PGTESTS_LOCALE"}
50+
}
51+
52+
53+
__pgtests_start ()
54+
{
55+
pg_ctl -D "$PGTESTS_DATADIR" -l "$PGTESTS_DATADIR"/start.log start -o "-k $PGTESTS_SOCKETDIR -p $PGTESTS_PORT" -w
56+
}
57+
58+
59+
__pgtests_create_admins_db ()
60+
{
61+
createdb -h "$PGTESTS_SOCKETDIR" "$PGTESTS_ADMINDB" --owner "$PGTESTS_ADMIN" -p "$PGTESTS_PORT"
62+
}
63+
64+
65+
__pgtests_passwd()
66+
{
67+
psql -d postgres --set=user="$1" --set=pass="$2" -tA \
68+
<<<"ALTER USER :\"user\" WITH ENCRYPTED PASSWORD :'pass';"
69+
}
70+
71+
pgtests_start ()
72+
{
73+
unset __TRAP_ACTIONS
74+
75+
if $PGTESTS_STARTCLEANUP; then
76+
# We don't plan to be serious here. This pgtests_* effort is just to
77+
# ease _testing_ against running postgresql server without too much
78+
# writing.
79+
if test -f "$PGTESTS_DATADIR"/postmaster.pid; then
80+
# Give it a try.
81+
warning "Seems like server works, trying to stop."
82+
pg_ctl stop -D "$PGTESTS_DATADIR" -w
83+
fi
84+
85+
# Cleanup testing directory
86+
if test -e "$PGTESTS_DATADIR"; then
87+
warning "Removing old data directory."
88+
rm -r "$PGTESTS_DATADIR"
89+
fi
90+
fi
91+
92+
__pgtests_initdb && __TRAP_ACTIONS="pgtests_cleanup $__TRAP_ACTIONS"
93+
__pgtests_start && __TRAP_ACTIONS="pgtests_stop $__TRAP_ACTIONS"
94+
__pgtests_create_admins_db
95+
96+
__pgtests_passwd "$PGTESTS_ADMIN" "$PGTESTS_ADMINPASS"
97+
98+
99+
for _pgt_user in $PGTESTS_USERS
100+
do
101+
save_IFS=$IFS
102+
IFS=:
103+
_user=
104+
_pass=
105+
for _part in $_pgt_user
106+
do
107+
if test -z "$_user"; then
108+
_user=$_part
109+
else
110+
_pass=$_part
111+
fi
112+
done
113+
114+
createuser "$_user"
115+
__pgtests_passwd "$_user" "$_pass"
116+
IFS=$save_IFS
117+
done
118+
119+
120+
for _pgt_db in $PGTESTS_DATABASES
121+
do
122+
save_IFS=$IFS
123+
IFS=:
124+
_db=
125+
_user=
126+
for _part in $_pgt_db
127+
do
128+
if test -z "$_user"; then
129+
_user=$_part
130+
else
131+
_db=$_part
132+
fi
133+
done
134+
135+
createdb "$_db" --owner "$_part"
136+
137+
IFS=$save_IFS
138+
done
139+
}
140+
141+
142+
__clean_trap_action ()
143+
{
144+
__new_actions=
145+
for __action in $__TRAP_ACTIONS
146+
do
147+
if test "$__action" = "$1"; then
148+
:
149+
else
150+
__new_actions="$__action $__new_actions"
151+
fi
152+
done
153+
154+
__TRAP_ACTIONS=$__new_actions
155+
}
156+
157+
158+
pgtests_cleanup ()
159+
{
160+
if $PGTESTS_CLEANUP && $PGTESTS_AUTOSTOP; then
161+
rm -r "$PGTESTS_DATADIR"
162+
fi
163+
__clean_trap_action pgtests_cleanup
164+
}
165+
166+
167+
pgtests_stop ()
168+
{
169+
if $PGTESTS_AUTOSTOP; then
170+
pg_ctl stop -D "$PGTESTS_DATADIR" -w
171+
fi
172+
__clean_trap_action pgtests_stop
173+
}

0 commit comments

Comments
 (0)