Skip to content

Commit 30469a6

Browse files
committed
Refactor log check logic for connect_ok/fails in PostgreSQL::Test::Cluster
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
1 parent fd3def3 commit 30469a6

File tree

1 file changed

+65
-54
lines changed

1 file changed

+65
-54
lines changed

src/test/perl/PostgresNode.pm

Lines changed: 65 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,15 +2148,9 @@ If this regular expression is set, matches it with the output generated.
21482148
21492149
=item log_like => [ qr/required message/ ]
21502150
2151-
If given, it must be an array reference containing a list of regular
2152-
expressions that must match against the server log, using
2153-
C<Test::More::like()>.
2154-
21552151
=item log_unlike => [ qr/prohibited message/ ]
21562152
2157-
If given, it must be an array reference containing a list of regular
2158-
expressions that must NOT match against the server log. They will be
2159-
passed to C<Test::More::unlike()>.
2153+
See C<log_check(...)>.
21602154
21612155
=back
21622156
@@ -2177,16 +2171,6 @@ sub connect_ok
21772171
$sql = "SELECT \$\$connected with $connstr\$\$";
21782172
}
21792173

2180-
my (@log_like, @log_unlike);
2181-
if (defined($params{log_like}))
2182-
{
2183-
@log_like = @{ $params{log_like} };
2184-
}
2185-
if (defined($params{log_unlike}))
2186-
{
2187-
@log_unlike = @{ $params{log_unlike} };
2188-
}
2189-
21902174
my $log_location = -s $self->logfile;
21912175

21922176
# Never prompt for a password, any callers of this routine should
@@ -2204,19 +2188,7 @@ sub connect_ok
22042188
{
22052189
like($stdout, $params{expected_stdout}, "$test_name: matches");
22062190
}
2207-
if (@log_like or @log_unlike)
2208-
{
2209-
my $log_contents = TestLib::slurp_file($self->logfile, $log_location);
2210-
2211-
while (my $regex = shift @log_like)
2212-
{
2213-
like($log_contents, $regex, "$test_name: log matches");
2214-
}
2215-
while (my $regex = shift @log_unlike)
2216-
{
2217-
unlike($log_contents, $regex, "$test_name: log does not match");
2218-
}
2219-
}
2191+
$self->log_check($test_name, $log_location, %params);
22202192
}
22212193

22222194
=pod
@@ -2236,7 +2208,7 @@ If this regular expression is set, matches it with the output generated.
22362208
22372209
=item log_unlike => [ qr/prohibited message/ ]
22382210
2239-
See C<connect_ok(...)>, above.
2211+
See C<log_check(...)>.
22402212
22412213
=back
22422214
@@ -2247,16 +2219,6 @@ sub connect_fails
22472219
local $Test::Builder::Level = $Test::Builder::Level + 1;
22482220
my ($self, $connstr, $test_name, %params) = @_;
22492221

2250-
my (@log_like, @log_unlike);
2251-
if (defined($params{log_like}))
2252-
{
2253-
@log_like = @{ $params{log_like} };
2254-
}
2255-
if (defined($params{log_unlike}))
2256-
{
2257-
@log_unlike = @{ $params{log_unlike} };
2258-
}
2259-
22602222
my $log_location = -s $self->logfile;
22612223

22622224
# Never prompt for a password, any callers of this routine should
@@ -2274,19 +2236,7 @@ sub connect_fails
22742236
like($stderr, $params{expected_stderr}, "$test_name: matches");
22752237
}
22762238

2277-
if (@log_like or @log_unlike)
2278-
{
2279-
my $log_contents = TestLib::slurp_file($self->logfile, $log_location);
2280-
2281-
while (my $regex = shift @log_like)
2282-
{
2283-
like($log_contents, $regex, "$test_name: log matches");
2284-
}
2285-
while (my $regex = shift @log_unlike)
2286-
{
2287-
unlike($log_contents, $regex, "$test_name: log does not match");
2288-
}
2289-
}
2239+
$self->log_check($test_name, $log_location, %params);
22902240
}
22912241

22922242
=pod
@@ -2459,6 +2409,67 @@ sub issues_sql_like
24592409

24602410
=pod
24612411
2412+
=item $node->log_check($offset, $test_name, %parameters)
2413+
2414+
Check contents of server logs.
2415+
2416+
=over
2417+
2418+
=item $test_name
2419+
2420+
Name of test for error messages.
2421+
2422+
=item $offset
2423+
2424+
Offset of the log file.
2425+
2426+
=item log_like => [ qr/required message/ ]
2427+
2428+
If given, it must be an array reference containing a list of regular
2429+
expressions that must match against the server log, using
2430+
C<Test::More::like()>.
2431+
2432+
=item log_unlike => [ qr/prohibited message/ ]
2433+
2434+
If given, it must be an array reference containing a list of regular
2435+
expressions that must NOT match against the server log. They will be
2436+
passed to C<Test::More::unlike()>.
2437+
2438+
=back
2439+
2440+
=cut
2441+
2442+
sub log_check
2443+
{
2444+
my ($self, $test_name, $offset, %params) = @_;
2445+
2446+
my (@log_like, @log_unlike);
2447+
if (defined($params{log_like}))
2448+
{
2449+
@log_like = @{ $params{log_like} };
2450+
}
2451+
if (defined($params{log_unlike}))
2452+
{
2453+
@log_unlike = @{ $params{log_unlike} };
2454+
}
2455+
2456+
if (@log_like or @log_unlike)
2457+
{
2458+
my $log_contents = TestLib::slurp_file($self->logfile, $offset);
2459+
2460+
while (my $regex = shift @log_like)
2461+
{
2462+
like($log_contents, $regex, "$test_name: log matches");
2463+
}
2464+
while (my $regex = shift @log_unlike)
2465+
{
2466+
unlike($log_contents, $regex, "$test_name: log does not match");
2467+
}
2468+
}
2469+
}
2470+
2471+
=pod
2472+
24622473
=item $node->run_log(...)
24632474
24642475
Runs a shell command like TestLib::run_log, but with connection parameters set

0 commit comments

Comments
 (0)