Skip to content

Commit c8c23af

Browse files
committed
in_tail: tests: Add UTF-16LE and UTF-16BE with BOM test cases for unicode encoder testing
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 4188d63 commit c8c23af

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed
62 Bytes
Binary file not shown.
134 Bytes
Binary file not shown.
68 Bytes
Binary file not shown.
134 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"log":"在 Fluent Bit 中处理汉字,感觉像是在做梦😀。"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"log":"にほんごテストログふぁいる。文字エンコーディングをUnicodeにできる!?☕😀⚪⚫🔴🔵🟠🟡🟢🟣🟤🇺🇸🇯🇵"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"log":"用汉字在 Fluent Bit 中处理日志,就像是一个梦一样😀"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"log":"にほんごテストログふぁいる。文字エンコーディングをUnicodeにできる!?☕😀⚪⚫🔴🔵🟠🟡🟢🟣🟤🇺🇸🇯🇵"}

tests/runtime/in_tail.c

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Approach for this tests is basing on filter_kubernetes tests
2626
#include <fluent-bit/flb_time.h>
2727
#include <fluent-bit/flb_pthread.h>
2828
#include <fluent-bit/flb_compat.h>
29+
#ifdef FLB_HAVE_UNICODE_ENCODER
30+
#include <fluent-bit/simdutf/flb_simdutf_connector.h>
31+
#endif
2932
#include <stdlib.h>
3033
#include <sys/stat.h>
3134
#include <sys/types.h>
@@ -436,6 +439,52 @@ static int cb_check_result(void *record, size_t size, void *data)
436439
return 0;
437440
}
438441

442+
#ifdef FLB_HAVE_UNICODE_ENCODER
443+
static int cb_check_result_unicode(void *record, size_t size, void *data)
444+
{
445+
struct tail_test_result *result;
446+
struct tail_file_lines *out;
447+
int valid = FLB_FALSE;
448+
449+
result = (struct tail_test_result *) data;
450+
451+
char *check;
452+
453+
out = get_out_file_content(result->target);
454+
if (!out->lines_c) {
455+
goto exit;
456+
}
457+
458+
valid = flb_simdutf_connector_validate_utf8(record, size);
459+
if (valid == FLB_FALSE) {
460+
goto exit;
461+
}
462+
/*
463+
* Our validation is: check that the one of the output lines
464+
* in the output record.
465+
*/
466+
int i;
467+
result->nLines = out->lines_c;
468+
for (i=0; i<out->lines_c; i++) {
469+
check = strstr(record, out->lines[i]);
470+
if (check != NULL) {
471+
result->nMatched++;
472+
goto exit;
473+
}
474+
}
475+
result->nNotMatched++;
476+
exit:
477+
if (size > 0) {
478+
flb_free(record);
479+
}
480+
if (out->lines_c) {
481+
flb_free(out->lines[0]);
482+
flb_free(out);
483+
}
484+
return 0;
485+
}
486+
#endif
487+
439488
void do_test(char *system, const char *target, int tExpected, int nExpected, ...)
440489
{
441490
int64_t ret;
@@ -557,6 +606,121 @@ void flb_test_in_tail_dockermode_firstline_detection()
557606
NULL);
558607
}
559608

