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 --- plugins/Makefile.am | 3 ++ plugins/tests/.cvsignore | 3 ++ plugins/tests/Makefile.am | 21 +++++++++ plugins/tests/README | 5 +++ plugins/tests/check_disk | 9 ---- plugins/tests/check_dns | 9 ---- plugins/tests/check_ftp | 9 ---- plugins/tests/check_hpjd | 9 ---- plugins/tests/check_http | 9 ---- plugins/tests/check_load | 9 ---- plugins/tests/check_ping | 13 ------ plugins/tests/check_procs | 23 ---------- plugins/tests/check_swap | 13 ------ plugins/tests/check_users | 9 ---- plugins/tests/check_vsz | 9 ---- plugins/tests/test_utils.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ plugins/tests/test_utils.t | 6 +++ plugins/utils.c | 55 +++++++++++++++++++++++ plugins/utils.h | 13 ++++++ 19 files changed, 215 insertions(+), 121 deletions(-) create mode 100644 plugins/tests/.cvsignore create mode 100644 plugins/tests/Makefile.am create mode 100644 plugins/tests/README delete mode 100644 plugins/tests/check_disk delete mode 100644 plugins/tests/check_dns delete mode 100644 plugins/tests/check_ftp delete mode 100644 plugins/tests/check_hpjd delete mode 100644 plugins/tests/check_http delete mode 100644 plugins/tests/check_load delete mode 100644 plugins/tests/check_ping delete mode 100644 plugins/tests/check_procs delete mode 100644 plugins/tests/check_swap delete mode 100644 plugins/tests/check_users delete mode 100644 plugins/tests/check_vsz create mode 100644 plugins/tests/test_utils.c create mode 100755 plugins/tests/test_utils.t (limited to 'plugins') diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 26f9c8a8..a67911ce 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 00000000..95112e15 --- /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 00000000..0d82be12 --- /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 00000000..66935e4d --- /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 c4323e0b..00000000 --- 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 2e9b9a09..00000000 --- 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 4266ebdc..00000000 --- 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 df35996f..00000000 --- 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 e09f6ff7..00000000 --- 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 5e4be3a4..00000000 --- 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 115cbaa3..00000000 --- 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 f7c65222..00000000 --- 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 b6cbb42f..00000000 --- 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 5a7959a5..00000000 --- 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 4fdec936..00000000 --- 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 00000000..1fda3675 --- /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 00000000..8686ad2a --- /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 8b31c5a2..dbb25202 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 2d976345..f47d0533 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 v1.2.3