[check_postgres] [commit] Put in a common testing schema.
check_postgres at bucardo.org
check_postgres at bucardo.org
Tue Apr 7 17:56:12 UTC 2009
Committed by Greg Sabino Mullane <greg at endpoint.com>
Put in a common testing schema.
Allow fake functions as well.
Add wal_files test, clean up others with some refactoring.
---
t/00_basic.t | 2 +-
t/02_backends.t | 97 ++++++++++++++++++++++++-------------------------
t/02_checkpoint.t | 31 ++++++++--------
t/02_locks.t | 35 +++++++++---------
t/02_replicate_row.t | 21 +++++------
t/02_version.t | 3 +-
t/02_walfiles.t | 54 ++++++++++++++++++++++++++++
t/CP_Testing.pm | 69 +++++++++++++++++++++++++++++++-----
8 files changed, 206 insertions(+), 106 deletions(-)
diff --git a/t/00_basic.t b/t/00_basic.t
index 45d6aaa..dd58b71 100644
--- a/t/00_basic.t
+++ b/t/00_basic.t
@@ -1,6 +1,6 @@
#!perl
-## Simply test that the script compiles and gives a good version
+## Simply test that the script compiles and gives a valid version
use strict;
use warnings;
diff --git a/t/02_backends.t b/t/02_backends.t
index f443d4d..23d9a4a 100644
--- a/t/02_backends.t
+++ b/t/02_backends.t
@@ -6,14 +6,13 @@ use strict;
use warnings;
use Data::Dumper;
use DBI;
-use Cwd;
use Test::More tests => 52;
use lib 't','.';
use CP_Testing;
use vars qw/$dbh $dbh2 $SQL $count $host $t $result/;
-my $cp = CP_Testing->new();
+my $cp = CP_Testing->new( {default_action => 'backends'} );
$dbh = $cp->test_database_handle();
@@ -27,7 +26,7 @@ is ($count, 1, $t);
$host = $cp->get_host();
-$result = $cp->run('backends');
+$result = $cp->run();
my $S = q{Action 'backends'};
@@ -47,110 +46,110 @@ $t=qq{$S returned correct performance data};
like ($result, qr{ \| time=(\d\.\d\d) ardala=0 beedeebeedee=0 postgres=2 template0=0 template1=0\s$}, $t);
$t=qq{$S fails when called with an invalid option};
-like ($cp->run('backends', 'foobar=12'), qr{^\s*Usage:}, $t);
+like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t);
$t=qq{$S fails when called with an invalid warning option};
-like ($cp->run('backends', '-w felz'), qr{^ERROR: Warning}, $t);
-like ($cp->run('backends', '-w " 12345"'), qr{^ERROR: Warning}, $t);
-like ($cp->run('backends', '-w 23%%'), qr{^ERROR: Warning}, $t);
+like ($cp->run('-w felz'), qr{^ERROR: Warning}, $t);
+like ($cp->run('-w " 12345"'), qr{^ERROR: Warning}, $t);
+like ($cp->run('-w 23%%'), qr{^ERROR: Warning}, $t);
$t=qq{$S fails when called with an invalid critical option};
-like ($cp->run('backends', '-c felz'), qr{^ERROR: Critical}, $t);
-like ($cp->run('backends', '-c " 12345"'), qr{^ERROR: Critical}, $t);
-like ($cp->run('backends', '-c 23%%'), qr{^ERROR: Critical}, $t);
+like ($cp->run('-c felz'), qr{^ERROR: Critical}, $t);
+like ($cp->run('-c " 12345"'), qr{^ERROR: Critical}, $t);
+like ($cp->run('-c 23%%'), qr{^ERROR: Critical}, $t);
$t=qq{$S fails when the warning option is greater than the critical option};
-like ($cp->run('backends', '-w 20 -c 10'), qr{^ERROR: The 'warning'.+greater}, $t);
-like ($cp->run('backends', '-w 20% -c 10%'), qr{^ERROR: The 'warning'.+greater}, $t);
+like ($cp->run('-w 20 -c 10'), qr{^ERROR: The 'warning'.+greater}, $t);
+like ($cp->run('-w 20% -c 10%'), qr{^ERROR: The 'warning'.+greater}, $t);
$t=qq{$S fails when the warning option is less than the critical option};
-like ($cp->run('backends', '-w -10 -c -20'), qr{^ERROR: The 'warning'.+less}, $t);
+like ($cp->run('-w -10 -c -20'), qr{^ERROR: The 'warning'.+less}, $t);
$t=qq{$S fails when the warning option is a negative percent};
-like ($cp->run('backends', '-w -10%'), qr{^ERROR: Cannot specify a negative percent}, $t);
+like ($cp->run('-w -10%'), qr{^ERROR: Cannot specify a negative percent}, $t);
$t=qq{$S fails when the critical option is a negative percent};
-like ($cp->run('backends', '-c -10%'), qr{^ERROR: Cannot specify a negative percent}, $t);
+like ($cp->run('-c -10%'), qr{^ERROR: Cannot specify a negative percent}, $t);
$t=qq{$S with the 'noidle' option returns expected result};
-like ($cp->run('backends', '-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 2 of 10 connections}, $t);
+like ($cp->run('-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 2 of 10 connections}, $t);
$dbh2 = $cp->get_fresh_dbh();
$dbh2->do('SELECT 123');
-like ($cp->run('backends', '-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 3 of 10 connections}, $t);
+like ($cp->run('-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 3 of 10 connections}, $t);
$dbh2->commit();
-like ($cp->run('backends', '-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 2 of 10 connections}, $t);
+like ($cp->run('-noidle'), qr{^POSTGRES_BACKENDS OK:.+ 2 of 10 connections}, $t);
$t=qq{$S has critical option trump the warning option};
-like ($cp->run('backends', '-w 1 -c 1'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '--critical=1 --warning=0'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-w 1 -c 1'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('--critical=1 --warning=0'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
$t=qq{$S works with warning option as an absolute number};
-like ($cp->run('backends', '-w 2'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w 3'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w 4'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-w 2'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w 3'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w 4'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works with warning option as an percentage};
-like ($cp->run('backends', '-w 20%'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w 30%'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w 40%'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-w 20%'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w 30%'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w 40%'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works with warning option as a negative number};
-like ($cp->run('backends', '-w -6'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w -7'), qr{^POSTGRES_BACKENDS WARNING}, $t);
-like ($cp->run('backends', '-w -8'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-w -6'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w -7'), qr{^POSTGRES_BACKENDS WARNING}, $t);
+like ($cp->run('-w -8'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works with critical option as an absolute number};
-like ($cp->run('backends', '-c 2'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c 3'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c 4'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-c 2'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c 3'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c 4'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works with critical option as an percentage};
-like ($cp->run('backends', '-c 20%'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c 30%'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c 40%'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-c 20%'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c 30%'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c 40%'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works with critical option as a negative number};
-like ($cp->run('backends', '-c -6'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c -7'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
-like ($cp->run('backends', '-c -8'), qr{^POSTGRES_BACKENDS OK}, $t);
+like ($cp->run('-c -6'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c -7'), qr{^POSTGRES_BACKENDS CRITICAL}, $t);
+like ($cp->run('-c -8'), qr{^POSTGRES_BACKENDS OK}, $t);
$t=qq{$S works when no items caught by pg_stat_activity};
## This is tricky to test properly.
$cp->create_fake_pg_table('pg_stat_activity');
-like ($cp->run('backends'), qr{^POSTGRES_BACKENDS OK: .+No connections}, $t);
+like ($cp->run(), qr{^POSTGRES_BACKENDS OK: .+No connections}, $t);
$t=qq{$S returns correct MRTG output when no rows};
-is ($cp->run('backends', '--output=MRTG'), qq{0\n0\n\nDB=postgres Max connections=10\n}, $t);
+is ($cp->run('--output=MRTG'), qq{0\n0\n\nDB=postgres Max connections=10\n}, $t);
$cp->remove_fake_pg_table('pg_stat_activity');
$t=qq{$S fails as expected when max_connections cannot be determined};
$cp->create_fake_pg_table('pg_settings');
-like ($cp->run('backends'), qr{^POSTGRES_BACKENDS UNKNOWN: .+max_connections}, $t);
+like ($cp->run(), qr{^POSTGRES_BACKENDS UNKNOWN: .+max_connections}, $t);
$cp->remove_fake_pg_table('pg_settings');
$t=qq{$S returns correct MRTG output when rows found};
-is ($cp->run('backends', '--output=MRTG'), qq{3\n0\n\nDB=postgres Max connections=10\n}, $t);
+is ($cp->run('--output=MRTG'), qq{3\n0\n\nDB=postgres Max connections=10\n}, $t);
$t=qq{$S works when include forces no matches};
-like ($cp->run('backends', '--include=foobar'), qr{POSTGRES_BACKENDS OK: .+No connections}, $t);
+like ($cp->run('--include=foobar'), qr{POSTGRES_BACKENDS OK: .+No connections}, $t);
$t=qq{$S works when include has valid database};
-like ($cp->run('backends', '--include=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
+like ($cp->run('--include=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
$t=qq{$S works when exclude forces no matches};
-like ($cp->run('backends', '--exclude=postgres'), qr{POSTGRES_BACKENDS OK: .+No connections}, $t);
+like ($cp->run('--exclude=postgres'), qr{POSTGRES_BACKENDS OK: .+No connections}, $t);
$t=qq{$S works when exclude excludes nothing};
-like ($cp->run('backends', '--exclude=foobar'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
+like ($cp->run('--exclude=foobar'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
$t=qq{$S works when include and exclude make a match};
-like ($cp->run('backends', '--exclude=postgres --include=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
+like ($cp->run('--exclude=postgres --include=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
$t=qq{$S works when include and exclude make a match};
-like ($cp->run('backends', '--include=postgres --exclude=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
+like ($cp->run('--include=postgres --exclude=postgres'), qr{POSTGRES_BACKENDS OK: .+3 of 10}, $t);
$t=qq{$S returned correct performance data with include};
-like ($cp->run('backends', '--include=postgres'), qr{ \| time=(\d\.\d\d) ardala=0 beedeebeedee=0 postgres=3}, $t);
+like ($cp->run('--include=postgres'), qr{ \| time=(\d\.\d\d) ardala=0 beedeebeedee=0 postgres=3}, $t);
exit;
diff --git a/t/02_checkpoint.t b/t/02_checkpoint.t
index b40fd56..9f77213 100644
--- a/t/02_checkpoint.t
+++ b/t/02_checkpoint.t
@@ -6,64 +6,63 @@ use strict;
use warnings;
use Data::Dumper;
use DBI;
-use Cwd;
use Test::More tests => 13;
use lib 't','.';
use CP_Testing;
-use vars qw/$dbh $dbh2 $SQL $count $host $t $result $info/;
+use vars qw/$dbh $SQL $t/;
-my $cp = CP_Testing->new();
+my $cp = CP_Testing->new( {default_action => 'checkpoint'} );
$dbh = $cp->test_database_handle();
my $S = q{Action 'checkpoint'};
$t=qq{$S fails when called with an invalid option};
-like ($cp->run('checkpoint', 'foobar=12'), qr{^\s*Usage:}, $t);
+like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t);
$t=qq{$S fails when called without warning or critical};
-like ($cp->run('checkpoint', ''), qr{Must provide a warning and/or critical}, $t);
+like ($cp->run(''), qr{Must provide a warning and/or critical}, $t);
$t=qq{$S fails when called with invalid warning option};
-like ($cp->run('checkpoint', '-w foo'), qr{ERROR: .+'warning'.+valid time}, $t);
+like ($cp->run('-w foo'), qr{ERROR: .+'warning'.+valid time}, $t);
$t=qq{$S fails when called with invalid critical option};
-like ($cp->run('checkpoint', '-c foo'), qr{ERROR: .+'critical'.+valid time}, $t);
+like ($cp->run('-c foo'), qr{ERROR: .+'critical'.+valid time}, $t);
$t=qq{$S fails when called without a datadir option and PGDATA is not set};
delete $ENV{PGDATA};
-like ($cp->run('checkpoint', '-c 10'), qr{ERROR: Must supply a --datadir}, $t);
+like ($cp->run('-c 10'), qr{ERROR: Must supply a --datadir}, $t);
$t=qq{$S fails when called with an invalid datadir option and PGDATA is not set};
-like ($cp->run('checkpoint', '-c 10 --datadir=foobar'), qr{ERROR: Invalid data_directory}, $t);
+like ($cp->run('-c 10 --datadir=foobar'), qr{ERROR: Invalid data_directory}, $t);
my $host = $cp->get_host();
$t=qq{$S fails when called against a non datadir datadir};
-like ($cp->run('checkpoint', "-c 10 --datadir=$host"), qr{ERROR: Call to pg_controldata}, $t);
+like ($cp->run("-c 10 --datadir=$host"), qr{ERROR: Call to pg_controldata}, $t);
$t=qq{$S works when called for a recent checkpoint};
my $dbh = $cp->get_dbh();
$dbh->do('CHECKPOINT');
$dbh->commit();
$host =~ s/socket$//;
-like ($cp->run('checkpoint', "-w 20 --datadir=$host"), qr{POSTGRES_CHECKPOINT OK}, $t);
+like ($cp->run("-w 20 --datadir=$host"), qr{POSTGRES_CHECKPOINT OK}, $t);
$t=qq{$S returns a warning when checkpoint older than warning option};
diag "Sleeping for a bit to age the checkpoint time...\n";
sleep 2;
-like ($cp->run('checkpoint', "-w 1 --datadir=$host"), qr{WARNING:}, $t);
+like ($cp->run("-w 1 --datadir=$host"), qr{WARNING:}, $t);
$t=qq{$S returns a critical when checkpoint older than critical option};
-like ($cp->run('checkpoint', "-c 1 --datadir=$host"), qr{CRITICAL:}, $t);
+like ($cp->run("-c 1 --datadir=$host"), qr{CRITICAL:}, $t);
$t=qq{$S returns the correct number of seconds};
-like ($cp->run('checkpoint', "-c 1 --datadir=$host"), qr{was \d seconds ago}, $t);
+like ($cp->run("-c 1 --datadir=$host"), qr{was \d seconds ago}, $t);
$t=qq{$S returns the expected output for MRTG};
-like ($cp->run('checkpoint', "-c 1 --output=MRTG --datadir=$host"), qr{^\d\n0\n\nLast checkpoint was \d seconds ago}, $t);
+like ($cp->run("-c 1 --output=MRTG --datadir=$host"), qr{^\d\n0\n\nLast checkpoint was \d seconds ago}, $t);
$t=qq{$S returns the expected output for MRTG};
-like ($cp->run('checkpoint', "-c 199 --output=MRTG --datadir=$host"), qr{^\d\n0\n\nLast checkpoint was \d seconds ago}, $t);
+like ($cp->run("-c 199 --output=MRTG --datadir=$host"), qr{^\d\n0\n\nLast checkpoint was \d seconds ago}, $t);
exit;
diff --git a/t/02_locks.t b/t/02_locks.t
index 594fd1f..2f231c5 100644
--- a/t/02_locks.t
+++ b/t/02_locks.t
@@ -6,70 +6,69 @@ use strict;
use warnings;
use Data::Dumper;
use DBI;
-use Cwd;
use Test::More tests => 14;
use lib 't','.';
use CP_Testing;
-use vars qw/$dbh $dbh2 $SQL $count $host $t $result $info/;
+use vars qw/$dbh $SQL $t/;
-my $cp = CP_Testing->new();
+my $cp = CP_Testing->new( {default_action => 'locks' } );
$dbh = $cp->test_database_handle();
my $S = q{Action 'locks'};
$t=qq{$S fails when called with an invalid option};
-like ($cp->run('locks', 'foobar=12'), qr{^\s*Usage:}, $t);
+like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t);
$t=qq{$S fails when invalid database used};
-like ($cp->run('locks', '--dbname=foo'), qr{database "foo" does not exist}, $t);
+like ($cp->run('--dbname=foo'), qr{database "foo" does not exist}, $t);
$t=qq{$S fails when no matching databases found};
-like ($cp->run('locks', '--include=foo'), qr{No matching databases found}, $t);
+like ($cp->run('--include=foo'), qr{No matching databases found}, $t);
$cp->create_fake_pg_table('pg_locks');
$SQL = q{SELECT oid FROM pg_database WHERE datname = 'postgres'};
my $dboid = $dbh->selectall_arrayref($SQL)->[0][0];
-$SQL = 'INSERT INTO public.pg_locks(database,mode,granted) VALUES (?,?,?)';
+$SQL = 'INSERT INTO cptest.pg_locks(database,mode,granted) VALUES (?,?,?)';
my $fakelock_sth = $dbh->prepare($SQL);
$fakelock_sth->execute($dboid,'Exclusive','t');
$dbh->commit();
$t=qq{$S returns correct OK message};
-like ($cp->run('locks', '--critical=100'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
+like ($cp->run('--critical=100'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
$t=qq{$S returns correct warning message};
-like ($cp->run('locks', '--warning=1'), qr{POSTGRES_LOCKS WARNING.*total locks: 1 }, $t);
+like ($cp->run('--warning=1'), qr{POSTGRES_LOCKS WARNING.*total locks: 1 }, $t);
$t=qq{$S returns correct critical message};
-like ($cp->run('locks', '--critical=1'), qr{POSTGRES_LOCKS CRITICAL.*total locks: 1 }, $t);
+like ($cp->run('--critical=1'), qr{POSTGRES_LOCKS CRITICAL.*total locks: 1 }, $t);
$t=qq{$S returns correct OK message for specific lock type check};
-like ($cp->run('locks', '--critical="total=10;exclusive=3"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
+like ($cp->run('--critical="total=10;exclusive=3"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
$t=qq{$S returns correct OK message for specific lock type check};
-like ($cp->run('locks', '--critical="total=10;foobar=3"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
+like ($cp->run('--critical="total=10;foobar=3"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
$t=qq{$S returns correct warning message for specific lock type check};
-like ($cp->run('locks', '--warning="total=10;exclusive=1"'), qr{POSTGRES_LOCKS WARNING.*total "exclusive" locks: 1 }, $t);
+like ($cp->run('--warning="total=10;exclusive=1"'), qr{POSTGRES_LOCKS WARNING.*total "exclusive" locks: 1 }, $t);
$t=qq{$S returns correct critical message for specific lock type check};
-like ($cp->run('locks', '--critical="total=10;exclusive=1"'), qr{POSTGRES_LOCKS CRITICAL.*total "exclusive" locks: 1 }, $t);
+like ($cp->run('--critical="total=10;exclusive=1"'), qr{POSTGRES_LOCKS CRITICAL.*total "exclusive" locks: 1 }, $t);
$t=qq{$S returns correct MRTG output};
-is ($cp->run('locks', '--output=MRTG'), qq{1\n0\n\nDB: postgres\n}, $t);
+is ($cp->run('--output=MRTG'), qq{1\n0\n\nDB: postgres\n}, $t);
$t=qq{$S returns correct OK message for 'waiting' option};
-like ($cp->run('locks', '--warning="waiting=1"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
+like ($cp->run('--warning="waiting=1"'), qr{POSTGRES_LOCKS OK.*total=1 }, $t);
$t=qq{$S returns correct warning message for 'waiting' option};
$fakelock_sth->execute($dboid,'Exclusive','f');
$dbh->commit();
-like ($cp->run('locks', '--warning="waiting=1"'), qr{POSTGRES_LOCKS WARNING.*total "waiting" locks: 1 }, $t);
+like ($cp->run('--warning="waiting=1"'), qr{POSTGRES_LOCKS WARNING.*total "waiting" locks: 1 }, $t);
$t=qq{$S returns correct multiple item output};
-like ($cp->run('locks', '--warning="waiting=1;exclusive=2"'),
+like ($cp->run('--warning="waiting=1;exclusive=2"'),
qr{POSTGRES_LOCKS WARNING.*total "waiting" locks: 1 \* total "exclusive" locks: 2 }, $t);
$cp->remove_fake_pg_table('pg_locks');
diff --git a/t/02_replicate_row.t b/t/02_replicate_row.t
index 3c3ac7b..0879579 100644
--- a/t/02_replicate_row.t
+++ b/t/02_replicate_row.t
@@ -6,14 +6,13 @@ use strict;
use warnings;
use Data::Dumper;
use DBI;
-use Cwd;
use Test::More tests => 19;
use lib 't','.';
use CP_Testing;
-use vars qw/$dbh $dbh2 $SQL $count $host $t $result $info/;
+use vars qw/$dbh $dbh2 $SQL $t $result/;
-my $cp = CP_Testing->new();
+my $cp = CP_Testing->new( {default_action => 'replicate-row'} );
$dbh = $cp->test_database_handle();
$dbh2 = $cp->get_fresh_dbh({dbname=>'ardala'});
@@ -35,29 +34,29 @@ $dbh2->commit();
my $S = q{Action 'replicate_rows'};
$t=qq{$S fails when called with an invalid option};
-like ($cp->run('replicate-row', 'foobar=12'), qr{^\s*Usage:}, $t);
+like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t);
$t=qq{$S fails when called without warning or critical};
-like ($cp->run('replicate-row', ''), qr{Must provide a warning and/or critical}, $t);
+like ($cp->run(''), qr{Must provide a warning and/or critical}, $t);
$t=qq{$S fails when called with invalid warning};
-like ($cp->run('replicate-row', '-w foo'), qr{ERROR:.+'warning' must be a valid time}, $t);
+like ($cp->run('-w foo'), qr{ERROR:.+'warning' must be a valid time}, $t);
$t=qq{$S fails when called with invalid critical};
-like ($cp->run('replicate-row', '-c foo'), qr{ERROR:.+'critical' must be a valid time}, $t);
+like ($cp->run('-c foo'), qr{ERROR:.+'critical' must be a valid time}, $t);
$t=qq{$S fails when critical is greater than warning time};
-like ($cp->run('replicate-row', '-w 22 -c 44'), qr{ERROR:.+'warning' option cannot be less}, $t);
+like ($cp->run('-w 22 -c 44'), qr{ERROR:.+'warning' option cannot be less}, $t);
$t=qq{$S fails when called with no repinfo argument};
-like ($cp->run('replicate-row', '-w 2'), qr{ERROR: Need a repinfo}, $t);
+like ($cp->run('-w 2'), qr{ERROR: Need a repinfo}, $t);
$t=qq{$S fails when called with bad repinfo argument};
-like ($cp->run('replicate-row', '-w 2 -repinfo=abc'), qr{ERROR: Invalid repinfo}, $t);
+like ($cp->run('-w 2 -repinfo=abc'), qr{ERROR: Invalid repinfo}, $t);
# table, pk, id, col, val1, val2
$t=qq{$S fails when supplied values are equal};
-like ($cp->run('replicate-row', '-w 2 -repinfo=reptest,id,2,foo,yin,yin'), qr{ERROR: .+same values}, $t);
+like ($cp->run('-w 2 -repinfo=reptest,id,2,foo,yin,yin'), qr{ERROR: .+same values}, $t);
$t=qq{$S fails when no matching source row is found};
like ($cp->run('DB2replicate-row', '-w 2 -repinfo=reptest,id,4,foo,yin,yang'), qr{ERROR: .+not the right ones}, $t);
diff --git a/t/02_version.t b/t/02_version.t
index e5f2e10..6a0b4a1 100644
--- a/t/02_version.t
+++ b/t/02_version.t
@@ -6,12 +6,11 @@ use strict;
use warnings;
use Data::Dumper;
use DBI;
-use Cwd;
use Test::More tests => 28;
use lib 't','.';
use CP_Testing;
-use vars qw/$dbh $dbh2 $SQL $count $host $t $result $info/;
+use vars qw/$dbh $SQL $t/;
my $cp = CP_Testing->new();
diff --git a/t/02_walfiles.t b/t/02_walfiles.t
new file mode 100644
index 0000000..5204666
--- /dev/null
+++ b/t/02_walfiles.t
@@ -0,0 +1,54 @@
+#!perl
+
+## Test the "wal_files" action
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Test::More tests => 11;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $t/;
+
+my $cp = CP_Testing->new({default_action => 'wal_files'});
+
+$dbh = $cp->test_database_handle();
+
+my $S = q{Action 'wal_files'};
+
+$t=qq{$S fails when called with an invalid option};
+like ($cp->run('foobar=12'), qr{^\s*Usage:}, $t);
+
+$t=qq{$S fails when called with an invalid option};
+like ($cp->run('--warning=30%'), qr{ERROR:.+must be an integer}, $t);
+like ($cp->run('--warning=-30'), qr{ERROR:.+must be an integer}, $t);
+
+$t=qq{$S works as expected for warnings};
+like ($cp->run('--warning=30'), qr{POSTGRES_WAL_FILES OK}, $t);
+like ($cp->run('--warning=0'), qr{POSTGRES_WAL_FILES WARNING}, $t);
+
+$t=qq{$S works as expected for criticals};
+like ($cp->run('--critical=30'), qr{POSTGRES_WAL_FILES OK}, $t);
+like ($cp->run('--critical=0'), qr{POSTGRES_WAL_FILES CRITICAL}, $t);
+
+$cp->create_fake_pg_table('pg_ls_dir', 'text');
+
+like ($cp->run('--critical=1'), qr{POSTGRES_WAL_FILES OK}, $t);
+
+$dbh->do("INSERT INTO cptest.pg_ls_dir SELECT 'ABCDEF123456ABCDEF123456' FROM generate_series(1,99)");
+$dbh->commit();
+
+$t=qq{$S returns correct number of files};
+like ($cp->run('--critical=1'), qr{POSTGRES_WAL_FILES CRITICAL.+ 99 \|}, $t);
+
+$t=qq{$S returns correct MRTG information};
+is ($cp->run('--critical=1 --output=mrtg'), "99\n0\n\n\n", $t);
+
+$t=qq{$S returns correct MRTG information};
+is ($cp->run('--critical=101 --output=mrtg'), "99\n0\n\n\n", $t);
+
+$cp->remove_fake_pg_table('pg_ls_dir()');
+
+exit;
diff --git a/t/CP_Testing.pm b/t/CP_Testing.pm
index af1fa2f..d59a02b 100644
--- a/t/CP_Testing.pm
+++ b/t/CP_Testing.pm
@@ -12,13 +12,19 @@ my $DEBUG = 0;
use vars qw/$com $info $count/;
+my $fakeschema = 'cptest';
+
sub new {
my $class = shift;
+ my $arg = shift || {};
my $self = {
started => time(),
dbdir => 'test_database_check_postgres',
testuser => 'check_postgres_testing',
};
+ if (exists $arg->{default_action}) {
+ $self->{action} = $arg->{default_action};
+ }
return bless $self => $class;
}
@@ -143,6 +149,12 @@ sub test_database_handle {
$self->{dsn} = $dsn;
$self->{superdsn} = \@superdsn;
+ if (! exists $self->{keep_old_schema}) {
+ local $dbh->{Warn};
+ $dbh->do("DROP SCHEMA IF EXISTS $fakeschema CASCADE");
+ }
+
+
## Sanity check
$dbh->do("ALTER USER $dbuser SET search_path = public");
$dbh->do("SET search_path = public");
@@ -156,8 +168,9 @@ sub test_database_handle {
sub run {
my $self = shift;
- my $action = shift or die "First arg must be the command\n";
- my $extra = shift || '';
+ my @arg = @_;
+ my $extra = pop @arg || '';
+ my $action = @arg ? $arg[0] : $self->{action} || die "First arg must be the command\n";
my $double = $action =~ s/DB2// ? 1 : 0;
@@ -219,14 +232,32 @@ sub create_fake_pg_table {
my $self = shift;
my $name = shift || die;
+ my $args = shift || '';
my $dbh = $self->{dbh} || die;
my $dbuser = $self->{testuser} || die;
- {
+ if ($self->schema_exists($dbh,$fakeschema)) {
local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS public.$name");
+ $dbh->do("DROP TABLE IF EXISTS $fakeschema.$name");
}
- $dbh->do("CREATE TABLE public.$name AS SELECT * FROM $name LIMIT 0");
- $dbh->do("ALTER USER $dbuser SET search_path = public, pg_catalog");
+ else {
+ $dbh->do("CREATE SCHEMA $fakeschema");
+ }
+
+ my $funcargs = '';
+ if ($args) {
+ ($funcargs = $args) =~ s/\w+/NULL/g;
+ $funcargs = qq{($funcargs)};
+ }
+
+ $dbh->do("CREATE TABLE $fakeschema.$name AS SELECT * FROM $name$funcargs LIMIT 0");
+
+ if ($args) {
+ local $dbh->{Warn};
+ $dbh->do("DROP FUNCTION IF EXISTS $fakeschema.$name($args)");
+ $dbh->do("CREATE FUNCTION $fakeschema.$name($args) RETURNS SETOF TEXT LANGUAGE SQL AS 'SELECT * FROM $fakeschema.$name; '");
+ }
+
+ $dbh->do("ALTER USER $dbuser SET search_path = $fakeschema, public, pg_catalog");
$dbh->commit();
} ## end of create_fake_pg_table
@@ -236,17 +267,19 @@ sub remove_fake_pg_table {
my $self = shift;
my $name = shift || die;
+ (my $name2 = $name) =~ s/\(.+//;
my $dbh = $self->{dbh} || die;
my $dbuser = $self->{testuser} || die;
{
local $dbh->{Warn};
- $dbh->do("DROP TABLE IF EXISTS public.$name");
+ $dbh->do("DROP TABLE IF EXISTS public.$name2");
}
$dbh->do("ALTER USER $dbuser SET search_path = public");
$dbh->commit();
} ## end of remove_fake_pg_table
+
sub table_exists {
my ($self,$dbh,$table) = @_;
@@ -259,6 +292,20 @@ sub table_exists {
} ## end of table_exists
+
+sub schema_exists {
+
+ my ($self,$dbh,$schema) = @_;
+
+ my $SQL = 'SELECT count(1) FROM pg_namespace WHERE nspname = ?';
+ my $sth = $dbh->prepare($SQL);
+ $sth->execute($schema);
+ my $count = $sth->fetchall_arrayref()->[0][0];
+ return $count;
+
+} ## end of schema_exists
+
+
sub fake_version {
my $self = shift;
@@ -266,15 +313,19 @@ sub fake_version {
my $dbh = $self->{dbh} || die;
my $dbuser = $self->{testuser} || die;
+ if (! $self->schema_exists($dbh, $fakeschema)) {
+ $dbh->do("CREATE SCHEMA $fakeschema");
+ }
+
$dbh->do(qq{
-CREATE OR REPLACE FUNCTION public.version()
+CREATE OR REPLACE FUNCTION $fakeschema.version()
RETURNS TEXT
LANGUAGE SQL
AS \$\$
SELECT 'PostgreSQL $version on fakefunction for check_postgres.pl testing'::text;
\$\$
});
- $dbh->do("ALTER USER $dbuser SET search_path = public, pg_catalog");
+ $dbh->do("ALTER USER $dbuser SET search_path = $fakeschema, public, pg_catalog");
$dbh->commit();
} ## end of fake version
--
1.6.0.5
More information about the Check_postgres
mailing list