609+
#ifdef FLB_HAVE_UNICODE_ENCODER
610+
void do_test_unicode(char *system, const char *target, int nExpected, ...)
611+
{
612+
int64_t ret;
613+
flb_ctx_t *ctx = NULL;
614+
int in_ffd;
615+
int out_ffd;
616+
va_list va;
617+
char *key;
618+
char *value;
619+
char path[PATH_MAX];
620+
struct tail_test_result result = {0};
621+
622+
result.nMatched = 0;
623+
result.target = target;
624+
625+
struct flb_lib_out_cb cb;
626+
cb.cb = cb_check_result_unicode;
627+
cb.data = &result;
628+
629+
/* initialize */
630+
set_result(0);
631+
632+
ctx = flb_create();
633+
634+
ret = flb_service_set(ctx,
635+
"Log_Level", "error",
636+
NULL);
637+
TEST_CHECK_(ret == 0, "setting service options");
638+
639+
in_ffd = flb_input(ctx, (char *) system, NULL);
640+
TEST_CHECK(in_ffd >= 0);
641+
TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0);
642+
643+
/* Compose path based on target */
644+
snprintf(path, sizeof(path) - 1, DPATH "/log/%s.log", target);
645+
TEST_CHECK_(access(path, R_OK) == 0, "accessing log file: %s", path);
646+
647+
TEST_CHECK(flb_input_set(ctx, in_ffd,
648+
"path" , path,
649+
"read_from_head", "true",
650+
NULL) == 0);
651+
652+
va_start(va, nExpected);
653+
while ((key = va_arg(va, char *))) {
654+
value = va_arg(va, char *);
655+
TEST_CHECK(value != NULL);
656+
TEST_CHECK(flb_input_set(ctx, in_ffd, key, value, NULL) == 0);
657+
}
658+
va_end(va);
659+
660+
out_ffd = flb_output(ctx, (char *) "lib", &cb);
661+
TEST_CHECK(out_ffd >= 0);
662+
TEST_CHECK(flb_output_set(ctx, out_ffd,
663+
"match", "test",
664+
"format", "json",
665+
NULL) == 0);
666+
667+
TEST_CHECK(flb_service_set(ctx, "Flush", "0.5",
668+
"Grace", "1",
669+
NULL) == 0);
670+
671+
/* Start test */
672+
/* Start the engine */
673+
ret = flb_start(ctx);
674+
TEST_CHECK_(ret == 0, "starting engine");
675+
676+
/* /\* Poll for up to 5 seconds or until we got a match *\/ */
677+
/* for (ret = 0; result.nMatched <= nExpected; ret++) { */
678+
/* usleep(1000); */
679+
/* } */
680+
681+
/* Wait until matching nExpected results */
682+
wait_with_timeout(5000, &result, nExpected);
683+
684+
TEST_CHECK(result.nMatched == nExpected);
685+
TEST_MSG("result.nMatched: %i\nnExpected: %i", result.nMatched, nExpected);
686+
687+
ret = flb_stop(ctx);
688+
TEST_CHECK_(ret == 0, "stopping engine");
689+
690+
if (ctx) {
691+
flb_destroy(ctx);
692+
}
693+
}
694+
695+
void flb_test_in_tail_utf16le_c()
696+
{
697+
do_test_unicode("tail", "unicode_c", 1,
698+
"Unicode.Encoding", "auto",
699+
NULL);
700+
}
701+
702+
void flb_test_in_tail_utf16be_c()
703+
{
704+
do_test_unicode("tail", "unicode_be_c", 1,
705+
"Unicode.Encoding", "auto",
706+
NULL);
707+
}
708+
709+
void flb_test_in_tail_utf16le_j()
710+
{
711+
do_test_unicode("tail", "unicode_j", 1,
712+
"Unicode.Encoding", "auto",
713+
NULL);
714+
}
715+
716+
void flb_test_in_tail_utf16be_j()
717+
{
718+
do_test_unicode("tail", "unicode_be_j", 1,
719+
"Unicode.Encoding", "auto",
720+
NULL);
721+
}
722+
#endif
723+
560724
int write_long_lines(int fd) {
561725
ssize_t ret;
562726
int i;
@@ -1978,6 +2142,13 @@ TEST_LIST = {
19782142
{"db_compare_filename", flb_test_db_compare_filename},
19792143
#endif
19802144

2145+
#ifdef FLB_HAVE_UNICODE_ENCODER
2146+
{"utf16le_c", flb_test_in_tail_utf16le_c},
2147+
{"utf16be_c", flb_test_in_tail_utf16be_c},
2148+
{"utf16le_j", flb_test_in_tail_utf16le_j},
2149+
{"utf16be_j", flb_test_in_tail_utf16be_j},
2150+
#endif
2151+
19812152
#ifdef in_tail
19822153
{"in_tail_dockermode", flb_test_in_tail_dockermode},
19832154
{"in_tail_dockermode_splitted_line", flb_test_in_tail_dockermode_splitted_line},

0 commit comments

Comments
 (0)