Skip to content

Commit df2cc51

Browse files
committed
tests: multiline: add tests for json multiline
Signed-off-by: lecaros <lecaros@chronosphere.io>
1 parent ae51533 commit df2cc51

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

tests/internal/multiline.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,41 @@ struct record_check go_output[] = {
364364
{"one more line, no multiline\n"}
365365
};
366366

367+
/* JSON (pretty-printed and single-line objects) */
368+
struct record_check json_input[] = {
369+
{"{\"id\":101,\"level\":\"info\",\"msg\":\"single-line record A\"}"},
370+
{"{"},
371+
{" \"id\": 102,"},
372+
{" \"level\": \"warn\","},
373+
{" \"msg\": \"multiline record B\""},
374+
{"}"},
375+
{"{\"id\":103,\"level\":\"info\",\"msg\":\"single-line record C\"}"},
376+
{"{"},
377+
{" \"id\": 104,"},
378+
{" \"level\": \"error\","},
379+
{" \"msg\": \"multiline record D\""},
380+
{"}"},
381+
};
382+
383+
struct record_check json_output[] = {
384+
{"{\"id\":101,\"level\":\"info\",\"msg\":\"single-line record A\"}\n"},
385+
{
386+
"{\n"
387+
" \"id\": 102,\n"
388+
" \"level\": \"warn\",\n"
389+
" \"msg\": \"multiline record B\"\n"
390+
"}\n"
391+
},
392+
{"{\"id\":103,\"level\":\"info\",\"msg\":\"single-line record C\"}\n"},
393+
{
394+
"{\n"
395+
" \"id\": 104,\n"
396+
" \"level\": \"error\",\n"
397+
" \"msg\": \"multiline record D\"\n"
398+
"}\n"
399+
},
400+
};
401+
367402
/*
368403
* Issue 3817 (case: 1)
369404
* --------------------
@@ -1191,14 +1226,63 @@ static void test_parser_go()
11911226
len = strlen(r->buf);
11921227

11931228
/* Package as msgpack */
1229+
-. flb_time_get(&tm);
1230+
flb_ml_append_text(ml, stream_id, &tm, r->buf, len);
1231+
}
1232+
1233+
if (ml) {
1234+
flb_ml_destroy(ml);
1235+
}
1236+
1237+
flb_config_exit(config);
1238+
}
1239+
1240+
static void test_parser_json()
1241+
{
1242+
int i;
1243+
int len;
1244+
int ret;
1245+
int entries;
1246+
uint64_t stream_id = 0;
1247+
struct record_check *r;
1248+
struct flb_config *config;
1249+
struct flb_time tm;
1250+
struct flb_ml *ml;
1251+
struct flb_ml_parser_ins *mlp_i;
1252+
struct expected_result res = {0};
1253+
1254+
res.key = "log";
1255+
res.out_records = json_output;
1256+
1257+
config = flb_config_init();
1258+
1259+
ml = flb_ml_create(config, "json-test");
1260+
TEST_CHECK(ml != NULL);
1261+
1262+
mlp_i = flb_ml_parser_instance_create(ml, "json");
1263+
TEST_CHECK(mlp_i != NULL);
1264+
1265+
ret = flb_ml_stream_create(ml, "json", -1, flush_callback, (void *) &res,
1266+
&stream_id);
1267+
TEST_CHECK(ret == 0);
1268+
1269+
entries = sizeof(json_input) / sizeof(struct record_check);
1270+
for (i = 0; i < entries; i++) {
1271+
r = &json_input[i];
1272+
len = strlen(r->buf);
1273+
11941274
flb_time_get(&tm);
11951275
flb_ml_append_text(ml, stream_id, &tm, r->buf, len);
11961276
}
11971277

1278+
flb_ml_flush_pending_now(ml);
1279+
11981280
if (ml) {
11991281
flb_ml_destroy(ml);
12001282
}
12011283

1284+
TEST_CHECK(res.current_record == (sizeof(json_output) / sizeof(struct record_check)));
1285+
12021286
flb_config_exit(config);
12031287
}
12041288

@@ -2129,6 +2213,7 @@ TEST_LIST = {
21292213
{ "parser_ruby", test_parser_ruby},
21302214
{ "parser_elastic", test_parser_elastic},
21312215
{ "parser_go", test_parser_go},
2216+
{ "parser_json", test_parser_json},
21322217
{ "container_mix", test_container_mix},
21332218
{ "endswith", test_endswith},
21342219
{ "buffer_limit_truncation", test_buffer_limit_truncation},

0 commit comments

Comments
 (0)