Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / pg_archivecleanup / t / 010_pg_archivecleanup.pl
blob8134c2a62e81cf452f1821d1e35c65049b05f1ce
2 # Copyright (c) 2021, PostgreSQL Global Development Group
4 use strict;
5 use warnings;
6 use TestLib;
7 use Test::More tests => 42;
9 program_help_ok('pg_archivecleanup');
10 program_version_ok('pg_archivecleanup');
11 program_options_handling_ok('pg_archivecleanup');
13 my $tempdir = TestLib::tempdir;
15 my @walfiles = (
16 '00000001000000370000000C.gz', '00000001000000370000000D',
17 '00000001000000370000000E', '00000001000000370000000F.partial',);
19 sub create_files
21 foreach my $fn (@walfiles, 'unrelated_file')
23 open my $file, '>', "$tempdir/$fn";
24 print $file 'CONTENT';
25 close $file;
27 return;
30 create_files();
32 command_fails_like(
33 ['pg_archivecleanup'],
34 qr/must specify archive location/,
35 'fails if archive location is not specified');
37 command_fails_like(
38 [ 'pg_archivecleanup', $tempdir ],
39 qr/must specify oldest kept WAL file/,
40 'fails if oldest kept WAL file name is not specified');
42 command_fails_like(
43 [ 'pg_archivecleanup', 'notexist', 'foo' ],
44 qr/archive location .* does not exist/,
45 'fails if archive location does not exist');
47 command_fails_like(
48 [ 'pg_archivecleanup', $tempdir, 'foo', 'bar' ],
49 qr/too many command-line arguments/,
50 'fails with too many command-line arguments');
52 command_fails_like(
53 [ 'pg_archivecleanup', $tempdir, 'foo' ],
54 qr/invalid file name argument/,
55 'fails with invalid restart file name');
58 # like command_like but checking stderr
59 my $stderr;
60 my $result = IPC::Run::run [ 'pg_archivecleanup', '-d', '-n', $tempdir,
61 $walfiles[2] ], '2>', \$stderr;
62 ok($result, "pg_archivecleanup dry run: exit code 0");
63 like(
64 $stderr,
65 qr/$walfiles[1].*would be removed/,
66 "pg_archivecleanup dry run: matches");
67 foreach my $fn (@walfiles)
69 ok(-f "$tempdir/$fn", "$fn not removed");
73 sub run_check
75 my ($suffix, $test_name) = @_;
77 create_files();
79 command_ok(
81 'pg_archivecleanup', '-x', '.gz', $tempdir,
82 $walfiles[2] . $suffix
84 "$test_name: runs");
86 ok(!-f "$tempdir/$walfiles[0]",
87 "$test_name: first older WAL file was cleaned up");
88 ok(!-f "$tempdir/$walfiles[1]",
89 "$test_name: second older WAL file was cleaned up");
90 ok(-f "$tempdir/$walfiles[2]",
91 "$test_name: restartfile was not cleaned up");
92 ok(-f "$tempdir/$walfiles[3]",
93 "$test_name: newer WAL file was not cleaned up");
94 ok(-f "$tempdir/unrelated_file",
95 "$test_name: unrelated file was not cleaned up");
96 return;
99 run_check('', 'pg_archivecleanup');
100 run_check('.partial', 'pg_archivecleanup with .partial file');
101 run_check('.00000020.backup', 'pg_archivecleanup with .backup file');