|
| 1 | +use strict; |
| 2 | +use warnings; |
| 3 | +use TestLib; |
| 4 | +use Test::Moretests=> 42; |
| 5 | + |
| 6 | +program_help_ok('pg_archivecleanup'); |
| 7 | +program_version_ok('pg_archivecleanup'); |
| 8 | +program_options_handling_ok('pg_archivecleanup'); |
| 9 | + |
| 10 | +my$tempdir = TestLib::tempdir; |
| 11 | + |
| 12 | +my@walfiles = ( |
| 13 | +'00000001000000370000000C.gz', |
| 14 | +'00000001000000370000000D', |
| 15 | +'00000001000000370000000E', |
| 16 | +'00000001000000370000000F.partial', |
| 17 | +); |
| 18 | + |
| 19 | +subcreate_files |
| 20 | +{ |
| 21 | +foreachmy$fn (@walfiles,'unrelated_file') |
| 22 | +{ |
| 23 | +openmy$file,'>',"$tempdir/$fn"; |
| 24 | +print$file'CONTENT'; |
| 25 | +close$file; |
| 26 | +} |
| 27 | +} |
| 28 | + |
| 29 | +create_files(); |
| 30 | + |
| 31 | +command_fails_like(['pg_archivecleanup'], |
| 32 | +qr/must specify archive location/, |
| 33 | +'fails if archive location is not specified'); |
| 34 | + |
| 35 | +command_fails_like(['pg_archivecleanup',$tempdir], |
| 36 | +qr/must specify oldest kept WAL file/, |
| 37 | +'fails if oldest kept WAL file name is not specified'); |
| 38 | + |
| 39 | +command_fails_like(['pg_archivecleanup','notexist','foo'], |
| 40 | +qr/archive location .* does not exist/, |
| 41 | +'fails if archive location does not exist'); |
| 42 | + |
| 43 | +command_fails_like(['pg_archivecleanup',$tempdir,'foo','bar'], |
| 44 | +qr/too many command-line arguments/, |
| 45 | +'fails with too many command-line arguments'); |
| 46 | + |
| 47 | +command_fails_like(['pg_archivecleanup',$tempdir,'foo'], |
| 48 | +qr/invalid file name argument/, |
| 49 | +'fails with invalid restart file name'); |
| 50 | + |
| 51 | +{ |
| 52 | +# like command_like but checking stderr |
| 53 | +my$stderr; |
| 54 | +my$result = IPC::Run::run ['pg_archivecleanup','-d','-n',$tempdir,$walfiles[2]],'2>', \$stderr; |
| 55 | +ok($result,"pg_archivecleanup dry run: exit code 0"); |
| 56 | +like($stderr,qr/$walfiles[1].*would be removed/,"pg_archivecleanup dry run: matches"); |
| 57 | +foreachmy$fn (@walfiles) |
| 58 | +{ |
| 59 | +ok(-f"$tempdir/$fn","$fn not removed"); |
| 60 | +} |
| 61 | +} |
| 62 | + |
| 63 | +subrun_check |
| 64 | +{ |
| 65 | +my ($suffix,$test_name) =@_; |
| 66 | + |
| 67 | +create_files(); |
| 68 | + |
| 69 | +command_ok(['pg_archivecleanup','-x','.gz',$tempdir,$walfiles[2] .$suffix], |
| 70 | +"$test_name: runs"); |
| 71 | + |
| 72 | +ok(!-f"$tempdir/$walfiles[0]","$test_name: first older WAL file was cleaned up"); |
| 73 | +ok(!-f"$tempdir/$walfiles[1]","$test_name: second older WAL file was cleaned up"); |
| 74 | +ok(-f"$tempdir/$walfiles[2]","$test_name: restartfile was not cleaned up"); |
| 75 | +ok(-f"$tempdir/$walfiles[3]","$test_name: newer WAL file was not cleaned up"); |
| 76 | +ok(-f"$tempdir/unrelated_file","$test_name: unrelated file was not cleaned up"); |
| 77 | +} |
| 78 | + |
| 79 | +run_check('','pg_archivecleanup'); |
| 80 | +run_check('.partial','pg_archivecleanup with .partial file'); |
| 81 | +run_check('.00000020.backup','pg_archivecleanup with .backup file'); |