summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_generic_output.c
blob: e67aefc9941c8c8e047f7b37c7c469754fdbbfc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "../lib/output.h"
#include "../../tap/tap.h"
#include "./states.h"

#include <string.h>

void test_one_subcheck(void);
void test_two_subchecks(void);

void test_perfdata_formatting(void);
void test_perfdata_formatting2(void);

void test_deep_check_hierarchy(void);
void test_deep_check_hierarchy2(void);

void test_default_states1(void);
void test_default_states2(void);

int main(void) {
	plan_tests(19);

	diag("Simple test with one subcheck");
	test_one_subcheck();

	diag("Test with two subchecks");
	test_two_subchecks();

	diag("Test for performance data formatting");
	test_perfdata_formatting();

	diag("Another test for performance data formatting");
	test_perfdata_formatting2();

	diag("Test for deeper hierarchies");
	test_deep_check_hierarchy();

	diag("Another test for deeper hierarchies");
	test_deep_check_hierarchy2();

	diag("Testing the default state logic");
	test_default_states1();

	diag("Testing the default state logic #2");
	test_default_states2();

	return exit_status();
}

void test_one_subcheck(void) {
	mp_subcheck sc1 = mp_subcheck_init();

	sc1.output = "foobar";
	sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);

	mp_check check = mp_check_init();
	mp_add_subcheck_to_check(&check, sc1);

	ok(mp_compute_check_state(check) == STATE_WARNING, "Main state should be warning");

	char *output = mp_fmt_output(check);

	// diag("Formatted output");
	// diag(output);

	char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n"
					  "\t\\_[WARNING] - foobar\n";

	// diag("Expected output");
	// diag(expected);

	ok(strcmp(output, expected) == 0, "Simple output test");
}

void test_perfdata_formatting2(void) {
	mp_perfdata pd1 = perfdata_init();
	mp_perfdata pd2 = perfdata_init();

	pd1.label = "foo";
	pd2.label = "bar";

	pd1 = mp_set_pd_value(pd1, 23);
	pd2 = mp_set_pd_value(pd2, 1LL);

	pd_list *tmp = pd_list_init();

	pd_list_append(tmp, pd1);
	pd_list_append(tmp, pd2);

	char *result = pd_list_to_string(*tmp);

	ok(strcmp(result, "foo=23;;; bar=1;;;") == 0, "Perfdata string formatting");
}

void test_perfdata_formatting(void) {
	mp_perfdata pd1 = perfdata_init();

	pd1.uom = "s";
	pd1.label = "foo";

	pd1 = mp_set_pd_value(pd1, 23);

	char *pd_string = pd_to_string(pd1);

	ok(strcmp(pd_string, "foo=23s;;;") == 0, "Perfdata string formatting");
}

void test_two_subchecks(void) {
	mp_subcheck sc1 = mp_subcheck_init();

	sc1.output = "foobar";
	sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);

	ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, "Test subcheck state directly after setting it");

	mp_perfdata pd1 = perfdata_init();

	pd1 = mp_set_pd_value(pd1, 23);

	pd1.uom = "s";
	pd1.label = "foo";

	mp_add_perfdata_to_subcheck(&sc1, pd1);

	mp_subcheck sc2 = mp_subcheck_init();
	sc2.output = "baz";
	sc2 = mp_set_subcheck_state(sc2, STATE_OK);

	ok(mp_compute_subcheck_state(sc2) == STATE_OK, "Test subcheck 2 state after setting it");

	mp_add_subcheck_to_subcheck(&sc1, sc2);

	ok(mp_compute_subcheck_state(sc1) == STATE_WARNING, "Test subcheck state after adding a subcheck");

	mp_check check = mp_check_init();
	mp_add_subcheck_to_check(&check, sc1);

	ok(mp_compute_check_state(check) == STATE_WARNING, "Test main check result");

	char *output = mp_fmt_output(check);

	// diag("Formatted output. Length: %u", strlen(output));
	// diag(output);

	ok(output != NULL, "Output should not be NULL");

	char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n"
					  "\t\\_[WARNING] - foobar\n"
					  "\t\t\\_[OK] - baz\n"
					  "|foo=23s;;; \n";

	// diag("Expected output. Length: %u", strlen(expected));
	// diag(expected);

	ok(strcmp(output, expected) == 0, "Output is as expected");
}

