From 7ee017f7080c34ec15e11e06e6cdc556ba55f1f4 Mon Sep 17 00:00:00 2001 From: Marat Abrarov Date: Thu, 5 Dec 2024 23:07:54 +0300 Subject: [PATCH] engine: support of tests which require generation of context passed to the test callback based on configuration of Fluent Bit and based on configuration of plugin Signed-off-by: Marat Abrarov --- include/fluent-bit/flb_lib.h | 19 ++-- src/flb_lib.c | 49 +++++++--- tests/runtime/out_elasticsearch.c | 152 +++++++++++++++++++----------- 3 files changed, 141 insertions(+), 79 deletions(-) diff --git a/include/fluent-bit/flb_lib.h b/include/fluent-bit/flb_lib.h index 4aee2fee667..418fc78c91c 100644 --- a/include/fluent-bit/flb_lib.h +++ b/include/fluent-bit/flb_lib.h @@ -66,17 +66,14 @@ FLB_EXPORT int flb_output_set_test(flb_ctx_t *ctx, int ffd, char *test_name, void (*out_callback) (void *, int, int, void *, size_t, void *), void *out_callback_data, - void *test_ctx); -FLB_EXPORT int flb_output_set_test_with_ctx_callback( - flb_ctx_t *ctx, int ffd, char *test_name, - void (*out_callback) (void *, int, int, - void *, size_t, void *), - void *out_callback_data, - void *test_ctx, - void *(*test_ctx_callback) ( - struct flb_config *, - struct flb_input_instance *, - void *, void *)); + void *flush_ctx); +FLB_EXPORT int flb_output_set_test_flush_ctx_callback(flb_ctx_t *ctx, int ffd, + char *test_name, + void *(*flush_ctx_callback)( + struct flb_config *, + struct flb_input_instance *, + void *, void *), + void *flush_ctx); FLB_EXPORT int flb_output_set_callback(flb_ctx_t *ctx, int ffd, char *name, void (*cb)(char *, void *, void *)); FLB_EXPORT int flb_output_set_http_test(flb_ctx_t *ctx, int ffd, char *test_name, diff --git a/src/flb_lib.c b/src/flb_lib.c index df5cedf6b49..a16a1312410 100644 --- a/src/flb_lib.c +++ b/src/flb_lib.c @@ -548,19 +548,42 @@ int flb_output_set_callback(flb_ctx_t *ctx, int ffd, char *name, int flb_output_set_test(flb_ctx_t *ctx, int ffd, char *test_name, void (*out_callback) (void *, int, int, void *, size_t, void *), void *out_callback_data, - void *test_ctx) + void *flush_ctx) { - return flb_output_set_test_with_ctx_callback(ctx, ffd, test_name, out_callback, - out_callback_data, test_ctx, NULL); + struct flb_output_instance *o_ins; + + o_ins = out_instance_get(ctx, ffd); + if (!o_ins) { + return -1; + } + + /* + * Enabling a test, set the output instance in 'test' mode, so no real + * flush callback is invoked, only the desired implemented test. + */ + + /* Formatter test */ + if (strcmp(test_name, "formatter") == 0) { + o_ins->test_mode = FLB_TRUE; + o_ins->test_formatter.rt_ctx = ctx; + o_ins->test_formatter.rt_ffd = ffd; + o_ins->test_formatter.rt_out_callback = out_callback; + o_ins->test_formatter.rt_data = out_callback_data; + o_ins->test_formatter.flush_ctx = flush_ctx; + } + else { + return -1; + } + + return 0; } -int flb_output_set_test_with_ctx_callback(flb_ctx_t *ctx, int ffd, char *test_name, - void (*out_callback) (void *, int, int, void *, size_t, void *), - void *out_callback_data, - void *test_ctx, - void *(*test_ctx_callback) (struct flb_config *, - struct flb_input_instance *, - void *, void *)) +int flb_output_set_test_flush_ctx_callback(flb_ctx_t *ctx, int ffd, + char *test_name, + void *(*flush_ctx_callback) (struct flb_config *, + struct flb_input_instance *, + void *, void *), + void *flush_ctx) { struct flb_output_instance *o_ins; @@ -579,10 +602,8 @@ int flb_output_set_test_with_ctx_callback(flb_ctx_t *ctx, int ffd, char *test_na o_ins->test_mode = FLB_TRUE; o_ins->test_formatter.rt_ctx = ctx; o_ins->test_formatter.rt_ffd = ffd; - o_ins->test_formatter.rt_out_callback = out_callback; - o_ins->test_formatter.rt_data = out_callback_data; - o_ins->test_formatter.flush_ctx = test_ctx; - o_ins->test_formatter.flush_ctx_callback = test_ctx_callback; + o_ins->test_formatter.flush_ctx = flush_ctx; + o_ins->test_formatter.flush_ctx_callback = flush_ctx_callback; } else { return -1; diff --git a/tests/runtime/out_elasticsearch.c b/tests/runtime/out_elasticsearch.c index 3c9c88eb9f1..351eaee1df2 100644 --- a/tests/runtime/out_elasticsearch.c +++ b/tests/runtime/out_elasticsearch.c @@ -301,9 +301,11 @@ void flb_test_write_operation_index() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_write_op_index, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_write_op_index, NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -346,9 +348,12 @@ void flb_test_write_operation_create() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_write_op_create, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_write_op_create, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -393,9 +398,12 @@ void flb_test_write_operation_update() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_write_op_update, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_write_op_update, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -440,9 +448,12 @@ void flb_test_write_operation_upsert() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_write_op_upsert, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_write_op_upsert, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -486,9 +497,11 @@ void flb_test_index_type() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_index_type, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_index_type, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -533,9 +546,12 @@ void flb_test_logstash_format() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_logstash_format, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_logstash_format, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -581,9 +597,12 @@ void flb_test_logstash_format_nanos() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_logstash_format_nanos, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_logstash_format_nanos, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -627,9 +646,11 @@ void flb_test_tag_key() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_tag_key, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_tag_key, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -672,9 +693,11 @@ void flb_test_replace_dots() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_replace_dots, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_replace_dots, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -717,9 +740,11 @@ void flb_test_id_key() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_id_key, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_id_key, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -772,9 +797,11 @@ void flb_test_div0() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_nothing, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_nothing, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -853,9 +880,11 @@ void flb_test_long_index() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_long_index, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_long_index, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -901,9 +930,12 @@ void flb_test_logstash_prefix_separator() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_logstash_prefix_separator, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_logstash_prefix_separator, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -957,9 +989,12 @@ void flb_test_upstream_write_operation() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_write_op_index, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_write_op_index, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -1013,9 +1048,11 @@ void flb_test_upstream_index_type() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_index_type, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_index_type, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -1078,9 +1115,12 @@ void flb_test_upstream_logstash_format() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_logstash_prefix_separator, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", + cb_check_logstash_prefix_separator, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -1137,9 +1177,11 @@ void flb_test_upstream_replace_dots() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_replace_dots, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_replace_dots, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */ @@ -1190,9 +1232,11 @@ void flb_test_upstream_id_key() NULL); /* Enable test mode */ - ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter", - cb_check_id_key, - NULL, NULL, cb_flush_context); + ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_id_key, + NULL, NULL); + TEST_CHECK(ret == 0); + ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter", + cb_flush_context, NULL); TEST_CHECK(ret == 0); /* Start */