Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9d5c22d

Browse files
committed
Improve logrotate test so that it meaningfully exercises syslogger.
Discussion of bug #15804 reveals that this test didn't really prove that the syslogger child process ever launched successfully, much less did anything. It was only checking that the expected log file gets created, and that's done in the postmaster. Moreover, the test assumed it could rename the log file, which is likely to fail on Windows (cf. commit d611175). Instead, use the default log file name pattern, which should result in a new file name being chosen after 1 second, and verify that rotation has occurred by checking for a new file name. Also add code to test that messages actually do propagate through the syslogger. In theory this version of the test should work on Windows, so revert d611175. Discussion: https://postgr.es/m/15804-3721117bf40fb654@postgresql.org
1 parent 8334515 commit 9d5c22d

File tree

1 file changed

+63
-28
lines changed

1 file changed

+63
-28
lines changed

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

+63-28
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,86 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More;
6+
use Test::More tests => 4;
77
use Time::HiRes qw(usleep);
88

9-
if ($windows_os)
10-
{
11-
plan skip_all => 'logrotate test not supported on Windows';
12-
exit;
13-
}
14-
else
15-
{
16-
plan tests => 1;
17-
}
18-
19-
20-
my $tempdir = TestLib::tempdir;
21-
9+
# Set up node with logging collector
2210
my $node = get_new_node('primary');
23-
$node->init(allows_streaming => 1);
11+
$node->init();
2412
$node->append_conf(
2513
'postgresql.conf', qq(
2614
logging_collector = on
27-
log_directory = 'log'
28-
log_filename = 'postgresql.log'
15+
lc_messages = 'C'
2916
));
3017

3118
$node->start();
3219

33-
# Rename log file and rotate log. Then log file should appear again.
20+
# Verify that log output gets to the file
3421

35-
my $logfile = $node->data_dir . '/log/postgresql.log';
36-
my $old_logfile = $node->data_dir . '/log/postgresql.old';
37-
rename($logfile, $old_logfile);
22+
$node->psql('postgres', 'SELECT 1/0');
3823

39-
$node->logrotate();
24+
my $current_logfiles = slurp_file($node->data_dir . '/current_logfiles');
25+
26+
note "current_logfiles = $current_logfiles";
27+
28+
like($current_logfiles, qr|^stderr log/postgresql-.*log$|,
29+
'current_logfiles is sane');
30+
31+
my $lfname = $current_logfiles;
32+
$lfname =~ s/^stderr //;
33+
chomp $lfname;
4034

41-
# pg_ctl logrotate doesn't wait until rotation request being completed. So
42-
# we have to wait some time until log file appears.
43-
my $attempts = 0;
35+
# might need to retry if logging collector process is slow...
4436
my $max_attempts = 180 * 10;
45-
while (not -e $logfile and $attempts < $max_attempts)
37+
38+
my $first_logfile;
39+
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
40+
{
41+
$first_logfile = slurp_file($node->data_dir . '/' . $lfname);
42+
last if $first_logfile =~ m/division by zero/;
43+
usleep(100_000);
44+
}
45+
46+
like($first_logfile, qr/division by zero/,
47+
'found expected log file content');
48+
49+
# Sleep 2 seconds and ask for log rotation; this should result in
50+
# output into a different log file name.
51+
sleep(2);
52+
$node->logrotate();
53+
54+
# pg_ctl logrotate doesn't wait for rotation request to be completed.
55+
# Allow a bit of time for it to happen.
56+
my $new_current_logfiles;
57+
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
58+
{
59+
$new_current_logfiles = slurp_file($node->data_dir . '/current_logfiles');
60+
last if $new_current_logfiles ne $current_logfiles;
61+
usleep(100_000);
62+
}
63+
64+
note "now current_logfiles = $new_current_logfiles";
65+
66+
like($new_current_logfiles, qr|^stderr log/postgresql-.*log$|,
67+
'new current_logfiles is sane');
68+
69+
$lfname = $new_current_logfiles;
70+
$lfname =~ s/^stderr //;
71+
chomp $lfname;
72+
73+
# Verify that log output gets to this file, too
74+
75+
$node->psql('postgres', 'fee fi fo fum');
76+
77+
my $second_logfile;
78+
for (my $attempts = 0; $attempts < $max_attempts; $attempts++)
4679
{
80+
$second_logfile = slurp_file($node->data_dir . '/' . $lfname);
81+
last if $second_logfile =~ m/syntax error/;
4782
usleep(100_000);
48-
$attempts++;
4983
}
5084

51-
ok(-e $logfile, "log file exists");
85+
like($second_logfile, qr/syntax error/,
86+
'found expected log file content in new log file');
5287

5388
$node->stop();

0 commit comments

Comments
 (0)