Makefile: Add target to run sql-dependant tests
[nagios-reports-module.git] / test_utils.c
blob053e1a98c2cd91a365d28fcd9b265d432fd1170c
1 #include <stdlib.h>
2 #include "nagios/objects.h"
3 #include "nagios/nagios.h"
4 #include "logutils.h" /* for colors */
5 #include "test_utils.h"
7 /*
8 * These only exist so dt_depth_*() functions
9 * in utils.c can fail gracefully
11 host *find_host(char *h)
13 return NULL;
16 service *find_service(char *h, char *s)
18 return NULL;
21 host *host_list = NULL, *host_list_tail = NULL;
22 service *service_list = NULL, *service_list_tail = NULL;
23 sched_info scheduling_info = { 0 };
26 /* test helper functions */
27 const char *cyan = "", *red = "", *green = "", *yellow = "", *reset = "";
28 uint passed, failed, t_verbose = 0;
29 static uint t_depth;
30 static const char *indent_str = " ";
32 void t_set_colors(int force)
34 if (force == 1 || isatty(fileno(stdout))) {
35 cyan = CLR_CYAN;
36 red = CLR_RED;
37 yellow = CLR_YELLOW;
38 green = CLR_GREEN;
39 reset = CLR_RESET;
43 static void t_indent(int depth)
45 uint i;
46 for (i = 0; i < depth; i++) {
47 printf("%s", indent_str);
51 void t_start(const char *fmt, ...)
53 va_list ap;
55 t_indent(t_depth++);
56 va_start(ap, fmt);
57 printf("%s### ", cyan);
58 vfprintf(stdout, fmt, ap);
59 printf("%s\n", reset);
60 va_end(ap);
63 int t_end(void)
65 if (t_depth)
66 t_depth--;
67 if (!t_depth || failed) {
68 t_indent(t_depth);
69 printf("Test results: %s%u passed%s, %s%u failed%s\n",
70 green, passed, reset, failed ? red : "", failed, failed ? reset : "");
72 return failed ? EXIT_FAILURE : EXIT_SUCCESS;
75 void t_pass(const char *fmt, ...)
77 va_list ap;
79 passed++;
80 if (!t_verbose)
81 return;
82 t_indent(t_depth);
83 printf("%sPASS%s ", green, reset);
84 va_start(ap, fmt);
85 vfprintf(stdout, fmt, ap);
86 va_end(ap);
87 putchar('\n');
90 void t_fail(const char *fmt, ...)
92 va_list ap;
94 failed++;
95 t_indent(t_depth);
96 printf("%sFAIL%s ", red, reset);
97 va_start(ap, fmt);
98 vfprintf(stdout, fmt, ap);
99 va_end(ap);
100 putchar('\n');
103 void t_diag(const char *fmt, ...)
105 if (fmt) {
106 va_list ap;
107 t_indent(t_depth + 1);
108 va_start(ap, fmt);
109 vfprintf(stdout, fmt, ap);
110 va_end(ap);
111 putchar('\n');
115 int ok_int(int a, int b, const char *name)
117 if (a == b) {
118 t_pass(name);
119 return TEST_PASS;
122 t_fail(name);
123 t_diag("%d != %d", a, b);
124 return TEST_FAIL;
127 int ok_uint(uint a, uint b, const char *name)
129 if (a == b) {
130 return TEST_PASS;
131 t_pass(name);
134 t_fail(name);
135 t_diag("%u != %d", a, b);
136 return TEST_FAIL;
139 int ok_str(const char *a, const char *b, const char *name)
141 if ((!a && !b) || (a && b && !strcmp(a, b))) {
142 t_pass(name);
143 return TEST_PASS;
146 t_fail(name);
147 t_diag("'%s' != '%s'", a, b);
148 return TEST_FAIL;