Skip to content

Commit 72b76f7

Browse files
committed
Add regression tests for csvlog with the logging collector
These are added in the existing tests of pg_ctl for log rotation, that already tested stderr. The same amount of coverage is added for csvlog: - Checks for pg_current_logfile(). - Log rotation with expected file name. - Log contents generated. This test is refactored to minimize the amount of work required to add tests for new log formats, easing some upcoming work. Author: Michael Paquier, Sehrope Sarkuni Discussion: https://postgr.es/m/CAH7T-aqswBM6JWe4pDehi1uOiufqe06DJWaU5=X7dDLyqUExHg@mail.gmail.com
1 parent 2d77d83 commit 72b76f7

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

src/bin/pg_ctl/t/004_logrotate.pl

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,64 @@
66

77
use PostgresNode;
88
use TestLib;
9-
use Test::More tests => 5;
9+
use Test::More tests => 10;
1010
use Time::HiRes qw(usleep);
1111

12+
# Extract the file name of a $format from the contents of
13+
# current_logfiles.
14+
sub fetch_file_name
15+
{
16+
my $logfiles = shift;
17+
my $format = shift;
18+
my @lines = split(/\n/, $logfiles);
19+
my $filename = undef;
20+
foreach my $line (@lines)
21+
{
22+
if ($line =~ /$format (.*)$/gm)
23+
{
24+
$filename = $1;
25+
}
26+
}
27+
28+
return $filename;
29+
}
30+
31+
# Check for a pattern in the logs associated to one format.
32+
sub check_log_pattern
33+
{
34+
my $format = shift;
35+
my $logfiles = shift;
36+
my $pattern = shift;
37+
my $node = shift;
38+
my $lfname = fetch_file_name($logfiles, $format);
39+
40+
my $max_attempts = 180 * 10;
41+
42+
my $logcontents;
43+
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
44+
{
45+
$logcontents = slurp_file($node->data_dir . '/' . $lfname);
46+
last if $logcontents =~ m/$pattern/;
47+
usleep(100_000);
48+
}
49+
50+
like($logcontents, qr/$pattern/,
51+
"found expected log file content for $format");
52+
53+
# While we're at it, test pg_current_logfile() function
54+
is( $node->safe_psql('postgres', "SELECT pg_current_logfile('$format')"),
55+
$lfname,
56+
"pg_current_logfile() gives correct answer with $format");
57+
return;
58+
}
59+
1260
# Set up node with logging collector
1361
my $node = PostgresNode->new('primary');
1462
$node->init();
1563
$node->append_conf(
1664
'postgresql.conf', qq(
1765
logging_collector = on
66+
log_destination = 'stderr, csvlog'
1867
# these ensure stability of test results:
1968
log_rotation_age = 0
2069
lc_messages = 'C'
@@ -44,26 +93,12 @@
4493

4594
like(
4695
$current_logfiles,
47-
qr|^stderr log/postgresql-.*log$|,
96+
qr|^stderr log/postgresql-.*log
97+
csvlog log/postgresql-.*csv$|,
4898
'current_logfiles is sane');
4999

50-
my $lfname = $current_logfiles;
51-
$lfname =~ s/^stderr //;
52-
chomp $lfname;
53-
54-
my $first_logfile;
55-
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
56-
{
57-
$first_logfile = slurp_file($node->data_dir . '/' . $lfname);
58-
last if $first_logfile =~ m/division by zero/;
59-
usleep(100_000);
60-
}
61-
62-
like($first_logfile, qr/division by zero/, 'found expected log file content');
63-
64-
# While we're at it, test pg_current_logfile() function
65-
is($node->safe_psql('postgres', "SELECT pg_current_logfile('stderr')"),
66-
$lfname, 'pg_current_logfile() gives correct answer');
100+
check_log_pattern('stderr', $current_logfiles, 'division by zero', $node);
101+
check_log_pattern('csvlog', $current_logfiles, 'division by zero', $node);
67102

68103
# Sleep 2 seconds and ask for log rotation; this should result in
69104
# output into a different log file name.
@@ -84,28 +119,14 @@
84119

85120
like(
86121
$new_current_logfiles,
87-
qr|^stderr log/postgresql-.*log$|,
122+
qr|^stderr log/postgresql-.*log
123+
csvlog log/postgresql-.*csv$|,
88124
'new current_logfiles is sane');
89125

90-
$lfname = $new_current_logfiles;
91-
$lfname =~ s/^stderr //;
92-
chomp $lfname;
93-
94126
# Verify that log output gets to this file, too
95-
96127
$node->psql('postgres', 'fee fi fo fum');
97128

98-
my $second_logfile;
99-
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
100-
{
101-
$second_logfile = slurp_file($node->data_dir . '/' . $lfname);
102-
last if $second_logfile =~ m/syntax error/;
103-
usleep(100_000);
104-
}
105-
106-
like(
107-
$second_logfile,
108-
qr/syntax error/,
109-
'found expected log file content in new log file');
129+
check_log_pattern('stderr', $new_current_logfiles, 'syntax error', $node);
130+
check_log_pattern('csvlog', $new_current_logfiles, 'syntax error', $node);
110131

111132
$node->stop();

0 commit comments

Comments
 (0)