Skip to content

Commit

Permalink
engine: support of tests which require generation of context passed t…
Browse files Browse the repository at this point in the history
…o the test callback based on configuration of Fluent Bit and based on configuration of plugin

Signed-off-by: Marat Abrarov <[email protected]>
  • Loading branch information
mabrarov committed Dec 5, 2024
1 parent f45def4 commit 7ee017f
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 79 deletions.
19 changes: 8 additions & 11 deletions include/fluent-bit/flb_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 35 additions & 14 deletions src/flb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down
152 changes: 98 additions & 54 deletions tests/runtime/out_elasticsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 7ee017f

Please sign in to comment.