From 548083b2ea865474915fc8a9ddd361e997585a02 Mon Sep 17 00:00:00 2001 From: Ton Voon Date: Thu, 13 Jul 2006 12:50:23 +0000 Subject: Move new util_* functions to lib/ git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1451 f882894a-f735-0410-b71e-b25c423dba1c --- lib/tests/.cvsignore | 5 ++ lib/tests/Makefile.am | 28 ++++++++ lib/tests/README | 5 ++ lib/tests/test_disk.c | 124 ++++++++++++++++++++++++++++++++++++ lib/tests/test_disk.t | 6 ++ lib/tests/test_utils.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/tests/test_utils.t | 6 ++ 7 files changed, 343 insertions(+) create mode 100644 lib/tests/.cvsignore create mode 100644 lib/tests/Makefile.am create mode 100644 lib/tests/README create mode 100644 lib/tests/test_disk.c create mode 100755 lib/tests/test_disk.t create mode 100644 lib/tests/test_utils.c create mode 100755 lib/tests/test_utils.t (limited to 'lib/tests') diff --git a/lib/tests/.cvsignore b/lib/tests/.cvsignore new file mode 100644 index 00000000..6dc692f5 --- /dev/null +++ b/lib/tests/.cvsignore @@ -0,0 +1,5 @@ +Makefile +Makefile.in +test_utils +test_disk +.deps diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am new file mode 100644 index 00000000..63dee76e --- /dev/null +++ b/lib/tests/Makefile.am @@ -0,0 +1,28 @@ + +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 -I$(top_srcdir)/plugins + +EXTRA_PROGRAMS = test_utils test_disk + +EXTRA_DIST = test_utils.t test_disk.t + +LIBS = @LIBINTL@ + +test_utils_SOURCES = test_utils.c +test_utils_CFLAGS = -g -I.. +test_utils_LDFLAGS = -L/usr/local/lib -ltap +test_utils_LDADD = ../utils_base.o + +test_disk_SOURCES = test_disk.c +test_disk_CFLAGS = -g -I.. +test_disk_LDFLAGS = -L/usr/local/lib -ltap +test_disk_LDADD = ../utils_disk.o + +test: ${noinst_PROGRAMS} + perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) + diff --git a/lib/tests/README b/lib/tests/README new file mode 100644 index 00000000..66935e4d --- /dev/null +++ b/lib/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/lib/tests/test_disk.c b/lib/tests/test_disk.c new file mode 100644 index 00000000..8940236b --- /dev/null +++ b/lib/tests/test_disk.c @@ -0,0 +1,124 @@ +/****************************************************************************** + + 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$ + +******************************************************************************/ + +#include "common.h" +#include "utils_disk.h" +#include "tap.h" + +int +main (int argc, char **argv) +{ + struct name_list *exclude_filesystem=NULL; + struct name_list *exclude_fstype=NULL; + struct name_list *dummy_mountlist = NULL; + struct name_list *temp_name; + struct parameter_list *paths = NULL; + struct parameter_list *p; + + struct mount_entry *dummy_mount_list; + struct mount_entry *me; + struct mount_entry **mtail = &dummy_mount_list; + + plan_tests(17); + + ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list"); + np_add_name(&exclude_filesystem, "/var/log"); + ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now"); + ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list"); + np_add_name(&exclude_filesystem, "/home"); + ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now"); + ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list"); + + ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list"); + np_add_name(&exclude_fstype, "iso9660"); + ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now"); + + ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables"); + + /* + for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { + printf("Name: %s\n", temp_name->name); + } + */ + + me = (struct mount_entry *) malloc(sizeof *me); + me->me_devname = strdup("/dev/c0t0d0s0"); + me->me_mountdir = strdup("/"); + *mtail = me; + mtail = &me->me_next; + + me = (struct mount_entry *) malloc(sizeof *me); + me->me_devname = strdup("/dev/c1t0d1s0"); + me->me_mountdir = strdup("/var"); + *mtail = me; + mtail = &me->me_next; + + me = (struct mount_entry *) malloc(sizeof *me); + me->me_devname = strdup("/dev/c2t0d0s0"); + me->me_mountdir = strdup("/home"); + *mtail = me; + mtail = &me->me_next; + + + np_add_parameter(&paths, "/home/groups"); + np_add_parameter(&paths, "/var"); + np_add_parameter(&paths, "/tmp"); + np_add_parameter(&paths, "/home/tonvoon"); + np_add_parameter(&paths, "/dev/c2t0d0s0"); + + np_set_best_match(paths, dummy_mount_list, FALSE); + for (p = paths; p; p = p->name_next) { + struct mount_entry *temp_me; + temp_me = p->best_match; + if (! strcmp(p->name, "/home/groups")) { + ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); + } else if (! strcmp(p->name, "/var")) { + ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); + } else if (! strcmp(p->name, "/tmp")) { + ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); + } else if (! strcmp(p->name, "/home/tonvoon")) { + ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); + } else if (! strcmp(p->name, "/dev/c2t0d0s0")) { + ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); + } + } + + paths = NULL; /* Bad boy - should free, but this is a test suite */ + np_add_parameter(&paths, "/home/groups"); + np_add_parameter(&paths, "/var"); + np_add_parameter(&paths, "/tmp"); + np_add_parameter(&paths, "/home/tonvoon"); + + np_set_best_match(paths, dummy_mount_list, TRUE); + for (p = paths; p; p = p->name_next) { + if (! strcmp(p->name, "/home/groups")) { + ok( p->found == 0, "/home/groups correctly not found"); + } else if (! strcmp(p->name, "/var")) { + ok( p->found == 1, "/var found"); + } else if (! strcmp(p->name, "/tmp")) { + ok( p->found == 0, "/tmp correctly not found"); + } else if (! strcmp(p->name, "/home/tonvoon")) { + ok( p->found == 0, "/home/tonvoon not found"); + } + } + + return exit_status(); +} + diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t new file mode 100755 index 00000000..d32567a9 --- /dev/null +++ b/lib/tests/test_disk.t @@ -0,0 +1,6 @@ +#!/usr/bin/perl +use Test::More; +if (! -e "./test_disk") { + plan skip_all => "./test_disk not compiled - please install tap library to test"; +} +exec "./test_disk"; diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c new file mode 100644 index 00000000..aedc2a58 --- /dev/null +++ b/lib/tests/test_utils.c @@ -0,0 +1,169 @@ +/****************************************************************************** + + 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$ + +******************************************************************************/ + +#include "common.h" +#include "utils_base.h" + +#include "tap.h" + +int +main (int argc, char **argv) +{ + range *range; + double temp; + thresholds *thresholds = NULL; + int rc; + + plan_tests(74); + + range = parse_range_string("6"); + ok( range != NULL, "'6' is valid range"); + 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_range_string("-7:23"); + ok( range != NULL, "'-7:23' is valid range"); + 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_range_string(":5.75"); + ok( range != NULL, "':5.75' is valid range"); + 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_range_string("~:-95.99"); + ok( range != NULL, "~:-95.99' is valid range"); + 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_range_string("12345678901234567890:"); + temp = atof("12345678901234567890"); /* Can't just use this because number too large */ + ok( range != NULL, "'12345678901234567890:' is valid range"); + ok( range->start == temp, "Start correct"); + ok( range->start_infinity == FALSE, "Not using negative infinity"); + ok( range->end_infinity == TRUE, "Using infinity"); + /* Cannot do a "-1" on temp, as it appears to be same value */ + ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert"); + ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert"); + ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert"); + free(range); + + range = parse_range_string("~:0"); + ok( range != NULL, "'~:0' is valid range"); + 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"); + ok( check_range(0.5, range) == TRUE, "0.5 - alert"); + ok( check_range(-10, range) == FALSE, "-10 - no alert"); + ok( check_range(0, range) == FALSE, "0 - no alert"); + free(range); + + range = parse_range_string("@0:657.8210567"); + ok( range != 0, "@0:657.8210567' is a valid range"); + 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" ); + ok( check_range(32.88, range) == TRUE, "32.88 - alert"); + ok( check_range(-2, range) == FALSE, "-2 - no alert"); + ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert"); + ok( check_range(0, range) == TRUE, "0 - alert"); + free(range); + + range = parse_range_string("1:1"); + ok( range != NULL, "'1:1' is a valid range"); + 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"); + ok( check_range(0.5, range) == TRUE, "0.5 - alert"); + ok( check_range(1, range) == FALSE, "1 - no alert"); + ok( check_range(5.2, range) == TRUE, "5.2 - alert"); + free(range); + + range = parse_range_string("2:1"); + ok( range == NULL, "'2:1' rejected"); + + rc = _set_thresholds(&thresholds, NULL, "80"); + ok( rc == 0, "Thresholds (NULL, '80') set"); + ok( thresholds->warning == NULL, "Warning not set"); + ok( thresholds->critical->end == 80, "Critical set correctly"); + + rc = _set_thresholds(&thresholds, "5:33", NULL); + ok( rc == 0, "Thresholds ('5:33', NULL) set"); + ok( thresholds->warning->start == 5, "Warning start set"); + ok( thresholds->warning->end == 33, "Warning end set"); + ok( thresholds->critical == NULL, "Critical not set"); + + rc = _set_thresholds(&thresholds, "30", "60"); + ok( rc == 0, "Thresholds ('30', '60') set"); + ok( thresholds->warning->end == 30, "Warning set correctly"); + ok( thresholds->critical->end == 60, "Critical set correctly"); + ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); + ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); + ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); + + char *test; + test = np_escaped_string("bob\\n"); + ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); + free(test); + + test = np_escaped_string("rhuba\\rb"); + ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay"); + free(test); + + test = np_escaped_string("ba\\nge\\r"); + ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay"); + free(test); + + test = np_escaped_string("\\rabbi\\t"); + ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay"); + free(test); + + test = np_escaped_string("and\\\\or"); + ok( strcmp(test, "and\\or") == 0, "and\\\\or okay"); + free(test); + + test = np_escaped_string("bo\\gus"); + ok( strcmp(test, "bogus") == 0, "bo\\gus okay"); + free(test); + + test = np_escaped_string("everything"); + ok( strcmp(test, "everything") == 0, "everything okay"); + free(test); + + test = basename("/here/is/a/path"); + ok( strcmp(test, "path") == 0, "basename okay"); + + return exit_status(); +} diff --git a/lib/tests/test_utils.t b/lib/tests/test_utils.t new file mode 100755 index 00000000..152eb718 --- /dev/null +++ b/lib/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 - please install tap library to test"; +} +exec "./test_utils"; -- cgit v1.2.3