[check_postgres] [commit] More tests.

check_postgres at bucardo.org check_postgres at bucardo.org
Wed Apr 22 21:39:16 UTC 2009


Committed by Jeff Boes <jeff at endpoint.com>

More tests.

---
 t/02_logfile.t       |   65 ++++++++++++++++++++++++++++++++++++
 t/02_query_runtime.t |   89 ++++++++++++++++++++++++++++++++++++++++++++++++++
 t/02_query_time.t    |   64 ++++++++++++++++++++++++++++++++++++
 t/02_txn_idle.t      |   73 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 291 insertions(+), 0 deletions(-)

diff --git a/t/02_logfile.t b/t/02_logfile.t
new file mode 100644
index 0000000..0269b26
--- /dev/null
+++ b/t/02_logfile.t
@@ -0,0 +1,65 @@
+#!perl
+
+## Test the "logfile" action
+## this does not test $S for syslog or stderr output
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Test::More tests => 11;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $result $t $host $dbname/;
+
+my $cp = CP_Testing->new( {default_action => 'logfile'} );
+
+$dbh = $cp->test_database_handle();
+
+$host = $cp->get_host();
+$dbname = $cp->get_dbname();
+my $label = 'POSTGRES_LOGFILE';
+
+my $S = q{Action 'logfile'};
+my $logfile = 'test_database_check_postgres/pg.log';
+
+my $cmd = $cp->get_command("--logfile=$logfile");
+
+$result = $cp->run("--logfile=$logfile");
+
+$t = qq{$S self-identifies correctly};
+like ($result, qr{^$label}, $t);
+
+$t = qq{$S identifies host};
+like ($result, qr{host:$host}, $t);
+
+$t = qq{$S correctly identified logfile};
+like ($result, qr{logs to: $logfile}, $t);
+
+$t = qq{$S correcly identifies host};
+like ($result, qr{host:$host}, $t);
+
+$t = qq{$S returned expected text};
+like ($result, qr{\bOK\b}, $t);
+
+$t = qq{$S flagged missing logfile param};
+like ($cp->run(""), qr{^ERROR:.*redirected.*stderr}, $t);
+
+$t = qq{$S flagged erroneous logfile param};
+like ($result = $cp->run("--logfile $logfile" . 'x'), qr{^$label\b}, $t);
+
+$t = qq{$S covers unknown};
+like ($result, qr{\bUNKNOWN\b}, $t);
+
+$t = qq{$S covers warning};
+like ($cp->run("--warning=1 --logfile $logfile" . 'x'), qr{\bWARNING\b}, $t);
+
+$t = qq{$S returns correct MRTG (OK)};
+is ($cp->run("--output=mrtg --warning=1 --logfile $logfile"), qq{1\n0\n\n\n}, $t);
+
+$t = qq{$S returns correct MRTG (fail)};
+is ($cp->run("--output=mrtg --warning=1 --logfile $logfile" . 'x'),
+	qq{ERROR: logfile ${logfile}x does not exist!\n}, $t);
+
+exit;
diff --git a/t/02_query_runtime.t b/t/02_query_runtime.t
new file mode 100644
index 0000000..224d187
--- /dev/null
+++ b/t/02_query_runtime.t
@@ -0,0 +1,89 @@
+#!perl
+
+## Test the "query_runtime" action
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Test::More tests => 17;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $result $t $host $dbname/;
+
+my $testtbl = 'test_query_runtime';
+my $testview = $testtbl . '_view';
+
+my $cp = CP_Testing->new( {default_action => 'query_runtime'} );
+
+$dbh = $cp->test_database_handle();
+$dbname = $cp->get_dbname;
+$host = $cp->get_host();
+my $label = 'POSTGRES_QUERY_RUNTIME';
+
+local $dbh->{Warn} = 0;
+$dbh->do(qq{DROP TABLE IF EXISTS $testtbl CASCADE});
+$dbh->do(qq{DROP VIEW IF EXISTS $testview});
+$dbh->do(qq{CREATE TABLE "$testtbl" ("a" integer)}) or die $dbh->errstr;
+$dbh->commit;
+
+my $S = q{Action 'query_runtime'};
+
+$t = qq{$S self-identifies correctly};
+$result = $cp->run(qq{-w 0 --queryname=$testtbl});
+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 accepts valid -w input};
+for ('1 second',
+     '1 minute',
+     '1 hour',
+     '1 day'
+    ) {
+   like ($cp->run(qq{--queryname=$testtbl -w "$_"}), qr/^$label/, $t . " ($_)");
+}
+
+$t = qq{$S rejects invalid -w input};
+for ('-1 second',
+     'abc'
+    ) {
+   like($cp->run(qq{--queryname=$testtbl -w "$_"}), qr/^ERROR:.*?must be a valid time/, $t . " ($_)");
+}
+
+$dbh->do(qq{INSERT INTO "$testtbl" SELECT a::int FROM generate_series(1,5000) a});
+$dbh->commit;
+
+$t = qq{$S measures simple table};
+like ($cp->run(qq{--queryname=$testtbl -w 10}), qr{$label OK: .*? query runtime: \d+\.\d* }, $t);
+
+$t .= ' (MRTG)';
+like ($cp->run(qq{--output=mrtg --queryname=$testtbl -w 10}), qr{\d+\.\d+\n0\n\nDB: $dbname\n}, $t);
+
+$t = qq{$S expires simple table};
+like ($cp->run(qq{--queryname=$testtbl -w 0}), qr{$label WARNING: .*? query runtime: \d+\.\d* }, $t);
+
+$t .= ' (MRTG)';
+like ($cp->run(qq{--output=mrtg --queryname=$testtbl -w 0}), qr{\d+\.\d+\n0\n\nDB: $dbname\n}, $t);
+
+$dbh->do(qq{CREATE VIEW $testview AS SELECT 123});
+$dbh->commit;
+
+$t = qq{$S measures view};
+like ($cp->run(qq{--queryname=$testview -w 0}), qr{$label WARNING: .*query runtime: \d+\.\d* }, $t);
+
+$t .= ' (MRTG)';
+like ($cp->run(qq{--output=mrtg --queryname=$testview -w 20}), qr{\d+\.\d+\n0\n\nDB: $dbname\n}, $t);
+
+$t = qq{$S expires view};
+like ($cp->run(qq{--queryname=$testview -w 0}), qr{$label WARNING: .*query runtime: \d+\.\d* }, $t);
+
+$t .= ' (MRTG)';
+like ($cp->run(qq{--output=mrtg --queryname=$testview -w 0}), qr{\d+\.\d+\n0\n\nDB: $dbname\n}, $t);
+
+exit;
diff --git a/t/02_query_time.t b/t/02_query_time.t
new file mode 100644
index 0000000..c26fab6
--- /dev/null
+++ b/t/02_query_time.t
@@ -0,0 +1,64 @@
+#!perl
+
+## Test the "query_time" action
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Test::More tests => 13;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $result $t $host $dbname/;
+
+my $cp = CP_Testing->new( {default_action => 'query_time'} );
+
+$dbh = $cp->test_database_handle();
+$dbname = $cp->get_dbname;
+$host = $cp->get_host();
+my $label = 'POSTGRES_QUERY_TIME';
+
+my $S = q{Action 'query_time'};
+
+$t = qq{$S self-identifies correctly};
+$result = $cp->run(qq{-w 0});
+like ($result, qr{^$label}, $t);
+
+$t = qq{$S identifies host};
+like ($result, qr{host:$host}, $t);
+
+$t = qq{$S accepts valid -w input};
+for ('1 second',
+     '1 minute',
+     '1 week',
+     '1 hour',
+     '1 day',
+     '1 week',
+	 ) {
+	like ($cp->run(qq{ -w "$_"}), qr/^$label/, $t . " ($_)");
+}
+
+$t = qq{$S rejects invalid -w input};
+for ('-1 second',
+     '-1 week',
+     'abc',
+	 '1 fortnight',
+	 ) {
+   like ($cp->run(qq{-w "$_"}), qr/^ERROR: Value for 'warning' must be a valid time/, $t . " ($_)");
+}
+
+my $child = fork();
+if ($child == 0) {
+    my $kiddbh = $cp->test_database_handle();
+    $kiddbh->do(q{SELECT pg_sleep(3)});
+    $kiddbh->disconnect;
+    exit;
+}
+
+sleep 1;
+$dbh = $cp->test_database_handle();
+$t = qq{$S detects running query};
+like ($cp->run(qq{-w 1}), qr{$label WARNING:}, $t);
+
+exit;
diff --git a/t/02_txn_idle.t b/t/02_txn_idle.t
new file mode 100644
index 0000000..8ceb4d6
--- /dev/null
+++ b/t/02_txn_idle.t
@@ -0,0 +1,73 @@
+#!perl
+
+## Test the "txn_idle" action
+
+use strict;
+use warnings;
+use Data::Dumper;
+use DBI;
+use Test::More tests => 13;
+use lib 't','.';
+use CP_Testing;
+
+use vars qw/$dbh $result $t $host $dbname/;
+
+my $cp = CP_Testing->new( {default_action => 'txn_idle'} );
+
+$dbh = $cp->test_database_handle();
+$dbh->{AutoCommit} = 1;
+$dbname = $cp->get_dbname;
+$host = $cp->get_host();
+my $label = 'POSTGRES_TXN_IDLE';
+
+my $S = q{Action 'txn_idle'};
+
+$t = qq{$S self-identifies correctly};
+$result = $cp->run(qq{-w 0});
+like ($result, qr{^$label}, $t);
+
+$t = qq{$S identifies host};
+like ($result, qr{host:$host}, $t);
+
+$t = qq{$S accepts valid -w input};
+for ('1 second',
+     '1 minute',
+     '1 hour',
+     '1 day'
+    ) {
+   like ($cp->run(qq{-w "$_"}), qr/^$label/, $t . " ($_)");
+}
+
+$t = qq{$S rejects invalid -w input};
+for ('-1 second',
+     'abc'
+    ) {
+   like ($cp->run(qq{-w "$_"}), qr/^ERROR:.*?must be a valid time/, $t . " ($_)");
+}
+
+$t = qq{$S flags no-match-user};
+like ($cp->run(qq{-w 0 --includeuser=gandalf}), qr{No matching.*user}, $t);
+
+if ($cp->run(qq{-w 0 --output=simple}) > 0) {
+    BAIL_OUT(qq{Cannot continue with "$S" test: txn_idle count > 0\nIs someone else connected to your test database?});
+}
+
+$t = qq{$S identifies no idles};
+like ($result, qr{no idle in transaction}, $t);
+
+$t .= ' (MRTG)';
+is ($cp->run(qq{--output=mrtg -w 0}), qq{0\n0\n\nDB: $dbname\n}, $t);
+
+$t = qq{$S identifies idle};
+my $idle_dbh = $cp->test_database_handle();
+$idle_dbh->do('SELECT 1');
+sleep(1);
+like ($cp->run(qq{-w 0}), qr{longest idle in txn: \d+s}, $t);
+
+$t .= ' (MRTG)';
+like ($cp->run(qq{--output=mrtg -w 0}), qr{\d+\n0\n\nDB: $dbname\n}, $t);
+
+$idle_dbh->commit;
+
+exit;
+
-- 
1.6.0.5



More information about the Check_postgres mailing list