On Mon, May 29, 2023 at 07:49:52AM +0530, vignesh C wrote:
> Thanks for the comment, the attached v3 version patch has the changes
> for the same.

-if (find_in_log(
-               $node, $log_offset,
-               qr/peer authentication is not supported on this platform/))
+if ($node->log_contains(
+               qr/peer authentication is not supported on this platform/),
+               $log_offset,)

This looks like a typo to me, the log offset is eaten.

Except of that, I am on board with log_contains().

There are two things that bugged me with the refactoring related to
connect_ok and connect_fails:
- check_connect_log_contents() is a name too complicated.  While
looking at that I have settled to a simpler log_check(), as we could
perfectly use that for something else than connections as long as we
want to check multiple patterns at once.
- The refactoring of the documentation for the routines of Cluster.pm
became incorrect.  For example, the patch does not list anymore
log_like and log_unlike for connect_ok.

With all that in mind I have hacked a few adjustments in a 0003,
though I agree with the separation between 0001 and 0002.

033_replay_tsp_drops and 019_replslot_limit are not new to v16, but
003_peer.pl and 035_standby_logical_decoding.pl, making the number of
places where find_in_log() exists twice as much.  So I would be
tempted to refactor these tests in v16.  Perhaps anybody from the RMT
could comment?  We've usually been quite flexible with the tests even
in beta.

Thoughts?
--
Michael
From 49473ab523adf43e82fe71d7e33be478287ad2c8 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignes...@gmail.com>
Date: Fri, 26 May 2023 20:07:30 +0530
Subject: [PATCH v4 1/3] Remove duplicate find_in_log sub routines from tap
 tests.

Remove duplicate find_in_log sub routines from tap tests.
---
 src/test/authentication/t/003_peer.pl         | 17 ++--------
 src/test/perl/PostgreSQL/Test/Cluster.pm      | 15 +++++++++
 src/test/recovery/t/019_replslot_limit.pl     | 33 +++++--------------
 src/test/recovery/t/033_replay_tsp_drops.pl   | 15 ++-------
 .../t/035_standby_logical_decoding.pl         | 26 +++------------
 5 files changed, 32 insertions(+), 74 deletions(-)

diff --git a/src/test/authentication/t/003_peer.pl 
b/src/test/authentication/t/003_peer.pl
index 3272e52cae..2a035c2d0d 100644
--- a/src/test/authentication/t/003_peer.pl
+++ b/src/test/authentication/t/003_peer.pl
@@ -69,17 +69,6 @@ sub test_role
        }
 }
 
-# Find $pattern in log file of $node.
-sub find_in_log
-{
-       my ($node, $offset, $pattern) = @_;
-
-       my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $offset);
-       return 0 if (length($log) <= 0);
-
-       return $log =~ m/$pattern/;
-}
-
 my $node = PostgreSQL::Test::Cluster->new('node');
 $node->init;
 $node->append_conf('postgresql.conf', "log_connections = on\n");
@@ -91,9 +80,9 @@ reset_pg_hba($node, 'peer');
 # Check if peer authentication is supported on this platform.
 my $log_offset = -s $node->logfile;
 $node->psql('postgres');
-if (find_in_log(
-               $node, $log_offset,
-               qr/peer authentication is not supported on this platform/))
+if ($node->log_contains(
+               qr/peer authentication is not supported on this platform/),
+               $log_offset,)
 {
        plan skip_all => 'peer authentication is not supported on this 
platform';
 }
diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm 
b/src/test/perl/PostgreSQL/Test/Cluster.pm
index baea0fcd1c..4df7dd4dec 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -2536,6 +2536,21 @@ sub log_content
 }
 
 
+=pod
+
+=item log_contains(pattern, offset)
+
+Find pattern in logfile of node after offset byte.
+
+=cut
+
+sub log_contains
+{
+       my ($self, $pattern, $offset) = @_;
+
+       return PostgreSQL::Test::Utils::slurp_file($self->logfile, $offset) =~ 
m/$pattern/;
+}
+
 =pod
 
 =item $node->run_log(...)
