diff options
Diffstat (limited to 'plugins/tests')
-rw-r--r-- | plugins/tests/test_check_disk.c | 197 | ||||
-rwxr-xr-x | plugins/tests/test_check_disk.t | 6 | ||||
-rw-r--r-- | plugins/tests/test_check_swap.c | 23 | ||||
-rwxr-xr-x | plugins/tests/test_check_swap.t | 6 | ||||
-rw-r--r-- | plugins/tests/var/proc_meminfo | 55 |
5 files changed, 287 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..35c57bce --- /dev/null +++ b/plugins/tests/test_check_disk.c | |||
@@ -0,0 +1,197 @@ | |||
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 | plan_tests(35); | ||
28 | |||
29 | struct name_list *exclude_filesystem = NULL; | ||
30 | ok(np_find_name(exclude_filesystem, "/var/log") == false, "/var/log not in list"); | ||
31 | np_add_name(&exclude_filesystem, "/var/log"); | ||
32 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "is in list now"); | ||
33 | ok(np_find_name(exclude_filesystem, "/home") == false, "/home not in list"); | ||
34 | np_add_name(&exclude_filesystem, "/home"); | ||
35 | ok(np_find_name(exclude_filesystem, "/home") == true, "is in list now"); | ||
36 | ok(np_find_name(exclude_filesystem, "/var/log") == true, "/var/log still in list"); | ||
37 | |||
38 | struct name_list *exclude_fstype = NULL; | ||
39 | ok(np_find_name(exclude_fstype, "iso9660") == false, "iso9660 not in list"); | ||
40 | np_add_name(&exclude_fstype, "iso9660"); | ||
41 | ok(np_find_name(exclude_fstype, "iso9660") == true, "is in list now"); | ||
42 | |||
43 | ok(np_find_name(exclude_filesystem, "iso9660") == false, "Make sure no clashing in variables"); | ||
44 | |||
45 | /* | ||
46 | for (temp_name = exclude_filesystem; temp_name; temp_name = temp_name->next) { | ||
47 | printf("Name: %s\n", temp_name->name); | ||
48 | } | ||
49 | */ | ||
50 | |||
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); | ||
54 | me->me_devname = strdup("/dev/c0t0d0s0"); | ||
55 | me->me_mountdir = strdup("/"); | ||
56 | *mtail = me; | ||
57 | mtail = &me->me_next; | ||
58 | |||
59 | me = (struct mount_entry *)malloc(sizeof *me); | ||
60 | me->me_devname = strdup("/dev/c1t0d1s0"); | ||
61 | me->me_mountdir = strdup("/var"); | ||
62 | *mtail = me; | ||
63 | mtail = &me->me_next; | ||
64 | |||
65 | me = (struct mount_entry *)malloc(sizeof *me); | ||
66 | me->me_devname = strdup("/dev/c2t0d0s0"); | ||
67 | me->me_mountdir = strdup("/home"); | ||
68 | *mtail = me; | ||
69 | mtail = &me->me_next; | ||
70 | |||
71 | int cflags = REG_NOSUB | REG_EXTENDED; | ||
72 | np_test_mount_entry_regex(dummy_mount_list, strdup("/"), cflags, 3, strdup("a")); | ||
73 | np_test_mount_entry_regex(dummy_mount_list, strdup("/dev"), cflags, 3, strdup("regex on dev names:")); | ||
74 | np_test_mount_entry_regex(dummy_mount_list, strdup("/foo"), cflags, 0, strdup("regex on non existent dev/path:")); | ||
75 | np_test_mount_entry_regex(dummy_mount_list, strdup("/Foo"), cflags | REG_ICASE, 0, strdup("regi on non existent dev/path:")); | ||
76 | np_test_mount_entry_regex(dummy_mount_list, strdup("/c.t0"), cflags, 3, strdup("partial devname regex match:")); | ||
77 | np_test_mount_entry_regex(dummy_mount_list, strdup("c0t0"), cflags, 1, strdup("partial devname regex match:")); | ||
78 | np_test_mount_entry_regex(dummy_mount_list, strdup("C0t0"), cflags | REG_ICASE, 1, strdup("partial devname regi match:")); | ||
79 | np_test_mount_entry_regex(dummy_mount_list, strdup("home"), cflags, 1, strdup("partial pathname regex match:")); | ||
80 | np_test_mount_entry_regex(dummy_mount_list, strdup("hOme"), cflags | REG_ICASE, 1, strdup("partial pathname regi match:")); | ||
81 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/home)|(/var)"), cflags, 2, strdup("grouped regex pathname match:")); | ||
82 | np_test_mount_entry_regex(dummy_mount_list, strdup("(/homE)|(/Var)"), cflags | REG_ICASE, 2, strdup("grouped regi pathname match:")); | ||
83 | |||
84 | filesystem_list test_paths = filesystem_list_init(); | ||
85 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
86 | mp_int_fs_list_append(&test_paths, "/var"); | ||
87 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
88 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
89 | mp_int_fs_list_append(&test_paths, "/dev/c2t0d0s0"); | ||
90 | ok(test_paths.length == 5, "List counter works correctly with appends"); | ||
91 | |||
92 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, false); | ||
93 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
94 | struct mount_entry *temp_me; | ||
95 | temp_me = p->best_match; | ||
96 | if (!strcmp(p->name, "/home/groups")) { | ||
97 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/groups got right best match: /home"); | ||
98 | } else if (!strcmp(p->name, "/var")) { | ||
99 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/var"), "/var got right best match: /var"); | ||
100 | } else if (!strcmp(p->name, "/tmp")) { | ||
101 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/"), "/tmp got right best match: /"); | ||
102 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
103 | ok(temp_me && !strcmp(temp_me->me_mountdir, "/home"), "/home/tonvoon got right best match: /home"); | ||
104 | } else if (!strcmp(p->name, "/dev/c2t0d0s0")) { | ||
105 | ok(temp_me && !strcmp(temp_me->me_devname, "/dev/c2t0d0s0"), "/dev/c2t0d0s0 got right best match: /dev/c2t0d0s0"); | ||
106 | } | ||
107 | } | ||
108 | |||
109 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
110 | mp_int_fs_list_del(&test_paths, p); | ||
111 | } | ||
112 | ok(test_paths.length == 0, "List delete sets counter properly"); | ||
113 | |||
114 | mp_int_fs_list_append(&test_paths, "/home/groups"); | ||
115 | mp_int_fs_list_append(&test_paths, "/var"); | ||
116 | mp_int_fs_list_append(&test_paths, "/tmp"); | ||
117 | mp_int_fs_list_append(&test_paths, "/home/tonvoon"); | ||
118 | mp_int_fs_list_append(&test_paths, "/home"); | ||
119 | |||
120 | mp_int_fs_list_set_best_match(test_paths, dummy_mount_list, true); | ||
121 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
122 | if (!strcmp(p->name, "/home/groups")) { | ||
123 | ok(!p->best_match, "/home/groups correctly not found"); | ||
124 | } else if (!strcmp(p->name, "/var")) { | ||
125 | ok(p->best_match, "/var found"); | ||
126 | } else if (!strcmp(p->name, "/tmp")) { | ||
127 | ok(!p->best_match, "/tmp correctly not found"); | ||
128 | } else if (!strcmp(p->name, "/home/tonvoon")) { | ||
129 | ok(!p->best_match, "/home/tonvoon not found"); | ||
130 | } else if (!strcmp(p->name, "/home")) { | ||
131 | ok(p->best_match, "/home found"); | ||
132 | } | ||
133 | } | ||
134 | |||
135 | bool found = false; | ||
136 | /* test deleting first element in paths */ | ||
137 | mp_int_fs_list_del(&test_paths, NULL); | ||
138 | for (parameter_list_elem *p = test_paths.first; p; p = mp_int_fs_list_get_next(p)) { | ||
139 | if (!strcmp(p->name, "/home/groups")) { | ||
140 | found = true; | ||
141 | } | ||
142 | } | ||
143 | ok(!found, "first element successfully deleted"); | ||
144 | found = false; | ||
145 | |||
146 | parameter_list_elem *prev = NULL; | ||
147 | parameter_list_elem *p = NULL; | ||
148 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
149 | if (!strcmp(path->name, "/tmp")) { | ||
150 | mp_int_fs_list_del(&test_paths, path); | ||
151 | } | ||
152 | p = path; | ||
153 | } | ||
154 | |||
155 | parameter_list_elem *last = NULL; | ||
156 | for (parameter_list_elem *path = test_paths.first; path; path = mp_int_fs_list_get_next(path)) { | ||
157 | if (!strcmp(path->name, "/tmp")) { | ||
158 | found = true; | ||
159 | } | ||
160 | if (path->next) { | ||
161 | prev = path; | ||
162 | } else { | ||
163 | last = path; | ||
164 | } | ||
165 | } | ||
166 | ok(!found, "/tmp element successfully deleted"); | ||
167 | |||
168 | int count = 0; | ||
169 | mp_int_fs_list_del(&test_paths, p); | ||
170 | for (p = test_paths.first; p; p = p->next) { | ||
171 | if (!strcmp(p->name, "/home")) { | ||
172 | found = true; | ||
173 | } | ||
174 | last = p; | ||
175 | count++; | ||
176 | } | ||
177 | ok(!found, "last (/home) element successfully deleted"); | ||
178 | ok(count == 2, "two elements remaining"); | ||
179 | |||
180 | return exit_status(); | ||
181 | } | ||
182 | |||
183 | void np_test_mount_entry_regex(struct mount_entry *dummy_mount_list, char *regstr, int cflags, int expect, char *desc) { | ||
184 | regex_t regex; | ||
185 | if (regcomp(®ex, regstr, cflags) == 0) { | ||
186 | int matches = 0; | ||
187 | for (struct mount_entry *me = dummy_mount_list; me; me = me->me_next) { | ||
188 | if (np_regex_match_mount_entry(me, ®ex)) { | ||
189 | matches++; | ||
190 | } | ||
191 | } | ||
192 | ok(matches == expect, "%s '%s' matched %i/3 entries. ok: %i/3", desc, regstr, expect, matches); | ||
193 | |||
194 | } else { | ||
195 | ok(false, "regex '%s' not compilable", regstr); | ||
196 | } | ||
197 | } | ||
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"; | ||
diff --git a/plugins/tests/test_check_swap.c b/plugins/tests/test_check_swap.c new file mode 100644 index 00000000..b85fb4ad --- /dev/null +++ b/plugins/tests/test_check_swap.c | |||
@@ -0,0 +1,23 @@ | |||
1 | |||
2 | #include "../check_swap.d/check_swap.h" | ||
3 | #include "../../tap/tap.h" | ||
4 | |||
5 | int verbose = 0; | ||
6 | |||
7 | void print_usage(void) {} | ||
8 | void print_help(swap_config config) { | ||
9 | (void) config; | ||
10 | } | ||
11 | |||
12 | const char *progname = "test_check_swap"; | ||
13 | |||
14 | int main(void) { | ||
15 | swap_result test_data = getSwapFromProcMeminfo("./var/proc_meminfo"); | ||
16 | |||
17 | plan_tests(4); | ||
18 | |||
19 | ok(test_data.errorcode == 0, "Test whether we manage to retrieve swap data"); | ||
20 | ok(test_data.metrics.total == 34233905152, "Is the total Swap correct"); | ||
21 | ok(test_data.metrics.free == 34233905152, "Is the free Swap correct"); | ||
22 | ok(test_data.metrics.used == 0, "Is the used Swap correct"); | ||
23 | } | ||
diff --git a/plugins/tests/test_check_swap.t b/plugins/tests/test_check_swap.t new file mode 100755 index 00000000..826fae01 --- /dev/null +++ b/plugins/tests/test_check_swap.t | |||
@@ -0,0 +1,6 @@ | |||
1 | #!/usr/bin/perl | ||
2 | use Test::More; | ||
3 | if (! -e "./test_check_swap") { | ||
4 | plan skip_all => "./test_check_swap not compiled - please enable libtap library to test"; | ||
5 | } | ||
6 | exec "./test_check_swap"; | ||
diff --git a/plugins/tests/var/proc_meminfo b/plugins/tests/var/proc_meminfo new file mode 100644 index 00000000..6c5a618d --- /dev/null +++ b/plugins/tests/var/proc_meminfo | |||
@@ -0,0 +1,55 @@ | |||
1 | MemTotal: 32767776 kB | ||
2 | MemFree: 1693508 kB | ||
3 | MemAvailable: 23807480 kB | ||
4 | Buffers: 438456 kB | ||
5 | Cached: 19124976 kB | ||
6 | SwapCached: 0 kB | ||
7 | Active: 7860680 kB | ||
8 | Inactive: 18886776 kB | ||
9 | Active(anon): 6108756 kB | ||
10 | Inactive(anon): 1364500 kB | ||
11 | Active(file): 1751924 kB | ||
12 | Inactive(file): 17522276 kB | ||
13 | Unevictable: 8548 kB | ||
14 | Mlocked: 8548 kB | ||
15 | SwapTotal: 33431548 kB | ||
16 | SwapFree: 33431548 kB | ||
17 | Zswap: 0 kB | ||
18 | Zswapped: 0 kB | ||
19 | Dirty: 784 kB | ||
20 | Writeback: 0 kB | ||
21 | AnonPages: 7139968 kB | ||
22 | Mapped: 1094916 kB | ||
23 | Shmem: 284160 kB | ||
24 | KReclaimable: 3303788 kB | ||
25 | Slab: 3801908 kB | ||
26 | SReclaimable: 3303788 kB | ||
27 | SUnreclaim: 498120 kB | ||
28 | KernelStack: 32992 kB | ||
29 | PageTables: 68160 kB | ||
30 | SecPageTables: 0 kB | ||
31 | NFS_Unstable: 0 kB | ||
32 | Bounce: 0 kB | ||
33 | WritebackTmp: 0 kB | ||
34 | CommitLimit: 49815436 kB | ||
35 | Committed_AS: 16888536 kB | ||
36 | VmallocTotal: 34359738367 kB | ||
37 | VmallocUsed: 91200 kB | ||
38 | VmallocChunk: 0 kB | ||
39 | Percpu: 41472 kB | ||
40 | HardwareCorrupted: 0 kB | ||
41 | AnonHugePages: 1708032 kB | ||
42 | ShmemHugePages: 0 kB | ||
43 | ShmemPmdMapped: 0 kB | ||
44 | FileHugePages: 0 kB | ||
45 | FilePmdMapped: 0 kB | ||
46 | Unaccepted: 0 kB | ||
47 | HugePages_Total: 0 | ||
48 | HugePages_Free: 0 | ||
49 | HugePages_Rsvd: 0 | ||
50 | HugePages_Surp: 0 | ||
51 | Hugepagesize: 2048 kB | ||
52 | Hugetlb: 0 kB | ||
53 | DirectMap4k: 860468 kB | ||
54 | DirectMap2M: 20023296 kB | ||
55 | DirectMap1G: 12582912 kB | ||