From 7fdf7fea37de980b6b475f9fd394326e1d9e8ce6 Mon Sep 17 00:00:00 2001 From: Athish Pranav D Date: Sat, 24 Aug 2024 09:46:29 +0530 Subject: [PATCH] Add UTs Signed-off-by: Athish Pranav D --- tests/runtime/filter_parser.c | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/tests/runtime/filter_parser.c b/tests/runtime/filter_parser.c index 8f25fec0e6e..49d6ddd7988 100644 --- a/tests/runtime/filter_parser.c +++ b/tests/runtime/filter_parser.c @@ -812,6 +812,89 @@ void flb_test_filter_parser_preserve_original_field() flb_destroy(ctx); } +void flb_test_filter_parser_hash_value_field() +{ + int ret; + int bytes; + char *p, *output, *expected; + flb_ctx_t *ctx; + int in_ffd; + int out_ffd; + int filter_ffd; + struct flb_parser *parser; + + struct flb_lib_out_cb cb; + cb.cb = callback_test; + cb.data = NULL; + + clear_output(); + + ctx = flb_create(); + + /* Configure service */ + flb_service_set(ctx, "Flush", FLUSH_INTERVAL, "Grace", "1", "Log_Level", "debug", NULL); + + /* Input */ + in_ffd = flb_input(ctx, (char *) "lib", NULL); + TEST_CHECK(in_ffd >= 0); + flb_input_set(ctx, in_ffd, + "Tag", "test", + NULL); + + /* Parser */ + parser = flb_parser_create("dummy_test", "regex", "^(?[^ ]+) (?[^ ]+) (?[^ ]+) (?.+)$", + FLB_TRUE, + NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE, NULL, 0, + NULL, ctx->config); + TEST_CHECK(parser != NULL); + + /* Filter */ + filter_ffd = flb_filter(ctx, (char *) "parser", NULL); + TEST_CHECK(filter_ffd >= 0); + ret = flb_filter_set(ctx, filter_ffd, + "Match", "test", + "Key_Name", "data", + "Parser", "dummy_test", + "Hash_Value_Field", "On", + NULL); + TEST_CHECK(ret == 0); + + /* Output */ + out_ffd = flb_output(ctx, (char *) "lib", &cb); + TEST_CHECK(out_ffd >= 0); + flb_output_set(ctx, out_ffd, + "Match", "*", + "format", "json", + NULL); + + /* Start the engine */ + ret = flb_start(ctx); + TEST_CHECK(ret == 0); + + /* Ingest data */ + p = "[1448403340,{\"data\":\"100 0.5 true This is an example\",\"log\":\"An example\"}]"; + bytes = flb_lib_push(ctx, in_ffd, p, strlen(p)); + TEST_CHECK(bytes == strlen(p)); + + wait_with_timeout(2000, &output); /* waiting flush and ensuring data flush */ + TEST_CHECK_(output != NULL, "Expected output to not be NULL"); + if (output != NULL) { + /* check original field is preserved */ + expected = "\"parsed\":{\"INT\":\"100\",\"FLOAT\":\"0.5\",\"BOOL\":\"true\",\"STRING\":\"This is an example\"}"; + TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output); + /* check fields were extracted */ + expected = "\"INT\":\"100\",\"FLOAT\":\"0.5\",\"BOOL\":\"true\",\"STRING\":\"This is an example\""; + TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output); + /* check other fields are preserved */ + // expected = "\"log\":\"An example\""; + // TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output); + free(output); + } + + flb_stop(ctx); + flb_destroy(ctx); +} + // https://github.com/fluent/fluent-bit/issues/2250 void flb_test_filter_parser_first_matched_when_mutilple_parser() { @@ -984,6 +1067,7 @@ TEST_LIST = { {"filter_parser_use_system_timezone", flb_test_filter_parser_use_system_timezone }, {"filter_parser_ignore_malformed_time", flb_test_filter_parser_ignore_malformed_time }, {"filter_parser_preserve_original_field", flb_test_filter_parser_preserve_original_field }, + {"filter_parser_hash_value_field", flb_test_filter_parser_hash_value_field }, {"filter_parser_first_matched_when_multiple_parser", flb_test_filter_parser_first_matched_when_mutilple_parser }, {"filter_parser_skip_empty_values_false", flb_test_filter_parser_skip_empty_values_false}, {NULL, NULL}