showlog: Remove downtime handling code
[nagios-reports-module.git] / test-lparse.c
blob62731aa3d3b89e0ed607389f212544da0cfe459c
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 size_t lines, empty;
25 int max_runtime;
26 } testfiles[] = {
27 { "logs/nuls.log", 20002, 8, 2 },
28 { "logs/no-lf-terminator.log", 1, 0, 1 },
29 { "logs/beta.log", 6114, 0, 2 },
30 { NULL },
33 static struct lparse_test *t;
35 void sighandler(int signum)
37 if (signum == SIGALRM) {
38 fprintf(stderr, "Failed to complete parsing %s in under %d seconds\n",
39 t->path, t->max_runtime);
41 exit(1);
44 static int lines, empty;
45 static unsigned long long bytes = 0;
46 static int check_line(char *str, size_t len)
48 if (!len)
49 empty++;
50 else
51 lines++;
52 bytes += len + 1;
53 return 0;
56 #define print_expected(a, b, what) \
57 printf("# expected %llu " what ", got %llu. delta %d\n", \
58 (unsigned long long)a, (unsigned long long)b, (int)(a - b));
60 int main(int argc, char **argv)
62 int i, passed = 0, failed = 0, use_alarm = 1;
63 const char *green, *cyan, *red, *yellow, *reset;
65 if (isatty(fileno(stdout))) {
66 green = CLR_GREEN;
67 red = CLR_RED;
68 yellow = CLR_YELLOW;
69 cyan = CLR_CYAN;
70 reset = CLR_RESET;
71 } else {
72 green = red = yellow = cyan = reset = "";
75 signal(SIGALRM, sighandler);
77 for (i = 1; i < argc; i++) {
78 if (!strcmp(argv[i], "--no-alarm"))
79 use_alarm = 0;
82 for (i = 0; testfiles[i].path; i++) {
83 struct stat st;
85 t = &testfiles[i];
87 if (stat(testfiles[i].path, &st) < 0) {
88 fprintf(stderr, "Failed to stat '%s': %s\n", t->path, strerror(errno));
89 exit(1);
92 if (use_alarm && t->max_runtime)
93 alarm(t->max_runtime);
94 lparse_path(t->path, st.st_size, check_line);
95 if (use_alarm && t->max_runtime)
96 alarm(0);
98 if (lines == t->lines && t->empty == empty && bytes == st.st_size) {
99 passed++;
100 printf("%sPASS%s %s\n", green, reset, t->path);
101 } else {
102 if (lines == t->lines)
103 printf("%spass%s %s\n", yellow, reset, t->path);
104 else {
105 failed++;
106 printf("%sFAIL%s %s\n", red, reset, t->path);
108 if (lines != t->lines)
109 print_expected(t->lines, lines, "lines");
110 if (empty != t->empty)
111 print_expected(t->empty, empty, "empty lines");
112 if (st.st_size != bytes)
113 print_expected(st.st_size, bytes, "bytes");
116 lines = bytes = empty = 0;
119 if (!failed)
120 return 0;
122 return 1;