1#!perl 2 3## Test the "fsm_relations" action 4 5use 5.008; 6use strict; 7use warnings; 8use Data::Dumper; 9use Test::More tests => 7; 10use lib 't','.'; 11use CP_Testing; 12 13use vars qw/$dbh $SQL $t/; 14 15my $cp = CP_Testing->new( {default_action => 'fsm_relations'} ); 16 17$dbh = $cp->test_database_handle(); 18 19my $S = q{Action 'fsm_relations'}; 20my $label = 'POSTGRES_FSM_RELATIONS'; 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=40'), qr{ERROR:.+must be a percentage}, $t); 27 28$t=qq{$S fails when called with an invalid option}; 29like ($cp->run('--critical=50'), qr{ERROR:.+must be a percentage}, $t); 30 31my $ver = $dbh->{pg_server_version}; 32if ($ver >= 80400) { 33 SKIP: { 34 skip 'Cannot test fsm_relations completely on Postgres 8.4 or higher', 3; 35 } 36 37 $t=qq{$S gives an unknown when running against a 8.4 or higher version}; 38 like ($cp->run('--warning=10%'), qr{^$label UNKNOWN.*Cannot check fsm_relations}, $t); 39 40 exit; 41} 42 43## Create a fake fsm 'view' for testing 44$cp->set_fake_schema(); 45my $schema = $cp->get_fake_schema(); 46$cp->drop_table_if_exists($schema, 'pg_freespacemap_pages'); 47$cp->drop_table_if_exists($schema, 'pg_freespacemap_relations'); 48 49$dbh->do(qq{ 50CREATE TABLE $schema.pg_freespacemap_pages ( 51 reltablespace oid, 52 reldatabase oid, 53 relfilenode oid, 54 relblocknumber bigint, 55 bytes integer 56); 57}); 58$dbh->do(qq{ 59CREATE TABLE $schema.pg_freespacemap_relations ( 60 reltablespace oid, 61 reldatabase oid, 62 relfilenode oid, 63 avgrequest integer, 64 interestingpages integer, 65 sortedpages integer, 66 nextpage integer 67); 68}); 69$dbh->commit(); 70 71$t=qq{$S gives normal output for empty tables}; 72like ($cp->run('--warning=10%'), qr{^$label OK: .+fsm relations used: 0 of \d+}, $t); 73 74$dbh->do("INSERT INTO $schema.pg_freespacemap_pages VALUES (1663,16389,16911,34,764)"); 75my $sth = $dbh->prepare("INSERT INTO $schema.pg_freespacemap_relations VALUES (?,?,?,?,?,?,?)"); 76for (1..999) { 77 $sth->execute(1663,16389,16911,1077,52283,52283,37176); 78} 79$dbh->commit(); 80 81$t=qq{$S gives normal warning output}; 82like ($cp->run('--warning=10%'), qr{^$label WARNING: .+fsm relations used: 999 of \d+}, $t); 83 84$t=qq{$S gives normal critical output}; 85like ($cp->run('--critical=5%'), qr{^$label CRITICAL: .+fsm relations used: 999 of \d+}, $t); 86 87$t=qq{$S gives normal output for MRTG}; 88is ($cp->run('--critical=5% --output=MRTG'), qq{100\n999\n\n\n}, $t); 89 90exit; 91