Skip to content

Commit 25ad3e2

Browse files
committed
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 <[email protected]>
1 parent 57e722c commit 25ad3e2

File tree

3 files changed

+141
-79
lines changed

3 files changed

+141
-79
lines changed

include/fluent-bit/flb_lib.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,14 @@ FLB_EXPORT int flb_output_set_test(flb_ctx_t *ctx, int ffd, char *test_name,
7070
void (*out_callback) (void *, int, int,
7171
void *, size_t, void *),
7272
void *out_callback_data,
73-
void *test_ctx);
74-
FLB_EXPORT int flb_output_set_test_with_ctx_callback(
75-
flb_ctx_t *ctx, int ffd, char *test_name,
76-
void (*out_callback) (void *, int, int,
77-
void *, size_t, void *),
78-
void *out_callback_data,
79-
void *test_ctx,
80-
void *(*test_ctx_callback) (
81-
struct flb_config *,
82-
struct flb_input_instance *,
83-
void *, void *));
73+
void *flush_ctx);
74+
FLB_EXPORT int flb_output_set_test_flush_ctx_callback(flb_ctx_t *ctx, int ffd,
75+
char *test_name,
76+
void *(*flush_ctx_callback)(
77+
struct flb_config *,
78+
struct flb_input_instance *,
79+
void *, void *),
80+
void *flush_ctx);
8481
FLB_EXPORT int flb_output_set_callback(flb_ctx_t *ctx, int ffd, char *name,
8582
void (*cb)(char *, void *, void *));
8683
FLB_EXPORT int flb_output_set_http_test(flb_ctx_t *ctx, int ffd, char *test_name,

src/flb_lib.c

+35-14
Original file line numberDiff line numberDiff line change
@@ -579,19 +579,42 @@ int flb_output_set_callback(flb_ctx_t *ctx, int ffd, char *name,
579579
int flb_output_set_test(flb_ctx_t *ctx, int ffd, char *test_name,
580580
void (*out_callback) (void *, int, int, void *, size_t, void *),
581581
void *out_callback_data,
582-
void *test_ctx)
582+
void *flush_ctx)
583583
{
584-
return flb_output_set_test_with_ctx_callback(ctx, ffd, test_name, out_callback,
585-
out_callback_data, test_ctx, NULL);
584+
struct flb_output_instance *o_ins;
585+
586+
o_ins = out_instance_get(ctx, ffd);
587+
if (!o_ins) {
588+
return -1;
589+
}
590+
591+
/*
592+
* Enabling a test, set the output instance in 'test' mode, so no real
593+
* flush callback is invoked, only the desired implemented test.
594+
*/
595+
596+
/* Formatter test */
597+
if (strcmp(test_name, "formatter") == 0) {
598+
o_ins->test_mode = FLB_TRUE;
599+
o_ins->test_formatter.rt_ctx = ctx;
600+
o_ins->test_formatter.rt_ffd = ffd;
601+
o_ins->test_formatter.rt_out_callback = out_callback;
602+
o_ins->test_formatter.rt_data = out_callback_data;
603+
o_ins->test_formatter.flush_ctx = flush_ctx;
604+
}
605+
else {
606+
return -1;
607+
}
608+
609+
return 0;
586610
}
587611

588-
int flb_output_set_test_with_ctx_callback(flb_ctx_t *ctx, int ffd, char *test_name,
589-
void (*out_callback) (void *, int, int, void *, size_t, void *),
590-
void *out_callback_data,
591-
void *test_ctx,
592-
void *(*test_ctx_callback) (struct flb_config *,
593-
struct flb_input_instance *,
594-
void *, void *))
612+
int flb_output_set_test_flush_ctx_callback(flb_ctx_t *ctx, int ffd,
613+
char *test_name,
614+
void *(*flush_ctx_callback) (struct flb_config *,
615+
struct flb_input_instance *,
616+
void *, void *),
617+
void *flush_ctx)
595618
{
596619
struct flb_output_instance *o_ins;
597620

@@ -610,10 +633,8 @@ int flb_output_set_test_with_ctx_callback(flb_ctx_t *ctx, int ffd, char *test_na
610633
o_ins->test_mode = FLB_TRUE;
611634
o_ins->test_formatter.rt_ctx = ctx;
612635
o_ins->test_formatter.rt_ffd = ffd;
613-
o_ins->test_formatter.rt_out_callback = out_callback;
614-
o_ins->test_formatter.rt_data = out_callback_data;
615-
o_ins->test_formatter.flush_ctx = test_ctx;
616-
o_ins->test_formatter.flush_ctx_callback = test_ctx_callback;
636+
o_ins->test_formatter.flush_ctx = flush_ctx;
637+
o_ins->test_formatter.flush_ctx_callback = flush_ctx_callback;
617638
}
618639
else {
619640
return -1;

tests/runtime/out_elasticsearch.c

+98-54
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,11 @@ void flb_test_write_operation_index()
301301
NULL);
302302

303303
/* Enable test mode */
304-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
305-
cb_check_write_op_index,
306-
NULL, NULL, cb_flush_context);
304+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
305+
cb_check_write_op_index, NULL, NULL);
306+
TEST_CHECK(ret == 0);
307+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
308+
cb_flush_context, NULL);
307309
TEST_CHECK(ret == 0);
308310

