Skip to content

Commit

Permalink
tests/internal: add parser_json time fail test
Browse files Browse the repository at this point in the history
Add a test for a JSON that fails timestamp parsing to verify that the
time key remains in the result.

Signed-off-by: braydonk <braydonk@google.com>
  • Loading branch information
braydonk committed May 4, 2023
1 parent 90b9c8a commit 6344dea
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/internal/parser_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,69 @@ void test_decode_field_json()
flb_config_exit(config);
}

void test_time_key_kept_if_parse_fails()
{
struct flb_parser *parser = NULL;
struct flb_config *config = NULL;
int ret = 0;
char *input = "{\"str\":\"text\", \"time\":\"nonsense\"}";
char *time_format = "%Y-%m-%dT%H:%M:%S.%L";
void *out_buf = NULL;
size_t out_size = 0;
struct flb_time out_time;
char *expected_strs[] = {"str", "text", "time", "nonsense"};
struct str_list expected = {
.size = sizeof(expected_strs)/sizeof(char*),
.lists = &expected_strs[0],
};

out_time.tm.tv_sec = 0;
out_time.tm.tv_nsec = 0;

config = flb_config_init();
if(!TEST_CHECK(config != NULL)) {
TEST_MSG("flb_config_init failed");
exit(1);
}

parser = flb_parser_create("json", "json", NULL, FLB_FALSE, time_format, "time", NULL,
FLB_FALSE, FLB_TRUE, FLB_FALSE,
NULL, 0, NULL, config);
if (!TEST_CHECK(parser != NULL)) {
TEST_MSG("flb_parser_create failed");
flb_config_exit(config);
exit(1);
}

ret = flb_parser_do(parser, input, strlen(input), &out_buf, &out_size, &out_time);
if (!TEST_CHECK(ret != -1)) {
TEST_MSG("flb_parser_do failed");
flb_parser_destroy(parser);
flb_config_exit(config);
exit(1);
}

ret = compare_msgpack(out_buf, out_size, &expected);
if (!TEST_CHECK(ret == 0)) {
TEST_MSG("compare failed");
flb_free(out_buf);
flb_parser_destroy(parser);
flb_config_exit(config);
exit(1);
}

flb_free(out_buf);
flb_parser_destroy(parser);
flb_config_exit(config);
}


TEST_LIST = {
{ "basic", test_basic},
{ "time_key", test_time_key},
{ "time_keep", test_time_keep},
{ "types_is_not_supported", test_types_is_not_supported},
{ "decode_field_json", test_decode_field_json},
{ "time_key_kept_if_parse_fails", test_time_key_kept_if_parse_fails},
{ 0 }
};

0 comments on commit 6344dea

Please sign in to comment.