From 8ce55029e2c640bd6bc57e5d4cd3caa1c5265ccc Mon Sep 17 00:00:00 2001 From: daidai_is_here Date: Wed, 8 Apr 2020 18:35:13 +0800 Subject: [PATCH] add postgresql-test-rpm-macros --- macros.postgresql | 9 ++ macros.postgresql-test | 18 ++++ postgresql.spec | 25 +++++- postgresql_pkg_tests.sh | 193 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 macros.postgresql create mode 100644 macros.postgresql-test create mode 100644 postgresql_pkg_tests.sh diff --git a/macros.postgresql b/macros.postgresql new file mode 100644 index 0000000..ff03dd8 --- /dev/null +++ b/macros.postgresql @@ -0,0 +1,9 @@ +# Macros from this file should exist on system only once, so it package +# maintainer's responsibility to guarantee that all packages which install +# these macros collide, or that (during package-build time) only one such +# package is installed (e.g. similarly to %%scl macro defined installed by +# *-build subpackage). + +%postgresql_major 10 + +%postgresql_module_requires Requires: postgresql-server(:MODULE_COMPAT_10) diff --git a/macros.postgresql-test b/macros.postgresql-test new file mode 100644 index 0000000..cad185d --- /dev/null +++ b/macros.postgresql-test @@ -0,0 +1,18 @@ +# Initialize the PostgreSQL tests environment. This is supposed to be invoked +# in prep/build/install (where you plan to run the testsuite), while it defines +# several useful shell variables and provies useful commands. Effect of this +# command end with shell exit. +%postgresql_tests_init . "/usr/share/postgresql-setup/postgresql_pkg_tests.sh" + +# Start the testing postgresql server, setting the actual unix user to be the +# PostgreSQL admin. The $PGHOST/$PGPORT are set appropriately, so psql is able +# to run without passwords. This also sets shell exit trap callback so that if +# something goes wrong and package build fails, the temporary database is +# automatically cleaned up. +%postgresql_tests_start pgtests_start + +# Initialize database and run server in one step, if this works well-enough. +%postgresql_tests_run %postgresql_tests_init ; %postgresql_tests_start + +%pgtests_init echo 2>&1 WARN: macro pgtests_init will be removed, use postgresql_tests_init ; %postgresql_tests_init +%pgtests_start echo 2>&1 WARN: macro pgtests_start will be removed, use postgresql_tests_start ; %postgresql_tests_start diff --git a/postgresql.spec b/postgresql.spec index bfc14f9..06793d3 100644 --- a/postgresql.spec +++ b/postgresql.spec @@ -4,7 +4,7 @@ Name: postgresql Version: 10.5 -Release: 13 +Release: 14 Summary: PostgreSQL client programs License: PostgreSQL URL: http://www.postgresql.org/ @@ -19,6 +19,9 @@ Source8: postgresql-setup Source9: postgresql.service # https://github.com/devexp-db/postgresql-setup/blob/master/share/postgresql-setup/library.sh.in Source10: library.sh +Source11: macros.postgresql +Source12: macros.postgresql-test +Source13: postgresql_pkg_tests.sh Patch0000: 0000-postgresql-var-run-socket.patch Patch0001: 0000-rpm-pgsql.patch @@ -139,6 +142,15 @@ Requires: %{name}-server = %{version}-%{release} %{name}-devel = %{version}-%{re The postgresql-test package contains files needed for various tests for the PostgreSQL database management system, including regression tests and benchmarks. +%package test-rpm-macros +Summary: Convenience RPM macros for build-time testing against PostgreSQL server +Requires: %{name}-server = %{version}-%{release} + +%description test-rpm-macros +This package is meant to be added as BuildRequires: dependency of other packages +that want to run build-time testsuite against running PostgreSQL server. + + %prep ( cd "$(dirname "%{SOURCE0}")" @@ -257,6 +269,9 @@ install -D %{SOURCE7} $RPM_BUILD_ROOT%{_libexecdir}/postgresql-check-db-dir install %{SOURCE8} $RPM_BUILD_ROOT%{_bindir}/postgresql-setup install -Dm 0644 %{SOURCE9} $RPM_BUILD_ROOT%{_unitdir}/postgresql.service install -D %{SOURCE10} $RPM_BUILD_ROOT%{_datadir}/postgresql-setup/library.sh +install -Dm 0644 %{SOURCE11} $RPM_BUILD_ROOT%{macrosdir}/macros.postgresql +install -m 0644 %{SOURCE12} $RPM_BUILD_ROOT%{macrosdir}/macros.postgresql-test +install %{SOURCE13} $RPM_BUILD_ROOT%{_datadir}/postgresql-setup/postgresql_pkg_tests.sh install -d $RPM_BUILD_ROOT%{_libdir}/pgsql/test cp -a src/test/regress $RPM_BUILD_ROOT%{_libdir}/pgsql/test @@ -385,11 +400,15 @@ find_lang_bins pltcl.lst pltcl %{_bindir}/{ecpg,pg_config} %{_libdir}/{pgsql/pgxs/,pkgconfig/*.pc} %{_libdir}/{libecpg,libecpg_compat,libpgtypes,libpq}.so +%{macrosdir}/macros.postgresql %files static %{_libdir}/libpgcommon.a %{_libdir}/libpgport.a +%files test-rpm-macros +%{_datadir}/postgresql-setup/postgresql_pkg_tests.sh +%{macrosdir}/macros.postgresql-test %files plperl -f plperl.lst %{_datadir}/pgsql/extension/plperl* @@ -409,6 +428,10 @@ find_lang_bins pltcl.lst pltcl %attr(-,postgres,postgres) %{_libdir}/pgsql/test %changelog +* Tue Apr 08 2020 daiqianwen - 10.5-14 +- Type: enhancement +- DESC: add postgresql-test-rpm-macros + * Tue Apr 07 2020 daiqianwen - 10.5-13 - Type: enhancement - DESC: delete unseless tarball diff --git a/postgresql_pkg_tests.sh b/postgresql_pkg_tests.sh new file mode 100644 index 0000000..c382c17 --- /dev/null +++ b/postgresql_pkg_tests.sh @@ -0,0 +1,193 @@ +#! /bin/sh + +# Do some "integration" testing against running PostgreSQL server. + +# This file is to be sourced. + +try_random_port () +{ + _port=$RANDOM + if test -n "$_port" && eval '_port=$(( $_port + 32000 ))' 2>/dev/null; then + echo "$_port" + elif test -x /usr/bin/shuf; then + /usr/bin/shuf -r -n 1 -i 32000-64767 + else + echo 54321 + fi +} + +: ${PGTESTS_DATADIR=`pwd`/datadir} +: ${PGTESTS_ADMIN=`id -u -n`} +: ${PGTESTS_ADMINDB=$PGTESTS_ADMIN} +: ${PGTESTS_ADMINPASS=$PGTESTS_ADMIN} +: ${PGTESTS_PORT=`try_random_port`} +: ${PGTESTS_SOCKETDIR=/tmp} +: ${PGTESTS_USERS=test:test} +: ${PGTESTS_DATABASES=test:test} + +# Stop the old cluster and/or remove it's data. +: ${PGTESTS_STARTCLEANUP=:} + +# Cleanup once we exit the script. +: ${PGTESTS_CLEANUP=:} + +# Cleanup once we exit the script. +: ${PGTESTS_CLEANUP=:} + +export PGPORT=$PGTESTS_PORT +export PGHOST=$PGTESTS_SOCKETDIR + +: + +warning () +{ + echo >&2 " ! $*" +} + + +__trap_cb () +{ + IFS=' ' + for __func in $__TRAP_ACTIONS + do + $__func + done +} +trap __trap_cb EXIT + + +__pgtests_initdb () +{ + initdb "$PGTESTS_DATADIR" -U "$PGTESTS_ADMIN" \ + --auth-local=peer --auth-host=md5 \ + ${PGTESTS_LOCALE+--locale="$PGTESTS_LOCALE"} +} + + +__pgtests_start () +{ + pg_ctl -D "$PGTESTS_DATADIR" -l "$PGTESTS_DATADIR"/start.log start \ + -o "-k $PGTESTS_SOCKETDIR -p $PGTESTS_PORT" -w || { + cat >&2 "$PGTESTS_DATADIR"/start.log + false + } +} + + +__pgtests_create_admins_db () +{ + createdb -h "$PGTESTS_SOCKETDIR" "$PGTESTS_ADMINDB" --owner "$PGTESTS_ADMIN" -p "$PGTESTS_PORT" +} + + +__pgtests_passwd() +{ + psql -d postgres --set=user="$1" --set=pass="$2" -tA \ + <<<"ALTER USER :\"user\" WITH ENCRYPTED PASSWORD :'pass';" +} + +pgtests_start () +{ + unset __TRAP_ACTIONS + + if $PGTESTS_STARTCLEANUP; then + # We don't plan to be serious here. This pgtests_* effort is just to + # ease _testing_ against running postgresql server without too much + # writing. + if test -f "$PGTESTS_DATADIR"/postmaster.pid; then + # Give it a try. + warning "Seems like server works, trying to stop." + pg_ctl stop -D "$PGTESTS_DATADIR" -w + fi + + # Cleanup testing directory + if test -e "$PGTESTS_DATADIR"; then + warning "Removing old data directory." + rm -r "$PGTESTS_DATADIR" + fi + fi + + __pgtests_initdb + __TRAP_ACTIONS="pgtests_cleanup $__TRAP_ACTIONS" + __pgtests_start + __TRAP_ACTIONS="pgtests_stop $__TRAP_ACTIONS" + __pgtests_create_admins_db + + __pgtests_passwd "$PGTESTS_ADMIN" "$PGTESTS_ADMINPASS" + + + for _pgt_user in $PGTESTS_USERS + do + save_IFS=$IFS + IFS=: + _user= + _pass= + for _part in $_pgt_user + do + if test -z "$_user"; then + _user=$_part + else + _pass=$_part + fi + done + + createuser "$_user" + __pgtests_passwd "$_user" "$_pass" + IFS=$save_IFS + done + + + for _pgt_db in $PGTESTS_DATABASES + do + save_IFS=$IFS + IFS=: + _db= + _user= + for _part in $_pgt_db + do + if test -z "$_user"; then + _user=$_part + else + _db=$_part + fi + done + + createdb "$_db" --owner "$_part" + + IFS=$save_IFS + done +} + + +__clean_trap_action () +{ + __new_actions= + for __action in $__TRAP_ACTIONS + do + if test "$__action" = "$1"; then + : + else + __new_actions="$__action $__new_actions" + fi + done + + __TRAP_ACTIONS=$__new_actions +} + + +pgtests_cleanup () +{ + if $PGTESTS_CLEANUP && $PGTESTS_AUTOSTOP; then + rm -r "$PGTESTS_DATADIR" + fi + __clean_trap_action pgtests_cleanup +} + + +pgtests_stop () +{ + if $PGTESTS_AUTOSTOP; then + pg_ctl stop -D "$PGTESTS_DATADIR" -w + fi + __clean_trap_action pgtests_stop +} -- Gitee