309311
/* Start */
@@ -346,9 +348,12 @@ void flb_test_write_operation_create()
346348
NULL);
347349

348350
/* Enable test mode */
349-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
350-
cb_check_write_op_create,
351-
NULL, NULL, cb_flush_context);
351+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
352+
cb_check_write_op_create,
353+
NULL, NULL);
354+
TEST_CHECK(ret == 0);
355+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
356+
cb_flush_context, NULL);
352357
TEST_CHECK(ret == 0);
353358

354359
/* Start */
@@ -393,9 +398,12 @@ void flb_test_write_operation_update()
393398
NULL);
394399

395400
/* Enable test mode */
396-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
397-
cb_check_write_op_update,
398-
NULL, NULL, cb_flush_context);
401+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
402+
cb_check_write_op_update,
403+
NULL, NULL);
404+
TEST_CHECK(ret == 0);
405+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
406+
cb_flush_context, NULL);
399407
TEST_CHECK(ret == 0);
400408

401409
/* Start */
@@ -440,9 +448,12 @@ void flb_test_write_operation_upsert()
440448
NULL);
441449

442450
/* Enable test mode */
443-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
444-
cb_check_write_op_upsert,
445-
NULL, NULL, cb_flush_context);
451+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
452+
cb_check_write_op_upsert,
453+
NULL, NULL);
454+
TEST_CHECK(ret == 0);
455+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
456+
cb_flush_context, NULL);
446457
TEST_CHECK(ret == 0);
447458

448459
/* Start */
@@ -486,9 +497,11 @@ void flb_test_index_type()
486497
NULL);
487498

488499
/* Enable test mode */
489-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
490-
cb_check_index_type,
491-
NULL, NULL, cb_flush_context);
500+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_index_type,
501+
NULL, NULL);
502+
TEST_CHECK(ret == 0);
503+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
504+
cb_flush_context, NULL);
492505
TEST_CHECK(ret == 0);
493506

494507
/* Start */
@@ -533,9 +546,12 @@ void flb_test_logstash_format()
533546
NULL);
534547

535548
/* Enable test mode */
536-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
537-
cb_check_logstash_format,
538-
NULL, NULL, cb_flush_context);
549+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
550+
cb_check_logstash_format,
551+
NULL, NULL);
552+
TEST_CHECK(ret == 0);
553+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
554+
cb_flush_context, NULL);
539555
TEST_CHECK(ret == 0);
540556

541557
/* Start */
@@ -581,9 +597,12 @@ void flb_test_logstash_format_nanos()
581597
NULL);
582598

583599
/* Enable test mode */
584-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
585-
cb_check_logstash_format_nanos,
586-
NULL, NULL, cb_flush_context);
600+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
601+
cb_check_logstash_format_nanos,
602+
NULL, NULL);
603+
TEST_CHECK(ret == 0);
604+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
605+
cb_flush_context, NULL);
587606
TEST_CHECK(ret == 0);
588607

589608
/* Start */
@@ -627,9 +646,11 @@ void flb_test_tag_key()
627646
NULL);
628647

629648
/* Enable test mode */
630-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
631-
cb_check_tag_key,
632-
NULL, NULL, cb_flush_context);
649+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_tag_key,
650+
NULL, NULL);
651+
TEST_CHECK(ret == 0);
652+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
653+
cb_flush_context, NULL);
633654
TEST_CHECK(ret == 0);
634655

635656
/* Start */
@@ -672,9 +693,11 @@ void flb_test_replace_dots()
672693
NULL);
673694

674695
/* Enable test mode */
675-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
676-
cb_check_replace_dots,
677-
NULL, NULL, cb_flush_context);
696+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_replace_dots,
697+
NULL, NULL);
698+
TEST_CHECK(ret == 0);
699+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
700+
cb_flush_context, NULL);
678701
TEST_CHECK(ret == 0);
679702

680703
/* Start */
@@ -717,9 +740,11 @@ void flb_test_id_key()
717740
NULL);
718741

