|
3 | 3 |
|
4 | 4 | use PostgresNode;
|
5 | 5 | use TestLib;
|
6 |
| -use Test::More; |
| 6 | +use Test::More tests => 4; |
7 | 7 | use Time::HiRes qw(usleep);
|
8 | 8 |
|
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 |
22 | 10 | my $node = get_new_node('primary');
|
23 |
| -$node->init(allows_streaming => 1); |
| 11 | +$node->init(); |
24 | 12 | $node->append_conf(
|
25 | 13 | 'postgresql.conf', qq(
|
26 | 14 | logging_collector = on
|
27 |
| -log_directory = 'log' |
28 |
| -log_filename = 'postgresql.log' |
| 15 | +lc_messages = 'C' |
29 | 16 | ));
|
30 | 17 |
|
31 | 18 | $node->start();
|
32 | 19 |
|
33 |
| -# Rename log file and rotate log. Then log file should appear again. |
| 20 | +# Verify that log output gets to the file |
34 | 21 |
|
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'); |
38 | 23 |
|
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; |
40 | 34 |
|
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... |
44 | 36 | 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++) |
46 | 79 | {
|
| 80 | + $second_logfile = slurp_file($node->data_dir . '/' . $lfname); |
| 81 | + last if $second_logfile =~ m/syntax error/; |
47 | 82 | usleep(100_000);
|
48 |
| - $attempts++; |
49 | 83 | }
|
50 | 84 |
|
51 |
| -ok(-e $logfile, "log file exists"); |
| 85 | +like($second_logfile, qr/syntax error/, |
| 86 | + 'found expected log file content in new log file'); |
52 | 87 |
|
53 | 88 | $node->stop();
|
0 commit comments