pgsql: Refactor Perl test code

Started by Alvaro Herreraover 10 years ago11 messageshackers
Jump to latest
#1Alvaro Herrera
alvherre@2ndquadrant.com

Refactor Perl test code

The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.

This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.

Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.

This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.

I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.

Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/1caef31d9e550408d0cbc5788a422dcb69736df5

Modified Files
--------------
src/bin/initdb/t/001_initdb.pl | 1 +
src/bin/pg_basebackup/t/010_pg_basebackup.pl | 176 +++++----
src/bin/pg_controldata/t/001_pg_controldata.pl | 11 +-
src/bin/pg_ctl/t/001_start_stop.pl | 9 +-
src/bin/pg_ctl/t/002_status.pl | 13 +-
src/bin/pg_rewind/RewindTest.pm | 216 ++++-------
src/bin/pg_rewind/t/003_extrafiles.pl | 3 +-
src/bin/pg_rewind/t/004_pg_xlog_symlink.pl | 4 +-
src/bin/scripts/t/010_clusterdb.pl | 22 +-
src/bin/scripts/t/011_clusterdb_all.pl | 13 +-
src/bin/scripts/t/020_createdb.pl | 14 +-
src/bin/scripts/t/030_createlang.pl | 19 +-
src/bin/scripts/t/040_createuser.pl | 18 +-
src/bin/scripts/t/050_dropdb.pl | 14 +-
src/bin/scripts/t/060_droplang.pl | 11 +-
src/bin/scripts/t/070_dropuser.pl | 14 +-
src/bin/scripts/t/080_pg_isready.pl | 9 +-
src/bin/scripts/t/090_reindexdb.pl | 23 +-
src/bin/scripts/t/091_reindexdb_all.pl | 10 +-
src/bin/scripts/t/100_vacuumdb.pl | 17 +-
src/bin/scripts/t/101_vacuumdb_all.pl | 10 +-
src/bin/scripts/t/102_vacuumdb_stages.pl | 13 +-
src/test/perl/PostgresNode.pm | 470 ++++++++++++++++++++++++
src/test/perl/RecursiveCopy.pm | 42 +++
src/test/perl/TestLib.pm | 319 ++++++----------
src/test/ssl/ServerSetup.pm | 34 +-
src/test/ssl/t/001_ssltests.pl | 33 +-
27 files changed, 989 insertions(+), 549 deletions(-)

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#1)
Re: pgsql: Refactor Perl test code

Alvaro Herrera <alvherre@alvh.no-ip.org> writes:

Refactor Perl test code

This has broken "make check-world" for me (on RHEL 6.7):

