[check_postgres] [commit] Add test for locks.

check_postgres at bucardo.org check_postgres at bucardo.org
Wed Mar 25 13:48:07 UTC 2009


Committed by Greg Sabino Mullane <greg at endpoint.com>

Add test for locks.

---
 check_postgres.pl |    1 +
 t/02_locks.t      |   77 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/check_postgres.pl b/check_postgres.pl
index 882b176..6ac0c86 100755
--- a/check_postgres.pl
+++ b/check_postgres.pl
@@ -3185,6 +3185,7 @@ sub check_locks {
 
 		if (!$gotone) {
 			add_unknown msg('no-match-db');
+			next;
 		}
 
 		## If not specific errors, just use the total
diff --git a/t/02_locks.t b/t/02_locks.t
new file mode 100644
index 0000000..594fd1f
--- /dev/null
+++ b/t/02_locks.t
@@ -0,0 +1,77 @@
+#!perl
+
+## Test the "locks" action
+
+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/;
+
+my $cp = CP_Testing->new();
+
+$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);
+
+$t=qq{$S fails when invalid database used};
+like ($cp->run('locks', '--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);
+
+$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 (?,?,?)';
+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);
+
+$t=qq{$S returns correct warning message};
+like ($cp->run('locks', '--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);
+
+$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);
+
+$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);
+
+$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);
+
+$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);
+
+$t=qq{$S returns correct MRTG output};
+is ($cp->run('locks', '--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);
+
+$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);
+
+$t=qq{$S returns correct multiple item output};
+like ($cp->run('locks', '--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');
+
+exit;
-- 
1.6.0.5



More information about the Check_postgres mailing list