summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/internal/config_format_fluentbit.c
blob: 3f158573963cebc67370c1824f4defbd904be776 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_config_format.h>
#include <fluent-bit/flb_sds.h>
#include <sys/stat.h>

#include <cfl/cfl.h>
#include <cfl/cfl_list.h>

#include "flb_tests_internal.h"

#define FLB_000 FLB_TESTS_DATA_PATH "/data/config_format/classic/fluent-bit.conf"
#define FLB_001 FLB_TESTS_DATA_PATH "/data/config_format/classic/issue_5880.conf"
#define FLB_002 FLB_TESTS_DATA_PATH "/data/config_format/classic/indent_level_error.conf"
#define FLB_003 FLB_TESTS_DATA_PATH "/data/config_format/classic/recursion.conf"
#define FLB_004 FLB_TESTS_DATA_PATH "/data/config_format/classic/issue6281.conf"
#define FLB_005 FLB_TESTS_DATA_PATH "/data/config_format/classic/nolimitline.conf"

#define ERROR_LOG "fluentbit_conf_error.log"

/* data/config_format/fluent-bit.conf */
void test_basic()
{
    struct mk_list *head;
	struct flb_cf *cf;
    struct flb_cf_section *s;
    struct flb_cf_group *g;

    cf = flb_cf_fluentbit_create(NULL, FLB_000, NULL, 0);
    TEST_CHECK(cf != NULL);

    /* Total number of sections */
    TEST_CHECK(mk_list_size(&cf->sections) == 8);

	/* SERVICE check */
    TEST_CHECK(cf->service != NULL);
    if (cf->service) {
        TEST_CHECK(cfl_list_size(&cf->service->properties->list) == 3);
    }

    /* Meta commands */
    TEST_CHECK(mk_list_size(&cf->metas) == 2);

    /* Check number sections per list */
    TEST_CHECK(mk_list_size(&cf->parsers) == 1);
    TEST_CHECK(mk_list_size(&cf->multiline_parsers) == 1);
    TEST_CHECK(mk_list_size(&cf->customs) == 1);
    TEST_CHECK(mk_list_size(&cf->inputs) == 1);
    TEST_CHECK(mk_list_size(&cf->filters) == 1);
    TEST_CHECK(mk_list_size(&cf->outputs) == 1);
    TEST_CHECK(mk_list_size(&cf->others) == 1);

    /* groups */
    s = flb_cf_section_get_by_name(cf, "input");
    TEST_CHECK(s != NULL);
    TEST_CHECK(mk_list_size(&s->groups) == 2);

    mk_list_foreach(head, &s->groups) {
        g = mk_list_entry(head, struct flb_cf_group, _head);
        TEST_CHECK(cfl_list_size(&g->properties->list) == 2);
    }

    printf("\n");
    flb_cf_dump(cf);

    flb_cf_destroy(cf);
}

struct str_list {
    size_t size;
    char **lists;
};

static int check_str_list(struct str_list *list, FILE *fp)
{
    flb_sds_t error_str = NULL;
    char *p = NULL;
    size_t size;
    int str_size = 4096;
    int i;
    int fd = fileno(fp);
    struct stat st;

    if (!TEST_CHECK(list != NULL)) {
        TEST_MSG("list is NULL");
        return -1;
    }
    if (!TEST_CHECK(fp != NULL)) {
        TEST_MSG("fp is NULL");
        return -1;
    }

    if (!TEST_CHECK(fstat(fd, &st) == 0)) {
        TEST_MSG("fstat failed");
        return -1;
    }
    str_size = st.st_size;
    error_str = flb_sds_create_size(str_size);
    if (!TEST_CHECK(error_str != NULL)) {
        TEST_MSG("flb_sds_create_size failed.");
        return -1;
    }

    size = fread(error_str, 1, str_size, fp);
    if (size < str_size) {
        if (!TEST_CHECK(ferror(fp) == 0)) {
            TEST_MSG("fread failed.");
            clearerr(fp);
            flb_sds_destroy(error_str);
            return -1;
        }
        clearerr(fp);
    }

    for (i=0; i<list->size; i++) {
        p = strstr(error_str, list->lists[i]);
        if (!TEST_CHECK(p != NULL)) {
            TEST_MSG("  Got   :%s\n  expect:%s", error_str, list->lists[i]);
        }
    }

    flb_sds_destroy(error_str);
    return 0;
}


/* https://github.com/fluent/fluent-bit/issues/5880 */
void missing_value()
{
	struct flb_cf *cf;
    FILE *fp = NULL;
    char *expected_strs[] = {"undefined value", ":9:" /* lieno*/ };
    struct str_list expected = {
                                .size = sizeof(expected_strs)/sizeof(char*),
                                .lists = &expected_strs[0],
    };

    unlink(ERROR_LOG);

    fp = freopen(ERROR_LOG, "w+", stderr);
    if (!TEST_CHECK(fp != NULL)) {
        TEST_MSG("freopen failed. errno=%d path=%s", errno, ERROR_LOG);
        exit(EXIT_FAILURE);
    }

    cf = flb_cf_fluentbit_create(NULL, FLB_001, NULL, 0);
    TEST_CHECK(cf == NULL);
    fflush(fp);
    fclose(fp);

    fp = fopen(ERROR_LOG, "r");
    if (!TEST_CHECK(fp != NULL)) {
        TEST_MSG("fopen failed. errno=%d path=%s", errno, ERROR_LOG);
        unlink(ERROR_LOG);
        exit(EXIT_FAILURE);
    }

    check_str_list(&expected, fp);

    fclose(fp);
    unlink(ERROR_LOG);
}