make -C src/bin check
make[1]: Entering directory `/home/postgres/pgsql/src/bin'
make -C initdb check
make[2]: Entering directory `/home/postgres/pgsql/src/bin/initdb'
rm -rf /home/postgres/pgsql/src/bin/initdb/tmp_check/log
cd . && TESTDIR='/home/postgres/pgsql/src/bin/initdb' PATH="/home/postgres/pgsql/tmp_install/home/postgres/testversion/bin:$PATH" LD_LIBRARY_PATH="/home/postgres/pgsql/tmp_install/home/postgres/testversion/lib" PGPORT='65440' PG_REGRESS='/home/postgres/pgsql/src/bin/initdb/../../../src/test/regress/pg_regress' prove -I ../../../src/test/perl/ --verbose t/*.pl
t/001_initdb.pl ..
1..14
ok 1 - initdb --help exit code 0
ok 2 - initdb --help goes to stdout
ok 3 - initdb --help nothing to stderr
ok 4 - initdb --version exit code 0
ok 5 - initdb --version goes to stdout
ok 6 - initdb --version nothing to stderr
ok 7 - initdb with invalid option nonzero exit code
ok 8 - initdb with invalid option prints error message
# Looks like you planned 14 tests but ran 8.
# Looks like your test exited with 25 just after 8.
Dubious, test returned 25 (wstat 6400, 0x1900)
Failed 6/14 subtests

Test Summary Report
-------------------
t/001_initdb.pl (Wstat: 6400 Tests: 8 Failed: 0)
Non-zero exit status: 25
Parse errors: Bad plan. You planned 14 tests but ran 8.
Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr 0.01 csys = 0.11 CPU)
Result: FAIL
make[2]: *** [check] Error 1
make[2]: Leaving directory `/home/postgres/pgsql/src/bin/initdb'
make[1]: *** [check-initdb-recurse] Error 2
make[1]: Leaving directory `/home/postgres/pgsql/src/bin'
make: *** [check-world-src/bin-recurse] Error 2

The buildfarm looks pretty unhappy too, though I've not checked to see if
it's exactly the same symptom everywhere.

regards, tom lane

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

#3Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#2)
Re: pgsql: Refactor Perl test code

On Thu, Dec 3, 2015 at 8:38 AM, Tom Lane wrote:

Test Summary Report
-------------------
t/001_initdb.pl (Wstat: 6400 Tests: 8 Failed: 0)
Non-zero exit status: 25
Parse errors: Bad plan. You planned 14 tests but ran 8.
Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr 0.01 csys = 0.11 CPU)
Result: FAIL
make[2]: *** [check] Error 1
make[2]: Leaving directory `/home/postgres/pgsql/src/bin/initdb'
make[1]: *** [check-initdb-recurse] Error 2
make[1]: Leaving directory `/home/postgres/pgsql/src/bin'
make: *** [check-world-src/bin-recurse] Error 2

The buildfarm looks pretty unhappy too, though I've not checked to see if
it's exactly the same symptom everywhere.

Or in more details:
Undefined subroutine &TestLib::run called at
/Users/mpaquier/git/postgres/src/bin/initdb/../../../src/test/perl/TestLib.pm
line 146.
I am seeing the same failure on all the machines in the buildfarm.

The issue is fixed by the patch attached. This has been visibly
forgotten in the version pushed.
Regards,
--
Michael

Attachments:

20151203_tap_fix.patchtext/x-patch; charset=US-ASCII; name=20151203_tap_fix.patchDownload+1-1
#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Michael Paquier (#3)
Re: pgsql: Refactor Perl test code

Michael Paquier wrote:

Or in more details:
Undefined subroutine &TestLib::run called at
/Users/mpaquier/git/postgres/src/bin/initdb/../../../src/test/perl/TestLib.pm
line 146.
I am seeing the same failure on all the machines in the buildfarm.

The issue is fixed by the patch attached. This has been visibly
forgotten in the version pushed.

Yeah. Pushed.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#4)
Re: pgsql: Refactor Perl test code

Alvaro Herrera <alvherre@2ndquadrant.com> writes:

Michael Paquier wrote:

The issue is fixed by the patch attached. This has been visibly
forgotten in the version pushed.

Yeah. Pushed.

"make check-world" passes again here. Thanks.

regards, tom lane

--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers

#6Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#3)
Re: [COMMITTERS] pgsql: Refactor Perl test code

Michael Paquier <michael.paquier@gmail.com> writes:

On Thu, Dec 3, 2015 at 8:38 AM, Tom Lane wrote:

Test Summary Report
-------------------
t/001_initdb.pl (Wstat: 6400 Tests: 8 Failed: 0)
Non-zero exit status: 25
Parse errors: Bad plan. You planned 14 tests but ran 8.
Files=1, Tests=8, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.08 cusr 0.01 csys = 0.11 CPU)
Result: FAIL
make[2]: *** [check] Error 1
make[2]: Leaving directory `/home/postgres/pgsql/src/bin/initdb'
make[1]: *** [check-initdb-recurse] Error 2
make[1]: Leaving directory `/home/postgres/pgsql/src/bin'
make: *** [check-world-src/bin-recurse] Error 2

The buildfarm looks pretty unhappy too, though I've not checked to see if
it's exactly the same symptom everywhere.

Or in more details:
Undefined subroutine &TestLib::run called at
/Users/mpaquier/git/postgres/src/bin/initdb/../../../src/test/perl/TestLib.pm
line 146.

BTW, not the fault of this patch in particular, but this example points
up the complaint I've had right along about how opaque TAP test failures
are. How did you dig down to see that error message? Is it even possible
to diagnose such a failure from what the buildfarm logs?

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#7Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#6)
Re: [COMMITTERS] pgsql: Refactor Perl test code

On Thu, Dec 3, 2015 at 12:19 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

BTW, not the fault of this patch in particular, but this example points
up the complaint I've had right along about how opaque TAP test failures
are. How did you dig down to see that error message?

Well, it showed up on my terminal...

Is it even possible to diagnose such a failure from what the buildfarm logs?

Yes. If you look at TestLib.pm, stderr/stdout redirection is done in
an INIT block, not BEGIN block, and INIT gets executed *after* the
code is compiled. FWIW, I recall arguing in favor of adding this
redirection logic in BEGIN so as we could get compilation errors
directly in the log files... The reason why it is done this way is
that it has been argued as well that we should not change the FS in
the BEGIN block, but in the INIT block when TestLib.pm is loaded.
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Paquier (#7)
Re: [COMMITTERS] pgsql: Refactor Perl test code

Michael Paquier <michael.paquier@gmail.com> writes:

On Thu, Dec 3, 2015 at 12:19 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

BTW, not the fault of this patch in particular, but this example points
up the complaint I've had right along about how opaque TAP test failures
are. How did you dig down to see that error message?

Well, it showed up on my terminal...

Not on mine, as per the extract I showed. Probably a Perl version
difference, but I don't think we can exactly write off RHEL6 as an
uninteresting obsolete distribution. (The Perl here is 5.10.1.)

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#9Michael Paquier
michael@paquier.xyz
In reply to: Tom Lane (#8)
Re: [COMMITTERS] pgsql: Refactor Perl test code

On Thu, Dec 3, 2015 at 12:50 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Michael Paquier <michael.paquier@gmail.com> writes:

On Thu, Dec 3, 2015 at 12:19 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

BTW, not the fault of this patch in particular, but this example points
up the complaint I've had right along about how opaque TAP test failures
are. How did you dig down to see that error message?

Well, it showed up on my terminal...

Not on mine, as per the extract I showed. Probably a Perl version
difference, but I don't think we can exactly write off RHEL6 as an
uninteresting obsolete distribution. (The Perl here is 5.10.1.)

Possible. On OSX 10.8:
$ perl --version

This is perl 5, version 16, subversion 2 (v5.16.2) built for
darwin-thread-multi-2level
(with 3 registered patches, see perl -V for more detail
--
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#10Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#8)
Re: [COMMITTERS] pgsql: Refactor Perl test code

Tom Lane wrote:

Michael Paquier <michael.paquier@gmail.com> writes:

On Thu, Dec 3, 2015 at 12:19 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

BTW, not the fault of this patch in particular, but this example points
up the complaint I've had right along about how opaque TAP test failures
are. How did you dig down to see that error message?

Well, it showed up on my terminal...

Not on mine, as per the extract I showed. Probably a Perl version
difference, but I don't think we can exactly write off RHEL6 as an
uninteresting obsolete distribution. (The Perl here is 5.10.1.)

Hmm, maybe a selinux policy or something like that is preventing some
system call from working, and this causes a Perl call to fail which we
may be failing to report; for instance we have
open(ORIG_STDOUT, ">&STDOUT");
in TestLib.pm's INIT block, without an "or die" which would let us know
that it failed.

--
�lvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#10)
Re: [COMMITTERS] pgsql: Refactor Perl test code

Alvaro Herrera <alvherre@2ndquadrant.com> writes:

Tom Lane wrote:

Michael Paquier <michael.paquier@gmail.com> writes:

Well, it showed up on my terminal...

Not on mine, as per the extract I showed. Probably a Perl version
difference, but I don't think we can exactly write off RHEL6 as an
uninteresting obsolete distribution. (The Perl here is 5.10.1.)

Hmm, maybe a selinux policy or something like that is preventing some
system call from working,

Good thought, but overcomplicated. After some quality time with strace,
I find that the
Undefined subroutine &TestLib::run called at /home/postgres/pgsql/src/bin/initdb
/../../../src/test/perl/TestLib.pm line 146.
error message does indeed appear, but it's printed only into
src/bin/initdb/tmp_check/log/regress_log_001_initdb

It appears that the reason this happens is that the child perl process
(of the two that are active in this area of the trace) decides at one
point along the line to redirect its own stdout and stderr into that
file, disconnecting them from the pipes leading to the parent process.
So when that process prints "Undefined subroutine" to its stderr, that
is where it goes.

I suppose that the redirection is a result of this bit in TestLib.pm:

# Hijack STDOUT and STDERR to the log file
open(ORIG_STDOUT, ">&STDOUT");
open(ORIG_STDERR, ">&STDERR");
open(STDOUT, ">&TESTLOG");
open(STDERR, ">&TESTLOG");

so the question from my end is not so much why it doesn't work for me
as how it could possibly work for you. Could the newer Perl version
be detecting the undefined-subroutine error before it executes the
file's INIT stanza, rather than after? Or maybe the "tie" stuff
happening just below this acts differently in more modern Perls?

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers