1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#!perl
## Test the "bloat" action
use 5.008;
use strict;
use warnings;
use Data::Dumper;
use Test::More tests => 30;
use lib 't','.';
use CP_Testing;
use vars qw/$dbh $SQL $t/;
my $cp = CP_Testing->new( {default_action => 'bloat'} );
$dbh = $cp->test_database_handle();
my $S = q{Action 'bloat'};
my $label = 'POSTGRES_BLOAT';
my $tname = 'cp_bloat_test';
$t=qq{$S fails when called with an invalid option};
like ($cp->run('foobar=12'), qr{Usage:}, $t);
$t=qq{$S fails when called with an invalid option};
like ($cp->run('-w=abc'), qr{must be a size or a percentage}, $t);
like ($cp->run('-c=abc'), qr{must be a size or a percentage}, $t);
$dbh->{AutoCommit} = 1;
$dbh->do('VACUUM FULL');
$dbh->{AutoCommit} = 0;
$t=qq{$S returns ok for no bloat};
like ($cp->run('-c=99GB'), qr{^$label OK: DB "postgres"}, $t);
$t=qq{$S returns ok for no bloat};
like ($cp->run('-w=10MB'), qr{^$label OK: DB "postgres"}, $t);
for my $size (qw/bytes kilobytes megabytes gigabytes terabytes exabytes petabytes zettabytes/) {
$t=qq{$S returns ok for no bloat with a unit of $size};
like ($cp->run("-w=1000000$size"), qr{^$label OK: DB "postgres"}, $t);
my $short = substr($size, 0, 1);
$t=qq{$S returns ok for no bloat with a unit of $short};
like ($cp->run("-w=1000000$short"), qr{^$label OK: DB "postgres"}, $t);
}
$t=qq{$S returns correct message if no tables due to exclusion};
like ($cp->run('-w=1% --include=foobar'), qr{^$label UNKNOWN:.+No matching relations found due to exclusion}, $t);
## Fresh database should have little bloat:
$t=qq{$S returns okay for fresh database with no bloat};
like ($cp->run('-w=1m'), qr{^$label OK: DB "postgres"}, $t);
$cp->drop_table_if_exists($tname);
$dbh->do("CREATE TABLE $tname AS SELECT 123::int AS foo FROM generate_series(1,10000)");
$dbh->do("UPDATE $tname SET foo = foo") for 1..1;
$dbh->do('ANALYZE');
$dbh->commit();
$t=qq{$S returns warning for bloated table};
like ($cp->run('-w 100000'), qr{^$label WARNING:.+$tname}, $t);
$t=qq{$S returns critical for bloated table};
like ($cp->run('-c 100000'), qr{^$label CRITICAL:.+$tname}, $t);
$t=qq{$S returns warning for bloated table using percentages};
like ($cp->run('-w 10%'), qr{^$label WARNING:.+$tname}, $t);
$t=qq{$S returns warning for bloated table using size or percent};
like ($cp->run('-w "100000 || 10%"'), qr{^$label WARNING:.+$tname}, $t);
$t=qq{$S returns warning for bloated table using big size or percent};
like ($cp->run('-w "1000000000 || 10%"'), qr{^$label WARNING:.+$tname}, $t);
$t=qq{$S returns warning for bloated table using size and percent};
like ($cp->run('-w "10000 && 10%"'), qr{^$label WARNING:.+$tname}, $t);
$t=qq{$S returns no warning for bloated table using big size and percent};
like ($cp->run('-w "1000000000 && 10%"'), qr{^$label OK: DB "postgres".+$tname}, $t);
$dbh->do("DROP TABLE $tname");
exit;
|