diff options
author | Ton Voon <tonvoon@users.sourceforge.net> | 2006-07-13 12:50:23 (GMT) |
---|---|---|
committer | Ton Voon <tonvoon@users.sourceforge.net> | 2006-07-13 12:50:23 (GMT) |
commit | 548083b2ea865474915fc8a9ddd361e997585a02 (patch) | |
tree | f3766c1f5d8f8ea1b2b721a5792e4fb4daf1cf68 /lib/tests | |
parent | 6b9cc76d0a27631fbab19a31ab8bd46e143b7580 (diff) | |
download | monitoring-plugins-548083b2ea865474915fc8a9ddd361e997585a02.tar.gz |
Move new util_* functions to lib/
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1451 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'lib/tests')
-rw-r--r-- | lib/tests/.cvsignore | 5 | ||||
-rw-r--r-- | lib/tests/Makefile.am | 28 | ||||
-rw-r--r-- | lib/tests/README | 5 | ||||
-rw-r--r-- | lib/tests/test_disk.c | 124 | ||||
-rwxr-xr-x | lib/tests/test_disk.t | 6 | ||||
-rw-r--r-- | lib/tests/test_utils.c | 169 | ||||
-rwxr-xr-x | lib/tests/test_utils.t | 6 |
7 files changed, 343 insertions, 0 deletions
diff --git a/lib/tests/.cvsignore b/lib/tests/.cvsignore new file mode 100644 index 0000000..6dc692f --- /dev/null +++ b/lib/tests/.cvsignore | |||
@@ -0,0 +1,5 @@ | |||
1 | Makefile | ||
2 | Makefile.in | ||
3 | test_utils | ||
4 | test_disk | ||
5 | .deps | ||
diff --git a/lib/tests/Makefile.am b/lib/tests/Makefile.am new file mode 100644 index 0000000..63dee76 --- /dev/null +++ b/lib/tests/Makefile.am | |||
@@ -0,0 +1,28 @@ | |||
1 | |||
2 | noinst_PROGRAMS = @EXTRA_TEST@ | ||
3 | |||
4 | # These two lines support "make check", but we use "make test" | ||
5 | TESTS = @EXTRA_TEST@ | ||
6 | check_PROGRAMS = @EXTRA_TEST@ | ||
7 | |||
8 | INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/intl -I$(top_srcdir)/plugins | ||
9 | |||
10 | EXTRA_PROGRAMS = test_utils test_disk | ||
11 | |||
12 | EXTRA_DIST = test_utils.t test_disk.t | ||
13 | |||
14 | LIBS = @LIBINTL@ | ||
15 | |||
16 | test_utils_SOURCES = test_utils.c | ||
17 | test_utils_CFLAGS = -g -I.. | ||
18 | test_utils_LDFLAGS = -L/usr/local/lib -ltap | ||
19 | test_utils_LDADD = ../utils_base.o | ||
20 | |||
21 | test_disk_SOURCES = test_disk.c | ||
22 | test_disk_CFLAGS = -g -I.. | ||
23 | test_disk_LDFLAGS = -L/usr/local/lib -ltap | ||
24 | test_disk_LDADD = ../utils_disk.o | ||
25 | |||
26 | test: ${noinst_PROGRAMS} | ||
27 | perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(map {$$_ .= ".t"} @ARGV)' $(EXTRA_PROGRAMS) | ||
28 | |||
diff --git a/lib/tests/README b/lib/tests/README new file mode 100644 index 0000000..66935e4 --- /dev/null +++ b/lib/tests/README | |||
@@ -0,0 +1,5 @@ | |||
1 | The tests in here use the libtap library functions | ||
2 | (http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap), so are | ||
3 | more for unit testing the utils.c library functions. | ||
4 | |||
5 | 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 0000000..8940236 --- /dev/null +++ b/lib/tests/test_disk.c | |||
@@ -0,0 +1,124 @@ | |||
1 | /****************************************************************************** | ||
2 | |||
3 | This program is free software; you can redistribute it and/or modify | ||
4 | it under the terms of the GNU General Public License as published by | ||
5 | the Free Software Foundation; either version 2 of the License, or | ||
6 | (at your option) any later version. | ||
7 | |||
8 | This program is distributed in the hope that it will be useful, | ||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
16 | |||
17 | $Id$ | ||
18 | |||
19 | ******************************************************************************/ | ||
20 | |||
21 | #include "common.h" | ||
22 | #include "utils_disk.h" | ||
23 | #include "tap.h" | ||
24 | |||
25 | int | ||
26 | main (int argc, char **argv) | ||
27 | { | ||
28 | struct name_list *exclude_filesystem=NULL; | ||
29 | struct name_list *exclude_fstype=NULL; | ||
30 | struct name_list *dummy_mountlist = NULL; | ||
31 | struct name_list *temp_name; | ||
32 | struct parameter_list *paths = NULL; | ||
33 | struct parameter_list *p; | ||
34 | |||
35 | struct mount_entry *dummy_mount_list; | ||
36 | struct mount_entry *me; | ||
37 | struct mount_entry **mtail = &dummy_mount_list; | ||
38 | |||
39 | plan_tests(17); | ||
40 | |||
41 | ok( np_find_name(exclude_filesystem, "/var/log") == FALSE, "/var/log not in list"); | ||
42 | np_add_name(&exclude_filesystem, "/var/log"); | ||
43 | ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "is in list now"); | ||
44 | ok( np_find_name(exclude_filesystem, "/home") == FALSE, "/home not in list"); | ||
45 | np_add_name(&exclude_filesystem, "/home"); | ||
46 | ok( np_find_name(exclude_filesystem, "/home") == TRUE, "is in list now"); | ||
47 | ok( np_find_name(exclude_filesystem, "/var/log") == TRUE, "/var/log still in list"); | ||
48 | |||
49 | ok( np_find_name(exclude_fstype, "iso9660") == FALSE, "iso9660 not in list"); | ||
50 | np_add_name(&exclude_fstype, "iso9660"); | ||
51 | ok( np_find_name(exclude_fstype, "iso9660") == TRUE, "is in list now"); | ||
52 | |||
53 | ok( np_find_name(exclude_filesystem, "iso9660") == FALSE, "Make sure no clashing in variables"); | ||
54 | |||
55 | /* | ||
56 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
57 | printf("Name: %s\n", temp_name->name); | ||
58 | } | ||
59 | */ | ||
60 | |||
61 | me = (struct mount_entry *) malloc(sizeof *me); | ||
62 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
63 | me->me_mountdir = strdup("/"); | ||
64 | *mtail = me; | ||
65 | mtail = &me->me_next; | ||
66 | |||
67 | me = (struct mount_entry *) malloc(sizeof *me); | ||
68 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
69 | me->me_mountdir = strdup("/var"); | ||
70 | *mtail = me; | ||
71 | mtail = &me->me_next; | ||
72 | |||
73 | me = (struct mount_entry *) malloc(sizeof *me); | ||
74 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
75 | me->me_mountdir = strdup("/home"); | ||
76 | *mtail = me; | ||
77 | mtail = &me->me_next; | ||
78 | |||
79 | |||
80 | np_add_parameter(&paths, "/home/groups"); | ||
81 | np_add_parameter(&paths, "/var"); | ||
82 | np_add_parameter(&paths, "/tmp"); | ||
83 | np_add_parameter(&paths, "/home/tonvoon"); | ||
84 | np_add_parameter(&paths, "/dev/c2t0d0s0"); | ||
85 | |||
86 | np_set_best_match(paths, dummy_mount_list, FALSE); | ||
87 | for (p = paths; p; p = p->name_next) { | ||
88 | struct mount_entry *temp_me; | ||
89 | temp_me = p->best_match; | ||
90 | if (! strcmp(p->name, "/home/groups")) { | ||
91 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); | ||
92 | } else if (! strcmp(p->name, "/var")) { | ||
93 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
94 | } else if (! strcmp(p->name, "/tmp")) { | ||
95 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
96 | } else if (! strcmp(p->name, "/home/tonvoon")) { | ||
97 | ok( temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); | ||
98 | } else if (! strcmp(p->name, "/dev/c2t0d0s0")) { | ||
99 | ok( temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | paths = NULL; /* Bad boy - should free, but this is a test suite */ | ||
104 | np_add_parameter(&paths, "/home/groups"); | ||
105 | np_add_parameter(&paths, "/var"); | ||
106 | np_add_parameter(&paths, "/tmp"); | ||
107 | np_add_parameter(&paths, "/home/tonvoon"); | ||
108 | |||
109 | np_set_best_match(paths, dummy_mount_list, TRUE); | ||
110 | for (p = paths; p; p = p->name_next) { | ||
111 | if (! strcmp(p->name, "/home/groups")) { | ||
112 | ok( p->found == 0, "/home/groups correctly not found"); | ||
113 | } else if (! strcmp(p->name, "/var")) { | ||
114 | ok( p->found == 1, "/var found"); | ||
115 | } else if (! strcmp(p->name, "/tmp")) { | ||
116 | ok( p->found == 0, "/tmp correctly not found"); | ||
117 | } else if (! strcmp(p->name, "/home/tonvoon")) { | ||
118 | ok( p->found == 0, "/home/tonvoon not found"); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | return exit_status(); | ||
123 | } | ||
124 | |||
diff --git a/lib/tests/test_disk.t b/lib/tests/test_disk.t new file mode 100755 index 0000000..d32567a --- /dev/null +++ b/lib/tests/test_disk.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_disk") { | ||
4 | plan skip_all => "./test_disk not compiled - please install tap library to test"; | ||
5 | } | ||
6 | exec "./test_disk"; | ||
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c new file mode 100644 index 0000000..aedc2a5 --- /dev/null +++ b/lib/tests/test_utils.c | |||
@@ -0,0 +1,169 @@ | |||
1 | /****************************************************************************** | ||
2 | |||
3 | This program is free software; you can redistribute it and/or modify | ||
4 | it under the terms of the GNU General Public License as published by | ||
5 | the Free Software Foundation; either version 2 of the License, or | ||
6 | (at your option) any later version. | ||
7 | |||
8 | This program is distributed in the hope that it will be useful, | ||
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
16 | |||
17 | $Id$ | ||
18 | |||
19 | ******************************************************************************/ | ||
20 | |||
21 | #include "common.h" | ||
22 | #include "utils_base.h" | ||
23 | |||
24 | #include "tap.h" | ||
25 | |||
26 | int | ||
27 | main (int argc, char **argv) | ||
28 | { | ||
29 | range *range; | ||
30 | double temp; | ||
31 | thresholds *thresholds = NULL; | ||
32 | int rc; | ||
33 | |||
34 | plan_tests(74); | ||
35 | |||
36 | range = parse_range_string("6"); | ||
37 | ok( range != NULL, "'6' is valid range"); | ||
38 | ok( range->start == 0, "Start correct"); | ||
39 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
40 | ok( range->end == 6, "End correct"); | ||
41 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
42 | free(range); | ||
43 | |||
44 | range = parse_range_string("-7:23"); | ||
45 | ok( range != NULL, "'-7:23' is valid range"); | ||
46 | ok( range->start == -7, "Start correct"); | ||
47 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
48 | ok( range->end == 23, "End correct"); | ||
49 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
50 | free(range); | ||
51 | |||
52 | range = parse_range_string(":5.75"); | ||
53 | ok( range != NULL, "':5.75' is valid range"); | ||
54 | ok( range->start == 0, "Start correct"); | ||
55 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
56 | ok( range->end == 5.75, "End correct"); | ||
57 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
58 | free(range); | ||
59 | |||
60 | range = parse_range_string("~:-95.99"); | ||
61 | ok( range != NULL, "~:-95.99' is valid range"); | ||
62 | ok( range->start_infinity == TRUE, "Using negative infinity"); | ||
63 | ok( range->end == -95.99, "End correct (with rounding errors)"); | ||
64 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
65 | free(range); | ||
66 | |||
67 | range = parse_range_string("12345678901234567890:"); | ||
68 | temp = atof("12345678901234567890"); /* Can't just use this because number too large */ | ||
69 | ok( range != NULL, "'12345678901234567890:' is valid range"); | ||
70 | ok( range->start == temp, "Start correct"); | ||
71 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
72 | ok( range->end_infinity == TRUE, "Using infinity"); | ||
73 | /* Cannot do a "-1" on temp, as it appears to be same value */ | ||
74 | ok( check_range(temp/1.1, range) == TRUE, "12345678901234567890/1.1 - alert"); | ||
75 | ok( check_range(temp, range) == FALSE, "12345678901234567890 - no alert"); | ||
76 | ok( check_range(temp*2, range) == FALSE, "12345678901234567890*2 - no alert"); | ||
77 | free(range); | ||
78 | |||
79 | range = parse_range_string("~:0"); | ||
80 | ok( range != NULL, "'~:0' is valid range"); | ||
81 | ok( range->start_infinity == TRUE, "Using negative infinity"); | ||
82 | ok( range->end == 0, "End correct"); | ||
83 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
84 | ok( range->alert_on == OUTSIDE, "Will alert on outside of this range"); | ||
85 | ok( check_range(0.5, range) == TRUE, "0.5 - alert"); | ||
86 | ok( check_range(-10, range) == FALSE, "-10 - no alert"); | ||
87 | ok( check_range(0, range) == FALSE, "0 - no alert"); | ||
88 | free(range); | ||
89 | |||
90 | range = parse_range_string("@0:657.8210567"); | ||
91 | ok( range != 0, "@0:657.8210567' is a valid range"); | ||
92 | ok( range->start == 0, "Start correct"); | ||
93 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
94 | ok( range->end == 657.8210567, "End correct"); | ||
95 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
96 | ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); | ||
97 | ok( check_range(32.88, range) == TRUE, "32.88 - alert"); | ||
98 | ok( check_range(-2, range) == FALSE, "-2 - no alert"); | ||
99 | ok( check_range(657.8210567, range) == TRUE, "657.8210567 - alert"); | ||
100 | ok( check_range(0, range) == TRUE, "0 - alert"); | ||
101 | free(range); | ||
102 | |||
103 | range = parse_range_string("1:1"); | ||
104 | ok( range != NULL, "'1:1' is a valid range"); | ||
105 | ok( range->start == 1, "Start correct"); | ||
106 | ok( range->start_infinity == FALSE, "Not using negative infinity"); | ||
107 | ok( range->end == 1, "End correct"); | ||
108 | ok( range->end_infinity == FALSE, "Not using infinity"); | ||
109 | ok( check_range(0.5, range) == TRUE, "0.5 - alert"); | ||
110 | ok( check_range(1, range) == FALSE, "1 - no alert"); | ||
111 | ok( check_range(5.2, range) == TRUE, "5.2 - alert"); | ||
112 | free(range); | ||
113 | |||
114 | range = parse_range_string("2:1"); | ||
115 | ok( range == NULL, "'2:1' rejected"); | ||
116 | |||
117 | rc = _set_thresholds(&thresholds, NULL, "80"); | ||
118 | ok( rc == 0, "Thresholds (NULL, '80') set"); | ||
119 | ok( thresholds->warning == NULL, "Warning not set"); | ||
120 | ok( thresholds->critical->end == 80, "Critical set correctly"); | ||
121 | |||
122 | rc = _set_thresholds(&thresholds, "5:33", NULL); | ||
123 | ok( rc == 0, "Thresholds ('5:33', NULL) set"); | ||
124 | ok( thresholds->warning->start == 5, "Warning start set"); | ||
125 | ok( thresholds->warning->end == 33, "Warning end set"); | ||
126 | ok( thresholds->critical == NULL, "Critical not set"); | ||
127 | |||
128 | rc = _set_thresholds(&thresholds, "30", "60"); | ||
129 | ok( rc == 0, "Thresholds ('30', '60') set"); | ||
130 | ok( thresholds->warning->end == 30, "Warning set correctly"); | ||
131 | ok( thresholds->critical->end == 60, "Critical set correctly"); | ||
132 | ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); | ||
133 | ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); | ||
134 | ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); | ||
135 | |||
136 | char *test; | ||
137 | test = np_escaped_string("bob\\n"); | ||
138 | ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); | ||
139 | free(test); | ||
140 | |||
141 | test = np_escaped_string("rhuba\\rb"); | ||
142 | ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay"); | ||
143 | free(test); | ||
144 | |||
145 | test = np_escaped_string("ba\\nge\\r"); | ||
146 | ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay"); | ||
147 | free(test); | ||
148 | |||
149 | test = np_escaped_string("\\rabbi\\t"); | ||
150 | ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay"); | ||
151 | free(test); | ||
152 | |||
153 | test = np_escaped_string("and\\\\or"); | ||
154 | ok( strcmp(test, "and\\or") == 0, "and\\\\or okay"); | ||
155 | free(test); | ||
156 | |||
157 | test = np_escaped_string("bo\\gus"); | ||
158 | ok( strcmp(test, "bogus") == 0, "bo\\gus okay"); | ||
159 | free(test); | ||
160 | |||
161 | test = np_escaped_string("everything"); | ||
162 | ok( strcmp(test, "everything") == 0, "everything okay"); | ||
163 | free(test); | ||
164 | |||
165 | test = basename("/here/is/a/path"); | ||
166 | ok( strcmp(test, "path") == 0, "basename okay"); | ||
167 | |||
168 | return exit_status(); | ||
169 | } | ||
diff --git a/lib/tests/test_utils.t b/lib/tests/test_utils.t new file mode 100755 index 0000000..152eb71 --- /dev/null +++ b/lib/tests/test_utils.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_utils") { | ||
4 | plan skip_all => "./test_utils not compiled - please install tap library to test"; | ||
5 | } | ||
6 | exec "./test_utils"; | ||