summaryrefslogtreecommitdiffstats
path: root/plugins/t/check_dbi.t
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/t/check_dbi.t')
-rw-r--r--plugins/t/check_dbi.t103
1 files changed, 103 insertions, 0 deletions
diff --git a/plugins/t/check_dbi.t b/plugins/t/check_dbi.t
new file mode 100644
index 00000000..c24b5a8c
--- /dev/null
+++ b/plugins/t/check_dbi.t
@@ -0,0 +1,103 @@
1#! /usr/bin/perl -w -I ..
2#
3# Database Server Tests via check_dbi
4#
5#
6# Uses the 'sqlite3' DBD driver and command line utility.
7
8use strict;
9use Test::More;
10use NPTest;
11
12use File::Temp;
13
14use vars qw($tests);
15
16plan skip_all => "check_dbi not compiled" unless (-x "check_dbi");
17
18$tests = 20;
19plan tests => $tests;
20
21my $missing_driver_output = "failed to open DBI driver 'sqlite3'";
22
23my $bad_driver_output = "/failed to open DBI driver 'nodriver'/";
24my $conn_time_output = "/OK - connection time: [0-9\.]+s \|/";
25my $missing_query_output = "/Must specify a query to execute/";
26my $no_rows_output = "/WARNING - no rows returned/";
27my $not_numeric_output = "/CRITICAL - result value is not a numeric:/";
28my $query_time_output = "/OK - connection time: [0-9\.]+s, 'SELECT 1' returned 1.000000 in [0-9\.]+s \|/";
29my $syntax_error_output = "/CRITICAL - failed to execute query 'GET ALL FROM test': 1: near \"GET\": syntax error/";
30
31my $result;
32
33SKIP: {
34 my $sqlite3 = qx(which sqlite3 2> /dev/null);
35 chomp($sqlite3);
36
37 skip "No Sqlite3 found", $tests unless $sqlite3;
38
39 my $sqlite3_check = qx(./check_dbi -d sqlite3 -q '');
40 if ($sqlite3_check =~ m/$missing_driver_output/) {
41 skip "No 'sqlite3' DBD driver found", $tests;
42 }
43
44 my $fh = File::Temp->new(
45 TEMPLATE => "/tmp/check_dbi_sqlite3.XXXXXXX",
46 UNLINK => 1,
47 );
48 my $filename = $fh->filename;
49 $filename =~ s/^\/tmp\///;
50
51 system("$sqlite3 /tmp/$filename 'CREATE TABLE test(a INT, b TEXT)'");
52 system("$sqlite3 /tmp/$filename 'INSERT INTO test VALUES (1, \"text1\")'");
53 system("$sqlite3 /tmp/$filename 'INSERT INTO test VALUES (2, \"text2\")'");
54
55 my $check_cmd = "./check_dbi -d sqlite3 -o sqlite3_dbdir=/tmp -o dbname=$filename";
56
57 $result = NPTest->testCmd("$check_cmd -q 'SELECT 1'");
58 cmp_ok($result->return_code, '==', 0, "Sqlite3 login okay and can run query");
59
60 $result = NPTest->testCmd("$check_cmd");
61 cmp_ok($result->return_code, '==', 3, "Missing query parameter");
62 like($result->output, $missing_query_output, "Missing query parameter error message");
63
64 $result = NPTest->testCmd("$check_cmd -q 'GET ALL FROM test'");
65 cmp_ok($result->return_code, '==', 2, "Invalid query");
66 like($result->output, $syntax_error_output, "Syntax error message");
67
68 $result = NPTest->testCmd("$check_cmd -q 'SELECT 2.71828' -w 2 -c 3");
69 cmp_ok($result->return_code, '==', 1, "Got warning");
70
71 $result = NPTest->testCmd("$check_cmd -q 'SELECT 3.1415' -w 2 -c 3");
72 cmp_ok($result->return_code, '==', 2, "Got critical");
73
74 $result = NPTest->testCmd("$check_cmd -q ''");
75 cmp_ok($result->return_code, '==', 1, "No rows returned");
76 like($result->output, $no_rows_output, "Now rows returned warning message");
77
78 $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test'");
79 cmp_ok($result->return_code, '==', 2, "Value is not a numeric");
80 like($result->output, $not_numeric_output, "Value is not a numeric error message");
81
82 $result = NPTest->testCmd("$check_cmd -m QUERY_RESULT -q 'SELECT b FROM test' -e text1");
83 cmp_ok($result->return_code, '==', 0, "Query result string comparison okay");
84
85 $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -r 'eXt[0-9]'");
86 cmp_ok($result->return_code, '==', 2, "Query result case-insensitive regex failure");
87
88 $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -R 'eXt[0-9]'");
89 cmp_ok($result->return_code, '==', 0, "Query result case-sensitive regex okay");
90
91 $result = NPTest->testCmd("$check_cmd -m CONN_TIME -w 0.5 -c 0.7");
92 cmp_ok($result->return_code, '==', 0, "CONN_TIME metric okay");
93 like($result->output, $conn_time_output, "CONN_TIME metric output okay");
94
95 $result = NPTest->testCmd("$check_cmd -m QUERY_TIME -q 'SELECT 1'");
96 cmp_ok($result->return_code, '==', 0, "QUERY_TIME metric okay");
97 like($result->output, $query_time_output, "QUERY_TIME metric output okay");
98
99 $result = NPTest->testCmd("./check_dbi -d nodriver -q ''");
100 cmp_ok($result->return_code, '==', 3, "Unknown DBI driver");
101 like($result->output, $bad_driver_output, "Correct error message");
102}
103