diff --git a/tests/internal/CMakeLists.txt b/tests/internal/CMakeLists.txt index 5075b4a4aee..194d57c1147 100644 --- a/tests/internal/CMakeLists.txt +++ b/tests/internal/CMakeLists.txt @@ -24,6 +24,20 @@ set(UNIT_TESTS_FILES typecast.c ) +# Config format +set(UNIT_TESTS_FILES + ${UNIT_TESTS_FILES} + config_format.c + config_format_fluentbit.c +) + +if(FLB_HAVE_LIBYAML) + set(UNIT_TESTS_FILES + ${UNIT_TESTS_FILES} + config_format_yaml.c + ) +endif() + if (NOT WIN32) set(UNIT_TESTS_FILES ${UNIT_TESTS_FILES} diff --git a/tests/internal/config_format.c b/tests/internal/config_format.c new file mode 100644 index 00000000000..9f24fffbd03 --- /dev/null +++ b/tests/internal/config_format.c @@ -0,0 +1,90 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include +#include +#include +#include + +#include "flb_tests_internal.h" + +void test_api() +{ + struct flb_cf *cf; + struct flb_cf_section *s_tmp; + struct flb_cf_section *service; + struct flb_cf_group *g_tmp; + struct flb_kv *meta; + struct flb_kv *kv; + + /* create context */ + cf = flb_cf_create(); + TEST_CHECK(cf != NULL); + + /* create service section */ + service = flb_cf_section_create(cf, "SERVICE", 7); + TEST_CHECK(service != NULL); + + /* add a property */ + kv = flb_cf_property_add(cf, &service->properties, "key", 3, "val", 3); + TEST_CHECK(kv != NULL); + + /* add a property with empty spaces on left/right */ + kv = flb_cf_property_add(cf, &service->properties, " key ", 5, " val ", 7); + TEST_CHECK(kv != NULL); + + /* property: check key */ + TEST_CHECK(flb_sds_len(kv->key) == 3); + TEST_CHECK(strcmp(kv->key, "key") == 0); + + /* property: check val */ + TEST_CHECK(flb_sds_len(kv->key) == 3); + TEST_CHECK(strcmp(kv->key, "key") == 0); + + /* add an invalid property */ + kv = flb_cf_property_add(cf, &service->properties, " ", 3, "", 0); + TEST_CHECK(kv == NULL); + + /* try to add another 'SERVICE' section, it should fail */ + s_tmp = flb_cf_section_create(cf, "SERVICE", 7); + TEST_CHECK(s_tmp == NULL); + + /* add a valid section */ + s_tmp = flb_cf_section_create(cf, "INPUT", 5); + TEST_CHECK(s_tmp != NULL); + + TEST_CHECK(mk_list_size(&cf->inputs) == 1); + + /* add property to the section recently created */ + kv = flb_cf_property_add(cf, &s_tmp->properties, "key", 3, "val", 3); + TEST_CHECK(kv != NULL); + + /* groups: add groups to the last section created */ + g_tmp = flb_cf_group_create(cf, s_tmp, "FLUENT GROUP", 12); + TEST_CHECK(g_tmp != NULL); + + /* add properties to the group */ + kv = flb_cf_property_add(cf, &g_tmp->properties, "key", 3, "val", 3); + TEST_CHECK(kv != NULL); + + /* groups: invalid group */ + g_tmp = flb_cf_group_create(cf, s_tmp, "", 0); + TEST_CHECK(g_tmp == NULL); + + /* Meta commands */ + meta = flb_cf_meta_create(cf, "@SET a=1 ", 20); + TEST_CHECK(meta != NULL); + TEST_CHECK(flb_sds_len(meta->key) == 3 && strcmp(meta->key, "SET") == 0); + TEST_CHECK(flb_sds_len(meta->val) == 3 && strcmp(meta->val, "a=1") == 0); + + /* invalid meta */ + meta = flb_cf_meta_create(cf, "@a=1 ", 5); + TEST_CHECK(meta == NULL); + + /* destroy context */ + flb_cf_destroy(cf); +} + +TEST_LIST = { + { "api" , test_api}, + { 0 } +}; diff --git a/tests/internal/config_format_fluentbit.c b/tests/internal/config_format_fluentbit.c new file mode 100644 index 00000000000..e0e92602e72 --- /dev/null +++ b/tests/internal/config_format_fluentbit.c @@ -0,0 +1,48 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include +#include +#include +#include + +#include "flb_tests_internal.h" + +#define FLB_000 FLB_TESTS_DATA_PATH "/data/config_format/fluent-bit.conf" + +/* data/config_format/fluent-bit.conf */ +void test_basic() +{ + struct flb_cf *cf; + + cf = flb_cf_fluentbit_create(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); + TEST_CHECK(mk_list_size(&cf->service->properties) == 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); + + printf("\n"); + flb_cf_dump(cf); + + flb_cf_destroy(cf); +} + +TEST_LIST = { + { "basic" , test_basic}, + { 0 } +}; diff --git a/tests/internal/config_format_yaml.c b/tests/internal/config_format_yaml.c new file mode 100644 index 00000000000..6735598e1c9 --- /dev/null +++ b/tests/internal/config_format_yaml.c @@ -0,0 +1,53 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +#include +#include +#include +#include + +#include "flb_tests_internal.h" + +#define FLB_000 FLB_TESTS_DATA_PATH "/data/config_format/fluent-bit.yaml" + +/* data/config_format/fluent-bit.yaml */ +void test_basic() +{ + struct flb_cf *cf; + + cf = flb_cf_yaml_create(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(mk_list_size(&cf->service->properties) == 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); + + printf("\n"); + flb_cf_dump(cf); + + flb_cf_destroy(cf); + + +} + +TEST_LIST = { + { "basic" , test_basic}, + { 0 } +}; diff --git a/tests/internal/data/config_format/fluent-bit.conf b/tests/internal/data/config_format/fluent-bit.conf new file mode 100644 index 00000000000..67b1cbbf473 --- /dev/null +++ b/tests/internal/data/config_format/fluent-bit.conf @@ -0,0 +1,31 @@ +@SET a=1 +@SET b=2 + +[SERVICE] + flush 1 + log_level info + http_server on + +[PARSER] + name test_api + +[MULTILINE_PARSER] + name abc + +[CUSTOM] + name calyptia + +[INPUT] + name tail + path /var/log/containers/*.log + +[FILTER] + name stdout + match * + +[OUTPUT] + name null + match * + +[UNKNOWN] + name blah diff --git a/tests/internal/data/config_format/fluent-bit.yaml b/tests/internal/data/config_format/fluent-bit.yaml new file mode 100644 index 00000000000..ac1bbe948b1 --- /dev/null +++ b/tests/internal/data/config_format/fluent-bit.yaml @@ -0,0 +1,40 @@ +service: + flush: 1 + log_level: info + http_server: on + +customs: + calyptia: + api_key: abcdefg + +inputs: + tail: + path: "/var/log/containers/*.log" + parser: docker + + tail: + path: "/var/log/messages" + parser: syslog + + forward: + listen: 0.0.0.0 + +filters: + grep: + exclude: $kubernetes['labels']['app'] myapp + +outputs: + stdout: + match: "*" + retry_limit: false + + http: + match: "*" + host: 192.168.3.2 + port: 9444 + tls: on + tls.verify: off + retry_limit: false + +unknown: + a: b