aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dunstan <andrew@dunslane.net>2021-04-16 16:54:04 -0400
committerAndrew Dunstan <andrew@dunslane.net>2021-04-16 17:40:57 -0400
commitf8ba416a98ceded2f97bd3aaa06b081c216ac4d7 (patch)
tree71963025d34aec68d8fe524008858a4dd751cc6c
parent46b6635b7742cea22b981e8f38f651b7f66c5f46 (diff)
downloadpostgresql-f8ba416a98ceded2f97bd3aaa06b081c216ac4d7.tar.gz
postgresql-f8ba416a98ceded2f97bd3aaa06b081c216ac4d7.zip
Allow TestLib::slurp_file to skip contents, and use as needed
In order to avoid getting old logfile contents certain functions in PostgresNode were doing one of two things. On Windows it rotated the logfile and restarted the server, while elsewhere it truncated the log file. Both of these are unnecessary. We borrow from the buildfarm which does this instead: note the size of the logfile before we start, and then when fetching the logfile skip to that position before accumulating contents. This is spelled differently on Windows but the effect is the same. This is largely centralized in TestLib's slurp_file function, which has a new optional parameter, the offset to skip to before starting to reading the file. Code in the client becomes much neater. Backpatch to all live branches. Michael Paquier, slightly modified by me. Discussion: https://postgr.es/m/YHajnhcMAI3++pJL@paquier.xyz
-rw-r--r--src/test/perl/PostgresNode.pm8
-rw-r--r--src/test/perl/TestLib.pm15
2 files changed, 16 insertions, 7 deletions
diff --git a/src/test/perl/PostgresNode.pm b/src/test/perl/PostgresNode.pm
index fc0432a9672..1c25a989c27 100644
--- a/src/test/perl/PostgresNode.pm
+++ b/src/test/perl/PostgresNode.pm
@@ -1479,9 +1479,6 @@ sub command_like
Run a command on the node, then verify that $expected_sql appears in the
server log file.
-Reads the whole log file so be careful when working with large log outputs.
-The log file is truncated prior to running the command, however.
-
=cut
sub issues_sql_like
@@ -1491,10 +1488,11 @@ sub issues_sql_like
local $ENV{PGHOST} = $self->host;
local $ENV{PGPORT} = $self->port;
- truncate $self->logfile, 0;
+ my $log_location = -s $self->logfile;
+
my $result = TestLib::run_log($cmd);
ok($result, "@$cmd exit code 0");
- my $log = TestLib::slurp_file($self->logfile);
+ my $log = TestLib::slurp_file($self->logfile, $log_location);
like($log, $expected_sql, "$test_name: SQL found in server log");
}
diff --git a/src/test/perl/TestLib.pm b/src/test/perl/TestLib.pm
index 878369095db..e99c62a0d72 100644
--- a/src/test/perl/TestLib.pm
+++ b/src/test/perl/TestLib.pm
@@ -13,6 +13,7 @@ use warnings;
use Config;
use Cwd;
use Exporter 'import';
+use Fcntl qw(:mode :seek);
use File::Basename;
use File::Spec;
use File::Temp ();
@@ -73,7 +74,7 @@ BEGIN
if ($windows_os)
{
require Win32API::File;
- Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle));
+ Win32API::File->import(qw(createFile OsFHandleOpen CloseHandle setFilePointer));
}
}
@@ -239,13 +240,18 @@ sub slurp_dir
sub slurp_file
{
- my ($filename) = @_;
+ my ($filename, $offset) = @_;
local $/;
my $contents;
if ($Config{osname} ne 'MSWin32')
{
open(my $in, '<', $filename)
or die "could not read \"$filename\": $!";
+ if (defined($offset))
+ {
+ seek($in, $offset, SEEK_SET)
+ or die "could not seek \"$filename\": $!";
+ }
$contents = <$in>;
close $in;
}
@@ -255,6 +261,11 @@ sub slurp_file
or die "could not open \"$filename\": $^E";
OsFHandleOpen(my $fh = IO::Handle->new(), $fHandle, 'r')
or die "could not read \"$filename\": $^E\n";
+ if (defined($offset))
+ {
+ setFilePointer($fh, $offset, qw(FILE_BEGIN))
+ or die "could not seek \"$filename\": $^E\n";
+ }
$contents = <$fh>;
CloseHandle($fHandle)
or die "could not close \"$filename\": $^E\n";