void indent_level_error()
{
	struct flb_cf *cf;
    FILE *fp = NULL;
    char *expected_strs[] = {"invalid", "indent", "level"};
    struct str_list expected = {
                                .size = sizeof(expected_strs)/sizeof(char*),
                                .lists = &expected_strs[0],
    };

    unlink(ERROR_LOG);

    fp = freopen(ERROR_LOG, "w+", stderr);
    if (!TEST_CHECK(fp != NULL)) {
        TEST_MSG("freopen failed. errno=%d path=%s", errno, ERROR_LOG);
        exit(EXIT_FAILURE);
    }

    cf = flb_cf_create();
    if (!TEST_CHECK(cf != NULL)) {
        TEST_MSG("flb_cf_create failed");
        exit(EXIT_FAILURE);
    }

    cf = flb_cf_fluentbit_create(cf, FLB_002, NULL, 0);
    TEST_CHECK(cf == NULL);
    fflush(fp);
    fclose(fp);

    fp = fopen(ERROR_LOG, "r");
    if (!TEST_CHECK(fp != NULL)) {
        TEST_MSG("fopen failed. errno=%d path=%s", errno, ERROR_LOG);
        if (cf != NULL) {
            flb_cf_destroy(cf);
        }
        unlink(ERROR_LOG);
        exit(EXIT_FAILURE);
    }

    check_str_list(&expected, fp);
    if (cf != NULL) {
        flb_cf_destroy(cf);
    }
    fclose(fp);
    unlink(ERROR_LOG);
}

void recursion()
{
	struct flb_cf *cf;

    cf = flb_cf_create();
    if (!TEST_CHECK(cf != NULL)) {
        TEST_MSG("flb_cf_create failed");
        exit(EXIT_FAILURE);
    }

    cf = flb_cf_fluentbit_create(cf, FLB_003, NULL, 0);

    /* No SIGSEGV means success */
}


/*
 *  https://github.com/fluent/fluent-bit/issues/6281
 *
 *  Fluent-bit loads issue6281.conf that loads issue6281_input/output.conf.
 *
 *    config dir -+-- issue6281.conf
 *                |
 *                +-- issue6281_input.conf
 *                |
 *                +-- issue6281_output.conf
 */
void not_current_dir_files()
{
    struct flb_cf *cf;

    cf = flb_cf_create();
    if (!TEST_CHECK(cf != NULL)) {
        TEST_MSG("flb_cf_create failed");
        exit(EXIT_FAILURE);
    }

    cf = flb_cf_fluentbit_create(cf, FLB_004, NULL, 0);
    if (!TEST_CHECK(cf != NULL)) {
        TEST_MSG("flb_cf_fluentbit_create failed");
        exit(EXIT_FAILURE);
    }

    if (cf != NULL) {
        flb_cf_destroy(cf);
    }
}

/* data/config_format/nolimitline.conf */
void test_nolimit_line()
{
    struct flb_cf *cf;
    struct flb_cf_section *s;
    struct cfl_list *p_head;
    struct cfl_kvpair *kv;

    cf = flb_cf_fluentbit_create(NULL, FLB_005, NULL, 0);
    TEST_CHECK(cf != NULL);

    /* Total number of sections */
    TEST_CHECK(mk_list_size(&cf->sections) == 3);

    /* SERVICE check */
    TEST_CHECK(cf->service == NULL);

    /* Meta commands */
    TEST_CHECK(mk_list_size(&cf->metas) == 0);

    /* Check number sections per list */
    TEST_CHECK(mk_list_size(&cf->inputs) == 1);
    TEST_CHECK(mk_list_size(&cf->filters) == 1);
    TEST_CHECK(mk_list_size(&cf->outputs) == 1);

    /* Check the previous line buffer limit */
    s = flb_cf_section_get_by_name(cf, "filter");
    TEST_CHECK(s != NULL);
    TEST_CHECK(mk_list_size(&s->groups) == 0);

    if (cfl_list_size(&s->properties->list) > 0) {
        TEST_CHECK(cfl_list_size(&s->properties->list) == 4);
        cfl_list_foreach(p_head, &s->properties->list) {
            kv = cfl_list_entry(p_head, struct cfl_kvpair, _head);
            if (strcmp(kv->key, "code") == 0) {
                TEST_CHECK(cfl_sds_len(kv->val->data.as_string) > FLB_DEFAULT_CF_BUF_SIZE);
            }
        }
    }

    printf("\n");
    flb_cf_dump(cf);

    flb_cf_destroy(cf);
}

static inline int check_snake_case(char *input, char *output)
{
    int len;
    int ret;
    flb_sds_t out;
    struct flb_cf *cf;


    cf = flb_cf_create();
    flb_cf_set_origin_format(cf, FLB_CF_CLASSIC);

    len = strlen(input);
    out = flb_cf_key_translate(cf, input, len);

    ret = strcmp(out, output);

    flb_sds_destroy(out);

    flb_cf_destroy(cf);

    if (ret == 0) {
        return FLB_TRUE;
    }

    return FLB_FALSE;
}

static void test_snake_case_key()
{
    /* normal conversion */
    TEST_CHECK(check_snake_case("a", "a") == FLB_TRUE);
    TEST_CHECK(check_snake_case("aB", "ab") == FLB_TRUE);
    TEST_CHECK(check_snake_case("aBc", "abc") == FLB_TRUE);
    TEST_CHECK(check_snake_case("interval_Sec", "interval_sec") == FLB_TRUE);
}

TEST_LIST = {
    { "basic"    , test_basic},
    { "missing_value_issue5880" , missing_value},
    { "indent_level_error" , indent_level_error},
    { "recursion" , recursion},
    { "not_current_dir_files", not_current_dir_files},
    { "no_limit_line", test_nolimit_line},
    { "snake_case_key", test_snake_case_key},
    { 0 }
};