Skip to content

Commit 57e6b0d

Browse files
Add UTs
Signed-off-by: Athish Pranav D <athishanna@gmail.com>
1 parent 64977b3 commit 57e6b0d

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

tests/runtime/filter_parser.c

+84
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,89 @@ void flb_test_filter_parser_preserve_original_field()
812812
flb_destroy(ctx);
813813
}
814814

815+
void flb_test_filter_parser_hash_value_field()
816+
{
817+
int ret;
818+
int bytes;
819+
char *p, *output, *expected;
820+
flb_ctx_t *ctx;
821+
int in_ffd;
822+
int out_ffd;
823+
int filter_ffd;
824+
struct flb_parser *parser;
825+
826+
struct flb_lib_out_cb cb;
827+
cb.cb = callback_test;
828+
cb.data = NULL;
829+
830+
clear_output();
831+
832+
ctx = flb_create();
833+
834+
/* Configure service */
835+
flb_service_set(ctx, "Flush", FLUSH_INTERVAL, "Grace", "1", "Log_Level", "debug", NULL);
836+
837+
/* Input */
838+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
839+
TEST_CHECK(in_ffd >= 0);
840+
flb_input_set(ctx, in_ffd,
841+
"Tag", "test",
842+
NULL);
843+
844+
/* Parser */
845+
parser = flb_parser_create("dummy_test", "regex", "^(?<INT>[^ ]+) (?<FLOAT>[^ ]+) (?<BOOL>[^ ]+) (?<STRING>.+)$",
846+
FLB_TRUE,
847+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE, NULL, 0,
848+
NULL, ctx->config);
849+
TEST_CHECK(parser != NULL);
850+
851+
/* Filter */
852+
filter_ffd = flb_filter(ctx, (char *) "parser", NULL);
853+
TEST_CHECK(filter_ffd >= 0);
854+
ret = flb_filter_set(ctx, filter_ffd,
855+
"Match", "test",
856+
"Key_Name", "data",
857+
"Parser", "dummy_test",
858+
"Hash_Value_Field", "On",
859+
NULL);
860+
TEST_CHECK(ret == 0);
861+
862+
/* Output */
863+
out_ffd = flb_output(ctx, (char *) "lib", &cb);
864+
TEST_CHECK(out_ffd >= 0);
865+
flb_output_set(ctx, out_ffd,
866+
"Match", "*",
867+
"format", "json",
868+
NULL);
869+
870+
/* Start the engine */
871+
ret = flb_start(ctx);
872+
TEST_CHECK(ret == 0);
873+
874+
/* Ingest data */
875+
p = "[1448403340,{\"data\":\"100 0.5 true This is an example\",\"log\":\"An example\"}]";
876+
bytes = flb_lib_push(ctx, in_ffd, p, strlen(p));
877+
TEST_CHECK(bytes == strlen(p));
878+
879+
wait_with_timeout(2000, &output); /* waiting flush and ensuring data flush */
880+
TEST_CHECK_(output != NULL, "Expected output to not be NULL");
881+
if (output != NULL) {
882+
/* check original field is preserved */
883+
expected = "\"parsed\":{\"INT\":\"100\",\"FLOAT\":\"0.5\",\"BOOL\":\"true\",\"STRING\":\"This is an example\"}";
884+
TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output);
885+
/* check fields were extracted */
886+
expected = "\"INT\":\"100\",\"FLOAT\":\"0.5\",\"BOOL\":\"true\",\"STRING\":\"This is an example\"";
887+
TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output);
888+
/* check other fields are preserved */
889+
// expected = "\"log\":\"An example\"";
890+
// TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output);
891+
free(output);
892+
}
893+
894+
flb_stop(ctx);
895+
flb_destroy(ctx);
896+
}
897+
815898
// https://github.com/fluent/fluent-bit/issues/2250
816899
void flb_test_filter_parser_first_matched_when_mutilple_parser()
817900
{
@@ -984,6 +1067,7 @@ TEST_LIST = {
9841067
{"filter_parser_use_system_timezone", flb_test_filter_parser_use_system_timezone },
9851068
{"filter_parser_ignore_malformed_time", flb_test_filter_parser_ignore_malformed_time },
9861069
{"filter_parser_preserve_original_field", flb_test_filter_parser_preserve_original_field },
1070+
{"filter_parser_hash_value_field", flb_test_filter_parser_hash_value_field },
9871071
{"filter_parser_first_matched_when_multiple_parser", flb_test_filter_parser_first_matched_when_mutilple_parser },
9881072
{"filter_parser_skip_empty_values_false", flb_test_filter_parser_skip_empty_values_false},
9891073
{NULL, NULL}

0 commit comments

Comments
 (0)