| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Utils; |
| 7 |
use Test::More; |
| 8 |
|
| 9 |
program_help_ok('pg_archivecleanup'); |
| 10 |
program_version_ok('pg_archivecleanup'); |
| 11 |
program_options_handling_ok('pg_archivecleanup'); |
| 12 |
|
| 13 |
my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 14 |
|
| 15 |
# WAL file patterns created before running each sub-scenario. "present" |
| 16 |
# tracks if the file with "name" still exists or not after running |
| 17 |
# pg_archivecleanup. |
| 18 |
my @walfiles_verbose = ( |
| 19 |
{ name => '00000001000000370000000D', present => 0 }, |
| 20 |
{ name => '00000001000000370000000E', present => 1 }); |
| 21 |
my @walfiles_with_gz = ( |
| 22 |
{ name => '00000001000000370000000C.gz', present => 0 }, |
| 23 |
{ name => '00000001000000370000000D', present => 0 }, |
| 24 |
{ name => '00000001000000370000000D.backup', present => 1 }, |
| 25 |
{ name => '00000001000000370000000E', present => 1 }, |
| 26 |
{ name => '00000001000000370000000F.partial', present => 1 }, |
| 27 |
{ name => 'unrelated_file', present => 1 }); |
| 28 |
my @walfiles_for_clean_backup_history = ( |
| 29 |
{ name => '00000001000000370000000D', present => 0 }, |
| 30 |
{ name => '00000001000000370000000D.00000028.backup', present => 0 }, |
| 31 |
{ name => '00000001000000370000000E', present => 1 }, |
| 32 |
{ name => '00000001000000370000000F.partial', present => 1 }, |
| 33 |
{ name => 'unrelated_file', present => 1 }); |
| 34 |
|
| 35 |
sub create_files |
| 36 |
{ |
| 37 |
foreach my $fn (map { $_->{name} } @_) |
| 38 |
{ |
| 39 |
open my $file, '>', "$tempdir/$fn" or die $!; |
| 40 |
|
| 41 |
print $file 'CONTENT'; |
| 42 |
close $file; |
| 43 |
} |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
sub remove_files |
| 48 |
{ |
| 49 |
foreach my $fn (map { $_->{name} } @_) |
| 50 |
{ |
| 51 |
unlink "$tempdir/$fn"; |
| 52 |
} |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
command_fails_like( |
| 57 |
['pg_archivecleanup'], |
| 58 |
qr/must specify archive location/, |
| 59 |
'fails if archive location is not specified'); |
| 60 |
|
| 61 |
command_fails_like( |
| 62 |
[ 'pg_archivecleanup', $tempdir ], |
| 63 |
qr/must specify oldest kept WAL file/, |
| 64 |
'fails if oldest kept WAL file name is not specified'); |
| 65 |
|
| 66 |
command_fails_like( |
| 67 |
[ 'pg_archivecleanup', 'notexist', 'foo' ], |
| 68 |
qr/archive location .* does not exist/, |
| 69 |
'fails if archive location does not exist'); |
| 70 |
|
| 71 |
command_fails_like( |
| 72 |
[ 'pg_archivecleanup', $tempdir, 'foo', 'bar' ], |
| 73 |
qr/too many command-line arguments/, |
| 74 |
'fails with too many command-line arguments'); |
| 75 |
|
| 76 |
command_fails_like( |
| 77 |
[ 'pg_archivecleanup', $tempdir, 'foo' ], |
| 78 |
qr/invalid file name argument/, |
| 79 |
'fails with invalid restart file name'); |
| 80 |
|
| 81 |
# Test a dry run, no files are physically removed, but logs are generated |
| 82 |
# to show what would be removed. |
| 83 |
{ |
| 84 |
create_files(@walfiles_verbose); |
| 85 |
|
| 86 |
my $stderr; |
| 87 |
my $oldestkeptwalfile = '00000001000000370000000E'; |
| 88 |
my $result = IPC::Run::run [ |
| 89 |
'pg_archivecleanup', '--debug', '--dry-run', $tempdir, |
| 90 |
$oldestkeptwalfile |
| 91 |
], |
| 92 |
'2>' => \$stderr; |
| 93 |
ok($result, "pg_archivecleanup dry run: exit code 0"); |
| 94 |
|
| 95 |
for my $walpair (@walfiles_verbose) |
| 96 |
{ |
| 97 |
if ($walpair->{present}) |
| 98 |
{ |
| 99 |
unlike( |
| 100 |
$stderr, |
| 101 |
qr/$walpair->{name}.*would be removed/, |
| 102 |
"pg_archivecleanup dry run for $walpair->{name}: matches"); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
like( |
| 107 |
$stderr, |
| 108 |
qr/$walpair->{name}.*would be removed/, |
| 109 |
"pg_archivecleanup dry run for $walpair->{name}: matches"); |
| 110 |
} |
| 111 |
} |
| 112 |
foreach my $fn (map { $_->{name} } @walfiles_verbose) |
| 113 |
{ |
| 114 |
ok(-f "$tempdir/$fn", "$fn not removed"); |
| 115 |
} |
| 116 |
|
| 117 |
remove_files(@walfiles_verbose); |
| 118 |
} |
| 119 |
|
| 120 |
sub run_check |
| 121 |
{ |
| 122 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 123 |
|
| 124 |
my ($testdata, $oldestkeptwalfile, $test_name, @options) = @_; |
| 125 |
|
| 126 |
create_files(@$testdata); |
| 127 |
|
| 128 |
command_ok( |
| 129 |
[ 'pg_archivecleanup', @options, $tempdir, $oldestkeptwalfile ], |
| 130 |
"$test_name: runs"); |
| 131 |
|
| 132 |
for my $walpair (@$testdata) |
| 133 |
{ |
| 134 |
if ($walpair->{present}) |
| 135 |
{ |
| 136 |
ok(-f "$tempdir/$walpair->{name}", |
| 137 |
"$test_name:$walpair->{name} was not cleaned up"); |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
ok(!-f "$tempdir/$walpair->{name}", |
| 142 |
"$test_name:$walpair->{name} was cleaned up"); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
remove_files(@$testdata); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
run_check(\@walfiles_with_gz, '00000001000000370000000E', |
| 151 |
'pg_archivecleanup', '-x.gz'); |
| 152 |
run_check( |
| 153 |
\@walfiles_with_gz, |
| 154 |
'00000001000000370000000E.partial', |
| 155 |
'pg_archivecleanup with .partial file', '-x.gz'); |
| 156 |
run_check( |
| 157 |
\@walfiles_with_gz, |
| 158 |
'00000001000000370000000E.00000020.backup', |
| 159 |
'pg_archivecleanup with .backup file', '-x.gz'); |
| 160 |
run_check(\@walfiles_for_clean_backup_history, |
| 161 |
'00000001000000370000000E', |
| 162 |
'pg_archivecleanup with --clean-backup-history', '-b'); |
| 163 |
|
| 164 |
done_testing(); |