Code

Added libtap tests for utils.c library functions. Removed redundant
authorTon Voon <tonvoon@users.sourceforge.net>
Mon, 30 Jan 2006 16:10:50 +0000 (16:10 +0000)
committerTon Voon <tonvoon@users.sourceforge.net>
Mon, 30 Jan 2006 16:10:50 +0000 (16:10 +0000)
test files

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1303 f882894a-f735-0410-b71e-b25c423dba1c

21 files changed:
configure.in
doc/developer-guidelines.sgml
plugins/Makefile.am
plugins/tests/.cvsignore [new file with mode: 0644]
plugins/tests/Makefile.am [new file with mode: 0644]
plugins/tests/README [new file with mode: 0644]
plugins/tests/check_disk [deleted file]
plugins/tests/check_dns [deleted file]
plugins/tests/check_ftp [deleted file]
plugins/tests/check_hpjd [deleted file]
plugins/tests/check_http [deleted file]
plugins/tests/check_load [deleted file]
plugins/tests/check_ping [deleted file]
plugins/tests/check_procs [deleted file]
plugins/tests/check_swap [deleted file]
plugins/tests/check_users [deleted file]
plugins/tests/check_vsz [deleted file]
plugins/tests/test_utils.c [new file with mode: 0644]
plugins/tests/test_utils.t [new file with mode: 0755]
plugins/utils.c
plugins/utils.h

index 9303850ae4fdcdfeb7cc3d9e0ce83474bd2beb9a..9afbf60e3639192209d68aedac4b40328d8d2273 100644 (file)
@@ -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 
index 3c37e5c104a858c9faeed19794f18fb776410813..ad6f59e93627a30e7cc87f570c7af5b2e4aef702 100644 (file)
        
                <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>
 
index 26f9c8a800648cabc8453bd61435970bb84fcee3..a67911ce0b88889d0569d2d1fdcddffe81385405 100644 (file)
@@ -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 (file)
index 0000000..95112e1
--- /dev/null
@@ -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 (file)
index 0000000..0d82be1
--- /dev/null
@@ -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 (file)
index 0000000..66935e4
--- /dev/null
@@ -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 (file)
index c4323e0..0000000
+++ /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 (file)
index 2e9b9a0..0000000
+++ /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 (file)
index 4266ebd..0000000
+++ /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 (file)
index df35996..0000000
+++ /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 (file)
index e09f6ff..0000000
+++ /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 (file)
index 5e4be3a..0000000
+++ /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 (file)
index 115cbaa..0000000
+++ /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 (file)
index f7c6522..0000000
+++ /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 (file)
index b6cbb42..0000000
+++ /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 (file)
index 5a7959a..0000000
+++ /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 (file)
index 4fdec93..0000000
+++ /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 (file)
index 0000000..1fda367
--- /dev/null
@@ -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 (executable)
index 0000000..8686ad2
--- /dev/null
@@ -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";
index 8b31c5a2d2197713f5aa55c7afbdcc8394f68eca..dbb25202424f12c19ac9e86cf02efa25055eb890 100644 (file)
@@ -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
index 2d97634538cdf8971f9ebec64e79396d4f74d9c0..f47d0533c1759f4a2cf74e0925b1a9045ebf7d3f 100644 (file)
@@ -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