diff options
Diffstat (limited to 'plugins/t')
-rw-r--r-- | 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 @@ | |||
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 | |||
8 | use strict; | ||
9 | use Test::More; | ||
10 | use NPTest; | ||
11 | |||
12 | use File::Temp; | ||
13 | |||
14 | use vars qw($tests); | ||
15 | |||
16 | plan skip_all => "check_dbi not compiled" unless (-x "check_dbi"); | ||
17 | |||
18 | $tests = 20; | ||
19 | plan tests => $tests; | ||
20 | |||
21 | my $missing_driver_output = "failed to open DBI driver 'sqlite3'"; | ||
22 | |||
23 | my $bad_driver_output = "/failed to open DBI driver 'nodriver'/"; | ||
24 | my $conn_time_output = "/OK - connection time: [0-9\.]+s \|/"; | ||
25 | my $missing_query_output = "/Must specify a query to execute/"; | ||
26 | my $no_rows_output = "/WARNING - no rows returned/"; | ||
27 | my $not_numeric_output = "/CRITICAL - result value is not a numeric:/"; | ||
28 | my $query_time_output = "/OK - connection time: [0-9\.]+s, 'SELECT 1' returned 1.000000 in [0-9\.]+s \|/"; | ||
29 | my $syntax_error_output = "/CRITICAL - failed to execute query 'GET ALL FROM test': 1: near \"GET\": syntax error/"; | ||
30 | |||
31 | my $result; | ||
32 | |||
33 | SKIP: { | ||
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\"), (2, \"text2\")'"); | ||
53 | |||
54 | my $check_cmd = "./check_dbi -d sqlite3 -o sqlite3_dbdir=/tmp -o dbname=$filename"; | ||
55 | |||
56 | $result = NPTest->testCmd("$check_cmd -q 'SELECT 1'"); | ||
57 | cmp_ok($result->return_code, '==', 0, "Sqlite3 login okay and can run query"); | ||
58 | |||
59 | $result = NPTest->testCmd("$check_cmd"); | ||
60 | cmp_ok($result->return_code, '==', 3, "Missing query parameter"); | ||
61 | like($result->output, $missing_query_output, "Missing query parameter error message"); | ||
62 | |||
63 | $result = NPTest->testCmd("$check_cmd -q 'GET ALL FROM test'"); | ||
64 | cmp_ok($result->return_code, '==', 2, "Invalid query"); | ||
65 | like($result->output, $syntax_error_output, "Syntax error message"); | ||
66 | |||
67 | $result = NPTest->testCmd("$check_cmd -q 'SELECT 2.71828' -w 2 -c 3"); | ||
68 | cmp_ok($result->return_code, '==', 1, "Got warning"); | ||
69 | |||
70 | $result = NPTest->testCmd("$check_cmd -q 'SELECT 3.1415' -w 2 -c 3"); | ||
71 | cmp_ok($result->return_code, '==', 2, "Got critical"); | ||
72 | |||
73 | $result = NPTest->testCmd("$check_cmd -q ''"); | ||
74 | cmp_ok($result->return_code, '==', 1, "No rows returned"); | ||
75 | like($result->output, $no_rows_output, "Now rows returned warning message"); | ||
76 | |||
77 | $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test'"); | ||
78 | cmp_ok($result->return_code, '==', 2, "Value is not a numeric"); | ||
79 | like($result->output, $not_numeric_output, "Value is not a numeric error message"); | ||
80 | |||
81 | $result = NPTest->testCmd("$check_cmd -m QUERY_RESULT -q 'SELECT b FROM test' -e text1"); | ||
82 | cmp_ok($result->return_code, '==', 0, "Query result string comparison okay"); | ||
83 | |||
84 | $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -r 'eXt[0-9]'"); | ||
85 | cmp_ok($result->return_code, '==', 2, "Query result case-insensitive regex failure"); | ||
86 | |||
87 | $result = NPTest->testCmd("$check_cmd -q 'SELECT b FROM test' -R 'eXt[0-9]'"); | ||
88 | cmp_ok($result->return_code, '==', 0, "Query result case-sensitive regex okay"); | ||
89 | |||
90 | $result = NPTest->testCmd("$check_cmd -m CONN_TIME -w 0.5 -c 0.7"); | ||
91 | cmp_ok($result->return_code, '==', 0, "CONN_TIME metric okay"); | ||
92 | like($result->output, $conn_time_output, "CONN_TIME metric output okay"); | ||
93 | |||
94 | $result = NPTest->testCmd("$check_cmd -m QUERY_TIME -q 'SELECT 1'"); | ||
95 | cmp_ok($result->return_code, '==', 0, "QUERY_TIME metric okay"); | ||
96 | like($result->output, $query_time_output, "QUERY_TIME metric output okay"); | ||
97 | |||
98 | $result = NPTest->testCmd("./check_dbi -d nodriver -q ''"); | ||
99 | cmp_ok($result->return_code, '==', 3, "Unknown DBI driver"); | ||
100 | like($result->output, $bad_driver_output, "Correct error message"); | ||
101 | } | ||
102 | |||