Refactor log check logic for connect_ok/fails in PostgreSQL::Test::Cluster
authorMichael Paquier
Fri, 9 Jun 2023 00:37:34 +0000 (09:37 +0900)
committerMichael Paquier
Fri, 9 Jun 2023 00:37:34 +0000 (09:37 +0900)
This commit refactors a bit the code in charge of checking for log
patterns when connections fail or succeed, by moving the log pattern
checks into their own routine, for clarity.  This has come up as
something to improve while discussing the refactoring of find_in_log().

Backpatch down to 14 where these routines are used, to ease the
introduction of new tests that could rely on them.

Author: Vignesh C, Michael Paquier
Discussion: https://postgr.es/m/CALDaNm0YSiLpjCmajwLfidQrFOrLNKPQir7s__PeVvh9U3uoTQ@mail.gmail.com
Backpatch-through: 14

src/test/perl/PostgresNode.pm

index cce3f621e713718100b39e3a977f1a3de71b78fe..d936d8ea42bc59362ff95621cfdb7a1e41c6eba9 100644 (file)
@@ -2148,15 +2148,9 @@ 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.
-
 =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.
+See C.
 
 =back
 
@@ -2177,16 +2171,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
@@ -2204,19 +2188,7 @@ sub connect_ok
    {
        like($stdout, $params{expected_stdout}, "$test_name: matches");
    }
-   if (@log_like or @log_unlike)
-   {
-       my $log_contents = TestLib::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->log_check($test_name, $log_location, %params);
 }
 
 =pod
@@ -2236,7 +2208,7 @@ If this regular expression is set, matches it with the output generated.
 
 =item log_unlike => [ qr/prohibited message/ ]
 
-See C<connect_ok(...)>, above.
+See C<log_check(...)>.
 
 =back
 
@@ -2247,16 +2219,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
@@ -2274,19 +2236,7 @@ sub connect_fails
        like($stderr, $params{expected_stderr}, "$test_name: matches");
    }
 
-   if (@log_like or @log_unlike)
-   {
-       my $log_contents = TestLib::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->log_check($test_name, $log_location, %params);
 }
 
 =pod
@@ -2459,6 +2409,67 @@ sub issues_sql_like
 
 =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.
+
+=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.
+
+=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 = TestLib::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->run_log(...)
 
 Runs a shell command like TestLib::run_log, but with connection parameters set