Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 392ea0c

Browse files
committed
Refactor routine to find single log content pattern in TAP tests
The same routine to check if a specific pattern can be found in the server logs was copied over four different test scripts. This refactors the whole to use a single routine located in PostgreSQL::Test::Cluster, named log_contains, to grab the contents of the server logs and check for a specific pattern. On HEAD, the code previously used assumed that slurp_file() could not handle an undefined offset, setting it to zero, but slurp_file() does do an extra fseek() before retrieving the log contents only if an offset is defined. In two places, the test was retrieving the full log contents with slurp_file() after calling substr() to apply an offset, ignoring that slurp_file() would be able to handle that. Backpatch all the way down to ease the introduction of new tests that could rely on the new routine. Author: Vignesh C Reviewed-by: Andrew Dunstan, Dagfinn Ilmari Mannsåker, Michael Paquier Discussion: https://postgr.es/m/CALDaNm0YSiLpjCmajwLfidQrFOrLNKPQir7s__PeVvh9U3uoTQ@mail.gmail.com Backpatch-through: 11
1 parent a83edea commit 392ea0c

File tree

5 files changed

+32
-75
lines changed

5 files changed

+32
-75
lines changed

src/test/authentication/t/003_peer.pl

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ sub test_role
6969
}
7070
}
7171

72-
# Find $pattern in log file of $node.
73-
sub find_in_log
74-
{
75-
my ($node, $offset, $pattern) = @_;
76-
77-
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $offset);
78-
return 0 if (length($log) <= 0);
79-
80-
return $log =~ m/$pattern/;
81-
}
82-
8372
my $node = PostgreSQL::Test::Cluster->new('node');
8473
$node->init;
8574
$node->append_conf('postgresql.conf', "log_connections = on\n");
@@ -91,9 +80,9 @@ sub find_in_log
9180
# Check if peer authentication is supported on this platform.
9281
my $log_offset = -s $node->logfile;
9382
$node->psql('postgres');
94-
if (find_in_log(
95-
$node, $log_offset,
96-
qr/peer authentication is not supported on this platform/))
83+
if ($node->log_contains(
84+
qr/peer authentication is not supported on this platform/,
85+
$log_offset))
9786
{
9887
plan skip_all => 'peer authentication is not supported on this platform';
9988
}

src/test/perl/PostgreSQL/Test/Cluster.pm

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,22 @@ sub log_check
25472547

25482548
=pod
25492549
2550+
=item log_contains(pattern, offset)
2551+
2552+
Find pattern in logfile of node after offset byte.
2553+
2554+
=cut
2555+
2556+
sub log_contains
2557+
{
2558+
my ($self, $pattern, $offset) = @_;
2559+
2560+
return PostgreSQL::Test::Utils::slurp_file($self->logfile, $offset) =~
2561+
m/$pattern/;
2562+
}
2563+
2564+
=pod
2565+
25502566
=item $node->run_log(...)
25512567
25522568
Runs a shell command like PostgreSQL::Test::Utils::run_log, but with connection parameters set

src/test/recovery/t/019_replslot_limit.pl

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@
161161

162162
$node_standby->stop;
163163

