File: 02_relation_size.t

package info (click to toggle)
check-postgres 2.21.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 952 kB
  • ctags: 319
  • sloc: perl: 10,232; makefile: 13; sh: 13
file content (133 lines) | stat: -rw-r--r-- 4,280 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!perl

## Test the "relation_size" action

use 5.006;
use strict;
use warnings;
use Data::Dumper;
use Test::More tests => 23;
use lib 't','.';
use CP_Testing;

use vars qw/$dbh $dbname $host $t $result $user/;

my $cp = CP_Testing->new({default_action => 'relation_size'});
$dbh = $cp->test_database_handle();
$dbname = $cp->get_dbname;
$host = $cp->get_host();
$user = $cp->get_user();

my $S = q{Action 'relation_size'};
my $label = q{POSTGRES_RELATION_SIZE};

my $testtbl = 'test_relation_size';

$t = qq{$S reports error when no warning/critical supplied};
is ($cp->run(), qq{ERROR: Must provide a warning and/or critical size\n}, $t);

$t = qq{$S reports error when warning/critical invalid};
is ($cp->run(q{-w -1}), qq{ERROR: Invalid size for 'warning' option\n}, $t);
is ($cp->run(q{-c -1}), qq{ERROR: Invalid size for 'critical' option\n}, $t);

my $ver = $dbh->{pg_server_version};
if ($ver < 80100) {

    $t=qq{$S gives an error when run against an old Postgres version};
    like ($cp->run('--warning=99'), qr{ERROR.*server version must be >= 8.1}, $t);

  SKIP: {
        skip 'Cannot test relation_size completely on Postgres 8.0 or lower', 19;
    }

    exit;
}

$result = $cp->run(q{-w 1});

$t = qq{$S self-identifies};
like ($result, qr:$label:, $t);

$t = qq{$S identifies database};
like ($result, qr{DB "$dbname"}, $t);

$t = qq{$S identifies host};
like ($result, qr{host:$host}, $t);

$t = qq{$S processes 'perflimit=1'};
like ($cp->run(q{-w 1 --perflimit 1}), qr{time=\d+\.\d\ds \w+\.\w+=\d+B;1\s+\Z}, $t);

$t = qq{$S processes 'perflimit=2'};
like ($cp->run(q{-w 1 --perflimit 2}), qr{time=\d+\.\d\ds \w+\.\w+=\d+B;1 \w+\.\w+=\d+B}, $t);

$t = qq{$S detects no matching tables due to unknown user};
like ($cp->run(q{-w 1 --includeuser foo}), qr{$label OK:.*No matching entries found due to user exclusion/inclusion options}, $t);

## We need to remove all tables to make this work correctly
$cp->drop_all_tables();
$dbh->do(qq{CREATE TABLE $testtbl (a integer)});

$dbh->commit;

$t = qq{$S detects matching tables using 'testuser'};
like ($cp->run(qq{-w 1 --includeuser=$user}),
     qr{$label OK:.*largest relation is table "public.$testtbl"}, $t);

$t = qq{$S detects no matching relations};
like ($cp->run(qq{-w 1 --includeuser=$user --include=foo}),
      qr{$label UNKNOWN.*No matching relations found due to exclusion/inclusion options}, $t);

$t = qq{$S detects largest relation (warning)};
$dbh->do(qq{INSERT INTO "$testtbl" SELECT a FROM generate_series(1,5000) AS s(a)});
$dbh->commit;
sleep 1;

like ($cp->run(qq{-w 1 --includeuser=$user --include=$testtbl}),
      qr{$label WARNING.*largest relation is table "\w+\.$testtbl": \d+ kB}, $t);

$t = qq{$S detects largest relation (critical)};
like ($cp->run(qq{-c 1 --includeuser=$user --include=$testtbl}),
      qr{$label CRITICAL.*largest relation is table "\w+\.$testtbl": \d+ kB}, $t);

$t = qq{$S outputs MRTG};
like ($cp->run(qq{--output=mrtg -w 1 --includeuser=$user --include=$testtbl}),
      qr{\A\d+\n0\n\nDB: $dbname TABLE: \w+\.$testtbl\n\z}, $t);

$t = qq{$S includes indexes};
$dbh->do(qq{CREATE INDEX "${testtbl}_index" ON "$testtbl" (a)});
$dbh->commit;
like ($cp->run(qq{-w 1 --includeuser=$user --include=${testtbl}_index}),
      qr{$label WARNING.*largest relation is index "${testtbl}_index": \d+ kB}, $t);

#### Switch gears, and test the related functions "check_table_size" and "check_index_size".

for $S (qw(table_size index_size)) {
    $result = $cp->run($S, q{-w 1});
    $label = "POSTGRES_\U$S";

    $t = qq{$S self-identifies};
    like ($result, qr:$label:, $t);

    $t = qq{$S identifies database};
    like ($result, qr{DB "$dbname"}, $t);

    $t = qq{$S identifies host};
    like ($result, qr{host:$host}, $t);

    $t = qq{$S includes its focus, excludes other};
    my $include = "--include=$testtbl" .
        ($S eq 'table_size'
         ? '_table'
         : '_index');
    my $exclude = "--exclude=$testtbl" .
        ($S ne 'table_size'
         ? '_table'
         : '_index');
    my $message = 'largest ' . ($S eq 'table_size'
                                ? 'table'
                                : 'index');
    like ($cp->run($S, qq{-w 1 --includeuser=$user $include $exclude}),
                   qr|$label.*$message|, $t)
}

exit;