diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-18 15:57:44 +0100 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-18 15:57:44 +0100 |
commit | 59e0a258f9c0f393bf29cced1f35743f74e7b10c (patch) | |
tree | b58756ef9284f4a02546fcc098f036a677e4481f /plugins/tests | |
parent | 7994b478187d38c04dd2d7d9241d237875589738 (diff) | |
download | monitoring-plugins-59e0a258f9c0f393bf29cced1f35743f74e7b10c.tar.gz |
Migrate disk tests from lib, tool
Diffstat (limited to 'plugins/tests')
-rw-r--r-- | plugins/tests/test_check_disk.c | 192 | ||||
-rwxr-xr-x | plugins/tests/test_check_disk.t | 6 |
2 files changed, 198 insertions, 0 deletions
diff --git a/plugins/tests/test_check_disk.c b/plugins/tests/test_check_disk.c new file mode 100644 index 00000000..92d0d270 --- /dev/null +++ b/plugins/tests/test_check_disk.c | |||
@@ -0,0 +1,192 @@ | |||
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 3 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, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "common.h" | ||
20 | #include "../check_disk.d/utils_disk.h" | ||
21 | #include "../../tap/tap.h" | ||
22 | #include "regex.h" | ||
23 | |||
24 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); | ||
25 | |||
26 | int main(int argc, char **argv) { | ||
27 | struct name_list *exclude_filesystem = NULL; | ||
28 | struct name_list *exclude_fstype = NULL; | ||
29 | struct name_list *dummy_mountlist = NULL; | ||
30 | struct name_list *temp_name; | ||
31 | struct parameter_list *paths = NULL; | ||
32 | struct parameter_list *p, *prev = NULL, *last = NULL; | ||
33 | |||
34 | struct mount_entry *dummy_mount_list; | ||
35 | struct mount_entry *me; | ||
36 | struct mount_entry **mtail = &dummy_mount_list; | ||
37 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
38 | int found = 0, count = 0; | ||
39 | |||
40 | plan_tests(33); | ||
41 | |||
42 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | ||
43 | np_add_name(&exclude_filesystem, "/var/log"); | ||
44 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | ||
45 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); | ||
46 | np_add_name(&exclude_filesystem, "/home"); | ||
47 | ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | ||
48 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); | ||
49 | |||
50 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | ||
51 | np_add_name(&exclude_fstype, "iso9660"); | ||
52 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | ||
53 | |||
54 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | ||
55 | |||
56 | /* | ||
57 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
58 | printf("Name: %s\n", temp_name->name); | ||
59 | } | ||
60 | */ | ||
61 | |||
62 | me = (struct mount_entry *)malloc(sizeof *me); | ||
63 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
64 | me->me_mountdir = strdup("/"); | ||
65 | *mtail = me; | ||
66 | mtail = &me->me_next; | ||
67 | |||
68 | me = (struct mount_entry *)malloc(sizeof *me); | ||
69 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
70 | me->me_mountdir = strdup("/var"); | ||
71 | *mtail = me; | ||
72 | mtail = &me->me_next; | ||
73 | |||
74 | me = (struct mount_entry *)malloc(sizeof *me); | ||
75 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
76 | me->me_mountdir = strdup("/home"); | ||
77 | *mtail = me; | ||
78 | mtail = &me->me_next; | ||
79 | |||
80 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); | ||
81 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); | ||
82 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); | ||
83 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); | ||
84 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); | ||
85 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); | ||
86 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); | ||
87 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); | ||
88 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); | ||
89 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); | ||
90 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); | ||
91 | |||
92 | np_add_parameter(&paths, "/home/groups"); | ||
93 | np_add_parameter(&paths, "/var"); | ||
94 | np_add_parameter(&paths, "/tmp"); | ||
95 | np_add_parameter(&paths, "/home/tonvoon"); | ||
96 | np_add_parameter(&paths, "/dev/c2t0d0s0"); | ||
97 | |||
98 | np_set_best_match(paths, dummy_mount_list, false); | ||
99 | for (p = paths; p; p = p->name_next) { | ||
100 | struct mount_entry *temp_me; | ||
101 | temp_me = p->best_match; | ||
102 | if (!strcmp(p->name, "/home/groups")) { | ||
103 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); | ||
104 | } else if (!strcmp(p->name, "/var")) { | ||
105 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
106 | } else if (!strcmp(p->name, "/tmp")) { | ||
107 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
108 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
109 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); | ||
110 | } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { | ||
111 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | paths = NULL; /* Bad boy - should free, but this is a test suite */ | ||
116 | np_add_parameter(&paths, "/home/groups"); | ||
117 | np_add_parameter(&paths, "/var"); | ||
118 | np_add_parameter(&paths, "/tmp"); | ||
119 | np_add_parameter(&paths, "/home/tonvoon"); | ||
120 | np_add_parameter(&paths, "/home"); | ||
121 | |||
122 | np_set_best_match(paths, dummy_mount_list, true); | ||
123 | for (p = paths; p; p = p->name_next) { | ||
124 | if (!strcmp(p->name, "/home/groups")) { | ||
125 | ok(!p->best_match, "/home/groups correctly not found"); | ||
126 | } else if (!strcmp(p->name, "/var")) { | ||
127 | ok(p->best_match, "/var found"); | ||
128 | } else if (!strcmp(p->name, "/tmp")) { | ||
129 | ok(!p->best_match, "/tmp correctly not found"); | ||
130 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
131 | ok(!p->best_match, "/home/tonvoon not found"); | ||
132 | } else if (!strcmp(p->name, "/home")) { | ||
133 | ok(p->best_match, "/home found"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | /* test deleting first element in paths */ | ||
138 | paths = np_del_parameter(paths, NULL); | ||
139 | for (p = paths; p; p = p->name_next) { | ||
140 | if (!strcmp(p->name, "/home/groups")) | ||
141 | found = 1; | ||
142 | } | ||
143 | ok(found == 0, "first element successfully deleted"); | ||
144 | found = 0; | ||
145 | |||
146 | p = paths; | ||
147 | while (p) { | ||
148 | if (!strcmp(p->name, "/tmp")) | ||
149 | p = np_del_parameter(p, prev); | ||
150 | else { | ||
151 | prev = p; | ||
152 | p = p->name_next; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | for (p = paths; p; p = p->name_next) { | ||
157 | if (!strcmp(p->name, "/tmp")) | ||
158 | found = 1; | ||
159 | if (p->name_next) | ||
160 | prev = p; | ||
161 | else | ||
162 | last = p; | ||
163 | } | ||
164 | ok(found == 0, "/tmp element successfully deleted"); | ||
165 | |||
166 | p = np_del_parameter(last, prev); | ||
167 | for (p = paths; p; p = p->name_next) { | ||
168 | if (!strcmp(p->name, "/home")) | ||
169 | found = 1; | ||
170 | last = p; | ||
171 | count++; | ||
172 | } | ||
173 | ok(found == 0, "last (/home) element successfully deleted"); | ||
174 | ok(count == 2, "two elements remaining"); | ||
175 | |||
176 | return exit_status(); | ||
177 | } | ||
178 | |||
179 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { | ||
180 | int matches = 0; | ||
181 | regex_t re; | ||
182 | struct mount_entry *me; | ||
183 | if (regcomp(&re, regstr, cflags) == 0) { | ||
184 | for (me = dummy_mount_list; me; me = me->me_next) { | ||
185 | if (np_regex_match_mount_entry(me, &re)) | ||
186 | matches++; | ||
187 | } | ||
188 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); | ||
189 | |||
190 | } else | ||
191 | ok(false, "regex '%s' not compilable", regstr); | ||
192 | } | ||
diff --git a/plugins/tests/test_check_disk.t b/plugins/tests/test_check_disk.t new file mode 100755 index 00000000..56354650 --- /dev/null +++ b/plugins/tests/test_check_disk.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_check_disk") { | ||
4 | plan skip_all => "./test_check_disk not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_check_disk"; | ||