719742
/* Enable test mode */
720-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
721-
cb_check_id_key,
722-
NULL, NULL, cb_flush_context);
743+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_id_key,
744+
NULL, NULL);
745+
TEST_CHECK(ret == 0);
746+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
747+
cb_flush_context, NULL);
723748
TEST_CHECK(ret == 0);
724749

725750
/* Start */
@@ -772,9 +797,11 @@ void flb_test_div0()
772797
NULL);
773798

774799
/* Enable test mode */
775-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
776-
cb_check_nothing,
777-
NULL, NULL, cb_flush_context);
800+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_nothing,
801+
NULL, NULL);
802+
TEST_CHECK(ret == 0);
803+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
804+
cb_flush_context, NULL);
778805
TEST_CHECK(ret == 0);
779806

780807
/* Start */
@@ -853,9 +880,11 @@ void flb_test_long_index()
853880
NULL);
854881

855882
/* Enable test mode */
856-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
857-
cb_check_long_index,
858-
NULL, NULL, cb_flush_context);
883+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_long_index,
884+
NULL, NULL);
885+
TEST_CHECK(ret == 0);
886+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
887+
cb_flush_context, NULL);
859888
TEST_CHECK(ret == 0);
860889

861890
/* Start */
@@ -901,9 +930,12 @@ void flb_test_logstash_prefix_separator()
901930
NULL);
902931

903932
/* Enable test mode */
904-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
905-
cb_check_logstash_prefix_separator,
906-
NULL, NULL, cb_flush_context);
933+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
934+
cb_check_logstash_prefix_separator,
935+
NULL, NULL);
936+
TEST_CHECK(ret == 0);
937+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
938+
cb_flush_context, NULL);
907939
TEST_CHECK(ret == 0);
908940

909941
/* Start */
@@ -957,9 +989,12 @@ void flb_test_upstream_write_operation()
957989
NULL);
958990

959991
/* Enable test mode */
960-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
961-
cb_check_write_op_index,
962-
NULL, NULL, cb_flush_context);
992+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
993+
cb_check_write_op_index,
994+
NULL, NULL);
995+
TEST_CHECK(ret == 0);
996+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
997+
cb_flush_context, NULL);
963998
TEST_CHECK(ret == 0);
964999

9651000
/* Start */
@@ -1013,9 +1048,11 @@ void flb_test_upstream_index_type()
10131048
NULL);
10141049

10151050
/* Enable test mode */
1016-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
1017-
cb_check_index_type,
1018-
NULL, NULL, cb_flush_context);
1051+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_index_type,
1052+
NULL, NULL);
1053+
TEST_CHECK(ret == 0);
1054+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
1055+
cb_flush_context, NULL);
10191056
TEST_CHECK(ret == 0);
10201057

10211058
/* Start */
@@ -1078,9 +1115,12 @@ void flb_test_upstream_logstash_format()
10781115
NULL);
10791116

10801117
/* Enable test mode */
1081-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
1082-
cb_check_logstash_prefix_separator,
1083-
NULL, NULL, cb_flush_context);
1118+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1119+
cb_check_logstash_prefix_separator,
1120+
NULL, NULL);
1121+
TEST_CHECK(ret == 0);
1122+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
1123+
cb_flush_context, NULL);
10841124
TEST_CHECK(ret == 0);
10851125

10861126
/* Start */
@@ -1137,9 +1177,11 @@ void flb_test_upstream_replace_dots()
11371177
NULL);
11381178

11391179
/* Enable test mode */
1140-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
1141-
cb_check_replace_dots,
1142-
NULL, NULL, cb_flush_context);
1180+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_replace_dots,
1181+
NULL, NULL);
1182+
TEST_CHECK(ret == 0);
1183+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
1184+
cb_flush_context, NULL);
11431185
TEST_CHECK(ret == 0);
11441186

11451187
/* Start */
@@ -1190,9 +1232,11 @@ void flb_test_upstream_id_key()
11901232
NULL);
11911233

11921234
/* Enable test mode */
1193-
ret = flb_output_set_test_with_ctx_callback(ctx, out_ffd, "formatter",
1194-
cb_check_id_key,
1195-
NULL, NULL, cb_flush_context);
1235+
ret = flb_output_set_test(ctx, out_ffd, "formatter", cb_check_id_key,
1236+
NULL, NULL);
1237+
TEST_CHECK(ret == 0);
1238+
ret = flb_output_set_test_flush_ctx_callback(ctx, out_ffd, "formatter",
1239+
cb_flush_context, NULL);
11961240
TEST_CHECK(ret == 0);
11971241

11981242
/* Start */

0 commit comments

Comments
 (0)