Skip to content

Commit 2148b1a

Browse files
nokute78edsiper
authored andcommitted
tests: runtime: out_lib: add test case for metrics input
Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent 2d8ff0b commit 2148b1a

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

tests/runtime/out_lib.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ static int cb_count_msgpack(void *record, size_t size, void *data)
240240
while (msgpack_unpack_next(&result, record, size, &off) == MSGPACK_UNPACK_SUCCESS) {
241241
pthread_mutex_lock(&result_mutex);
242242
num_output++;
243+
/*
244+
msgpack_object_print(stdout, result.data);
245+
*/
243246
pthread_mutex_unlock(&result_mutex);
244247
}
245248
msgpack_unpacked_destroy(&result);
@@ -248,6 +251,19 @@ static int cb_count_msgpack(void *record, size_t size, void *data)
248251
return 0;
249252
}
250253

254+
static int cb_count(void *record, size_t size, void *data)
255+
{
256+
if (!TEST_CHECK(data != NULL)) {
257+
TEST_MSG("data is NULL");
258+
}
259+
pthread_mutex_lock(&result_mutex);
260+
num_output++;
261+
pthread_mutex_unlock(&result_mutex);
262+
263+
flb_free(record);
264+
return 0;
265+
}
266+
251267
static struct test_ctx *test_ctx_create(struct flb_lib_out_cb *data)
252268
{
253269
int o_ffd;
@@ -440,8 +456,105 @@ static void test_max_records(void)
440456

441457
test_ctx_destroy(ctx);
442458
}
459+
#ifdef FLB_HAVE_METRICS
460+
static void test_metrics_msgpack(void)
461+
{
462+
struct flb_lib_out_cb cb_data;
463+
struct test_ctx *ctx;
464+
int ret;
465+
int num;
466+
int unused;
467+
468+
clear_output_num();
469+
470+
cb_data.cb = cb_count_msgpack;
471+
cb_data.data = &unused;
472+
473+
ctx = test_ctx_create(&cb_data);
474+
if (!TEST_CHECK(ctx != NULL)) {
475+
TEST_MSG("test_ctx_create failed");
476+
exit(EXIT_FAILURE);
477+
}
478+
/* Input */
479+
ctx->i_ffd = flb_input(ctx->flb, (char *) "fluentbit_metrics", NULL);
480+
TEST_CHECK(ctx->i_ffd >= 0);
481+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
482+
"scrape_interval", "1",
483+
NULL);
484+
TEST_CHECK(ret == 0);
485+
486+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
487+
"format", "msgpack",
488+
NULL);
489+
TEST_CHECK(ret == 0);
490+
491+
/* Start the engine */
492+
ret = flb_start(ctx->flb);
493+
TEST_CHECK(ret == 0);
494+
495+
/* waiting to flush */
496+
flb_time_msleep(1500);
497+
498+
num = get_output_num();
499+
if (!TEST_CHECK(num > 0)) {
500+
TEST_MSG("no outputs");
501+
}
502+
503+
test_ctx_destroy(ctx);
504+
}
505+
506+
static void test_metrics_json(void)
507+
{
508+
struct flb_lib_out_cb cb_data;
509+
struct test_ctx *ctx;
510+
int ret;
511+
int num;
512+
int unused;
513+
514+
clear_output_num();
515+
516+
cb_data.cb = cb_count;
517+
cb_data.data = &unused;
518+
519+
ctx = test_ctx_create(&cb_data);
520+
if (!TEST_CHECK(ctx != NULL)) {
521+
TEST_MSG("test_ctx_create failed");
522+
exit(EXIT_FAILURE);
523+
}
524+
/* Input */
525+
ctx->i_ffd = flb_input(ctx->flb, (char *) "fluentbit_metrics", NULL);
526+
TEST_CHECK(ctx->i_ffd >= 0);
527+
ret = flb_input_set(ctx->flb, ctx->i_ffd,
528+
"scrape_interval", "1",
529+
NULL);
530+
TEST_CHECK(ret == 0);
531+
532+
ret = flb_output_set(ctx->flb, ctx->o_ffd,
533+
"format", "json",
534+
NULL);
535+
TEST_CHECK(ret == 0);
536+
537+
/* Start the engine */
538+
ret = flb_start(ctx->flb);
539+
TEST_CHECK(ret == 0);
540+
541+
/* waiting to flush */
542+
flb_time_msleep(1500);
543+
544+
num = get_output_num();
545+
if (!TEST_CHECK(num > 0)) {
546+
TEST_MSG("no outputs");
547+
}
548+
549+
test_ctx_destroy(ctx);
550+
}
551+
#endif
443552

444553
TEST_LIST = {
554+
#ifdef FLB_HAVE_METRICS
555+
{"metrics_msgpack", test_metrics_msgpack},
556+
{"metrics_json", test_metrics_json},
557+
#endif
445558
{"format_json", test_format_json},
446559
{"format_msgpack", test_format_msgpack},
447560
{"max_records", test_max_records},

0 commit comments

Comments
 (0)