164-
ok( !find_in_log(
165-
$node_standby,
164+
ok( !$node_standby->log_contains(
166165
"requested WAL segment [0-9A-F]+ has already been removed"),
167166
'check that required WAL segments are still available');
168167

@@ -184,9 +183,8 @@
184183
my $invalidated = 0;
185184
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
186185
{
187-
if (find_in_log(
188-
$node_primary, 'invalidating obsolete replication slot "rep1"',
189-
$logstart))
186+
if ($node_primary->log_contains(
187+
'invalidating obsolete replication slot "rep1"', $logstart))
190188
{
191189
$invalidated = 1;
192190
last;
@@ -207,7 +205,7 @@
207205
my $checkpoint_ended = 0;
208206
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
209207
{
210-
if (find_in_log($node_primary, "checkpoint complete: ", $logstart))
208+
if ($node_primary->log_contains("checkpoint complete: ", $logstart))
211209
{
212210
$checkpoint_ended = 1;
213211
last;
@@ -237,8 +235,7 @@
237235
my $failed = 0;
238236
for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
239237
{
240-
if (find_in_log(
241-
$node_standby,
238+
if ($node_standby->log_contains(
242239
"requested WAL segment [0-9A-F]+ has already been removed",
243240
$logstart))
244241
{
@@ -381,8 +378,7 @@
381378
my $max_attempts = $PostgreSQL::Test::Utils::timeout_default;
382379
while ($max_attempts-- >= 0)
383380
{
384-
if (find_in_log(
385-
$node_primary3,
381+
if ($node_primary3->log_contains(
386382
"terminating process $senderpid to release replication slot \"rep3\"",
387383
$logstart))
388384
{
@@ -406,9 +402,8 @@
406402
$max_attempts = $PostgreSQL::Test::Utils::timeout_default;
407403
while ($max_attempts-- >= 0)
408404
{
409-
if (find_in_log(
410-
$node_primary3, 'invalidating obsolete replication slot "rep3"',
411-
$logstart))
405+
if ($node_primary3->log_contains(
406+
'invalidating obsolete replication slot "rep3"', $logstart))
412407
{
413408
$msg_logged = 1;
414409
last;
@@ -446,18 +441,4 @@ sub get_log_size
446441
return (stat $node->logfile)[7];
447442
}
448443

449-
# find $pat in logfile of $node after $off-th byte
450-
sub find_in_log
451-
{
452-
my ($node, $pat, $off) = @_;
453-
454-
$off = 0 unless defined $off;
455-
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);
456-
return 0 if (length($log) <= $off);
457-
458-
$log = substr($log, $off);
459-
460-
return $log =~ m/$pat/;
461-
}
462-
463444
done_testing();

src/test/recovery/t/033_replay_tsp_drops.pl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,11 @@ sub test_tablespace
135135
{
136136
last
137137
if (
138-
find_in_log(
139-
$node_standby,
138+
$node_standby->log_contains(
140139
qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: pg_tblspc/!,
141140
$logstart));
142141
usleep(100_000);
143142
}
144143
ok($max_attempts > 0, "invalid directory creation is detected");
145144

146145
done_testing();
147-
148-
# find $pat in logfile of $node after $off-th byte
149-
sub find_in_log
150-
{
151-
my ($node, $pat, $off) = @_;
152-
153-
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $off);
154-
155-
return $log =~ m/$pat/;
156-
}

src/test/recovery/t/035_standby_logical_decoding.pl

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828
my $primary_slotname = 'primary_physical';
2929
my $standby_physical_slotname = 'standby_physical';
3030

31-
# find $pat in logfile of $node after $off-th byte
32-
sub find_in_log
33-
{
34-
my ($node, $pat, $off) = @_;
35-
36-
$off = 0 unless defined $off;
37-
my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);
38-
return 0 if (length($log) <= $off);
39-
40-
$log = substr($log, $off);
41-
42-
return $log =~ m/$pat/;
43-
}
44-
4531
# Fetch xmin columns from slot's pg_replication_slots row, after waiting for
4632
# given boolean condition to be true to ensure we've reached a quiescent state.
4733
sub wait_for_xmins
@@ -235,14 +221,12 @@ sub check_for_invalidation
235221
my $inactive_slot = $slot_prefix . 'inactiveslot';
236222

237223
# message should be issued
238-
ok( find_in_log(
239-
$node_standby,
224+
ok( $node_standby->log_contains(
240225
"invalidating obsolete replication slot \"$inactive_slot\"",
241226
$log_start),
242227
"inactiveslot slot invalidation is logged $test_name");
243228

244-
ok( find_in_log(
245-
$node_standby,
229+
ok( $node_standby->log_contains(
246230
"invalidating obsolete replication slot \"$active_slot\"",
247231
$log_start),
248232
"activeslot slot invalidation is logged $test_name");
@@ -657,14 +641,12 @@ sub check_for_invalidation
657641
$node_primary->wait_for_replay_catchup($node_standby);
658642

659643
# message should not be issued
660-
ok( !find_in_log(
661-
$node_standby,
644+
ok( !$node_standby->log_contains(
662645
"invalidating obsolete slot \"no_conflict_inactiveslot\"", $logstart),
663646
'inactiveslot slot invalidation is not logged with vacuum on conflict_test'
664647
);
665648

666-
ok( !find_in_log(
667-
$node_standby,
649+
ok( !$node_standby->log_contains(
668650
"invalidating obsolete slot \"no_conflict_activeslot\"", $logstart),
669651
'activeslot slot invalidation is not logged with vacuum on conflict_test'
670652
);

0 commit comments

Comments
 (0)