diff --git a/src/test/recovery/t/019_replslot_limit.pl 
b/src/test/recovery/t/019_replslot_limit.pl
index a1aba16e14..95acf9e357 100644
--- a/src/test/recovery/t/019_replslot_limit.pl
+++ b/src/test/recovery/t/019_replslot_limit.pl
@@ -161,8 +161,7 @@ $node_primary->wait_for_catchup($node_standby);
 
 $node_standby->stop;
 
-ok( !find_in_log(
-               $node_standby,
+ok( !$node_standby->log_contains(
                "requested WAL segment [0-9A-F]+ has already been removed"),
        'check that required WAL segments are still available');
 
@@ -184,8 +183,8 @@ $node_primary->safe_psql('postgres', "CHECKPOINT;");
 my $invalidated = 0;
 for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
 {
-       if (find_in_log(
-                       $node_primary, 'invalidating obsolete replication slot 
"rep1"',
+       if ($node_primary->log_contains(
+                       'invalidating obsolete replication slot "rep1"',
                        $logstart))
        {
                $invalidated = 1;
@@ -207,7 +206,7 @@ is($result, "rep1|f|t|lost|",
 my $checkpoint_ended = 0;
 for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
 {
-       if (find_in_log($node_primary, "checkpoint complete: ", $logstart))
+       if ($node_primary->log_contains("checkpoint complete: ", $logstart))
        {
                $checkpoint_ended = 1;
                last;
@@ -237,8 +236,7 @@ $node_standby->start;
 my $failed = 0;
 for (my $i = 0; $i < 10 * $PostgreSQL::Test::Utils::timeout_default; $i++)
 {
-       if (find_in_log(
-                       $node_standby,
+       if ($node_standby->log_contains(
                        "requested WAL segment [0-9A-F]+ has already been 
removed",
                        $logstart))
        {
@@ -381,8 +379,7 @@ my $msg_logged = 0;
 my $max_attempts = $PostgreSQL::Test::Utils::timeout_default;
 while ($max_attempts-- >= 0)
 {
-       if (find_in_log(
-                       $node_primary3,
+       if ($node_primary3->log_contains(
                        "terminating process $senderpid to release replication 
slot \"rep3\"",
                        $logstart))
        {
@@ -406,8 +403,8 @@ $msg_logged = 0;
 $max_attempts = $PostgreSQL::Test::Utils::timeout_default;
 while ($max_attempts-- >= 0)
 {
-       if (find_in_log(
-                       $node_primary3, 'invalidating obsolete replication slot 
"rep3"',
+       if ($node_primary3->log_contains(
+                       'invalidating obsolete replication slot "rep3"',
                        $logstart))
        {
                $msg_logged = 1;
@@ -446,18 +443,4 @@ sub get_log_size
        return (stat $node->logfile)[7];
 }
 
-# find $pat in logfile of $node after $off-th byte
-sub find_in_log
-{
-       my ($node, $pat, $off) = @_;
-
-       $off = 0 unless defined $off;
-       my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);
-       return 0 if (length($log) <= $off);
-
-       $log = substr($log, $off);
-
-       return $log =~ m/$pat/;
-}
-
 done_testing();
diff --git a/src/test/recovery/t/033_replay_tsp_drops.pl 
b/src/test/recovery/t/033_replay_tsp_drops.pl
index 0a35a7bda6..307c30bc6b 100644
--- a/src/test/recovery/t/033_replay_tsp_drops.pl
+++ b/src/test/recovery/t/033_replay_tsp_drops.pl
@@ -135,22 +135,11 @@ while ($max_attempts-- >= 0)
 {
        last
          if (
-               find_in_log(
-                       $node_standby,
+               $node_standby->log_contains(
                        qr!WARNING: ( [A-Z0-9]+:)? creating missing directory: 
pg_tblspc/!,
                        $logstart));
        usleep(100_000);
 }
 ok($max_attempts > 0, "invalid directory creation is detected");
 
-done_testing();
-
-# find $pat in logfile of $node after $off-th byte
-sub find_in_log
-{
-       my ($node, $pat, $off) = @_;
-
-       my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile, $off);
-
-       return $log =~ m/$pat/;
-}
+done_testing();
\ No newline at end of file
diff --git a/src/test/recovery/t/035_standby_logical_decoding.pl 
b/src/test/recovery/t/035_standby_logical_decoding.pl
index 64beec4bd3..480e6d6caa 100644
--- a/src/test/recovery/t/035_standby_logical_decoding.pl
+++ b/src/test/recovery/t/035_standby_logical_decoding.pl
@@ -28,20 +28,6 @@ my $res;
 my $primary_slotname = 'primary_physical';
 my $standby_physical_slotname = 'standby_physical';
 
-# find $pat in logfile of $node after $off-th byte
-sub find_in_log
-{
-       my ($node, $pat, $off) = @_;
-
-       $off = 0 unless defined $off;
-       my $log = PostgreSQL::Test::Utils::slurp_file($node->logfile);
-       return 0 if (length($log) <= $off);
-
-       $log = substr($log, $off);
-
-       return $log =~ m/$pat/;
-}
-
 # Fetch xmin columns from slot's pg_replication_slots row, after waiting for
 # given boolean condition to be true to ensure we've reached a quiescent state.
 sub wait_for_xmins
@@ -235,14 +221,12 @@ sub check_for_invalidation
        my $inactive_slot = $slot_prefix . 'inactiveslot';
 
        # message should be issued
-       ok( find_in_log(
-                       $node_standby,
+       ok( $node_standby->log_contains(
                        "invalidating obsolete replication slot 
\"$inactive_slot\"",
                        $log_start),
                "inactiveslot slot invalidation is logged $test_name");
 
-       ok( find_in_log(
-                       $node_standby,
+       ok( $node_standby->log_contains(
                        "invalidating obsolete replication slot 
\"$active_slot\"",
                        $log_start),
                "activeslot slot invalidation is logged $test_name");
@@ -657,14 +641,12 @@ $node_primary->safe_psql(
 $node_primary->wait_for_replay_catchup($node_standby);
 
 # message should not be issued
-ok( !find_in_log(
-               $node_standby,
+ok( !$node_standby->log_contains(
                "invalidating obsolete slot \"no_conflict_inactiveslot\"", 
$logstart),
        'inactiveslot slot invalidation is not logged with vacuum on 
conflict_test'
 );
 
-ok( !find_in_log(
-               $node_standby,
+ok( !$node_standby->log_contains(
                "invalidating obsolete slot \"no_conflict_activeslot\"", 
$logstart),
        'activeslot slot invalidation is not logged with vacuum on 
conflict_test'
 );
-- 
2.40.1

From 966ea0c337a187522bcabc877214761d9818b58a Mon Sep 17 00:00:00 2001
From: Vignesh C <vignes...@gmail.com>
Date: Sat, 27 May 2023 05:36:34 +0530
Subject: [PATCH v4 2/3] Move common connection log content verification code
 to a common function.

Move common connection log content verification code to a common
function.
---
 src/test/perl/PostgreSQL/Test/Cluster.pm | 130 +++++++++++------------
 1 file changed, 64 insertions(+), 66 deletions(-)

diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm 
b/src/test/perl/PostgreSQL/Test/Cluster.pm
index 4df7dd4dec..912892e28b 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -2168,6 +2168,68 @@ sub pgbench
 
 =pod
 
+=item $node->check_connect_log_contents($offset, $test_name, %parameters)
+
+Check connection log contents.
+
+=over
+
+=item $test_name
+
+Name of test for error messages.
+
+=item $offset
+
+Offset of the log file.
+
+=item log_like => [ qr/required message/ ]
+
+If given, it must be an array reference containing a list of regular
+expressions that must match against the server log, using
+C<Test::More::like()>.
+
+=item log_unlike => [ qr/prohibited message/ ]
+
+If given, it must be an array reference containing a list of regular
+expressions that must NOT match against the server log.  They will be
+passed to C<Test::More::unlike()>.
+
+=back
+
+=cut
+
+sub check_connect_log_contents
+{
+       my ($self, $test_name, $offset, %params) = @_;
+
+       my (@log_like, @log_unlike);
+       if (defined($params{log_like}))
+       {
+               @log_like = @{ $params{log_like} };
+       }
+       if (defined($params{log_unlike}))
+       {
+               @log_unlike = @{ $params{log_unlike} };
+       }
+
+       if (@log_like or @log_unlike)
+       {
+               my $log_contents =
+                 PostgreSQL::Test::Utils::slurp_file($self->logfile, $offset);
+
+               while (my $regex = shift @log_like)
+               {
+                       like($log_contents, $regex, "$test_name: log matches");
+               }
+               while (my $regex = shift @log_unlike)
+               {
+                       unlike($log_contents, $regex, "$test_name: log does not 
match");
+               }
+       }
+}
+
+=pod
+
 =item $node->connect_ok($connstr, $test_name, %params)
 
 Attempt a connection with a custom connection string.  This is expected
@@ -2184,18 +2246,6 @@ instead of the default.
 
 If this regular expression is set, matches it with the output generated.
 
-=item log_like => [ qr/required message/ ]
-
-If given, it must be an array reference containing a list of regular
-expressions that must match against the server log, using
-C<Test::More::like()>.
-
-=item log_unlike => [ qr/prohibited message/ ]
-
-If given, it must be an array reference containing a list of regular
-expressions that must NOT match against the server log.  They will be
-passed to C<Test::More::unlike()>.
-
 =back
 
 =cut
@@ -2215,16 +2265,6 @@ sub connect_ok
                $sql = "SELECT \$\$connected with $connstr\$\$";
        }
 
-       my (@log_like, @log_unlike);
-       if (defined($params{log_like}))
-       {
-               @log_like = @{ $params{log_like} };
-       }
-       if (defined($params{log_unlike}))
-       {
-               @log_unlike = @{ $params{log_unlike} };
-       }
-
        my $log_location = -s $self->logfile;
 
        # Never prompt for a password, any callers of this routine should
@@ -2245,20 +2285,7 @@ sub connect_ok
 
        is($stderr, "", "$test_name: no stderr");
 
-       if (@log_like or @log_unlike)
-       {
-               my $log_contents =
-                 PostgreSQL::Test::Utils::slurp_file($self->logfile, 
$log_location);
-
-               while (my $regex = shift @log_like)
-               {
-                       like($log_contents, $regex, "$test_name: log matches");
-               }
-               while (my $regex = shift @log_unlike)
-               {
-                       unlike($log_contents, $regex, "$test_name: log does not 
match");
-               }
-       }
+       $self->check_connect_log_contents($test_name, $log_location, %params);
 }
 
 =pod
@@ -2274,12 +2301,6 @@ to fail.
 
 If this regular expression is set, matches it with the output generated.
 
-=item log_like => [ qr/required message/ ]
-
-=item log_unlike => [ qr/prohibited message/ ]
-
-See C<connect_ok(...)>, above.
-
 =back
 
 =cut
@@ -2289,16 +2310,6 @@ sub connect_fails
        local $Test::Builder::Level = $Test::Builder::Level + 1;
        my ($self, $connstr, $test_name, %params) = @_;
 
-       my (@log_like, @log_unlike);
-       if (defined($params{log_like}))
-       {
-               @log_like = @{ $params{log_like} };
-       }
-       if (defined($params{log_unlike}))
-       {
-               @log_unlike = @{ $params{log_unlike} };
-       }
-
        my $log_location = -s $self->logfile;
 
        # Never prompt for a password, any callers of this routine should
@@ -2316,20 +2327,7 @@ sub connect_fails
                like($stderr, $params{expected_stderr}, "$test_name: matches");
        }
 
-       if (@log_like or @log_unlike)
-       {
-               my $log_contents =
-                 PostgreSQL::Test::Utils::slurp_file($self->logfile, 
$log_location);
-
-               while (my $regex = shift @log_like)
-               {
-                       like($log_contents, $regex, "$test_name: log matches");
-               }
-               while (my $regex = shift @log_unlike)
-               {
-                       unlike($log_contents, $regex, "$test_name: log does not 
match");
-               }
-       }
+       $self->check_connect_log_contents($test_name, $log_location, %params);
 }
 
 =pod
-- 
2.40.1

From fce041c5223d14b8bc654461e344f098df95c52a Mon Sep 17 00:00:00 2001
From: Michael Paquier <mich...@paquier.xyz>
Date: Sat, 3 Jun 2023 18:15:42 -0400
Subject: [PATCH v4 3/3] Adjust a bit previous patches ;)

---
 src/test/authentication/t/003_peer.pl       |   4 +-
 src/test/perl/PostgreSQL/Test/Cluster.pm    | 139 +++++++++++---------
 src/test/recovery/t/033_replay_tsp_drops.pl |   2 +-
 3 files changed, 78 insertions(+), 67 deletions(-)

diff --git a/src/test/authentication/t/003_peer.pl 
b/src/test/authentication/t/003_peer.pl
index 2a035c2d0d..d8e4976072 100644
--- a/src/test/authentication/t/003_peer.pl
+++ b/src/test/authentication/t/003_peer.pl
@@ -81,8 +81,8 @@ reset_pg_hba($node, 'peer');
 my $log_offset = -s $node->logfile;
 $node->psql('postgres');
 if ($node->log_contains(
-               qr/peer authentication is not supported on this platform/),
-               $log_offset,)
+               qr/peer authentication is not supported on this platform/,
+               $log_offset))
 {
        plan skip_all => 'peer authentication is not supported on this 
platform';
 }
diff --git a/src/test/perl/PostgreSQL/Test/Cluster.pm 
b/src/test/perl/PostgreSQL/Test/Cluster.pm
index 912892e28b..19cbc46390 100644
--- a/src/test/perl/PostgreSQL/Test/Cluster.pm
+++ b/src/test/perl/PostgreSQL/Test/Cluster.pm
@@ -2168,68 +2168,6 @@ sub pgbench
 
 =pod
 
-=item $node->check_connect_log_contents($offset, $test_name, %parameters)
-
-Check connection log contents.
-
-=over
-
-=item $test_name
-
-Name of test for error messages.
-
-=item $offset
-
-Offset of the log file.
-
-=item log_like => [ qr/required message/ ]
-
-If given, it must be an array reference containing a list of regular
-expressions that must match against the server log, using
-C<Test::More::like()>.
-
-=item log_unlike => [ qr/prohibited message/ ]
-
-If given, it must be an array reference containing a list of regular
-expressions that must NOT match against the server log.  They will be
-passed to C<Test::More::unlike()>.
-
-=back
-
-=cut
-
-sub check_connect_log_contents
-{
-       my ($self, $test_name, $offset, %params) = @_;
-
-       my (@log_like, @log_unlike);
-       if (defined($params{log_like}))
-       {
-               @log_like = @{ $params{log_like} };
-       }
-       if (defined($params{log_unlike}))
-       {
-               @log_unlike = @{ $params{log_unlike} };
-       }
-
-       if (@log_like or @log_unlike)
-       {
-               my $log_contents =
-                 PostgreSQL::Test::Utils::slurp_file($self->logfile, $offset);
-
-               while (my $regex = shift @log_like)
-               {
-                       like($log_contents, $regex, "$test_name: log matches");
-               }
-               while (my $regex = shift @log_unlike)
-               {
-                       unlike($log_contents, $regex, "$test_name: log does not 
match");
-               }
-       }
-}
-
-=pod
-
 =item $node->connect_ok($connstr, $test_name, %params)
 
 Attempt a connection with a custom connection string.  This is expected
@@ -2246,6 +2184,12 @@ instead of the default.
 
 If this regular expression is set, matches it with the output generated.
 
+=item log_like => [ qr/required message/ ]
+
+=item log_unlike => [ qr/prohibited message/ ]
+
+See C<log_check(...)>.
+
 =back
 
 =cut
@@ -2285,7 +2229,7 @@ sub connect_ok
 
        is($stderr, "", "$test_name: no stderr");
 
-       $self->check_connect_log_contents($test_name, $log_location, %params);
+       $self->log_check($test_name, $log_location, %params);
 }
 
 =pod
@@ -2301,6 +2245,12 @@ to fail.
 
 If this regular expression is set, matches it with the output generated.
 
+=item log_like => [ qr/required message/ ]
+
+=item log_unlike => [ qr/prohibited message/ ]
+
+See C<log_check(...)>.
+
 =back
 
 =cut
@@ -2327,7 +2277,7 @@ sub connect_fails
                like($stderr, $params{expected_stderr}, "$test_name: matches");
        }
 
-       $self->check_connect_log_contents($test_name, $log_location, %params);
+       $self->log_check($test_name, $log_location, %params);
 }
 
 =pod
@@ -2533,6 +2483,67 @@ sub log_content
        return PostgreSQL::Test::Utils::slurp_file($self->logfile);
 }
 
+=pod
+
+=item $node->log_check($offset, $test_name, %parameters)
+
+Check contents of server logs.
+
+=over
+
+=item $test_name
+
+Name of test for error messages.
+
+=item $offset
+
+Offset of the log file.
+
+=item log_like => [ qr/required message/ ]
+
+If given, it must be an array reference containing a list of regular
+expressions that must match against the server log, using
+C<Test::More::like()>.
+
+=item log_unlike => [ qr/prohibited message/ ]
+
+If given, it must be an array reference containing a list of regular
+expressions that must NOT match against the server log.  They will be
+passed to C<Test::More::unlike()>.
+
+=back
+
+=cut
+
+sub log_check
+{
+       my ($self, $test_name, $offset, %params) = @_;
+
+       my (@log_like, @log_unlike);
+       if (defined($params{log_like}))
+       {
+               @log_like = @{ $params{log_like} };
+       }
+       if (defined($params{log_unlike}))
+       {
+               @log_unlike = @{ $params{log_unlike} };
+       }
+
+       if (@log_like or @log_unlike)
+       {
+               my $log_contents =
+                 PostgreSQL::Test::Utils::slurp_file($self->logfile, $offset);
+
+               while (my $regex = shift @log_like)
+               {
+                       like($log_contents, $regex, "$test_name: log matches");
+               }
+               while (my $regex = shift @log_unlike)
+               {
+                       unlike($log_contents, $regex, "$test_name: log does not 
match");
+               }
+       }
+}
 
 =pod
 
diff --git a/src/test/recovery/t/033_replay_tsp_drops.pl 
b/src/test/recovery/t/033_replay_tsp_drops.pl
index 307c30bc6b..af97ed9f70 100644
--- a/src/test/recovery/t/033_replay_tsp_drops.pl
+++ b/src/test/recovery/t/033_replay_tsp_drops.pl
@@ -142,4 +142,4 @@ while ($max_attempts-- >= 0)
 }
 ok($max_attempts > 0, "invalid directory creation is detected");
 
-done_testing();
\ No newline at end of file
+done_testing();
-- 
2.40.1

Attachment: signature.asc
Description: PGP signature

Reply via email to