Makefile: Add target to run sql-dependant tests
[nagios-reports-module.git] / test-lparse.c
blob003e6c98e944d72b2a4edd23107cc7e9efa7d21f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <strings.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <signal.h>
8 #include <unistd.h>
9 #include "logutils.h"
10 #include "lparse.h"
11 #include "test_utils.h"
13 static struct lparse_test *lpt;
15 static struct lparse_test {
16 char *path;
17 uint lines, empty;
18 int max_runtime;
19 int byte_delta;
20 } testfiles[] = {
21 { "logs/no-lf-terminator.log", 1, 0, 1, -1 },
22 { "logs/single-read.log", 1, 0, 1, 0 },
23 { "logs/beta.log", 6114, 0, 2, 0 },
24 { "logs/nuls.log", 20002, 8, 2, 0 },
25 { NULL },
28 void sighandler(int signum)
30 if (signum == SIGALRM) {
31 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
32 lpt->path, lpt->max_runtime);
34 exit(1);
37 static int lines, empty;
38 static unsigned long long bytes = 0;
39 static int check_line(char *str, uint len)
41 if (!len)
42 empty++;
43 else
44 lines++;
45 bytes += len + 1;
46 return 0;
49 #define print_expected(a, b, what) \
50 printf("# expected %llu " what ", got %llu. delta %d\n", \
51 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
54 static int use_alarm = 1;
56 static void test_one(int rev, struct lparse_test *t, struct stat *st)
58 lines = bytes = empty = 0;
60 if (use_alarm && t->max_runtime)
61 alarm(t->max_runtime);
62 lparse_path_real(rev, t->path, st->st_size, check_line);
63 if (use_alarm && t->max_runtime)
64 alarm(0);
66 if (lines == t->lines && t->empty == empty) {
67 if (bytes == st->st_size || st->st_size - bytes == t->byte_delta) {
68 t_pass("%s", t->path);
69 return;
73 if (lines == t->lines)
74 printf("%spass%s %s\n", yellow, reset, t->path);
75 else {
76 failed++;
77 printf("%sFAIL%s %s\n", red, reset, t->path);
79 if (lines != t->lines)
80 print_expected(t->lines, lines, "lines");
81 if (empty != t->empty)
82 print_expected(t->empty, empty, "empty lines");
83 if (st->st_size != bytes && st->st_size - bytes != t->byte_delta)
84 print_expected(st->st_size, bytes, "bytes");
87 static void test_all(int reverse, const char *msg)
89 int i;
91 t_start("%s", msg);
92 for (i = 0; testfiles[i].path; i++) {
93 struct stat st;
95 lpt = &testfiles[i];
97 if (stat(lpt->path, &st) < 0) {
98 fprintf(stderr, "Failed to stat '%s': %s\n", lpt->path, strerror(errno));
99 exit(1);
102 test_one(reverse, lpt, &st);
104 t_end();
107 static struct path_cmp_test {
108 char *path;
109 uint correct;
110 } testpaths[] = {
111 { "nagios-12-01-2002-00.log", 2002120100 },
112 { "nagios-11-30-2006-01.log", 2006113001 },
113 { "nagios-08-01-2009-00.log", 2009080100 },
114 { "nagios-12-30-2147-99.log", 2147123099 },
115 /* nagios.log is a special case, as is all logfiles without a dash */
116 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
117 { "foo.log", 0 },
118 { NULL },
120 static void test_path_cmp(void)
122 int i;
124 t_start("testing path comparison");
125 for (i = 0; testpaths[i].path; i++) {
126 struct path_cmp_test *t;
127 uint cmp;
129 t = &testpaths[i];
130 cmp = path_cmp_number(t->path);
131 if (cmp == t->correct) {
132 t_pass("%s parses to %u", t->path, cmp);
133 } else {
134 t_fail("%s should be %u, got %u", t->path, t->correct, cmp);
137 t_end();
140 int main(int argc, char **argv)
142 int i;
144 t_set_colors(0);
145 t_verbose = 1;
147 signal(SIGALRM, sighandler);
149 for (i = 1; i < argc; i++) {
150 if (!strcmp(argv[i], "--no-alarm"))
151 use_alarm = 0;
154 t_start("testing logfile parsing and sorting");
155 test_all(0, "testing forward parsing");
156 test_all(1, "testing reverse parsing");
157 test_path_cmp();
158 return t_end();