showlog: Get rid of EVT_INITIAL
[nagios-reports-module.git] / nebtest.c
blobf921d252e772f97c1d6a65fd3908aeaeb9d35cc7
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 void check_callbacks(void)
69 int i;
71 for (i = 0; i < NEBCALLBACK_NUMITEMS; i++) {
72 /* only print it if something's wrong, to suppress noise */
73 if (!hooks[i])
74 continue;
76 t_fail("hook %d not unregistered by module unload function", i);
80 static int check_symbols(void *dso)
82 int i, result = 0;
84 const char *syms[] = {
85 "__neb_api_version", "nebmodule_init", "nebmodule_deinit",
86 NULL,
89 for (i = 0; syms[i]; i++) {
90 const char *sym = syms[i];
92 if (dlsym(dso, sym))
93 t_pass("%s exists in module", sym);
94 else {
95 t_fail("%s can't be looked up: %s", sym, dlerror());
96 result = -1;
100 return result;
104 static char *__host_name = "ndb, host";
105 static char *__service_description = "ndb, service";
106 #define init_ds(x) \
107 do { \
108 memset(&x, 0, sizeof(x)); \
109 x.timestamp.tv_sec = time(NULL); \
110 x.host_name = __host_name; \
111 } while(0)
112 #define set_service(x) x.service_description = __service_description
114 static int test_sql_host_insert(void)
116 int (*hook)(int, void *);
117 nebstruct_host_check_data ds;
118 int cb = NEBCALLBACK_HOST_CHECK_DATA;
120 if (!(hook = hooks[cb])) {
121 return -1;
124 init_ds(ds);
125 ds.type = NEBTYPE_HOSTCHECK_PROCESSED;
126 ds.output = "nebtest host check";
127 return hook(cb, &ds);
130 static int test_sql_service_insert(void)
132 int (*hook)(int, void *);
133 nebstruct_service_check_data ds;
134 int cb = NEBCALLBACK_SERVICE_CHECK_DATA;
136 if (!(hook = hooks[cb])) {
137 printf(" ! Missing service_check hook\n");
138 return -1;
141 init_ds(ds);
142 set_service(ds);
143 ds.type = NEBTYPE_SERVICECHECK_PROCESSED;
144 ds.output = strdup("nebtest service check");
146 return hook(cb, &ds);
149 static int test_sql_process_data_insert(void)
151 int (*hook)(int, void *);
152 nebstruct_process_data ds;
153 int cb = NEBCALLBACK_PROCESS_DATA;
155 if (!(hook = hooks[cb])) {
156 printf(" ! Missing process data hook\n");
157 return -1;
160 memset(&ds, 0, sizeof(ds));
161 ds.timestamp.tv_sec = time(NULL);
162 ds.type = NEBTYPE_PROCESS_START;
164 return hook(cb, &ds);
167 static void test_sql_downtime_insert(void)
169 int (*hook)(int, void *);
170 nebstruct_downtime_data ds;
171 int cb = NEBCALLBACK_DOWNTIME_DATA;
173 if (!(hook = hooks[cb])) {
174 t_fail("downtime data hook missing");
175 return;
177 t_pass("downtime data hook exists");
179 init_ds(ds);
180 ds.type = NEBTYPE_DOWNTIME_START;
181 ds.comment_data = "nebtest downtime";
183 ok_int(hook(cb, &ds), 0, "host downtime insertion");
184 set_service(ds);
185 ok_int(hook(cb, &ds), 0, "service downtime insertion");
189 static void test_sql_inserts(void)
191 t_start("Testing hooks");
192 ok_int(test_sql_host_insert(), 0, "host check hook");
193 ok_int(test_sql_service_insert(), 0, "service check hook");
194 test_sql_downtime_insert();
195 ok_int(test_sql_process_data_insert(), 0, "process data hook");
196 t_end();
199 static nebmodule *neb;
200 static void test_one_module(char *arg, int test_sql)
202 static void *dso = NULL;
203 char *path;
204 int (*init_func)(int, const char *, nebmodule *);
205 int (*deinit_func)(int, int);
206 int (*log_grok_var)(const char *, const char *);
208 if (dso)
209 dlclose(dso);
211 if (strchr(arg, '/'))
212 path = arg;
213 else {
214 int len = strlen(arg);
215 path = calloc(len + 3, 1);
216 path[0] = '.';
217 path[1] = '/';
218 memcpy(path + 2, arg, len);
221 dso = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
222 t_start("Testing module '%s'", path);
223 if (!dso) {
224 t_fail("dlopen(%s) failed: %s", path, dlerror());
225 return;
228 t_pass("dlopen(%s) worked out fine", path);
229 log_grok_var = dlsym(dso, "log_grok_var");
230 if (log_grok_var) {
231 log_grok_var("log_file", "/dev/null");
233 t_start("Checking symbols in '%s'", path);
234 check_symbols(dso);
235 t_end();
237 if (test_sql) {
238 t_start("Testing sql inserts");
239 init_func = dlsym(dso, "nebmodule_init");
240 ok_int(init_func(-1, neb->args, neb), 0, "module init function");
242 test_sql_inserts();
244 deinit_func = dlsym(dso, "nebmodule_deinit");
245 ok_int(deinit_func(0, 0), 0, "module deinit function");
246 ok_int(reg_errors, 0, "registering callbacks");
247 ok_int(dereg_errors, 0, "deregistering callbacks");
248 check_callbacks();
249 t_end();
252 return;
255 int main(int argc, char **argv)
257 int i, test_sql = 0;
259 t_set_colors(0);
260 t_verbose = 1;
261 neb = calloc(sizeof(*neb), 1);
263 for (i = 1; i < argc; i++) {
264 char *arg = argv[i];
266 if (*arg == '-' && arg[1] == 'f' && i < argc - 1) {
267 neb->args = argv[++i];
268 continue;
270 if (!strcmp(arg, "--test-sql")) {
271 test_sql = 1;
272 continue;
275 test_one_module(arg, test_sql);
278 return t_end();