Remove --ignore-process-events
[nagios-reports-module.git] / test-lparse.c
blob738a8a595fc869c398e5fee7a6fa62c26762d71d
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"
12 #define CLR_RESET "\033[m"
13 #define CLR_BOLD "\033[1m"
14 #define CLR_RED "\033[31m"
15 #define CLR_GREEN "\033[32m"
16 #define CLR_BROWN "\033[33m"
17 #define CLR_YELLOW "\033[33m\033[1m"
18 #define CLR_BLUE "\033[34m"
19 #define CLR_MAGENTA "\033[35m"
20 #define CLR_CYAN "\033[36m"
21 #define CLR_BG_RED "\033[41m"
23 static struct lparse_test *lpt;
25 static struct lparse_test {
26 char *path;
27 uint lines, empty;
28 int max_runtime;
29 } testfiles[] = {
30 { "logs/no-lf-terminator.log", 1, 0, 1 },
31 { "logs/single-read.log", 1, 0, 1 },
32 { "logs/beta.log", 6114, 0, 2 },
33 { "logs/nuls.log", 20002, 8, 2 },
34 { NULL },
37 void sighandler(int signum)
39 if (signum == SIGALRM) {
40 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
41 lpt->path, lpt->max_runtime);
43 exit(1);
46 static int lines, empty;
47 static unsigned long long bytes = 0;
48 static int check_line(char *str, uint len)
50 if (!len)
51 empty++;
52 else
53 lines++;
54 bytes += len + 1;
55 return 0;
58 #define print_expected(a, b, what) \
59 printf("# expected %llu " what ", got %llu. delta %d\n", \
60 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
63 static const char *green, *cyan, *red, *yellow, *reset;
64 static int passed, failed, use_alarm = 1;
66 static void test_one(int rev, struct lparse_test *t, struct stat *st)
68 if (use_alarm && t->max_runtime)
69 alarm(t->max_runtime);
70 lparse_path_real(rev, t->path, st->st_size, check_line);
71 if (use_alarm && t->max_runtime)
72 alarm(0);
74 if (lines == t->lines && t->empty == empty && bytes == st->st_size) {
75 passed++;
76 printf("%sPASS%s %s\n", green, reset, t->path);
77 } else {
78 if (lines == t->lines)
79 printf("%spass%s %s\n", yellow, reset, t->path);
80 else {
81 failed++;
82 printf("%sFAIL%s %s\n", red, reset, t->path);
84 if (lines != t->lines)
85 print_expected(t->lines, lines, "lines");
86 if (empty != t->empty)
87 print_expected(t->empty, empty, "empty lines");
88 if (st->st_size != bytes)
89 print_expected(st->st_size, bytes, "bytes");
92 lines = bytes = empty = 0;
95 static void test_all(int reverse, const char *msg)
97 int i;
99 printf("%s%s%s\n", cyan, msg, reset);
100 for (i = 0; testfiles[i].path; i++) {
101 struct stat st;
103 lpt = &testfiles[i];
105 if (stat(lpt->path, &st) < 0) {
106 fprintf(stderr, "Failed to stat '%s': %s\n", lpt->path, strerror(errno));
107 exit(1);
110 test_one(reverse, lpt, &st);
114 static struct path_cmp_test {
115 char *path;
116 uint correct;
117 } testpaths[] = {
118 { "nagios-12-01-2002-00.log", 2002120100 },
119 { "nagios-11-30-2006-01.log", 2006113001 },
120 { "nagios-08-01-2009-00.log", 2009080100 },
121 { "nagios-12-30-2147-99.log", 2147123099 },
122 /* nagios.log is a special case */
123 { "nagios.log", 1 << ((8 * (sizeof(int))) - 1) },
124 { NULL },
126 static void test_path_cmp(void)
128 int i;
130 printf("%stesting path comparison%s\n", cyan, reset);
131 for (i = 0; testpaths[i].path; i++) {
132 struct path_cmp_test *t;
133 uint cmp;
135 t = &testpaths[i];
136 cmp = path_cmp_number(t->path);
137 if (cmp == t->correct) {
138 printf("%sPASS%s %s parses to %u\n", green, reset, t->path, cmp);
139 passed++;
140 continue;
143 failed++;
144 printf("%sFAIL%s %s should be %u, got %u\n",
145 red, reset, t->path, t->correct, cmp);
149 int main(int argc, char **argv)
151 int i;
153 if (isatty(fileno(stdout))) {
154 green = CLR_GREEN;
155 red = CLR_RED;
156 yellow = CLR_YELLOW;
157 cyan = CLR_CYAN;
158 reset = CLR_RESET;
159 } else {
160 green = red = yellow = cyan = reset = "";
163 signal(SIGALRM, sighandler);
165 for (i = 1; i < argc; i++) {
166 if (!strcmp(argv[i], "--no-alarm"))
167 use_alarm = 0;
170 test_all(0, "testing forward parsing");
171 test_all(1, "testing reverse parsing");
172 test_path_cmp();
174 if (!failed)
175 return 0;
177 return 1;