Debian Codegraph @

src/logparser_test.c

raw
1 /**
2 * collectd - src/logparser_test.c
3 *
4 * Copyright(c) 2018 Intel Corporation. All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Authors:
25 * Marcin Mozejko <[email protected]>
26 * Adrian Boczkowski <[email protected]>
27 **/
28
29 #include "logparser.c"
30 #include "testing.h"
31
32 DEF_TEST(init) {
33 logparser_ctx.parsers_len = 3;
34 logparser_ctx.parsers = calloc(3, sizeof(log_parser_t));
35
36 logparser_ctx.parsers[0].filename = "test_filename0";
37 logparser_ctx.parsers[0].patterns_len = 3;
38 logparser_ctx.parsers[0].patterns = calloc(3, sizeof(message_pattern_t));
39 logparser_ctx.parsers[0].patterns[0].regex = "test_regex00";
40 logparser_ctx.parsers[0].patterns[0].excluderegex = "exclude_regex00";
41 logparser_ctx.parsers[0].patterns[0].is_mandatory = true;
42 logparser_ctx.parsers[0].patterns[0].submatch_idx = 1;
43 logparser_ctx.parsers[0].patterns[0].name = "test_name00";
44 logparser_ctx.parsers[0].patterns[1].regex = "test_regex01";
45 logparser_ctx.parsers[0].patterns[1].excluderegex = "exclude_regex01";
46 logparser_ctx.parsers[0].patterns[1].is_mandatory = true;
47 logparser_ctx.parsers[0].patterns[1].submatch_idx = 1;
48 logparser_ctx.parsers[0].patterns[1].name = "test_name01";
49 logparser_ctx.parsers[0].patterns[2].regex = "test_regex02";
50 logparser_ctx.parsers[0].patterns[2].excluderegex = "exclude_regex02";
51 logparser_ctx.parsers[0].patterns[2].is_mandatory = true;
52 logparser_ctx.parsers[0].patterns[2].submatch_idx = 1;
53 logparser_ctx.parsers[0].patterns[2].name = "test_name02";
54
55 logparser_ctx.parsers[1].filename = "test_filename1";
56 logparser_ctx.parsers[1].patterns_len = 3;
57 logparser_ctx.parsers[1].patterns = calloc(3, sizeof(message_pattern_t));
58 logparser_ctx.parsers[1].patterns[0].regex = "test_regex10";
59 logparser_ctx.parsers[1].patterns[0].excluderegex = "exclude_regex10";
60 logparser_ctx.parsers[1].patterns[0].is_mandatory = true;
61 logparser_ctx.parsers[1].patterns[0].submatch_idx = 1;
62 logparser_ctx.parsers[1].patterns[0].name = "test_name10";
63 logparser_ctx.parsers[1].patterns[1].regex = "test_regex11";
64 logparser_ctx.parsers[1].patterns[1].excluderegex = "exclude_regex11";
65 logparser_ctx.parsers[1].patterns[1].is_mandatory = true;
66 logparser_ctx.parsers[1].patterns[1].submatch_idx = 1;
67 logparser_ctx.parsers[1].patterns[1].name = "test_name11";
68 logparser_ctx.parsers[1].patterns[2].regex = "test_regex12";
69 logparser_ctx.parsers[1].patterns[2].excluderegex = "exclude_regex12";
70 logparser_ctx.parsers[1].patterns[2].is_mandatory = true;
71 logparser_ctx.parsers[1].patterns[2].submatch_idx = 1;
72 logparser_ctx.parsers[1].patterns[2].name = "test_name12";
73
74 logparser_ctx.parsers[2].filename = "test_filename2";
75 logparser_ctx.parsers[2].patterns_len = 3;
76 logparser_ctx.parsers[2].patterns = calloc(3, sizeof(message_pattern_t));
77 logparser_ctx.parsers[2].patterns[0].regex = "test_regex20";
78 logparser_ctx.parsers[2].patterns[0].excluderegex = "exclude_regex20";
79 logparser_ctx.parsers[2].patterns[0].is_mandatory = true;
80 logparser_ctx.parsers[2].patterns[0].submatch_idx = 1;
81 logparser_ctx.parsers[2].patterns[0].name = "test_name20";
82 logparser_ctx.parsers[2].patterns[1].regex = "test_regex21";
83 logparser_ctx.parsers[2].patterns[1].excluderegex = "exclude_regex21";
84 logparser_ctx.parsers[2].patterns[1].is_mandatory = true;
85 logparser_ctx.parsers[2].patterns[1].submatch_idx = 1;
86 logparser_ctx.parsers[2].patterns[1].name = "test_name21";
87 logparser_ctx.parsers[2].patterns[2].regex = "test_regex22";
88 logparser_ctx.parsers[2].patterns[2].excluderegex = "exclude_regex22";
89 logparser_ctx.parsers[2].patterns[2].is_mandatory = true;
90 logparser_ctx.parsers[2].patterns[2].submatch_idx = 1;
91 logparser_ctx.parsers[2].patterns[2].name = "test_name22";
92
93 int ret = logparser_init();
94 sfree(logparser_ctx.parsers[0].patterns);
95 sfree(logparser_ctx.parsers[1].patterns);
96 sfree(logparser_ctx.parsers[2].patterns);
97 message_parser_cleanup(logparser_ctx.parsers[0].job);
98 message_parser_cleanup(logparser_ctx.parsers[1].job);
99 message_parser_cleanup(logparser_ctx.parsers[2].job);
100 sfree(logparser_ctx.parsers);
101 logparser_ctx.parsers_len = 0;
102 EXPECT_EQ_INT(0, ret);
103
104 return 0;
105 }
106
107 DEF_TEST(free_user_data) {
108 message_item_user_data_t *user_data = calloc(1, sizeof(*user_data));
109
110 user_data->infos_len = 4;
111 user_data->infos[0].type = MSG_ITEM_SEVERITY;
112 user_data->infos[0].val.severity = NOTIF_OKAY;
113 user_data->infos[1].type = MSG_ITEM_PLUGIN_INST;
114 user_data->infos[1].val.str_override = strdup("test_plugin_inst");
115 user_data->infos[2].type = MSG_ITEM_TYPE;
116 user_data->infos[2].val.str_override = strdup("test_type");
117 user_data->infos[3].type = MSG_ITEM_TYPE_INST;
118 user_data->infos[3].val.str_override = strdup("test_type_inst");
119 logparser_free_user_data(user_data);
120
121 return 0;
122 }
123
124 DEF_TEST(config_logfile) {
125 oconfig_item_t *logfile_ci = calloc(1, sizeof(*logfile_ci));
126 assert(logfile_ci != NULL);
127 logfile_ci->key = "Logfile";
128
129 logfile_ci->values_num = 1;
130 logfile_ci->values =
131 calloc(logfile_ci->values_num, sizeof(*logfile_ci->values));
132 assert(logfile_ci->values != NULL);
133 logfile_ci->values->type = OCONFIG_TYPE_STRING;
134 logfile_ci->values->value.string = "/path/to/a/file";
135
136 logfile_ci->children_num = 2;
137 logfile_ci->children =
138 calloc(logfile_ci->children_num, sizeof(*logfile_ci->children));
139 assert(logfile_ci->children != NULL);
140
141 oconfig_item_t *first_full_read_ci = &logfile_ci->children[0];
142 first_full_read_ci->key = "FirstFullRead";
143
144 first_full_read_ci->values_num = 1;
145 first_full_read_ci->values = calloc(first_full_read_ci->values_num,
146 sizeof(*first_full_read_ci->values));
147 assert(first_full_read_ci->values != NULL);
148 first_full_read_ci->values->type = OCONFIG_TYPE_BOOLEAN;
149 first_full_read_ci->values->value.boolean = true;
150
151 oconfig_item_t *msg_ci = &logfile_ci->children[1];
152 msg_ci->key = "Message";
153
154 msg_ci->values_num = 1;
155 msg_ci->values = calloc(msg_ci->values_num, sizeof(*msg_ci->values));
156 assert(msg_ci->values != NULL);
157 msg_ci->values->type = OCONFIG_TYPE_STRING;
158 msg_ci->values->value.string = "msg-name";
159
160 msg_ci->children_num = 6;
161 msg_ci->children = calloc(msg_ci->children_num, sizeof(*msg_ci->children));
162 assert(msg_ci->children != NULL);
163
164 oconfig_item_t *def_plugin_inst_ci = &msg_ci->children[0];
165 def_plugin_inst_ci->key = "DefaultPluginInstance";
166 def_plugin_inst_ci->values_num = 1;
167 def_plugin_inst_ci->values = calloc(def_plugin_inst_ci->values_num,
168 sizeof(*def_plugin_inst_ci->values));
169 assert(def_plugin_inst_ci->values != NULL);
170 def_plugin_inst_ci->values->type = OCONFIG_TYPE_STRING;
171 def_plugin_inst_ci->values->value.string = "test_default_plugin_instance_1";
172
173 oconfig_item_t *def_type_ci = &msg_ci->children[1];
174 def_type_ci->key = "DefaultType";
175 def_type_ci->values_num = 1;
176 def_type_ci->values =
177 calloc(def_type_ci->values_num, sizeof(*def_type_ci->values));
178 assert(def_type_ci->values != NULL);
179 def_type_ci->values->type = OCONFIG_TYPE_STRING;
180 def_type_ci->values->value.string = "test_default_type_1";
181
182 oconfig_item_t *def_type_inst_ci = &msg_ci->children[2];
183 def_type_inst_ci->key = "DefaultTypeInstance";
184 def_type_inst_ci->values_num = 1;
185 def_type_inst_ci->values =
186 calloc(def_type_inst_ci->values_num, sizeof(*def_type_inst_ci->values));
187 assert(def_type_inst_ci->values != NULL);
188 def_type_inst_ci->values->type = OCONFIG_TYPE_STRING;
189 def_type_inst_ci->values->value.string = "test_default_type_instance_1";
190
191 oconfig_item_t *def_severity_ci = &msg_ci->children[3];
192 def_severity_ci->key = "DefaultSeverity";
193 def_severity_ci->values_num = 1;
194 def_severity_ci->values =
195 calloc(def_severity_ci->values_num, sizeof(*def_severity_ci->values));
196 assert(def_severity_ci->values != NULL);
197 def_severity_ci->values->type = OCONFIG_TYPE_STRING;
198 def_severity_ci->values->value.string = "OK";
199
200 oconfig_item_t *match1_ci = &msg_ci->children[4];
201 assert(match1_ci != NULL);
202 match1_ci->key = "Match";
203 match1_ci->values_num = 1;
204 match1_ci->values = calloc(match1_ci->values_num, sizeof(*match1_ci->values));
205 assert(match1_ci->values != NULL);
206 match1_ci->values->type = OCONFIG_TYPE_STRING;
207 match1_ci->values->value.string = "test_match_1";
208
209 match1_ci->children_num = 6;
210 match1_ci->children =
211 calloc(match1_ci->children_num, sizeof(*match1_ci->children));
212 assert(match1_ci->children != NULL);
213
214 oconfig_item_t *regex1_ci = &match1_ci->children[0];
215 regex1_ci->key = "Regex";
216 regex1_ci->values_num = 1;
217 regex1_ci->values = calloc(regex1_ci->values_num, sizeof(*regex1_ci->values));
218 assert(regex1_ci->values != NULL);
219 regex1_ci->values->type = OCONFIG_TYPE_STRING;
220 regex1_ci->values->value.string = "test_regex_1";
221
222 oconfig_item_t *submatch_idx1_ci = &match1_ci->children[1];
223 submatch_idx1_ci->key = "SubmatchIdx";
224 submatch_idx1_ci->values_num = 1;
225 submatch_idx1_ci->values =
226 calloc(submatch_idx1_ci->values_num, sizeof(*submatch_idx1_ci->values));
227 assert(submatch_idx1_ci->values != NULL);
228 submatch_idx1_ci->values->type = OCONFIG_TYPE_NUMBER;
229 submatch_idx1_ci->values->value.number = 15;
230
231 oconfig_item_t *is_mandatory1_ci = &match1_ci->children[2];
232 is_mandatory1_ci->key = "IsMandatory";
233 is_mandatory1_ci->values_num = 1;
234 is_mandatory1_ci->values =
235 calloc(is_mandatory1_ci->values_num, sizeof(*is_mandatory1_ci->values));
236 assert(is_mandatory1_ci->values != NULL);
237 is_mandatory1_ci->values->type = OCONFIG_TYPE_BOOLEAN;
238 is_mandatory1_ci->values->value.boolean = false;
239
240 oconfig_item_t *severity1_ci = &match1_ci->children[3];
241 severity1_ci->key = "Severity";
242 severity1_ci->values_num = 1;
243 severity1_ci->values =
244 calloc(severity1_ci->values_num, sizeof(*severity1_ci->values));
245 assert(severity1_ci->values != NULL);
246 severity1_ci->values->type = OCONFIG_TYPE_STRING;
247 severity1_ci->values->value.string = "failure";
248
249 oconfig_item_t *type_inst1_ci = &match1_ci->children[4];
250 type_inst1_ci->key = "TypeInstance";
251 type_inst1_ci->values_num = 1;
252 type_inst1_ci->values =
253 calloc(type_inst1_ci->values_num, sizeof(*type_inst1_ci->values));
254 assert(type_inst1_ci->values != NULL);
255 type_inst1_ci->values->type = OCONFIG_TYPE_STRING;
256 type_inst1_ci->values->value.string = "test_type_instance_1";
257
258 oconfig_item_t *plugin_inst1_ci = &match1_ci->children[5];
259 plugin_inst1_ci->key = "PluginInstance";
260 plugin_inst1_ci->values_num = 1;
261 plugin_inst1_ci->values =
262 calloc(plugin_inst1_ci->values_num, sizeof(*plugin_inst1_ci->values));
263 assert(plugin_inst1_ci->values != NULL);
264 plugin_inst1_ci->values->type = OCONFIG_TYPE_STRING;
265 plugin_inst1_ci->values->value.string = "test_plugin_instance_1";
266
267 oconfig_item_t *match2_ci = &msg_ci->children[5];
268 assert(match2_ci != NULL);
269 match2_ci->key = "Match";
270 match2_ci->values_num = 1;
271 match2_ci->values = calloc(match2_ci->values_num, sizeof(*match2_ci->values));
272 assert(match2_ci->values != NULL);
273 match2_ci->values->type = OCONFIG_TYPE_STRING;
274 match2_ci->values->value.string = "test_match_2";
275
276 match2_ci->children_num = 6;
277 match2_ci->children =
278 calloc(match2_ci->children_num, sizeof(*match2_ci->children));
279 assert(match2_ci->children != NULL);
280
281 oconfig_item_t *regex2_ci = &match2_ci->children[0];
282 regex2_ci->key = "Regex";
283 regex2_ci->values_num = 1;
284 regex2_ci->values = calloc(regex2_ci->values_num, sizeof(*regex2_ci->values));
285 assert(regex2_ci->values != NULL);
286 regex2_ci->values->type = OCONFIG_TYPE_STRING;
287 regex2_ci->values->value.string = "test_regex_2";
288
289 oconfig_item_t *submatch_idx2_ci = &match2_ci->children[1];
290 submatch_idx2_ci->key = "SubmatchIdx";
291 submatch_idx2_ci->values_num = 1;
292 submatch_idx2_ci->values =
293 calloc(submatch_idx2_ci->values_num, sizeof(*submatch_idx2_ci->values));
294 assert(submatch_idx2_ci->values != NULL);
295 submatch_idx2_ci->values->type = OCONFIG_TYPE_NUMBER;
296 submatch_idx2_ci->values->value.number = 8;
297
298 oconfig_item_t *is_mandatory2_ci = &match2_ci->children[2];
299 is_mandatory2_ci->key = "IsMandatory";
300 is_mandatory2_ci->values_num = 1;
301 is_mandatory2_ci->values =
302 calloc(is_mandatory2_ci->values_num, sizeof(*is_mandatory2_ci->values));
303 assert(is_mandatory2_ci->values != NULL);
304 is_mandatory2_ci->values->type = OCONFIG_TYPE_BOOLEAN;
305 is_mandatory2_ci->values->value.boolean = true;
306
307 oconfig_item_t *severity2_ci = &match2_ci->children[3];
308 severity2_ci->key = "Severity";
309 severity2_ci->values_num = 1;
310 severity2_ci->values =
311 calloc(severity2_ci->values_num, sizeof(*severity2_ci->values));
312 assert(severity2_ci->values != NULL);
313 severity2_ci->values->type = OCONFIG_TYPE_STRING;
314 severity2_ci->values->value.string = "warning";
315
316 oconfig_item_t *type_inst2_ci = &match2_ci->children[4];
317 type_inst2_ci->key = "TypeInstance";
318 type_inst2_ci->values_num = 1;
319 type_inst2_ci->values =
320 calloc(type_inst2_ci->values_num, sizeof(*type_inst2_ci->values));
321 assert(type_inst2_ci->values != NULL);
322 type_inst2_ci->values->type = OCONFIG_TYPE_STRING;
323 type_inst2_ci->values->value.string = "test_type_instance_2";
324
325 oconfig_item_t *plugin_inst2_ci = &match2_ci->children[5];
326 plugin_inst2_ci->key = "PluginInstance";
327 plugin_inst2_ci->values_num = 1;
328 plugin_inst2_ci->values =
329 calloc(plugin_inst2_ci->values_num, sizeof(*plugin_inst2_ci->values));
330 assert(plugin_inst2_ci->values != NULL);
331 plugin_inst2_ci->values->type = OCONFIG_TYPE_STRING;
332 plugin_inst2_ci->values->value.string = "test_plugin_instance_2";
333
334 int ret = logparser_config_logfile(logfile_ci);
335 EXPECT_EQ_INT(0, ret);
336
337 EXPECT_EQ_INT(1, logparser_ctx.parsers_len);
338
339 log_parser_t *parser = &logparser_ctx.parsers[0];
340
341 EXPECT_EQ_INT(1, parser->first_read);
342 EXPECT_EQ_STR("/path/to/a/file", parser->filename);
343 EXPECT_EQ_STR("test_default_plugin_instance_1", parser->def_plugin_inst);
344 EXPECT_EQ_INT(NOTIF_OKAY, parser->def_severity);
345 EXPECT_EQ_STR("test_default_type_1", parser->def_type);
346 EXPECT_EQ_STR("test_default_type_instance_1", parser->def_type_inst);
347 EXPECT_EQ_INT(2, parser->patterns_len);
348
349 message_pattern_t *pattern = &logparser_ctx.parsers[0].patterns[0];
350 EXPECT_EQ_STR("test_regex_1", pattern->regex);
351 EXPECT_EQ_INT(0, pattern->is_mandatory);
352 EXPECT_EQ_INT(15, pattern->submatch_idx);
353 EXPECT_EQ_STR("test_match_1", pattern->name);
354
355 message_item_user_data_t *user_data = pattern->user_data;
356 EXPECT_EQ_INT(3, user_data->infos_len);
357 for (size_t i = 0; i < user_data->infos_len; ++i) {
358 switch (user_data->infos[i].type) {
359 case MSG_ITEM_PLUGIN_INST:
360 EXPECT_EQ_STR("test_plugin_instance_1",
361 user_data->infos[i].val.str_override);
362 break;
363 case MSG_ITEM_TYPE_INST:
364 EXPECT_EQ_STR("test_type_instance_1",
365 user_data->infos[i].val.str_override);
366 break;
367 case MSG_ITEM_SEVERITY:
368 EXPECT_EQ_INT(NOTIF_FAILURE, user_data->infos[i].val.severity);
369 break;
370 default:
371 OK1(false, "Unknown message item");
372 }
373 }
374
375 pattern = &logparser_ctx.parsers[0].patterns[1];
376 EXPECT_EQ_STR("test_regex_2", pattern->regex);
377 EXPECT_EQ_INT(1, pattern->is_mandatory);
378 EXPECT_EQ_INT(8, pattern->submatch_idx);
379 EXPECT_EQ_STR("test_match_2", pattern->name);
380
381 user_data = pattern->user_data;
382 EXPECT_EQ_INT(3, user_data->infos_len);
383 for (size_t i = 0; i < user_data->infos_len; ++i) {
384 switch (user_data->infos[i].type) {
385 case MSG_ITEM_PLUGIN_INST:
386 EXPECT_EQ_STR("test_plugin_instance_2",
387 user_data->infos[i].val.str_override);
388 break;
389 case MSG_ITEM_TYPE_INST:
390 EXPECT_EQ_STR("test_type_instance_2",
391 user_data->infos[i].val.str_override);
392 break;
393 case MSG_ITEM_SEVERITY:
394 EXPECT_EQ_INT(NOTIF_WARNING, user_data->infos[i].val.severity);
395 break;
396 default:
397 OK1(false, "Unknown message item");
398 }
399 }
400
401 sfree(regex1_ci->values);
402 sfree(submatch_idx1_ci->values);
403 sfree(is_mandatory1_ci->values);
404 sfree(severity1_ci->values);
405 sfree(type_inst1_ci->values);
406 sfree(plugin_inst1_ci->values);
407
408 sfree(regex2_ci->values);
409 sfree(submatch_idx2_ci->values);
410 sfree(is_mandatory2_ci->values);
411 sfree(severity2_ci->values);
412 sfree(type_inst2_ci->values);
413 sfree(plugin_inst2_ci->values);
414
415 sfree(match2_ci->children);
416 sfree(match2_ci->values);
417
418 sfree(match1_ci->children);
419 sfree(match1_ci->values);
420
421 sfree(def_plugin_inst_ci->values);
422 sfree(def_type_ci->values);
423 sfree(def_type_inst_ci->values);
424 sfree(def_severity_ci->values);
425
426 sfree(msg_ci->children);
427 sfree(msg_ci->values);
428
429 sfree(first_full_read_ci->values);
430
431 sfree(logfile_ci->children);
432 sfree(logfile_ci->values);
433
434 sfree(logfile_ci);
435
436 logparser_shutdown();
437
438 return 0;
439 }
440
441 int main(void) {
442
443 RUN_TEST(init);
444 RUN_TEST(free_user_data);
445 RUN_TEST(config_logfile);
446
447 END_TEST;
448 }