summaryrefslogtreecommitdiffstats
path: root/plugins/t/check_mysql_query.t
blob: 407af881af4da9bf53f5d77b2befa36bfd166e4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/perl -w -I ..
#
# MySQL Database Server Tests via check_mysql
#
#
#
# These are the database permissions required for this test:
#  GRANT SELECT ON $db.* TO $user@$host IDENTIFIED BY '$password';
# Check with:
#  mysql -u$user -p$password -h$host $db

use strict;
use Test::More;
use NPTest;

use vars qw($tests);

plan skip_all => "check_mysql_query not compiled" unless (-x "check_mysql_query");

my $mysqlserver = getTestParameter( 
		"NP_MYSQL_SERVER", 
		"A MySQL Server with no slaves setup"
		);
my $mysql_login_details = getTestParameter( 
		"MYSQL_LOGIN_DETAILS", 
		"Command line parameters to specify login access",
		"-u user -ppw -d db",
		);
my $result;

if (! $mysqlserver) {
	plan skip_all => "No mysql server defined";
} else {
	plan tests => 13;
}

$result = NPTest->testCmd("./check_mysql_query -q 'SELECT 1+1' -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 0, "Can run query");

$result = NPTest->testCmd("./check_mysql_query -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 3, "Missing query parmeter");
like( $result->output, "/Must specify a SQL query to run/", "Missing query error message");

$result = NPTest->testCmd("./check_mysql_query -q 'SELECT 1+1' -H $mysqlserver -u dummy -d mysql");
cmp_ok( $result->return_code, '==', 2, "Login failure");
like( $result->output, "/Access denied for user /", "Expected login failure message");

$result = NPTest->testCmd("./check_mysql_query -q 'SELECT PI()' -w 3 -c 4 -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 1, "Got warning");

$result = NPTest->testCmd("./check_mysql_query -q 'SELECT PI()*2' -w 3 -c 4 -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 2, "Got critical");

$result = NPTest->testCmd("./check_mysql_query -q 'SELECT * FROM adsf' -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 2, "Bad query");
like( $result->output, "/Error with query/", "Bad query error message");

$result = NPTest->testCmd("./check_mysql_query -q 'SHOW VARIABLES LIKE \"bob\"' -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 1, "No rows");
like( $result->output, "/No rows returned/", "No rows error message");

$result = NPTest->testCmd("./check_mysql_query -q 'SHOW VARIABLES' -H $mysqlserver $mysql_login_details");
cmp_ok( $result->return_code, '==', 2, "Data not numeric");
like( $result->output, "/Is not a numeric/", "Data not numeric error message");