Skip to content

Commit bd123ad

Browse files
nokute78edsiper
authored andcommitted
tests: runtime: filter_lua: add type_array_key test
Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent bb11c2c commit bd123ad

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

tests/runtime/filter_lua.c

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void flb_test_type_int_key(void)
9292
char *script_body = ""
9393
"function lua_main(tag, timestamp, record)\n"
9494
" new_record = record\n"
95-
" new_record[\"lua_int\"] = 10\n"
95+
" new_record[\"lua_int\"] = 10.2\n"
9696
" return 1, timestamp, new_record\n"
9797
"end\n";
9898

@@ -136,9 +136,13 @@ void flb_test_type_int_key(void)
136136
sleep(1);
137137
output = get_output();
138138
result = strstr(output, "\"lua_int\":10.");
139-
TEST_CHECK(result == NULL);
139+
if(!TEST_CHECK(result == NULL)) {
140+
TEST_MSG("output:%s\n", output);
141+
}
140142
result = strstr(output, "\"lua_int\":10");
141-
TEST_CHECK(result != NULL);
143+
if(!TEST_CHECK(result != NULL)) {
144+
TEST_MSG("output:%s\n", output);
145+
}
142146

143147
/* clean up */
144148
flb_lib_free(output);
@@ -355,10 +359,88 @@ void flb_test_helloworld(void)
355359
flb_destroy(ctx);
356360
}
357361

362+
// https://github.com/fluent/fluent-bit/issues/3343
363+
void flb_test_type_array_key(void)
364+
{
365+
int ret;
366+
flb_ctx_t *ctx;
367+
int in_ffd;
368+
int out_ffd;
369+
int filter_ffd;
370+
char *output = NULL;
371+
char *input = "[0, {\"key\":\"val\"}]";
372+
char *result;
373+
struct flb_lib_out_cb cb_data;
374+
375+
char *script_body = ""
376+
"function lua_main(tag, timestamp, record)\n"
377+
" new_record = record\n"
378+
" new_record[\"lua_array\"] = {};\n"
379+
" new_record[\"lua_array2\"] = {1,2,3};\n"
380+
" return 1, timestamp, new_record\n"
381+
"end\n";
382+
383+
/* Create context, flush every second (some checks omitted here) */
384+
ctx = flb_create();
385+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
386+
387+
/* Prepare output callback context*/
388+
cb_data.cb = callback_test;
389+
cb_data.data = NULL;
390+
391+
ret = create_script(script_body, strlen(script_body));
392+
TEST_CHECK(ret == 0);
393+
/* Filter */
394+
filter_ffd = flb_filter(ctx, (char *) "lua", NULL);
395+
TEST_CHECK(filter_ffd >= 0);
396+
ret = flb_filter_set(ctx, filter_ffd,
397+
"Match", "*",
398+
"call", "lua_main",
399+
"type_array_key", "lua_array lua_array2",
400+
"script", TMP_LUA_PATH,
401+
NULL);
402+
403+
/* Input */
404+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
405+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
406+
TEST_CHECK(in_ffd >= 0);
407+
408+
/* Lib output */
409+
out_ffd = flb_output(ctx, (char *) "lib", (void *)&cb_data);
410+
TEST_CHECK(out_ffd >= 0);
411+
flb_output_set(ctx, out_ffd,
412+
"match", "test",
413+
"format", "json",
414+
NULL);
415+
416+
ret = flb_start(ctx);
417+
TEST_CHECK(ret==0);
418+
419+
flb_lib_push(ctx, in_ffd, input, strlen(input));
420+
sleep(1);
421+
output = get_output();
422+
result = strstr(output, "\"lua_array\":[]");
423+
if(!TEST_CHECK(result != NULL)) {
424+
TEST_MSG("output:%s\n", output);
425+
}
426+
result = strstr(output, "\"lua_array2\":[1,2,3]");
427+
if(!TEST_CHECK(result != NULL)) {
428+
TEST_MSG("output:%s\n", output);
429+
}
430+
431+
/* clean up */
432+
flb_lib_free(output);
433+
delete_script();
434+
435+
flb_stop(ctx);
436+
flb_destroy(ctx);
437+
}
438+
358439
TEST_LIST = {
359440
{"hello_world", flb_test_helloworld},
360441
{"append_tag", flb_test_append_tag},
361442
{"type_int_key", flb_test_type_int_key},
362443
{"type_int_key_multi", flb_test_type_int_key_multi},
444+
{"type_array_key", flb_test_type_array_key},
363445
{NULL, NULL}
364446
};

0 commit comments

Comments
 (0)