summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/tests/internal/config_format_yaml.c
blob: 3135022baa54c6b6fa5fe226ed427e6ba3747c03 (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
/* -*- 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_str.h>
#include <fluent-bit/flb_config_format.h>

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

#include "flb_tests_internal.h"

#define FLB_TESTS_CONF_PATH FLB_TESTS_DATA_PATH "/data/config_format/yaml"
#define FLB_000 FLB_TESTS_CONF_PATH "/fluent-bit.yaml"
#define FLB_001 FLB_TESTS_CONF_PATH "/issue_7559.yaml"

/*
 * Configurations to test:
 *  * basic single input to single output
 *  * basic single input to single output with a filter
 *  * includes
 *  * slist
 *  * conf parsers
 *  * yaml parsers
 *  * customs
 *  * service
 *  * env
 */

/* data/config_format/fluent-bit.yaml */
static void test_basic()
{
    struct mk_list *head;
    struct flb_cf *cf;
    struct flb_cf_section *s;
    struct flb_cf_group *g;
    struct cfl_variant *v;
    int idx = 0;

    cf = flb_cf_yaml_create(NULL, FLB_000, NULL, 0);
    TEST_CHECK(cf != NULL);
    if (!cf) {
        exit(EXIT_FAILURE);
    }

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

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

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

    /* check inputs */
    idx = 0;
    mk_list_foreach(head, &cf->inputs) {
        s = mk_list_entry(head, struct flb_cf_section, _head_section);
        switch (idx) {
        case 0:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "dummy") == 0);
            break;
        case 1:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "tail") == 0);
            v = flb_cf_section_property_get(cf, s, "path");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "./test.log") == 0);
            break;
        case 2:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "tail") == 0);
            v = flb_cf_section_property_get(cf, s, "path");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "./test.log") == 0);
            break;
        }
        idx++;
    }

    /* check outputs */
    idx = 0;
    mk_list_foreach(head, &cf->outputs) {
        s = mk_list_entry(head, struct flb_cf_section, _head_section);
        switch (idx) {
        case 0:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "stdout") == 0);
            break;
        case 1:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "stdout") == 0);
            break;
        }
        idx++;
    }

    /* check filters */
    idx = 0;
    mk_list_foreach(head, &cf->filters) {
        s = mk_list_entry(head, struct flb_cf_section, _head_section);
        switch (idx) {
        case 0:
            v = flb_cf_section_property_get(cf, s, "name");
            TEST_CHECK(v->type == CFL_VARIANT_STRING);
            TEST_CHECK(strcmp(v->data.as_string, "record_modifier") == 0);
            break;
        }
        idx++;
    }

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

    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);
}

/* https://github.com/fluent/fluent-bit/issues/7559 */
static void test_customs_section()
{
    struct flb_cf *cf;
    struct flb_cf_section *s;

    cf = flb_cf_yaml_create(NULL, FLB_001, NULL, 0);
    TEST_CHECK(cf != NULL);
    if (!cf) {
        exit(EXIT_FAILURE);
    }

    /* Total number of sections */
    if(!TEST_CHECK(mk_list_size(&cf->sections) == 3)) {
        TEST_MSG("Section number error. Got=%d expect=3", mk_list_size(&cf->sections));
    }

    s = flb_cf_section_get_by_name(cf, "customs");
    TEST_CHECK(s != NULL);
    if (!TEST_CHECK(s->type == FLB_CF_CUSTOM)) {
        TEST_MSG("Section type error. Got=%d expect=%d", s->type, FLB_CF_CUSTOM);
    }

    flb_cf_dump(cf);
    flb_cf_destroy(cf);
}

