summaryrefslogtreecommitdiff
path: root/src/test/perl/TestLib.pm
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-11-17 14:10:24 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2015-11-17 14:10:24 -0500
commit8bc496c3b68f6b5296eed3ebccc6f322d4d0ba52 (patch)
tree6a8c88729449186d5928c56d18a6bf155e9734dd /src/test/perl/TestLib.pm
parenta6c4c07fc5cd0665568ff48ada3b65900fafa1af (diff)
Back-patch fixes to make TAP tests work on Windows.
This back-ports commit 13d856e177e69083 and assorted followon patches into 9.4 and 9.5. 9.5 and HEAD are now substantially identical in all the files touched by this commit, except that 010_pg_basebackup.pl has a few more tests related to the new --slot option. 9.4 has many fewer TAP tests, but the test infrastructure files are substantially the same, with the exception that 9.4 lacks the single-tmp-install infrastructure introduced in 9.5 (commit dcae5faccab64776). The primary motivation for this patch is to ensure that TAP test case fixes can be back-patched without hazards of the kind seen in commits 34557f544/06dd4b44f. In principle it should also make the world safe for running the TAP tests in the buildfarm in these branches; although we might want to think about back-porting dcae5faccab64776 to 9.4 if we're going to do that for real, because the TAP tests are quite disk space hungry without it. Michael Paquier did the back-porting work; original patches were by him and assorted other people.
Diffstat (limited to 'src/test/perl/TestLib.pm')
-rw-r--r--src/test/perl/TestLib.pm108
1 files changed, 96 insertions, 12 deletions
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index fdb7b533729..02533ebde53 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -9,9 +9,12 @@ our @EXPORT = qw(
tempdir
tempdir_short
standard_initdb
+ configure_hba_for_replication
start_test_server
restart_test_server
psql
+ slurp_dir
+ slurp_file
system_or_bail
system_log
run_log
@@ -107,39 +110,89 @@ $ENV{PGPORT} = int($ENV{PGPORT}) % 65536;
sub tempdir
{
- return File::Temp::tempdir('tmp_testXXXX', DIR => $ENV{TESTDIR} || cwd(), CLEANUP => 1);
+ return File::Temp::tempdir(
+ 'tmp_testXXXX',
+ DIR => $ENV{TESTDIR} || cwd(),
+ CLEANUP => 1);
}
sub tempdir_short
{
+
# Use a separate temp dir outside the build tree for the
# Unix-domain socket, to avoid file name length issues.
return File::Temp::tempdir(CLEANUP => 1);
}
+# Initialize a new cluster for testing.
+#
+# The PGHOST environment variable is set to connect to the new cluster.
+#
+# Authentication is set up so that only the current OS user can access the
+# cluster. On Unix, we use Unix domain socket connections, with the socket in
+# a directory that's only accessible to the current user to ensure that.
+# On Windows, we use SSPI authentication to ensure the same (by pg_regress
+# --config-auth).
sub standard_initdb
{
my $pgdata = shift;
system_or_bail('initdb', '-D', "$pgdata", '-A' , 'trust', '-N');
- system_or_bail("$ENV{top_builddir}/src/test/regress/pg_regress",
- '--config-auth', $pgdata);
+ system_or_bail($ENV{PG_REGRESS}, '--config-auth', $pgdata);
+
+ my $tempdir_short = tempdir_short;
+
+ open CONF, ">>$pgdata/postgresql.conf";
+ print CONF "\n# Added by TestLib.pm)\n";
+ print CONF "fsync = off\n";
+ if ($windows_os)
+ {
+ print CONF "listen_addresses = '127.0.0.1'\n";
+ }
+ else
+ {
+ print CONF "unix_socket_directories = '$tempdir_short'\n";
+ print CONF "listen_addresses = ''\n";
+ }
+ close CONF;
+
+ $ENV{PGHOST} = $windows_os ? "127.0.0.1" : $tempdir_short;
+}
+
+# Set up the cluster to allow replication connections, in the same way that
+# standard_initdb does for normal connections.
+sub configure_hba_for_replication
+{
+ my $pgdata = shift;
+
+ open HBA, ">>$pgdata/pg_hba.conf";
+ print HBA "\n# Allow replication (set up by TestLib.pm)\n";
+ if (! $windows_os)
+ {
+ print HBA "local replication all trust\n";
+ }
+ else
+ {
+ print HBA "host replication all 127.0.0.1/32 sspi include_realm=1 map=regress\n";
+ }
+ close HBA;
}
my ($test_server_datadir, $test_server_logfile);
+
+# Initialize a new cluster for testing in given directory, and start it.
sub start_test_server
{
my ($tempdir) = @_;
my $ret;
- my $tempdir_short = tempdir_short;
-
print("### Starting test server in $tempdir\n");
standard_initdb "$tempdir/pgdata";
+
$ret = system_log('pg_ctl', '-D', "$tempdir/pgdata", '-w', '-l',
- "$log_path/postmaster.log", '-o',
-"--fsync=off -k \"$tempdir_short\" --listen-addresses='' --log-statement=all",
- 'start');
+ "$log_path/postmaster.log", '-o', "--log-statement=all",
+ 'start');
+
if ($ret != 0)
{
print "# pg_ctl failed; logfile:\n";
@@ -147,7 +200,6 @@ sub start_test_server
BAIL_OUT("pg_ctl failed");
}
- $ENV{PGHOST} = $tempdir_short;
$test_server_datadir = "$tempdir/pgdata";
$test_server_logfile = "$log_path/postmaster.log";
}
@@ -171,8 +223,30 @@ END
sub psql
{
my ($dbname, $sql) = @_;
+ my ($stdout, $stderr);
print("# Running SQL command: $sql\n");
- run [ 'psql', '-X', '-q', '-d', $dbname, '-f', '-' ], '<', \$sql or die;
+ run [ 'psql', '-X', '-A', '-t', '-q', '-d', $dbname, '-f', '-' ], '<', \$sql, '>', \$stdout, '2>', \$stderr or die;
+ chomp $stdout;
+ $stdout =~ s/\r//g if $Config{osname} eq 'msys';
+ return $stdout;
+}
+
+sub slurp_dir
+{
+ my ($dir) = @_;
+ opendir(my $dh, $dir) or die;
+ my @direntries = readdir $dh;
+ closedir $dh;
+ return @direntries;
+}
+
+sub slurp_file
+{
+ local $/;
+ local @ARGV = @_;
+ my $contents = <>;
+ $contents =~ s/\r//g if $Config{osname} eq 'msys';
+ return $contents;
}
sub system_or_bail
@@ -221,7 +295,17 @@ sub command_exit_is
print("# Running: " . join(" ", @{$cmd}) ."\n");
my $h = start $cmd;
$h->finish();
- is($h->result(0), $expected, $test_name);
+
+ # On Windows, the exit status of the process is returned directly as the
+ # process's exit code, while on Unix, it's returned in the high bits
+ # of the exit code (see WEXITSTATUS macro in the standard <sys/wait.h>
+ # header file). IPC::Run's result function always returns exit code >> 8,
+ # assuming the Unix convention, which will always return 0 on Windows as
+ # long as the process was not terminated by an exception. To work around
+ # that, use $h->full_result on Windows instead.
+ my $result = ($Config{osname} eq "MSWin32") ?
+ ($h->full_results)[0] : $h->result(0);
+ is($result, $expected, $test_name);
}
sub program_help_ok
@@ -274,7 +358,7 @@ sub issues_sql_like
truncate $test_server_logfile, 0;
my $result = run_log($cmd);
ok($result, "@$cmd exit code 0");
- my $log = `cat '$test_server_logfile'`;
+ my $log = slurp_file($test_server_logfile);
like($log, $expected_sql, "$test_name: SQL found in server log");
}