summaryrefslogtreecommitdiffstats
path: root/lib/tests
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tests')
-rw-r--r--lib/tests/Makefile.am2
-rwxr-xr-xlib/tests/test_base64.t2
-rw-r--r--lib/tests/test_cmd.t2
-rwxr-xr-xlib/tests/test_disk.t2
-rwxr-xr-xlib/tests/test_ini1.t2
-rwxr-xr-xlib/tests/test_ini3.t2
-rwxr-xr-xlib/tests/test_opts1.t2
-rwxr-xr-xlib/tests/test_opts2.t2
-rwxr-xr-xlib/tests/test_opts3.t2
-rwxr-xr-xlib/tests/test_tcp.t2
-rw-r--r--lib/tests/test_utils.c120
-rwxr-xr-xlib/tests/test_utils.t2
12 files changed, 102 insertions, 40 deletions
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am
index 1af5891..c0411ac 100644
--- a/lib/tests/Makefile.am
+++ b/lib/tests/Makefile.am
@@ -28,7 +28,7 @@ endif
28 28
29AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags) 29AM_CFLAGS = -g -I$(top_srcdir)/lib -I$(top_srcdir)/gl $(tap_cflags)
30AM_LDFLAGS = $(tap_ldflags) -ltap 30AM_LDFLAGS = $(tap_ldflags) -ltap
31LDADD = $(top_srcdir)/lib/libnagiosplug.a $(top_srcdir)/gl/libgnu.a 31LDADD = $(top_srcdir)/lib/libmonitoringplug.a $(top_srcdir)/gl/libgnu.a
32 32
33SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c 33SOURCES = test_utils.c test_disk.c test_tcp.c test_cmd.c test_base64.c test_ini1.c test_ini3.c test_opts1.c test_opts2.c test_opts3.c
34 34
diff --git a/lib/tests/test_base64.t b/lib/tests/test_base64.t
index 4b9236f..4ad6473 100755
--- a/lib/tests/test_base64.t
+++ b/lib/tests/test_base64.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_base64") { 3if (! -e "./test_base64") {
4 plan skip_all => "./test_base64 not compiled - please install tap library to test"; 4 plan skip_all => "./test_base64 not compiled - please enable libtap library to test";
5} 5}
6exec "./test_base64"; 6exec "./test_base64";
diff --git a/lib/tests/test_cmd.t b/lib/tests/test_cmd.t
index 4dd54ef..c50dded 100644
--- a/lib/tests/test_cmd.t
+++ b/lib/tests/test_cmd.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_cmd") { 3if (! -e "./test_cmd") {
4 plan skip_all => "./test_cmd not compiled - please install tap library to test"; 4 plan skip_all => "./test_cmd not compiled - please enable libtap library to test";
5} 5}
6exec "./test_cmd"; 6exec "./test_cmd";
diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t
index d32567a..da84dfd 100755
--- a/lib/tests/test_disk.t
+++ b/lib/tests/test_disk.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_disk") { 3if (! -e "./test_disk") {
4 plan skip_all => "./test_disk not compiled - please install tap library to test"; 4 plan skip_all => "./test_disk not compiled - please enable libtap library to test";
5} 5}
6exec "./test_disk"; 6exec "./test_disk";
diff --git a/lib/tests/test_ini1.t b/lib/tests/test_ini1.t
index d481011..25baed9 100755
--- a/lib/tests/test_ini1.t
+++ b/lib/tests/test_ini1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_ini1") { 3if (! -e "./test_ini1") {
4 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_ini1"; 6exec "./test_ini1";
diff --git a/lib/tests/test_ini3.t b/lib/tests/test_ini3.t
index 4b0be4c..a2ca94a 100755
--- a/lib/tests/test_ini3.t
+++ b/lib/tests/test_ini3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_ini3") { 6if (! -e "./test_ini3") {
7 plan skip_all => "./test_ini not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_ini not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_opts1.t b/lib/tests/test_opts1.t
index 52eed94..1934f12 100755
--- a/lib/tests/test_opts1.t
+++ b/lib/tests/test_opts1.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts1") { 3if (! -e "./test_opts1") {
4 plan skip_all => "./test_opts1 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts1 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6exec "./test_opts1"; 6exec "./test_opts1";
diff --git a/lib/tests/test_opts2.t b/lib/tests/test_opts2.t
index f7444f6..59b9fdb 100755
--- a/lib/tests/test_opts2.t
+++ b/lib/tests/test_opts2.t
@@ -1,7 +1,7 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_opts2") { 3if (! -e "./test_opts2") {
4 plan skip_all => "./test_opts2 not compiled - please install tap library and/or enable parse-ini to test"; 4 plan skip_all => "./test_opts2 not compiled - please enable libtap library and/or extra-opts to test";
5} 5}
6$ENV{"NAGIOS_CONFIG_PATH"} = "."; 6$ENV{"NAGIOS_CONFIG_PATH"} = ".";
7exec "./test_opts2"; 7exec "./test_opts2";
diff --git a/lib/tests/test_opts3.t b/lib/tests/test_opts3.t
index fa7876d..8d974ca 100755
--- a/lib/tests/test_opts3.t
+++ b/lib/tests/test_opts3.t
@@ -4,7 +4,7 @@ use strict;
4use warnings; 4use warnings;
5 5
6if (! -e "./test_opts3") { 6if (! -e "./test_opts3") {
7 plan skip_all => "./test_opts3 not compiled - please install tap library and/or enable parse-ini to test"; 7 plan skip_all => "./test_opts3 not compiled - please enable libtap library and/or extra-opts to test";
8} 8}
9 9
10# array of argument arrays 10# array of argument arrays
diff --git a/lib/tests/test_tcp.t b/lib/tests/test_tcp.t
index 8f34b67..e5e12fc 100755
--- a/lib/tests/test_tcp.t
+++ b/lib/tests/test_tcp.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_tcp") { 3if (! -e "./test_tcp") {
4 plan skip_all => "./test_tcp not compiled - please install tap library to test"; 4 plan skip_all => "./test_tcp not compiled - please enable libtap library to test";
5} 5}
6exec "./test_tcp"; 6exec "./test_tcp";
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 79e1110..356887d 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -32,28 +32,28 @@ main (int argc, char **argv)
32 range *range; 32 range *range;
33 double temp; 33 double temp;
34 thresholds *thresholds = NULL; 34 thresholds *thresholds = NULL;
35 int rc; 35 int i, rc;
36 char *temp_string; 36 char *temp_string;
37 state_key *temp_state_key = NULL; 37 state_key *temp_state_key = NULL;
38 state_data *temp_state_data; 38 state_data *temp_state_data;
39 time_t current_time; 39 time_t current_time;
40 40
41 plan_tests(150); 41 plan_tests(185);
42 42
43 ok( this_nagios_plugin==NULL, "nagios_plugin not initialised"); 43 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised");
44 44
45 np_init( "check_test", argc, argv ); 45 np_init( "check_test", argc, argv );
46 46
47 ok( this_nagios_plugin!=NULL, "nagios_plugin now initialised"); 47 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised");
48 ok( !strcmp(this_nagios_plugin->plugin_name, "check_test"), "plugin name initialised" ); 48 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" );
49 49
50 ok( this_nagios_plugin->argc==argc, "Argc set" ); 50 ok( this_monitoring_plugin->argc==argc, "Argc set" );
51 ok( this_nagios_plugin->argv==argv, "Argv set" ); 51 ok( this_monitoring_plugin->argv==argv, "Argv set" );
52 52
53 np_set_args(0,0); 53 np_set_args(0,0);
54 54
55 ok( this_nagios_plugin->argc==0, "argc changed" ); 55 ok( this_monitoring_plugin->argc==0, "argc changed" );
56 ok( this_nagios_plugin->argv==0, "argv changed" ); 56 ok( this_monitoring_plugin->argv==0, "argv changed" );
57 57
58 np_set_args(argc, argv); 58 np_set_args(argc, argv);
59 59
@@ -182,6 +182,21 @@ main (int argc, char **argv)
182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 182 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 183 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
184 184
185 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
186 ok( rc == 0, "Thresholds ('-30:20', '-10:-2') set");
187 ok( thresholds->warning->start == -10, "Warning start set correctly");
188 ok( thresholds->warning->end == -2, "Warning end set correctly");
189 ok( thresholds->critical->start == -30, "Critical start set correctly");
190 ok( thresholds->critical->end == 20, "Critical end set correctly");
191 ok( get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
192 ok( get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
193 ok( get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
194 ok( get_status(-10, thresholds) == STATE_OK, "-10 - ok");
195 ok( get_status(-2, thresholds) == STATE_OK, "-2 - ok");
196 ok( get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
197 ok( get_status(19, thresholds) == STATE_WARNING, "19 - warning");
198 ok( get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
199
185 char *test; 200 char *test;
186 test = np_escaped_string("bob\\n"); 201 test = np_escaped_string("bob\\n");
187 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 202 ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
@@ -296,23 +311,23 @@ main (int argc, char **argv)
296 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 311 diag( "You are probably running in wrong directory. Must run as ./test_utils" );
297 312
298 313
299 this_nagios_plugin->argc=4; 314 this_monitoring_plugin->argc=4;
300 this_nagios_plugin->argv[0] = "./test_utils"; 315 this_monitoring_plugin->argv[0] = "./test_utils";
301 this_nagios_plugin->argv[1] = "here"; 316 this_monitoring_plugin->argv[1] = "here";
302 this_nagios_plugin->argv[2] = "--and"; 317 this_monitoring_plugin->argv[2] = "--and";
303 this_nagios_plugin->argv[3] = "now"; 318 this_monitoring_plugin->argv[3] = "now";
304 temp_string = (char *) _np_state_generate_key(); 319 temp_string = (char *) _np_state_generate_key();
305 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" ); 320 ok(!strcmp(temp_string, "94b5e17bf5abf51cb15aff5f69b96f2f8dac5ecd"), "Got based on expected argv" );
306 321
307 unsetenv("NAGIOS_PLUGIN_STATE_DIRECTORY"); 322 unsetenv("MP_STATE_PATH");
308 temp_string = (char *) _np_state_calculate_location_prefix(); 323 temp_string = (char *) _np_state_calculate_location_prefix();
309 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 324 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" );
310 325
311 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "", 1); 326 setenv("MP_STATE_PATH", "", 1);
312 temp_string = (char *) _np_state_calculate_location_prefix(); 327 temp_string = (char *) _np_state_calculate_location_prefix();
313 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 328 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" );
314 329
315 setenv("NAGIOS_PLUGIN_STATE_DIRECTORY", "/usr/local/nagios/var", 1); 330 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
316 temp_string = (char *) _np_state_calculate_location_prefix(); 331 temp_string = (char *) _np_state_calculate_location_prefix();
317 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 332 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" );
318 333
@@ -320,16 +335,16 @@ main (int argc, char **argv)
320 335
321 ok(temp_state_key==NULL, "temp_state_key initially empty"); 336 ok(temp_state_key==NULL, "temp_state_key initially empty");
322 337
323 this_nagios_plugin->argc=1; 338 this_monitoring_plugin->argc=1;
324 this_nagios_plugin->argv[0] = "./test_utils"; 339 this_monitoring_plugin->argv[0] = "./test_utils";
325 np_enable_state(NULL, 51); 340 np_enable_state(NULL, 51);
326 temp_state_key = this_nagios_plugin->state; 341 temp_state_key = this_monitoring_plugin->state;
327 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 342 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
328 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" ); 343 ok( !strcmp(temp_state_key->name, "83d877b6cdfefb5d6f06101fd6fe76762f21792c"), "Got generated filename" );
329 344
330 345
331 np_enable_state("allowedchars_in_keyname", 77); 346 np_enable_state("allowedchars_in_keyname", 77);
332 temp_state_key = this_nagios_plugin->state; 347 temp_state_key = this_monitoring_plugin->state;
333 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 348 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
334 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); 349 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" );
335 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" ); 350 ok( !strcmp(temp_state_key->_filename, "/usr/local/nagios/var/check_test/allowedchars_in_keyname"), "Got internal filename" );
@@ -338,12 +353,12 @@ main (int argc, char **argv)
338 /* Don't do this test just yet. Will die */ 353 /* Don't do this test just yet. Will die */
339 /* 354 /*
340 np_enable_state("bad^chars$in@here", 77); 355 np_enable_state("bad^chars$in@here", 77);
341 temp_state_key = this_nagios_plugin->state; 356 temp_state_key = this_monitoring_plugin->state;
342 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" ); 357 ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" );
343 */ 358 */
344 359
345 np_enable_state("funnykeyname", 54); 360 np_enable_state("funnykeyname", 54);
346 temp_state_key = this_nagios_plugin->state; 361 temp_state_key = this_monitoring_plugin->state;
347 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 362 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" );
348 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 363 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" );
349 364
@@ -367,9 +382,9 @@ main (int argc, char **argv)
367 382
368 temp_state_key->_filename="var/statefile"; 383 temp_state_key->_filename="var/statefile";
369 temp_state_data = np_state_read(); 384 temp_state_data = np_state_read();
370 ok( this_nagios_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); 385 ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not");
371 ok( this_nagios_plugin->state->state_data->time==1234567890, "Got time" ); 386 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" );
372 ok( !strcmp((char *)this_nagios_plugin->state->state_data->data, "String to read"), "Data as expected" ); 387 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" );
373 388
374 temp_state_key->data_version=53; 389 temp_state_key->data_version=53;
375 temp_state_data = np_state_read(); 390 temp_state_data = np_state_read();
@@ -379,7 +394,7 @@ main (int argc, char **argv)
379 temp_state_key->_filename="var/nonexistant"; 394 temp_state_key->_filename="var/nonexistant";
380 temp_state_data = np_state_read(); 395 temp_state_data = np_state_read();
381 ok( temp_state_data==NULL, "Missing file gives NULL" ); 396 ok( temp_state_data==NULL, "Missing file gives NULL" );
382 ok( this_nagios_plugin->state->state_data==NULL, "No state information" ); 397 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" );
383 398
384 temp_state_key->_filename="var/oldformat"; 399 temp_state_key->_filename="var/oldformat";
385 temp_state_data = np_state_read(); 400 temp_state_data = np_state_read();
@@ -426,7 +441,7 @@ main (int argc, char **argv)
426 temp_state_data = np_state_read(); 441 temp_state_data = np_state_read();
427 /* Check time is set to current_time */ 442 /* Check time is set to current_time */
428 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 443 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time");
429 ok(this_nagios_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 444 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time");
430 445
431 446
432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 447 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
@@ -438,7 +453,54 @@ main (int argc, char **argv)
438 453
439 np_cleanup(); 454 np_cleanup();
440 455
441 ok( this_nagios_plugin==NULL, "Free'd this_nagios_plugin" ); 456 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin");
457
458 ok(mp_suid() == FALSE, "Test aren't suid");
459
460 /* base states with random case */
461 char *states[] = {
462 "Ok",
463 "wArnINg",
464 "cRiTIcaL",
465 "UnKNoWN",
466 NULL
467 };
468
469 for (i=0; states[i]!=NULL; i++) {
470 /* out of the random case states, create the lower and upper versions + numeric string one */
471 char *statelower = strdup(states[i]);
472 char *stateupper = strdup(states[i]);
473 char statenum[2];
474 char *temp_ptr;
475 for (temp_ptr = statelower; *temp_ptr; temp_ptr++) {
476 *temp_ptr = tolower(*temp_ptr);
477 }
478 for (temp_ptr = stateupper; *temp_ptr; temp_ptr++) {
479 *temp_ptr = toupper(*temp_ptr);
480 }
481 snprintf(statenum, 2, "%i", i);
482
483 /* Base test names, we'll append the state string */
484 char testname[64] = "Translate state string: ";
485 int tlen = strlen(testname);
486
487 strcpy(testname+tlen, states[i]);
488 ok(i==mp_translate_state(states[i]), testname);
489
490 strcpy(testname+tlen, statelower);
491 ok(i==mp_translate_state(statelower), testname);
492
493 strcpy(testname+tlen, stateupper);
494 ok(i==mp_translate_state(stateupper), testname);
495
496 strcpy(testname+tlen, statenum);
497 ok(i==mp_translate_state(statenum), testname);
498 }
499 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage");
500 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1");
501 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2");
502 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3");
503 ok(ERROR==mp_translate_state(""), "Translate state string: empty string");
442 504
443 return exit_status(); 505 return exit_status();
444} 506}
diff --git a/lib/tests/test_utils.t b/lib/tests/test_utils.t
index 152eb71..e2a9311 100755
--- a/lib/tests/test_utils.t
+++ b/lib/tests/test_utils.t
@@ -1,6 +1,6 @@
1#!/usr/bin/perl 1#!/usr/bin/perl
2use Test::More; 2use Test::More;
3if (! -e "./test_utils") { 3if (! -e "./test_utils") {
4 plan skip_all => "./test_utils not compiled - please install tap library to test"; 4 plan skip_all => "./test_utils not compiled - please enable libtap library to test";
5} 5}
6exec "./test_utils"; 6exec "./test_utils";