static void test_slist_even()
{
    struct flb_cf *cf;
    struct flb_cf_section *s;
    struct cfl_variant *v;
    struct mk_list *head;

    cf = flb_cf_yaml_create(NULL, FLB_TESTS_CONF_PATH "/pipelines/slist/even.yaml", NULL, 0);
    TEST_CHECK(cf != NULL);
    if (!cf) {
        exit(EXIT_FAILURE);
    }

    /* Total number of inputs */
    if(!TEST_CHECK(mk_list_size(&cf->inputs) == 1)) {
        TEST_MSG("Section number error. Got=%d expect=1", mk_list_size(&cf->inputs));
    }

    mk_list_foreach(head, &cf->inputs) {
        s = mk_list_entry(head, struct flb_cf_section, _head_section);
        TEST_CHECK(s != NULL);

        v = flb_cf_section_property_get(cf, s, "success_header");
        TEST_CHECK(v->type == CFL_VARIANT_ARRAY);
        if (!TEST_CHECK(v->data.as_array->entry_count == 2)) {
            TEST_MSG("Section number error. Got=%lud expect=2", v->data.as_array->entry_count);
        }
    }

    flb_cf_dump(cf);
    flb_cf_destroy(cf);
}

static void test_slist_odd()
{
    struct flb_cf *cf;
    struct flb_cf_section *s;
    struct cfl_variant *v;
    struct mk_list *head;

    cf = flb_cf_yaml_create(NULL, FLB_TESTS_CONF_PATH "/pipelines/slist/odd.yaml", NULL, 0);
    TEST_CHECK(cf != NULL);
    if (!cf) {
        exit(EXIT_FAILURE);
    }

    /* Total number of inputs */
    if(!TEST_CHECK(mk_list_size(&cf->inputs) == 1)) {
        TEST_MSG("Section number error. Got=%d expect=1", mk_list_size(&cf->inputs));
    }

    mk_list_foreach(head, &cf->inputs) {
        s = mk_list_entry(head, struct flb_cf_section, _head_section);
        TEST_CHECK(s != NULL);

        v = flb_cf_section_property_get(cf, s, "success_header");
        TEST_CHECK(v->type == CFL_VARIANT_ARRAY);
        if (!TEST_CHECK(v->data.as_array->entry_count == 3)) {
            TEST_MSG("Section number error. Got=%lud expect=3", v->data.as_array->entry_count);
        }
    }

    flb_cf_dump(cf);
    flb_cf_destroy(cf);
}


static void test_parser_conf()
{
    struct flb_cf *cf;
    struct flb_config *config;
    int ret;
    int cnt;

    cf = flb_cf_yaml_create(NULL, FLB_TESTS_CONF_PATH "/parsers/parsers-conf.yaml", NULL, 0);
    TEST_CHECK(cf != NULL);
    if (!cf) {
        exit(EXIT_FAILURE);
    }

    config = flb_config_init();
    TEST_CHECK(config != NULL);
    config->conf_path = flb_strdup(FLB_TESTS_CONF_PATH "/parsers/");

    // count the parsers registered automatically by fluent-bit
    cnt = mk_list_size(&config->parsers);
    // load the parsers from the configuration
    ret = flb_config_load_config_format(config, cf);
    if (ret != 0) {
        exit(EXIT_FAILURE);;
    }

    /* Total number of inputs */
    if(!TEST_CHECK(mk_list_size(&config->parsers) == cnt+1)) {
        TEST_MSG("Section number error. Got=%d expect=%d", 
	         mk_list_size(&config->parsers),
		 cnt+1);
    }

    flb_cf_dump(cf);
    flb_cf_destroy(cf);
    flb_config_exit(config);
}

static inline int check_camel_to_snake(char *input, char *output)
{
    int len;
    int ret = -1;
    flb_sds_t out;
    struct flb_cf *cf;

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

    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_camel_case_key()
{
    /* normal conversion */
    TEST_CHECK(check_camel_to_snake("a", "a") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("aB", "a_b") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("aBc", "a_bc") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("aBcA", "a_bc_a") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("aBCD", "a_b_c_d") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("intervalSec", "interval_sec") == FLB_TRUE);

    /* unsupported conversion, we force lowercase in Yaml */
    TEST_CHECK(check_camel_to_snake("AA", "AA") == FLB_TRUE);
    TEST_CHECK(check_camel_to_snake("Interval_Sec", "Interval_Sec") == FLB_TRUE);

}

TEST_LIST = {
    { "basic"    , test_basic},
    { "customs section", test_customs_section},
    { "slist odd", test_slist_odd},
    { "slist even", test_slist_even},
    { "parsers file conf", test_parser_conf},
    { "camel_case_key", test_camel_case_key},
    { 0 }
};