import: Remove downtime as we walk the hashes
[nagios-reports-module.git] / nebtest.c
blobb0d4a63e8b8b5b5e7a60701ae556b467f65a08bf
1 #include "ndbneb.h"
2 #include <string.h>
3 #include <stdlib.h>
4 #include <dlfcn.h>
5 #include "logutils.h"
6 #include "test_utils.h"
7 #include "nagios/nebstructs.h"
8 #include "nagios/nebmodules.h"
9 #include "nagios/nebmods.h"
10 #include "nagios/broker.h"
11 #include "nagios/objects.h"
13 static int (*hooks[NEBCALLBACK_NUMITEMS])(int, void *);
14 static int reg_errors;
15 static int dereg_errors;
16 int event_broker_options = 0;
17 int daemon_dumps_core = 0;
19 int is_tested(int callback_type, const char *caller_func)
21 switch (callback_type) {
22 case NEBCALLBACK_SERVICE_CHECK_DATA:
23 case NEBCALLBACK_HOST_CHECK_DATA:
24 case NEBCALLBACK_DOWNTIME_DATA:
25 case NEBCALLBACK_PROCESS_DATA:
26 t_pass("%s: callback %d is handled by nebtest",
27 caller_func, callback_type);
28 return 1;
31 t_fail("%s: callback %d is unhandled by nebtest",
32 caller_func, callback_type);
33 return 0;
36 int neb_register_callback(int callback_type, void *mod_handle,
37 int priority, int (*callback_func)(int,void *))
39 if (!is_tested(callback_type, __func__))
40 reg_errors |= 1 << callback_type;
42 if (hooks[callback_type]) {
43 reg_errors |= 1 << callback_type;
44 printf("! Registering a second hook for %d!", callback_type);
47 hooks[callback_type] = callback_func;
49 return 0;
52 int neb_deregister_callback(int callback_type, int (*callback_func)(int, void *))
54 if (!is_tested(callback_type, __func__))
55 dereg_errors |= 1 << callback_type;
57 if (!hooks[callback_type]) {
58 dereg_errors |= 1 << callback_type;
59 printf("! Trying to unregister an unregistered hook!\n");
62 hooks[callback_type] = NULL;
64 return 0;
67 static int check_callbacks(void)
69 int i, ret = 0;
71 for (i = 0; i < NEBCALLBACK_NUMITEMS; i++) {
72 if (hooks[i]) {
73 ret = -1;
74 printf("! hook %d not unregistered by module unload function\n", i);
78 return ret;
81 static int check_symbols(void *dso)
83 int i, result = 0;
85 const char *syms[] = {
86 "__neb_api_version", "nebmodule_init", "nebmodule_deinit",
87 NULL,
90 for (i = 0; syms[i]; i++) {
91 const char *sym = syms[i];
93 if (dlsym(dso, sym))
94 t_pass("%s exists in module", sym);
95 else {
96 t_fail("%s can't be looked up: %s", sym, dlerror());
97 result = -1;
101 return result;
105 static char *__host_name = "ndb, host";
106 static char *__service_description = "ndb, service";
107 #define init_ds(x) \
108 do { \
109 memset(&x, 0, sizeof(x)); \
110 x.timestamp.tv_sec = time(NULL); \
111 x.host_name = __host_name; \
112 } while(0)
113 #define set_service(x) x.service_description = __service_description
115 static int test_sql_host_insert(void)
117 int (*hook)(int, void *);
118 nebstruct_host_check_data ds;
119 int cb = NEBCALLBACK_HOST_CHECK_DATA;
121 if (!(hook = hooks[cb])) {
122 printf(" ! Missing host_check hook\n");
123 return -1;
126 init_ds(ds);
127 ds.type = NEBTYPE_HOSTCHECK_PROCESSED;
128 ds.output = "nebtest host check";
129 return hook(cb, &ds);
132 static int test_sql_service_insert(void)
134 int (*hook)(int, void *);
135 nebstruct_service_check_data ds;
136 int cb = NEBCALLBACK_SERVICE_CHECK_DATA;
138 if (!(hook = hooks[cb])) {
139 printf(" ! Missing service_check hook\n");
140 return -1;
143 init_ds(ds);
144 set_service(ds);
145 ds.type = NEBTYPE_SERVICECHECK_PROCESSED;
146 ds.output = strdup("nebtest service check");
148 return hook(cb, &ds);
151 static int test_sql_process_data_insert(void)
153 int (*hook)(int, void *);
154 nebstruct_process_data ds;
155 int cb = NEBCALLBACK_PROCESS_DATA;
157 if (!(hook = hooks[cb])) {
158 printf(" ! Missing process data hook\n");
159 return -1;
162 memset(&ds, 0, sizeof(ds));
163 ds.timestamp.tv_sec = time(NULL);
164 ds.type = NEBTYPE_PROCESS_START;
166 return hook(cb, &ds);
169 static void test_sql_downtime_insert(void)
171 int (*hook)(int, void *);
172 nebstruct_downtime_data ds;
173 int cb = NEBCALLBACK_DOWNTIME_DATA;
175 if (!(hook = hooks[cb])) {
176 t_fail("downtime data hook missing");
177 return;
179 t_pass("downtime data hook exists");
181 init_ds(ds);
182 ds.type = NEBTYPE_DOWNTIME_START;
183 ds.comment_data = "nebtest downtime";
185 ok_int(hook(cb, &ds), 0, "host downtime insertion");
186 set_service(ds);
187 ok_int(hook(cb, &ds), 0, "service downtime insertion");
191 static void test_sql_inserts(void)
193 printf("%sTesting hooks%s\n", cyan, reset);
194 ok_int(test_sql_host_insert(), 0, "host check hook");
195 ok_int(test_sql_service_insert(), 0, "service check hook");
196 test_sql_downtime_insert();
197 ok_int(test_sql_process_data_insert(), 0, "process data hook");
200 static nebmodule *neb;
201 static void test_one_module(char *arg)
203 static void *dso = NULL;
204 char *path;
205 int (*init_func)(int, const char *, nebmodule *);
206 int (*deinit_func)(int, int);
207 int (*log_grok_var)(const char *, const char *);
209 if (dso)
210 dlclose(dso);
212 if (strchr(arg, '/'))
213 path = arg;
214 else {
215 int len = strlen(arg);
216 path = calloc(len + 3, 1);
217 path[0] = '.';
218 path[1] = '/';
219 memcpy(path + 2, arg, len);
222 dso = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
223 if (!dso) {
224 t_fail("dlopen() failed: %s", dlerror());
225 return;
228 t_pass("dlopen() worked out fine");
229 log_grok_var = dlsym(dso, "log_grok_var");
230 if (log_grok_var) {
231 log_grok_var("log_file", "/dev/null");
233 printf("%sChecking symbols in '%s'%s\n", cyan, path, reset);
235 check_symbols(dso);
237 init_func = dlsym(dso, "nebmodule_init");
238 ok_int(init_func(-1, neb->args, neb), 0, "module init function");
240 test_sql_inserts();
241 deinit_func = dlsym(dso, "nebmodule_deinit");
242 ok_int(deinit_func(0, 0), 0, "module deinit function");
243 ok_int(reg_errors, 0, "registering callbacks");
244 ok_int(dereg_errors, 0, "deregistering callbacks");
246 return;
249 int main(int argc, char **argv)
251 int i;
253 t_set_colors(0);
254 t_verbose = 1;
255 neb = calloc(sizeof(*neb), 1);
257 for (i = 1; i < argc; i++) {
258 char *arg = argv[i];
260 if (*arg == '-' && arg[1] == 'f' && i < argc - 1) {
261 neb->args = argv[++i];
262 continue;
265 test_one_module(arg);
268 return t_end();