void test_deep_check_hierarchy(void) {
	// level 4
	mp_subcheck sc4 = mp_subcheck_init();
	sc4.output = "level4";
	sc4 = mp_set_subcheck_state(sc4, STATE_OK);

	// level 3
	mp_subcheck sc3 = mp_subcheck_init();
	sc3.output = "level3";
	sc3 = mp_set_subcheck_state(sc3, STATE_OK);

	// level 2
	mp_subcheck sc2 = mp_subcheck_init();
	sc2.output = "baz";
	sc2 = mp_set_subcheck_state(sc2, STATE_OK);

	// level 1
	mp_subcheck sc1 = mp_subcheck_init();

	sc1.output = "foobar";
	sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);

	mp_perfdata pd1 = perfdata_init();

	pd1.uom = "s";
	pd1.label = "foo";
	pd1 = mp_set_pd_value(pd1, 23);

	mp_add_perfdata_to_subcheck(&sc1, pd1);

	// main check
	mp_check check = mp_check_init();

	mp_add_subcheck_to_subcheck(&sc3, sc4);
	mp_add_subcheck_to_subcheck(&sc2, sc3);
	mp_add_subcheck_to_subcheck(&sc1, sc2);
	mp_add_subcheck_to_check(&check, sc1);

	char *output = mp_fmt_output(check);

	size_t output_length = strlen(output);

	// diag("Formatted output of length %i", output_length);
	// diag(output);

	ok(output != NULL, "Output should not be NULL");

	char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n"
					  "\t\\_[WARNING] - foobar\n"
					  "\t\t\\_[OK] - baz\n"
					  "\t\t\t\\_[OK] - level3\n"
					  "\t\t\t\t\\_[OK] - level4\n"
					  "|foo=23s;;; \n";

	size_t expected_length = strlen(expected);

	// diag("Expected output of length: %i", expected_length);
	// diag(expected);

	ok(output_length == expected_length, "Outputs are of equal length");
	ok(strcmp(output, expected) == 0, "Output is as expected");
}

void test_deep_check_hierarchy2(void) {
	// level 1
	mp_subcheck sc1 = mp_subcheck_init();

	sc1.output = "foobar";
	sc1 = mp_set_subcheck_state(sc1, STATE_WARNING);

	mp_perfdata pd1 = perfdata_init();
	pd1.uom = "s";
	pd1.label = "foo";
	pd1 = mp_set_pd_value(pd1, 23);

	mp_add_perfdata_to_subcheck(&sc1, pd1);

	// level 2
	mp_subcheck sc2 = mp_subcheck_init();
	sc2.output = "baz";
	sc2 = mp_set_subcheck_state(sc2, STATE_OK);

	mp_perfdata pd2 = perfdata_init();
	pd2.uom = "B";
	pd2.label = "baz";
	pd2 = mp_set_pd_value(pd2, 1024);
	mp_add_perfdata_to_subcheck(&sc2, pd2);

	// level 3
	mp_subcheck sc3 = mp_subcheck_init();
	sc3.output = "level3";
	sc3 = mp_set_subcheck_state(sc3, STATE_OK);

	mp_perfdata pd3 = perfdata_init();
	pd3.label = "floatMe";
	pd3 = mp_set_pd_value(pd3, 1024.1024);
	mp_add_perfdata_to_subcheck(&sc3, pd3);

	// level 4
	mp_subcheck sc4 = mp_subcheck_init();
	sc4.output = "level4";
	sc4 = mp_set_subcheck_state(sc4, STATE_OK);

	mp_check check = mp_check_init();

	mp_add_subcheck_to_subcheck(&sc3, sc4);
	mp_add_subcheck_to_subcheck(&sc2, sc3);
	mp_add_subcheck_to_subcheck(&sc1, sc2);
	mp_add_subcheck_to_check(&check, sc1);

	char *output = mp_fmt_output(check);

	// diag("Formatted output of length: %i", strlen(output));
	// diag(output);

	ok(output != NULL, "Output should not be NULL");

	char expected[] = "[WARNING] - ok=0, warning=1, critical=0, unknown=0\n"
					  "\t\\_[WARNING] - foobar\n"
					  "\t\t\\_[OK] - baz\n"
					  "\t\t\t\\_[OK] - level3\n"
					  "\t\t\t\t\\_[OK] - level4\n"
					  "|foo=23s;;; baz=1024B;;; floatMe=1024.102400;;; \n";

	// diag("Expected output of length: %i", strlen(expected));
	// diag(expected);

	ok(strcmp(output, expected) == 0, "Output is as expected");
}

void test_default_states1(void) {
	mp_subcheck sc = mp_subcheck_init();

	mp_state_enum state1 = mp_compute_subcheck_state(sc);
	ok(state1 == STATE_UNKNOWN, "Default default state is Unknown");

	sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL);

	mp_state_enum state2 = mp_compute_subcheck_state(sc);
	ok(state2 == STATE_CRITICAL, "Default state is Critical");

	sc = mp_set_subcheck_state(sc, STATE_OK);

	mp_state_enum state3 = mp_compute_subcheck_state(sc);
	ok(state3 == STATE_OK, "Default state is Critical");
}

void test_default_states2(void) {
	mp_check check = mp_check_init();

	mp_subcheck sc = mp_subcheck_init();
	sc.output = "placeholder";
	sc = mp_set_subcheck_default_state(sc, STATE_CRITICAL);

	mp_add_subcheck_to_check(&check, sc);

	mp_state_enum result_state = mp_compute_check_state(check);
	ok(result_state == STATE_CRITICAL, "Derived state is the proper default state");
}