| 1 |
Perl-based TAP tests |
| 2 |
==================== |
| 3 |
|
| 4 |
src/test/perl/ contains shared infrastructure that's used by Perl-based tests |
| 5 |
across the source tree, particularly tests in src/bin and src/test. It's used |
| 6 |
to drive tests for backup and restore, replication, etc - anything that can't |
| 7 |
really be expressed using pg_regress or the isolation test framework. |
| 8 |
|
| 9 |
The tests are invoked via perl's 'prove' command, wrapped in PostgreSQL |
| 10 |
makefiles to handle instance setup etc. See the $(prove_check) and |
| 11 |
$(prove_installcheck) targets in Makefile.global. By default every test in the |
| 12 |
t/ subdirectory is run. Individual test(s) can be run instead by passing |
| 13 |
something like PROVE_TESTS="t/001_testname.pl t/002_othertestname.pl" to make. |
| 14 |
|
| 15 |
By default, to keep the noise low during runs, we do not set any flags via |
| 16 |
PROVE_FLAGS, but this can be done on the 'make' command line if desired, eg: |
| 17 |
|
| 18 |
make check-world PROVE_FLAGS='--verbose' |
| 19 |
|
| 20 |
When a test fails, the terminal output from 'prove' is usually not sufficient |
| 21 |
to diagnose the problem. Look into the log files that are left under |
| 22 |
tmp_check/log/ to get more info. Files named 'regress_log_XXX' are log |
| 23 |
output from the perl test scripts themselves, and should be examined first. |
| 24 |
Other files are postmaster logs, and may be helpful as additional data. |
| 25 |
|
| 26 |
The tests default to a timeout of 180 seconds for many individual operations. |
| 27 |
Slow hosts may avoid load-induced, spurious failures by setting environment |
| 28 |
variable PG_TEST_TIMEOUT_DEFAULT to some number of seconds greater than 180. |
| 29 |
Developers may see faster failures by setting that environment variable to |
| 30 |
some lesser number of seconds. |
| 31 |
|
| 32 |
Data directories will also be left behind for analysis when a test fails; |
| 33 |
they are named according to the test filename. But if the environment |
| 34 |
variable PG_TEST_NOCLEAN is set, the data directories will be retained |
| 35 |
regardless of test status. This environment variable also prevents the |
| 36 |
test's temporary directories from being removed. |
| 37 |
|
| 38 |
|
| 39 |
Writing tests |
| 40 |
------------- |
| 41 |
|
| 42 |
You should prefer to write tests using pg_regress in src/test/regress, or |
| 43 |
isolation tester specs in src/test/isolation, if possible. If not, check to |
| 44 |
see if your new tests make sense under an existing tree in src/test, like |
| 45 |
src/test/ssl, or should be added to one of the suites for an existing utility. |
| 46 |
|
| 47 |
Note that all tests and test tools should have perltidy run on them before |
| 48 |
patches are submitted, using perltidy --profile=src/tools/pgindent/perltidyrc |
| 49 |
|
| 50 |
Tests are written using Perl's Test::More with some PostgreSQL-specific |
| 51 |
infrastructure from src/test/perl providing node management, support for |
| 52 |
invoking 'psql' to run queries and get results, etc. You should read the |
| 53 |
documentation for Test::More before trying to write tests. |
| 54 |
|
| 55 |
Test scripts in the t/ subdirectory of a suite are executed in alphabetical |
| 56 |
order. |
| 57 |
|
| 58 |
Each test script should begin with: |
| 59 |
|
| 60 |
use strict; |
| 61 |
use warnings FATAL => 'all'; |
| 62 |
use PostgreSQL::Test::Cluster; |
| 63 |
use PostgreSQL::Test::Utils; |
| 64 |
use Test::More; |
| 65 |
|
| 66 |
then it will generally need to set up one or more nodes, run commands |
| 67 |
against them and evaluate the results. For example: |
| 68 |
|
| 69 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 70 |
$node->init; |
| 71 |
$node->start; |
| 72 |
|
| 73 |
my $ret = $node->safe_psql('postgres', 'SELECT 1'); |
| 74 |
is($ret, '1', 'SELECT 1 returns 1'); |
| 75 |
|
| 76 |
$node->stop('fast'); |
| 77 |
|
| 78 |
Each test script should end with: |
| 79 |
|
| 80 |
done_testing(); |
| 81 |
|
| 82 |
Test::Builder::Level controls how far up in the call stack a test will look |
| 83 |
at when reporting a failure. This should be incremented by any subroutine |
| 84 |
which directly or indirectly calls test routines from Test::More, such as |
| 85 |
ok() or is(): |
| 86 |
|
| 87 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 88 |
|
| 89 |
Read the documentation for more on how to write tests: |
| 90 |
|
| 91 |
perldoc Test::More |
| 92 |
perldoc Test::Builder |
| 93 |
|
| 94 |
For available PostgreSQL-specific test methods and some example tests read the |
| 95 |
perldoc for the test modules, e.g.: |
| 96 |
|
| 97 |
perldoc src/test/perl/PostgreSQL/Test/Cluster.pm |
| 98 |
|
| 99 |
Portability |
| 100 |
----------- |
| 101 |
|
| 102 |
Avoid using any bleeding-edge Perl features. We have buildfarm animals |
| 103 |
running Perl versions as old as 5.14, so your tests will be expected |
| 104 |
to pass on that. |
| 105 |
|
| 106 |
Also, do not use any non-core Perl modules except IPC::Run. Or, if you |
| 107 |
must do so for a particular test, arrange to skip the test when the needed |
| 108 |
module isn't present. If unsure, you can consult Module::CoreList to find |
| 109 |
out whether a given module is part of the Perl core, and which module |
| 110 |
versions shipped with which Perl releases. |
| 111 |
|
| 112 |
One way to test for compatibility with old Perl versions is to use |
| 113 |
perlbrew; see http://perlbrew.pl . After installing that, do |
| 114 |
|
| 115 |
export PERLBREW_CONFIGURE_FLAGS='-de -Duseshrplib' |
| 116 |
perlbrew --force install 5.14.0 |
| 117 |
perlbrew use 5.14.0 |
| 118 |
perlbrew install-cpanm |
| 119 |
cpanm install Test::[email protected] |
| 120 |
cpanm install IPC::[email protected] |
| 121 |
cpanm install ExtUtils::[email protected] # downgrade |
| 122 |
|
| 123 |
TIP: if Test::Simple's utf8 regression test hangs up, try setting a |
| 124 |
UTF8-compatible locale, e.g. "export LANG=en_US.utf8". |
| 125 |
|
| 126 |
Then re-run Postgres' configure to ensure the correct Perl is used when |
| 127 |
running tests. To verify that the right Perl was found: |
| 128 |
|
| 129 |
grep ^PERL= config.log |
| 130 |
|
| 131 |
Due to limitations of cpanm, this recipe doesn't exactly duplicate the |
| 132 |
module list of older buildfarm animals. The discrepancies should seldom |
| 133 |
matter, but if you want to be sure, bypass cpanm and instead manually |
| 134 |
install the desired versions of Test::Simple and IPC::Run. |