1#!perl
2
3## Test the "wal_files" action
4
5use 5.008;
6use strict;
7use warnings;
8use Data::Dumper;
9use Test::More tests => 12;
10use lib 't','.';
11use CP_Testing;
12
13use vars qw/$dbh $t/;
14
15my $cp = CP_Testing->new({default_action => 'wal_files'});
16
17$dbh = $cp->test_database_handle();
18
19my $S = q{Action 'wal_files'};
20my $label = 'POSTGRES_WAL_FILES';
21
22$t=qq{$S fails when called with an invalid option};
23like ($cp->run('foobar=12'), qr{Usage:}, $t);
24
25$t=qq{$S fails when called with an invalid option};
26like ($cp->run('--warning=30%'), qr{ERROR:.+must be a positive integer}, $t);
27like ($cp->run('--warning=-30'), qr{ERROR:.+must be a positive integer}, $t);
28
29my $ver = $dbh->{pg_server_version};
30if ($ver < 80100) {
31
32    $t=qq{$S gives an error when run against an old Postgres version};
33    like ($cp->run('--warning=99'), qr{ERROR.*server version must be >= 8.1}, $t);
34
35  SKIP: {
36        skip 'Cannot test wal_files completely on Postgres 8.0 or lower', 7;
37    }
38
39    exit;
40}
41
42$t=qq{$S works as expected for warnings};
43like ($cp->run('--warning=30'), qr{^$label OK}, $t);
44like ($cp->run('--warning=1'), qr{^$label WARNING}, $t);
45
46$t=qq{$S works as expected for criticals};
47like ($cp->run('--critical=30'), qr{^$label OK}, $t);
48like ($cp->run('--critical=1'), qr{^$label CRITICAL}, $t);
49
50$cp->drop_schema_if_exists();
51$cp->create_fake_pg_table('pg_ls_dir', 'text');
52if ($ver >= 100000) {
53    $dbh->do(q{CREATE OR REPLACE FUNCTION cptest.pg_ls_waldir() RETURNS table(name text) AS 'SELECT * FROM cptest.pg_ls_dir' LANGUAGE SQL});
54}
55$dbh->commit();
56
57like ($cp->run('--critical=1'), qr{^$label OK}, $t);
58
59$dbh->do(q{INSERT INTO cptest.pg_ls_dir SELECT 'ABCDEF123456ABCDEF123456' FROM generate_series(1,99)});
60$dbh->commit();
61
62$t=qq{$S returns correct number of files};
63like ($cp->run('--critical=1'), qr{^$label CRITICAL.+ 99 \|}, $t);
64
65$t=qq{$S returns correct MRTG information};
66is ($cp->run('--critical=1 --output=mrtg'), "99\n0\n\n\n", $t);
67
68$t=qq{$S returns correct MRTG information};
69is ($cp->run('--critical=101 --output=mrtg'), "99\n0\n\n\n", $t);
70
71# test --lsfunc
72my $xlogdir = $ver >= 100000 ? 'pg_wal' : 'pg_xlog';
73$dbh->do(qq{CREATE OR REPLACE FUNCTION ls_xlog_dir()
74      RETURNS SETOF TEXT
75      AS \$\$ SELECT pg_ls_dir('$xlogdir') \$\$
76      LANGUAGE SQL
77      SECURITY DEFINER});
78$cp->create_fake_pg_table('ls_xlog_dir', ' ');
79$dbh->do(q{INSERT INTO cptest.ls_xlog_dir SELECT 'ABCDEF123456ABCDEF123456' FROM generate_series(1,55)});
80$dbh->commit();
81$t=qq{$S returns correct number of files};
82like ($cp->run('--critical=1 --lsfunc=ls_xlog_dir'), qr{^$label CRITICAL.+ 55 \|}, $t);
83
84$cp->drop_schema_if_exists();
85
86exit;
87