summaryrefslogtreecommitdiffstats
path: root/plugins/tests
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-18 16:23:33 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-18 16:23:33 +0100
commit42531fa92a97318f2b96265f501f37e7fd96ea4c (patch)
tree723dffbf160dc1b73a44f3f39182b0bdb330048f /plugins/tests
parent75cf0d307285c413d5547662e5ba677e1f70de62 (diff)
downloadmonitoring-plugins-42531fa92a97318f2b96265f501f37e7fd96ea4c.tar.gz
Refactor test_check_disk.c
Diffstat (limited to 'plugins/tests')
-rw-r--r--plugins/tests/test_check_disk.c86
1 files changed, 44 insertions, 42 deletions
diff --git a/plugins/tests/test_check_disk.c b/plugins/tests/test_check_disk.c
index 92d0d270..963a9413 100644
--- a/plugins/tests/test_check_disk.c
+++ b/plugins/tests/test_check_disk.c
@@ -24,21 +24,9 @@
24void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc); 24void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc);
25 25
26int main(int argc, char **argv) { 26int 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); 27 plan_tests(33);
41 28
29 struct name_list *exclude_filesystem = NULL;
42 ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); 30 ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list");
43 np_add_name(&exclude_filesystem, "/var/log"); 31 np_add_name(&exclude_filesystem, "/var/log");
44 ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); 32 ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now");
@@ -47,6 +35,7 @@ int main(int argc, char **argv) {
47 ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); 35 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"); 36 ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list");
49 37
38 struct name_list *exclude_fstype = NULL;
50 ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); 39 ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list");
51 np_add_name(&exclude_fstype, "iso9660"); 40 np_add_name(&exclude_fstype, "iso9660");
52 ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); 41 ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now");
@@ -59,7 +48,9 @@ int main(int argc, char **argv) {
59 } 48 }
60 */ 49 */
61 50
62 me = (struct mount_entry *)malloc(sizeof *me); 51 struct mount_entry *dummy_mount_list;
52 struct mount_entry **mtail = &dummy_mount_list;
53 struct mount_entry *me = (struct mount_entry *)malloc(sizeof *me);
63 me->me_devname = strdup("/dev/c0t0d0s0"); 54 me->me_devname = strdup("/dev/c0t0d0s0");
64 me->me_mountdir = strdup("/"); 55 me->me_mountdir = strdup("/");
65 *mtail = me; 56 *mtail = me;
@@ -77,6 +68,7 @@ int main(int argc, char **argv) {
77 *mtail = me; 68 *mtail = me;
78 mtail = &me->me_next; 69 mtail = &me->me_next;
79 70
71 int cflags = REG_NOSUB | REG_EXTENDED;
80 np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); 72 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:")); 73 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:")); 74 np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:"));
@@ -89,6 +81,7 @@ int main(int argc, char **argv) {
89 np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); 81 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:")); 82 np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:"));
91 83
84 struct parameter_list *paths = NULL;
92 np_add_parameter(&paths, "/home/groups"); 85 np_add_parameter(&paths, "/home/groups");
93 np_add_parameter(&paths, "/var"); 86 np_add_parameter(&paths, "/var");
94 np_add_parameter(&paths, "/tmp"); 87 np_add_parameter(&paths, "/tmp");
@@ -96,7 +89,7 @@ int main(int argc, char **argv) {
96 np_add_parameter(&paths, "/dev/c2t0d0s0"); 89 np_add_parameter(&paths, "/dev/c2t0d0s0");
97 90
98 np_set_best_match(paths, dummy_mount_list, false); 91 np_set_best_match(paths, dummy_mount_list, false);
99 for (p = paths; p; p = p->name_next) { 92 for (struct parameter_list *p = paths; p; p = p->name_next) {
100 struct mount_entry *temp_me; 93 struct mount_entry *temp_me;
101 temp_me = p->best_match; 94 temp_me = p->best_match;
102 if (!strcmp(p->name, "/home/groups")) { 95 if (!strcmp(p->name, "/home/groups")) {
@@ -120,7 +113,7 @@ int main(int argc, char **argv) {
120 np_add_parameter(&paths, "/home"); 113 np_add_parameter(&paths, "/home");
121 114
122 np_set_best_match(paths, dummy_mount_list, true); 115 np_set_best_match(paths, dummy_mount_list, true);
123 for (p = paths; p; p = p->name_next) { 116 for (struct parameter_list *p = paths; p; p = p->name_next) {
124 if (!strcmp(p->name, "/home/groups")) { 117 if (!strcmp(p->name, "/home/groups")) {
125 ok(!p->best_match, "/home/groups correctly not found"); 118 ok(!p->best_match, "/home/groups correctly not found");
126 } else if (!strcmp(p->name, "/var")) { 119 } else if (!strcmp(p->name, "/var")) {
@@ -134,59 +127,68 @@ int main(int argc, char **argv) {
134 } 127 }
135 } 128 }
136 129
130 bool found = false;
137 /* test deleting first element in paths */ 131 /* test deleting first element in paths */
138 paths = np_del_parameter(paths, NULL); 132 paths = np_del_parameter(paths, NULL);
139 for (p = paths; p; p = p->name_next) { 133 for (struct parameter_list *p = paths; p; p = p->name_next) {
140 if (!strcmp(p->name, "/home/groups")) 134 if (!strcmp(p->name, "/home/groups")) {
141 found = 1; 135 found = true;
136 }
142 } 137 }
143 ok(found == 0, "first element successfully deleted"); 138 ok(!found, "first element successfully deleted");
144 found = 0; 139 found = false;
145 140
146 p = paths; 141 struct parameter_list *prev = NULL;
142 struct parameter_list *p = paths;
147 while (p) { 143 while (p) {
148 if (!strcmp(p->name, "/tmp")) 144 if (!strcmp(p->name, "/tmp")) {
149 p = np_del_parameter(p, prev); 145 p = np_del_parameter(p, prev);
150 else { 146 } else {
151 prev = p; 147 prev = p;
152 p = p->name_next; 148 p = p->name_next;
153 } 149 }
154 } 150 }
155 151
156 for (p = paths; p; p = p->name_next) { 152 struct parameter_list *last = NULL;
157 if (!strcmp(p->name, "/tmp")) 153 for (struct parameter_list *path = paths; path; path = path->name_next) {
158 found = 1; 154 if (!strcmp(path->name, "/tmp")) {
159 if (p->name_next) 155 found = true;
160 prev = p; 156 }
161 else 157 if (path->name_next) {
162 last = p; 158 prev = path;
159 } else {
160 last = path;
161 }
163 } 162 }
164 ok(found == 0, "/tmp element successfully deleted"); 163 ok(!found, "/tmp element successfully deleted");
165 164
165 int count = 0;
166 p = np_del_parameter(last, prev); 166 p = np_del_parameter(last, prev);
167 for (p = paths; p; p = p->name_next) { 167 for (p = paths; p; p = p->name_next) {
168 if (!strcmp(p->name, "/home")) 168 if (!strcmp(p->name, "/home")) {
169 found = 1; 169 found = true;
170 }
170 last = p; 171 last = p;
171 count++; 172 count++;
172 } 173 }
173 ok(found == 0, "last (/home) element successfully deleted"); 174 ok(!found, "last (/home) element successfully deleted");
174 ok(count == 2, "two elements remaining"); 175 ok(count == 2, "two elements remaining");
175 176
176 return exit_status(); 177 return exit_status();
177} 178}
178 179
179void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { 180void 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 regex;
181 regex_t re; 182 if (regcomp(&regex, regstr, cflags) == 0) {
182 struct mount_entry *me; 183 int matches = 0;
183 if (regcomp(&re, regstr, cflags) == 0) { 184 for (struct mount_entry *me = dummy_mount_list; me; me = me->me_next) {
184 for (me = dummy_mount_list; me; me = me->me_next) { 185 if (np_regex_match_mount_entry(me, &regex)) {
185 if (np_regex_match_mount_entry(me, &re))
186 matches++; 186 matches++;
187 }
187 } 188 }
188 ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); 189 ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches);
189 190
190 } else 191 } else {
191 ok(false, "regex '%s' not compilable", regstr); 192 ok(false, "regex '%s' not compilable", regstr);
193 }
192} 194}