Skip to content

Commit

Permalink
out_es: test: Add more test cases for using real HTTP responses
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Sep 18, 2024
1 parent d7c9918 commit 1d61715
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/runtime/data/es/json_es.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,37 @@
#define JSON_DOTS \
"[1448403340," \
"{\".le.vel\":\"error\", \".fo.o\":[{\".o.k\": [{\".b.ar\": \"baz\"}]}]}]"

#define JSON_RESPONSE_SUCCESSES "{\"errors\":false,\"took\":0,\"items\":[" \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"dcfJBJIBHhdJuKsoC7Tm\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":6,\"_primary_term\":1,\"status\":201}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"dsfJBJIBHhdJuKsoC7Tm\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":7,\"_primary_term\":1,\"status\":201}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"d8fJBJIBHhdJuKsoC7Tm\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":8,\"_primary_term\":1,\"status\":201}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"eMfJBJIBHhdJuKsoC7Tm\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":9,\"_primary_term\":1,\"status\":201}}]}"

#define JSON_RESPONSE_SUCCESSES_SIZE 783

#define JSON_RESPONSE_PARTIALLY_SUCCESS "{\"errors\":true,\"took\":316737025,\"items\":" \
"[{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"hxELapEB_XqxG5Ydupgb\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":7,\"_primary_term\":1,\"status\":201}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"iBELapEB_XqxG5Ydupgb\",\"status\":400," \
"\"error\":{\"type\":\"document_parsing_exception\"," \
"\"reason\":\"[1:65] failed to parse field [_id] of type [_id] in document with id 'iBELapEB_XqxG5Ydupgb'. " \
"Preview of field's value: 'fhHraZEB_XqxG5Ydzpjv'\"," \
"\"caused_by\":{\"type\":\"document_parsing_exception\"," \
"\"reason\":\"[1:65] Field [_id] is a metadata field and cannot be added inside a document. " \
"Use the index API request parameters.\"}}}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"iRELapEB_XqxG5Ydupgb\",\"status\":400," \
"\"error\":{\"type\":\"document_parsing_exception\"," \
"\"reason\":\"[1:65] failed to parse field [_id] of type [_id] in document with id 'iRELapEB_XqxG5Ydupgb'. " \
"Preview of field's value: 'fhHraZEB_XqxG5Ydzpjv'\"," \
"\"caused_by\":{\"type\":\"document_parsing_exception\"," \
"\"reason\":\"[1:65] Field [_id] is a metadata field and cannot be added inside a document. " \
"Use the index API request parameters.\"}}}}," \
"{\"create\":{\"_index\":\"fluent-bit\",\"_id\":\"ihELapEB_XqxG5Ydupgb\",\"_version\":1,\"result\":\"created\"," \
"\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":8,\"_primary_term\":1,\"status\":201}}]}"

#define JSON_RESPONSE_PARTIALLY_SUCCESS_SIZE 1322
107 changes: 107 additions & 0 deletions tests/runtime/out_elasticsearch.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,111 @@ void flb_test_response_success()
flb_destroy(ctx);
}

void flb_test_response_successes()
{
int ret;
char *response = JSON_RESPONSE_SUCCESSES;
int size = JSON_RESPONSE_SUCCESSES_SIZE;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

/* Create context, flush every second (some checks omitted here) */
ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

/* Lib input mode */
in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

/* Elasticsearch output */
out_ffd = flb_output(ctx, (char *) "es", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
NULL);

/* Override defaults of index and type */
flb_output_set(ctx, out_ffd,
"write_operation", "create",
NULL);

/* Enable test mode */
ret = flb_output_set_http_test(ctx, out_ffd, "response",
cb_check_response_success,
NULL);

/* Start */
ret = flb_start(ctx);
TEST_CHECK(ret == 0);

/* Ingest data sample */
ret = flb_lib_response(ctx, out_ffd, 200, response, size);
TEST_CHECK(ret == 0);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}

static void cb_check_response_partially_success(void *ctx, int ffd,
int res_ret, void *res_data,
size_t res_size, void *data)
{
int composed_ret = 0;
composed_ret |= (1 << 0);
composed_ret |= (1 << 7);

TEST_CHECK(res_ret == composed_ret);
/* Check whether contains a success flag or not */
TEST_CHECK((res_ret & (1 << 0)));
}

void flb_test_response_partially_success()
{
int ret;
char *response = JSON_RESPONSE_PARTIALLY_SUCCESS;
int size = JSON_RESPONSE_PARTIALLY_SUCCESS_SIZE;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

/* Create context, flush every second (some checks omitted here) */
ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

/* Lib input mode */
in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

/* Elasticsearch output */
out_ffd = flb_output(ctx, (char *) "es", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
NULL);

/* Override defaults of index and type */
flb_output_set(ctx, out_ffd,
"write_operation", "create",
NULL);

/* Enable test mode */
ret = flb_output_set_http_test(ctx, out_ffd, "response",
cb_check_response_partially_success,
NULL);

/* Start */
ret = flb_start(ctx);
TEST_CHECK(ret == 0);

/* Ingest data sample */
ret = flb_lib_response(ctx, out_ffd, 200, response, size);
TEST_CHECK(ret == 0);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}

/* Test list */
TEST_LIST = {
{"long_index" , flb_test_long_index },
Expand All @@ -868,5 +973,7 @@ TEST_LIST = {
{"id_key" , flb_test_id_key },
{"logstash_prefix_separator" , flb_test_logstash_prefix_separator },
{"response_success" , flb_test_response_success },
{"response_successes", flb_test_response_successes },
{"response_partially_success" , flb_test_response_partially_success },
{NULL, NULL}
};

0 comments on commit 1d61715

Please sign in to comment.