Skip to content

Commit

Permalink
tests: internal: add 'config_format' for fluentbit and YAML
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Jan 17, 2022
1 parent 7c17c5b commit 786c0d7
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
90 changes: 90 additions & 0 deletions tests/internal/config_format.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* -*- 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 "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 }
};
48 changes: 48 additions & 0 deletions tests/internal/config_format_fluentbit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* -*- 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 "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 }
};
53 changes: 53 additions & 0 deletions tests/internal/config_format_yaml.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* -*- 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 "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 }
};
31 changes: 31 additions & 0 deletions tests/internal/data/config_format/fluent-bit.conf
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions tests/internal/data/config_format/fluent-bit.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 786c0d7

Please sign in to comment.