showlog: Support printing downtime and flapping alerts
[nagios-reports-module.git] / test-lparse.c
blob8b6d51418ea23c9eb81a62aafc80f7ab30801bc9
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 "lparse.h"
11 #define CLR_RESET "\033[m"
12 #define CLR_BOLD "\033[1m"
13 #define CLR_RED "\033[31m"
14 #define CLR_GREEN "\033[32m"
15 #define CLR_BROWN "\033[33m"
16 #define CLR_YELLOW "\033[33m\033[1m"
17 #define CLR_BLUE "\033[34m"
18 #define CLR_MAGENTA "\033[35m"
19 #define CLR_CYAN "\033[36m"
20 #define CLR_BG_RED "\033[41m"
22 static struct lparse_test {
23 char *path;
24 uint lines, empty;
25 int max_runtime;
26 } testfiles[] = {
27 { "logs/no-lf-terminator.log", 1, 0, 1 },
28 { "logs/single-read.log", 1, 0, 1 },
29 { "logs/beta.log", 6114, 0, 2 },
30 { "logs/nuls.log", 20002, 8, 2 },
31 { NULL },
34 static struct lparse_test *t;
36 void sighandler(int signum)
38 if (signum == SIGALRM) {
39 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
40 t->path, t->max_runtime);
42 exit(1);
45 static int lines, empty;
46 static unsigned long long bytes = 0;
47 static int check_line(char *str, uint len)
49 if (!len)
50 empty++;
51 else
52 lines++;
53 bytes += len + 1;
54 return 0;
57 #define print_expected(a, b, what) \
58 printf("# expected %llu " what ", got %llu. delta %d\n", \
59 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
62 static const char *green, *cyan, *red, *yellow, *reset;
63 static int passed, failed, use_alarm = 1;
65 static void test_one(int rev, struct lparse_test *t, struct stat *st)
67 if (use_alarm && t->max_runtime)
68 alarm(t->max_runtime);
69 lparse_path_real(rev, t->path, st->st_size, check_line);
70 if (use_alarm && t->max_runtime)
71 alarm(0);
73 if (lines == t->lines && t->empty == empty && bytes == st->st_size) {
74 passed++;
75 printf("%sPASS%s %s\n", green, reset, t->path);
76 } else {
77 if (lines == t->lines)
78 printf("%spass%s %s\n", yellow, reset, t->path);
79 else {
80 failed++;
81 printf("%sFAIL%s %s\n", red, reset, t->path);
83 if (lines != t->lines)
84 print_expected(t->lines, lines, "lines");
85 if (empty != t->empty)
86 print_expected(t->empty, empty, "empty lines");
87 if (st->st_size != bytes)
88 print_expected(st->st_size, bytes, "bytes");
91 lines = bytes = empty = 0;
94 static void test_all(int reverse, const char *msg)
96 int i;
98 printf("%s%s%s\n", cyan, msg, reset);
99 for (i = 0; testfiles[i].path; i++) {
100 struct stat st;
102 t = &testfiles[i];
104 if (stat(testfiles[i].path, &st) < 0) {
105 fprintf(stderr, "Failed to stat '%s': %s\n", t->path, strerror(errno));
106 exit(1);
109 test_one(reverse, t, &st);
113 int main(int argc, char **argv)
115 int i;
117 if (isatty(fileno(stdout))) {
118 green = CLR_GREEN;
119 red = CLR_RED;
120 yellow = CLR_YELLOW;
121 cyan = CLR_CYAN;
122 reset = CLR_RESET;
123 } else {
124 green = red = yellow = cyan = reset = "";
127 signal(SIGALRM, sighandler);
129 for (i = 1; i < argc; i++) {
130 if (!strcmp(argv[i], "--no-alarm"))
131 use_alarm = 0;
134 test_all(0, "testing forward parsing");
135 test_all(1, "testing reverse parsing");
137 if (!failed)
138 return 0;
140 return 1;