[nagiosplug] Added a few test cases for the check_dbi plugin.

"Nagios Plugin Development" <[email protected]> Wed, 4 Jul 2012 22:11:10 +0000
Newsgroups gmane.network.nagios.plugins.cvs
Message-ID <[email protected]>
 Module: nagiosplug
 Branch: master
 Commit: 84554196509fdbb595f93c05368b36dce1cde7c1
 Author: Sebastian Harl <[email protected]>
   Date: Fri Jun  8 11:14:21 2012 +0200
    URL: http://nagiosplug.git.sf.net/git/gitweb.cgi?p=nagiosplug/nagiosplug;a=commit;h=8455419

Added a few test cases for the check_dbi plugin.

All tests use sqlite3. A temporary database is created for the purpose of the
tests.

---

 plugins/t/check_dbi.t |  102 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 102 insertions(+), 0 deletions(-)

diff --git a/plugins/t/check_dbi.t b/plugins/t/check_dbi.t
new file mode 100644
index 0000000..e542ba5
--- /dev/null
+++ b/plugins/t/check_dbi.t
@@ -0,0 +1,102 @@
+#! /usr/bin/perl -w -I ..
+#
+# Database Server Tests via check_dbi
+#
+#
+# Uses the 'sqlite3' DBD driver and command line utility.
+
+use strict;
+use Test::More;
+use NPTest;
+
+use File::Temp;
+
+use vars qw($tests);
+
+plan skip_all => "check_dbi not compiled" unless (-x "check_dbi");
+
+$tests = 20;
+plan tests => $tests;
+
+my $missing_driver_output = "failed to open DBI driver 'sqlite3'";
+
+my $bad_driver_output    = "/failed to open DBI driver 'nodriver'/";
+my $conn_time_output     = "/OK - connection time: [0-9\.]+s \|/";
+my $missing_query_output = "/Must specify a query to execute/";
+my $no_rows_output       = "/WARNING - no rows returned/";
+my $not_numeric_output   = "/CRITICAL - result value is not a numeric:/";
+my $query_time_output    = "/OK - connection time: [0-9\.]+s, 'SELECT 1' returned 1.000000 in [0-9\.]+s \|/";
+my $syntax_error_output  = "/CRITICAL - failed to execute query 'GET ALL FROM test': 1: near \"GET\": syntax error/";
+
+my $result;
+
+SKIP: {
+	my $sqlite3 = qx(which sqlite3 2> /dev/null);
+	chomp($sqlite3);
+
+	skip "No Sqlite3 found", $tests unless $sqlite3;
+
+	my $sqlite3_check = qx(./check_dbi -d sqlite3 -q '');
+	if ($sqlite3_check =~ m/$missing_driver_output/) {
+		skip "No 'sqlite3' DBD driver found", $tests;
+	}
+
+	my $fh = File::Temp->new(
+		TEMPLATE => "/tmp/check_dbi_sqlite3.XXXXXXX",
+		UNLINK   => 1,
+	);
+	my $filename = $fh->filename;
+	$filename =~ s/^\/tmp\///;
+
+	system("$sqlite3 /tmp/$filename 'CREATE TABLE test(a INT, b TEXT)'");
+	system("$sqlite3 /tmp/$filename 'INSERT INTO test VALUES (1, \"text1\"), (2, \"text2\")'");
+
+	my $check_cmd = "./check_dbi -d sqlite3 -o sqlite3_dbdir=/tmp -o dbname=$filename";
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT 1'");
+	cmp_ok($result->return_code, '==', 0, "Sqlite3 login okay and can run query");
+
+	$result = NPTest->testCmd("$check_cmd");
+	cmp_ok($result->return_code, '==', 3, "Missing query parameter");
+	like($result->output, $missing_query_output, "Missing query parameter error message");
+
+	$result = NPTest->testCmd("$check_cmd -q 'GET ALL FROM test'");
+	cmp_ok($result->return_code, '==', 2, "Invalid query");
+	like($result->output, $syntax_error_output, "Syntax error message");
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT 2.71828' -w 2 -c 3");
+	cmp_ok($result->return_code, '==', 1, "Got warning");
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT 3.1415' -w 2 -c 3");
+	cmp_ok($result->return_code, '==', 2, "Got critical");
+
+	$result = NPTest->testCmd("$check_cmd -q ''");
+	cmp_ok($result->return_code, '==', 1, "No rows returned");
+	like($result->output, $no_rows_output, "Now rows returned warning message");
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test'");
+	cmp_ok($result->return_code, '==', 2, "Value is not a numeric");
+	like($result->output, $not_numeric_output, "Value is not a numeric error message");
+
+	$result = NPTest->testCmd("$check_cmd -m QUERY_RESULT -q 'SELECT b FROM test' -e text1");
+	cmp_ok($result->return_code, '==', 0, "Query result string comparison okay");
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -r 'eXt[0-9]'");
+	cmp_ok($result->return_code, '==', 2, "Query result case-insensitive regex failure");
+
+	$result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -R 'eXt[0-9]'");
+	cmp_ok($result->return_code, '==', 0, "Query result case-sensitive regex okay");
+
+	$result = NPTest->testCmd("$check_cmd -m CONN_TIME -w 0.5 -c 0.7");
+	cmp_ok($result->return_code, '==', 0, "CONN_TIME metric okay");
+	like($result->output, $conn_time_output, "CONN_TIME metric output okay");
+
+	$result = NPTest->testCmd("$check_cmd -m QUERY_TIME -q 'SELECT 1'");
+	cmp_ok($result->return_code, '==', 0, "QUERY_TIME metric okay");
+	like($result->output, $query_time_output, "QUERY_TIME metric output okay");
+
+	$result = NPTest->testCmd("./check_dbi -d nodriver -q ''");
+	cmp_ok($result->return_code, '==', 3, "Unknown DBI driver");
+	like($result->output, $bad_driver_output, "Correct error message");
+}
+


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/