Просмотр исходного кода

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

Ton Voon 20 лет назад
Родитель
Сommit
795100ae51

+ 10 - 0
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 

+ 19 - 5
doc/developer-guidelines.sgml

@@ -209,7 +209,7 @@
 	
 		<para>Notes:</para>
 		<orderedlist>
-		<listitem><para>start &gt; end</para>
+		<listitem><para>start &le; end</para>
 			</listitem>
 		<listitem><para>start and ":" is not required if start=0</para>
 			</listitem>
@@ -598,7 +598,7 @@ need to be fixed. As long as the return code is 0, it will be reported as "test
 </para>
 
 <para>
-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.
 </para>
 
@@ -612,9 +612,23 @@ link</ulink>
 
 <section><title>Testing the C library functions</title>
 <para>
-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 <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap">the libtap library</ulink>, which gives 
+perl's TAP
+(Test Anything Protocol) output. This is used by the FreeBSD team for their regression testing.
+</para>
+
+<para>
+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 <ulink url="http://jc.ngo.org.uk/trac-bin/trac.cgi/ticket/25">pthread support doesn't appear to work</ulink>
+properly on non-FreeBSD systems. Compile with 'CPPFLAGS="-UHAVE_LIBPTHREAD" ./configure'. You do not need 
+to install.
+</para>
+
+<para>
+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.
 </para>
 </section>
 

+ 3 - 0
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@

+ 3 - 0
plugins/tests/.cvsignore

@@ -0,0 +1,3 @@
+Makefile
+Makefile.in
+test_utils

+ 21 - 0
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)
+

+ 5 - 0
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. 

+ 0 - 9
plugins/tests/check_disk

@@ -1,9 +0,0 @@
-check_disk
-
-[normal]
-100 100 /
-^Disk ok - +[\.0-9]+
-
-[critical]
-0 0 /
-^Only +[\.0-9]+

+ 0 - 9
plugins/tests/check_dns

@@ -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)

+ 0 - 9
plugins/tests/check_ftp

@@ -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)

+ 0 - 9
plugins/tests/check_hpjd

@@ -1,9 +0,0 @@
-check_hpjd
-
-[normal]
-$printer
-^Printer ok - 
-
-[critical]
-$noserver
-Timeout: No response from ns

+ 0 - 9
plugins/tests/check_http

@@ -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)$

+ 0 - 9
plugins/tests/check_load

@@ -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$

+ 0 - 13
plugins/tests/check_ping

@@ -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

+ 0 - 23
plugins/tests/check_procs

@@ -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$

+ 0 - 13
plugins/tests/check_swap

@@ -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]+\)$

+ 0 - 9
plugins/tests/check_users

@@ -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$

+ 0 - 9
plugins/tests/check_vsz

@@ -1,9 +0,0 @@
-check_vsz
-
-[normal]
-100000 1000000 init
-^ok \(all VSZ\<[0-9]+\)
-
-[critical]
-0 0
-^CRITICAL \(VSZ\>[0-9]+\)

+ 109 - 0
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");
+}

+ 6 - 0
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";

+ 55 - 0
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

+ 13 - 0
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