From 795100ae5124915bb647a304d5dfe2ada2f44ab0 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Mon, 30 Jan 2006 16:10:50 +0000 Subject: Added libtap tests for utils.c library functions. Removed redundant test files git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1303 f882894a-f735-0410-b71e-b25c423dba1c diff --git a/configure.in b/configure.in index 9303850..9afbf60 100644 --- a/configure.in +++ b/configure.in @@ -74,6 +74,15 @@ AC_SUBST(with_nagios_group) INSTALL_OPTS="-o $with_nagios_user -g $with_nagios_group" AC_SUBST(INSTALL_OPTS) +AC_ARG_WITH(libtap-object, + ACX_HELP_STRING([--with-libtap-object=path], + [full path to tap.o]), + EXTRA_TEST=test_utils + EXTRA_TAPOBJ=$withval + AC_SUBST(EXTRA_TEST) + AC_SUBST(EXTRA_TAPOBJ) + ) + AC_ARG_WITH(trusted_path, ACX_HELP_STRING([--with-trusted-path=PATH], [sets trusted path for executables called by scripts (default=/bin:/sbin:/usr/bin:/usr/sbin)]), @@ -1711,6 +1720,7 @@ AC_OUTPUT( lib/Makefile m4/Makefile plugins/Makefile + plugins/tests/Makefile plugins-root/Makefile plugins-scripts/Makefile plugins-scripts/subst diff --git a/doc/developer-guidelines.sgml b/doc/developer-guidelines.sgml index 3c37e5c..ad6f59e 100644 --- a/doc/developer-guidelines.sgml +++ b/doc/developer-guidelines.sgml @@ -209,7 +209,7 @@ Notes: - start > end + start ≤ end start and ":" is not required if start=0 @@ -598,7 +598,7 @@ need to be fixed. As long as the return code is 0, it will be reported as "test -If you want a summary test, run: "cd plugins && perl -MTest::Harness -e 'runtests(@ARGV)' t/check_disk.t". +If you want a summary test, run: "cd plugins && prove t/check_disk.t". This runs the test in a summary format. @@ -612,9 +612,23 @@ link
Testing the C library functions -Will be looking at using libtap, which is utilised by the FreeBSD team. The output is -based on perl's TAP (Test Anything Protocol) format, so that Test::Harness will understand -results. This is still in planning stages. +Uses the libtap library, which gives +perl's TAP +(Test Anything Protocol) output. This is used by the FreeBSD team for their regression testing. + + + +To run tests using the libtap library, download from http://people.freebsd.org/~nik/public_distfiles/ +and compile. There is a problem with tap-1.01 +where pthread support doesn't appear to work +properly on non-FreeBSD systems. Compile with 'CPPFLAGS="-UHAVE_LIBPTHREAD" ./configure'. You do not need +to install. + + + +You then have to run the Nagios Plugins' configure with the --with-libtap-object=full_path to the compiled +tap.o file. +Then run "make" and "make test" to run all tests.
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 26f9c8a..a67911c 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -1,5 +1,7 @@ ## Process this file with automake to produce Makefile.in +SUBDIRS = tests + VPATH = $(top_srcdir) $(top_srcdir)/lib $(top_srcdir)/plugins $(top_srcdir)/plugins/t INCLUDES = -I.. -I$(top_srcdir)/lib -I$(top_srcdir)/intl @LDAPINCLUDE@ @PGINCLUDE@ @SSLINCLUDE@ @@ -41,6 +43,7 @@ TESTS_ENVIRONMENT = perl -I $(top_builddir) -I $(top_srcdir) TESTS = @PLUGIN_TEST@ test: + cd tests && make test perl -I $(top_builddir) -I $(top_srcdir) ../test.pl AM_INSTALL_PROGRAM_FLAGS = @INSTALL_OPTS@ diff --git a/plugins/tests/.cvsignore b/plugins/tests/.cvsignore new file mode 100644 index 0000000..95112e1 --- /dev/null +++ b/plugins/tests/.cvsignore @@ -0,0 +1,3 @@ +Makefile +Makefile.in +test_utils diff --git a/plugins/tests/Makefile.am b/plugins/tests/Makefile.am new file mode 100644 index 0000000..0d82be1 --- /dev/null +++ b/plugins/tests/Makefile.am @@ -0,0 +1,21 @@ + +noinst_PROGRAMS = @EXTRA_TEST@ + +# These two lines support "make check", but we use "make test" +TESTS = @EXTRA_TEST@ +check_PROGRAMS = @EXTRA_TEST@ + +INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl + +EXTRA_PROGRAMS = test_utils + +LIBS = @LIBINTL@ + +test_utils_SOURCES = test_utils.c +test_utils_CFLAGS = -g -I.. +test_utils_LDFLAGS = -L.. +test_utils_LDADD = ../utils.o @EXTRA_TAPOBJ@ + +test: ${noinst_PROGRAMS} + perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) + diff --git a/plugins/tests/README b/plugins/tests/README new file mode 100644 index 0000000..66935e4 --- /dev/null +++ b/plugins/tests/README @@ -0,0 +1,5 @@ +The tests in here use the libtap library functions +(http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap), so are +more for unit testing the utils.c library functions. + +However, it probably should be merged into the plugins/t subdirectory. diff --git a/plugins/tests/check_disk b/plugins/tests/check_disk deleted file mode 100644 index c4323e0..0000000 --- a/plugins/tests/check_disk +++ /dev/null @@ -1,9 +0,0 @@ -check_disk - -[normal] -100 100 / -^Disk ok - +[\.0-9]+ - -[critical] -0 0 / -^Only +[\.0-9]+ diff --git a/plugins/tests/check_dns b/plugins/tests/check_dns deleted file mode 100644 index 2e9b9a0..0000000 --- a/plugins/tests/check_dns +++ /dev/null @@ -1,9 +0,0 @@ -check_dns - -[normal] -127.0.0.1 -DNS ok - +[\.0-9]+ seconds response time, Address\(es\) is\/are 127\.0\.0\.1 - -[critical] -$nullhost -DNS (problem - Probably a non-existent host/domain|CRITICAL - Non-existent host/domain|CRITICAL - Connection timed out after [0-9]+ seconds) diff --git a/plugins/tests/check_ftp b/plugins/tests/check_ftp deleted file mode 100644 index 4266ebd..0000000 --- a/plugins/tests/check_ftp +++ /dev/null @@ -1,9 +0,0 @@ -check_ftp - -[normal] -$hostname -wt 300 -ct 600 -FTP ok - [0-9]+ second response time - -[critical] -$noserver -wt 0 -ct 0 -(Invalid FTP response received from host|Connection refused by host) diff --git a/plugins/tests/check_hpjd b/plugins/tests/check_hpjd deleted file mode 100644 index df35996..0000000 --- a/plugins/tests/check_hpjd +++ /dev/null @@ -1,9 +0,0 @@ -check_hpjd - -[normal] -$printer -^Printer ok - - -[critical] -$noserver -Timeout: No response from ns diff --git a/plugins/tests/check_http b/plugins/tests/check_http deleted file mode 100644 index e09f6ff..0000000 --- a/plugins/tests/check_http +++ /dev/null @@ -1,9 +0,0 @@ -check_http - -[normal] -www.infoplease.com -wt 300 -ct 600 -HTTP/1.1 200 OK - [0-9]+ second response time - -[critical] -$nullhost -wt 1 -ct 2 -(Connection refused by host|Network is unreachable|Connection refused or timed out|Socket timeout after [0-9]+ seconds)$ diff --git a/plugins/tests/check_load b/plugins/tests/check_load deleted file mode 100644 index 5e4be3a..0000000 --- a/plugins/tests/check_load +++ /dev/null @@ -1,9 +0,0 @@ -check_load - -[normal] -100 100 100 100 100 100 -^load average: +[\.0-9]+, +[\.0-9]+, +[\.0-9]+$ - -[critical] -0 0 0 0 0 0 -^load average: +[\.0-9]+, +[\.0-9]+, +[\.0-9]+ CRITICAL$ diff --git a/plugins/tests/check_ping b/plugins/tests/check_ping deleted file mode 100644 index 115cbaa..0000000 --- a/plugins/tests/check_ping +++ /dev/null @@ -1,13 +0,0 @@ -check_ping - -[normal] -127.0.0.1 100 100 1000 1000 -p 1 -PING ok - Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms - -[critical] -127.0.0.1 0 0 0 0 -p 1 -Packet loss = +[0-9]{1,2}\%, +RTA = [\.0-9]+ ms - -[critical] -$nullhost 0 0 0 0 -p 1 -to 1 -CRITICAL - Plugin timed out after 1 seconds diff --git a/plugins/tests/check_procs b/plugins/tests/check_procs deleted file mode 100644 index f7c6522..0000000 --- a/plugins/tests/check_procs +++ /dev/null @@ -1,23 +0,0 @@ -check_procs - -# this is a comment - -[normal] -100000 100000 -^OK - [0-9]+ processes running$ - -[normal] -100000 100000 Z -^OK - [0-9]+ processes with Z status$ - -[warning] -0 10000000 -^WARNING - [0-9]+ processes running$ - -[critical] -0 0 -^CRITICAL - [0-9]+ processes running$ - -[critical] -0 0 S -^CRITICAL - [0-9]+ processes with S status$ diff --git a/plugins/tests/check_swap b/plugins/tests/check_swap deleted file mode 100644 index b6cbb42..0000000 --- a/plugins/tests/check_swap +++ /dev/null @@ -1,13 +0,0 @@ -check_swap - -[normal] -100 100 -^Swap ok - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$ - -[critical] -0 0 -^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$ - -[critical] -100 100 1000000000 10000000000 -^CRITICAL - Swap used\: +[0-9]{1,2}\% \([0-9]+ bytes out of [0-9]+\)$ diff --git a/plugins/tests/check_users b/plugins/tests/check_users deleted file mode 100644 index 5a7959a..0000000 --- a/plugins/tests/check_users +++ /dev/null @@ -1,9 +0,0 @@ -check_users - -[normal] -1000 1000 -^Users ok - +[0-9]+ users logged in$ - -[critical] -0 0 -^[0-9]+ +users currently logged in$ diff --git a/plugins/tests/check_vsz b/plugins/tests/check_vsz deleted file mode 100644 index 4fdec93..0000000 --- a/plugins/tests/check_vsz +++ /dev/null @@ -1,9 +0,0 @@ -check_vsz - -[normal] -100000 1000000 init -^ok \(all VSZ\<[0-9]+\) - -[critical] -0 0 -^CRITICAL \(VSZ\>[0-9]+\) diff --git a/plugins/tests/test_utils.c b/plugins/tests/test_utils.c new file mode 100644 index 0000000..1fda367 --- /dev/null +++ b/plugins/tests/test_utils.c @@ -0,0 +1,109 @@ +/****************************************************************************** + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + + $Id$ + +******************************************************************************/ + +const char *progname = "utils"; + +#include "common.h" +#include "utils.h" +#include "popen.h" + +#include "tap.h" + +int +main (int argc, char **argv) +{ + threshold *range; + double temp; + + plan_tests(40); + + range = parse_threshold("6"); + ok( range != NULL, "'6' is valid threshold"); + ok( range->start == 0, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end == 6, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + free(range); + + range = parse_threshold("-7:23"); + ok( range != NULL, "'-7:23' is valid threshold"); + ok( range->start == -7, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end == 23, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + free(range); + + range = parse_threshold(":5.75"); + ok( range != NULL, "':5.75' is valid threshold"); + ok( range->start == 0, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end == 5.75, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + free(range); + + range = parse_threshold("~:-95.99"); + ok( range != NULL, "~:-95.99' is valid threshold"); + ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->end == -95.99, "End correct (with rounding errors)"); + ok( range->end_infinity == FALSE, "Not using infinity"); + free(range); + + range = parse_threshold("12345678901234567890:"); + temp = atof("12345678901234567890"); /* Can't just use this because number too large */ + ok( range != NULL, "'12345678901234567890:' is valid threshold"); + ok( range->start == temp, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end_infinity == TRUE, "Using infinity"); + free(range); + + range = parse_threshold("~:0"); + ok( range != NULL, "'~:0' is valid threshold"); + ok( range->start_infinity == TRUE, "Using negative infinity"); + ok( range->end == 0, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->alert_on == OUTSIDE, "Will alert on outside of this range"); + free(range); + + range = parse_threshold("@0:657.8210567"); + ok( range != 0, "@0:657.8210567' is a valid threshold"); + ok( range->start == 0, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end == 657.8210567, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); + free(range); + + range = parse_threshold("1:1"); + ok( range != NULL, "'1:1' is a valid threshold"); + ok( range->start == 1, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end == 1, "End correct"); + ok( range->end_infinity == FALSE, "Not using infinity"); + free(range); + + range = parse_threshold("2:1"); + ok( range == NULL, "''2:1' rejected"); + + return exit_status(); +} + +void print_usage() { + printf("Dummy"); +} diff --git a/plugins/tests/test_utils.t b/plugins/tests/test_utils.t new file mode 100755 index 0000000..8686ad2 --- /dev/null +++ b/plugins/tests/test_utils.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use Test::More; +if (! -e "./test_utils") { + plan skip_all => "./test_utils not compiled - check ./configure --with-libtap-object is defined"; +} +exec "./test_utils"; diff --git a/plugins/utils.c b/plugins/utils.c index 8b31c5a..dbb2520 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -265,7 +265,62 @@ is_option (char *str) return FALSE; } +void set_threshold_start (threshold *this, double value) { + this->start = value; + this->start_infinity = FALSE; +} + +void set_threshold_end (threshold *this, double value) { + this->end = value; + this->end_infinity = FALSE; +} + +threshold +*parse_threshold (char *str) { + threshold *temp_threshold; + double start; + double end; + char *end_str; + temp_threshold = (threshold *) malloc(sizeof(threshold)); + + /* Set defaults */ + temp_threshold->start = 0; + temp_threshold->start_infinity = FALSE; + temp_threshold->end = 0; + temp_threshold->end_infinity = TRUE; + temp_threshold->alert_on = OUTSIDE; + + if (str[0] == '@') { + temp_threshold->alert_on = INSIDE; + str++; + } + + end_str = index(str, ':'); + if (end_str != NULL) { + if (str[0] == '~') { + temp_threshold->start_infinity = TRUE; + } else { + start = strtod(str, NULL); /* Will stop at the ':' */ + set_threshold_start(temp_threshold, start); + } + end_str++; /* Move past the ':' */ + } else { + end_str = str; + } + end = strtod(end_str, NULL); + if (strcmp(end_str, "") != 0) { + set_threshold_end(temp_threshold, end); + } + + if (temp_threshold->start_infinity == TRUE || + temp_threshold->end_infinity == TRUE || + temp_threshold->start <= temp_threshold->end) { + return temp_threshold; + } + free(temp_threshold); + return NULL; +} #ifdef NEED_GETTIMEOFDAY int diff --git a/plugins/utils.h b/plugins/utils.h index 2d97634..f47d053 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -58,6 +58,19 @@ struct timeval { }; #endif +#define OUTSIDE 0 +#define INSIDE 1 + +typedef struct threshold_struct { + double start; + int start_infinity; /* FALSE (default) or TRUE */ + double end; + int end_infinity; + int alert_on; /* OUTSIDE (default) or INSIDE */ + } threshold; + +threshold *parse_threshold (char *); + #ifndef HAVE_GETTIMEOFDAY int gettimeofday(struct timeval *, struct timezone *); #endif -- cgit v0.10-9-g596f