summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--NPTest.pm4
-rw-r--r--plugins/check_mysql.c16
-rw-r--r--plugins/t/check_mysql.t53
3 files changed, 57 insertions, 16 deletions
diff --git a/NPTest.pm b/NPTest.pm
index 4036a9d0..7ecf7439 100644
--- a/NPTest.pm
+++ b/NPTest.pm
@@ -46,6 +46,8 @@ default via the C<use NPTest;> statement.
46 46
47=item getTestParameter( "ENV_VARIABLE", $brief_description, $default ) 47=item getTestParameter( "ENV_VARIABLE", $brief_description, $default )
48 48
49$default is optional.
50
49This function allows the test harness 51This function allows the test harness
50developer to interactively request test parameter information from the 52developer to interactively request test parameter information from the
51user. The user can accept the developer's default value or reply "none" 53user. The user can accept the developer's default value or reply "none"
@@ -302,7 +304,7 @@ sub getTestParameter
302{ 304{
303 my( $param, $envvar, $default, $brief, $scoped ); 305 my( $param, $envvar, $default, $brief, $scoped );
304 my $new_style; 306 my $new_style;
305 if (scalar @_ == 3) { 307 if (scalar @_ <= 3) {
306 ($param, $brief, $default) = @_; 308 ($param, $brief, $default) = @_;
307 $envvar = $param; 309 $envvar = $param;
308 $new_style = 1; 310 $new_style = 1;
diff --git a/plugins/check_mysql.c b/plugins/check_mysql.c
index 3194eced..45f86a9c 100644
--- a/plugins/check_mysql.c
+++ b/plugins/check_mysql.c
@@ -53,6 +53,7 @@ main (int argc, char **argv)
53 /* should be status */ 53 /* should be status */
54 54
55 char *result = NULL; 55 char *result = NULL;
56 char *error = NULL;
56 char slaveresult[SLAVERESULTSIZE]; 57 char slaveresult[SLAVERESULTSIZE];
57 58
58 setlocale (LC_ALL, ""); 59 setlocale (LC_ALL, "");
@@ -99,21 +100,30 @@ main (int argc, char **argv)
99 if(check_slave) { 100 if(check_slave) {
100 /* check the slave status */ 101 /* check the slave status */
101 if (mysql_query (&mysql, "show slave status") != 0) { 102 if (mysql_query (&mysql, "show slave status") != 0) {
103 error = strdup(mysql_error(&mysql));
102 mysql_close (&mysql); 104 mysql_close (&mysql);
103 die (STATE_CRITICAL, _("slave query error: %s\n"), mysql_error (&mysql)); 105 die (STATE_CRITICAL, _("slave query error: %s\n"), error);
104 } 106 }
105 107
106 /* store the result */ 108 /* store the result */
107 if ( (res = mysql_store_result (&mysql)) == NULL) { 109 if ( (res = mysql_store_result (&mysql)) == NULL) {
110 error = strdup(mysql_error(&mysql));
108 mysql_close (&mysql); 111 mysql_close (&mysql);
109 die (STATE_CRITICAL, _("slave store_result error: %s\n"), mysql_error (&mysql)); 112 die (STATE_CRITICAL, _("slave store_result error: %s\n"), error);
113 }
114
115 /* Check there is some data */
116 if (mysql_num_rows(res) == 0) {
117 mysql_close(&mysql);
118 die (STATE_WARNING, "%s\n", _("No slaves defined"));
110 } 119 }
111 120
112 /* fetch the first row */ 121 /* fetch the first row */
113 if ( (row = mysql_fetch_row (res)) == NULL) { 122 if ( (row = mysql_fetch_row (res)) == NULL) {
123 error = strdup(mysql_error(&mysql));
114 mysql_free_result (res); 124 mysql_free_result (res);
115 mysql_close (&mysql); 125 mysql_close (&mysql);
116 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), mysql_error (&mysql)); 126 die (STATE_CRITICAL, _("slave fetch row error: %s\n"), error);
117 } 127 }
118 128
119 if (mysql_field_count (&mysql) == 12) { 129 if (mysql_field_count (&mysql) == 12) {
diff --git a/plugins/t/check_mysql.t b/plugins/t/check_mysql.t
index b29c5c6a..764db72c 100644
--- a/plugins/t/check_mysql.t
+++ b/plugins/t/check_mysql.t
@@ -13,20 +13,49 @@ use vars qw($tests);
13 13
14plan skip_all => "check_mysql not compiled" unless (-x "check_mysql"); 14plan skip_all => "check_mysql not compiled" unless (-x "check_mysql");
15 15
16plan tests => 3; 16plan tests => 7;
17 17
18my $failureOutput = '/Access denied for user /'; 18my $bad_login_output = '/Access denied for user /';
19my $mysqlserver = getTestParameter( "mysql_server", "NP_MYSQL_SERVER", undef, 19my $mysqlserver = getTestParameter(
20 "A MySQL Server"); 20 "NP_MYSQL_SERVER",
21my $mysql_login_details = getTestParameter( "mysql_login_details", "MYSQL_LOGIN_DETAILS", undef, 21 "A MySQL Server with no slaves setup"
22 "Command line parameters to specify login access"); 22 );
23my $mysql_login_details = getTestParameter(
24 "MYSQL_LOGIN_DETAILS",
25 "Command line parameters to specify login access",
26 "-u user -ppw",
27 );
28my $with_slave = getTestParameter(
29 "NP_MYSQL_WITH_SLAVE",
30 "MySQL server with slaves setup"
31 );
32my $with_slave_login = getTestParameter(
33 "NP_MYSQL_WITH_SLAVE_LOGIN",
34 "Login details for server with slave",
35 "-uroot -ppw"
36 );
23 37
24my $result; 38my $result;
25 39
26$result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details"); 40SKIP: {
27cmp_ok( $result->return_code, '==', 0, "Login okay"); 41 skip "No mysql server defined", 5 unless $mysqlserver;
42 $result = NPTest->testCmd("./check_mysql -H $mysqlserver $mysql_login_details");
43 cmp_ok( $result->return_code, '==', 0, "Login okay");
44
45 $result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy");
46 cmp_ok( $result->return_code, '==', 2, "Login failure");
47 like( $result->output, $bad_login_output, "Expected login failure message");
48
49 $result = NPTest->testCmd("./check_mysql -S -H $mysqlserver $mysql_login_details");
50 cmp_ok( $result->return_code, "==", 1, "No slaves defined" );
51 like( $result->output, "/No slaves defined/", "Correct error message");
52}
28 53
29$result = NPTest->testCmd("./check_mysql -H $mysqlserver -u dummy"); 54SKIP: {
30cmp_ok( $result->return_code, '==', 2, "Login expected failure"); 55 skip "No mysql server with slaves defined", 2 unless $with_slave;
31like( $result->output, $failureOutput, "Error string as expected"); 56 $result = NPTest->testCmd("./check_mysql -H $with_slave $with_slave_login");
57 cmp_ok( $result->return_code, '==', 0, "Login okay");
32 58
59 $result = NPTest->testCmd("./check_mysql -S -H $with_slave $with_slave_login");
60 cmp_ok( $result->return_code, "==", 0, "Slaves okay" );
61}