Skip to content

Commit 8d2cf71

Browse files
committed
in_tail: tests: Add test cases for long line truncations
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 57a1f26 commit 8d2cf71

File tree

1 file changed

+232
-0
lines changed

1 file changed

+232
-0
lines changed

tests/runtime/in_tail.c

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,236 @@ void flb_test_in_tail_skip_long_lines()
991991
unlink(path);
992992
}
993993

994+
static int write_long_ascii_line(int fd, size_t total_bytes)
995+
{
996+
const char *chunk = "0123456789abcdef0123456789abcdef"; /* 32 bytes */
997+
size_t chunk_len = strlen(chunk);
998+
size_t written = 0;
999+
ssize_t ret;
1000+
size_t rest = 0;
1001+
1002+
while (written + chunk_len <= total_bytes) {
1003+
ret = write(fd, chunk, chunk_len);
1004+
if (ret < 0) {
1005+
flb_errno();
1006+
return -1;
1007+
}
1008+
written += (size_t) ret;
1009+
}
1010+
if (written < total_bytes) {
1011+
rest = total_bytes - written;
1012+
ret = write(fd, chunk, rest);
1013+
if (ret < 0) {
1014+
flb_errno();
1015+
return -1;
1016+
}
1017+
written += (size_t) ret;
1018+
}
1019+
if (write(fd, "\n", 1) != 1) {
1020+
flb_errno();
1021+
return -1;
1022+
}
1023+
return 0;
1024+
}
1025+
1026+
static int write_long_utf8_line(int fd, size_t total_bytes)
1027+
{
1028+
const char *u8_aa = "あ";
1029+
size_t u8_len = strlen(u8_aa); /* 3 */
1030+
size_t written = 0;
1031+
ssize_t ret;
1032+
const char *ascii = "XYZ";
1033+
size_t rest = 0;
1034+
1035+
while (written + u8_len <= total_bytes) {
1036+
ret = write(fd, u8_aa, u8_len);
1037+
if (ret < 0) {
1038+
flb_errno();
1039+
return -1;
1040+
}
1041+
written += (size_t) ret;
1042+
}
1043+
1044+
if (written < total_bytes) {
1045+
rest = total_bytes - written;
1046+
if (rest > strlen(ascii)) {
1047+
rest = strlen(ascii);
1048+
}
1049+
ret = write(fd, ascii, rest);
1050+
if (ret < 0) {
1051+
flb_errno();
1052+
return -1;
1053+
}
1054+
written += (size_t) ret;
1055+
}
1056+
if (write(fd, "\n", 1) != 1) {
1057+
flb_errno();
1058+
return -1;
1059+
}
1060+
return 0;
1061+
}
1062+
1063+
void flb_test_in_tail_truncate_long_lines()
1064+
{
1065+
int64_t ret;
1066+
flb_ctx_t *ctx = NULL;
1067+
int in_ffd, out_ffd;
1068+
char path[PATH_MAX];
1069+
struct tail_test_result result = {0};
1070+
int fd;
1071+
1072+
const char *target = "truncate_long_lines_basic";
1073+
int nExpected = 3; /* before + truncated long line + after */
1074+
int nExpectedNotMatched = 0; /* unused */
1075+
int nExpectedLines = 0; /* unused */
1076+
1077+
struct flb_lib_out_cb cb;
1078+
int unused = 0;
1079+
int num = 0;
1080+
1081+
cb.cb = cb_count_msgpack;
1082+
cb.data = &unused;
1083+
1084+
clear_output_num();
1085+
1086+
ctx = flb_create();
1087+
TEST_CHECK_(ctx != NULL, "flb_create failed");
1088+
1089+
TEST_CHECK_(flb_service_set(ctx, "Log_Level", "error", NULL) == 0,
1090+
"setting service options");
1091+
1092+
in_ffd = flb_input(ctx, "tail", NULL);
1093+
TEST_CHECK(in_ffd >= 0);
1094+
TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0);
1095+
1096+
snprintf(path, sizeof(path) - 1, DPATH "/log/%s.log", target);
1097+
fd = creat(path, S_IRWXU | S_IRGRP);
1098+
TEST_CHECK(fd >= 0);
1099+
1100+
write(fd, "before_long_line\n", strlen("before_long_line\n"));
1101+
1102+
TEST_CHECK(write_long_ascii_line(fd, 10 * 1024) == 0);
1103+
1104+
write(fd, "after_long_line\n", strlen("after_long_line\n"));
1105+
close(fd);
1106+
1107+
TEST_CHECK_(access(path, R_OK) == 0, "accessing log file: %s", path);
1108+
1109+
TEST_CHECK(flb_input_set(ctx, in_ffd,
1110+
"path", path,
1111+
"read_from_head", "true",
1112+
"truncate_long_lines", "on",
1113+
"skip_long_lines", "off",
1114+
"Buffer_Chunk_Size", "1k",
1115+
"Buffer_Max_Size", "4k",
1116+
NULL) == 0);
1117+
1118+
out_ffd = flb_output(ctx, "lib", &cb);
1119+
TEST_CHECK(out_ffd >= 0);
1120+
TEST_CHECK(flb_output_set(ctx, out_ffd,
1121+
"match", "test",
1122+
NULL) == 0);
1123+
1124+
TEST_CHECK(flb_service_set(ctx, "Flush", "0.5",
1125+
"Grace", "1",
1126+
NULL) == 0);
1127+
1128+
ret = flb_start(ctx);
1129+
TEST_CHECK_(ret == 0, "starting engine");
1130+
1131+
wait_num_with_timeout(5000, &num);
1132+
1133+
num = get_output_num();
1134+
TEST_CHECK(num == nExpected);
1135+
TEST_MSG("output count (truncate basic): got=%d expected=%d", num, nExpected);
1136+
1137+
ret = flb_stop(ctx);
1138+
TEST_CHECK_(ret == 0, "stopping engine");
1139+
1140+
if (ctx) {
1141+
flb_destroy(ctx);
1142+
}
1143+
1144+
unlink(path);
1145+
}
1146+
1147+
void flb_test_in_tail_truncate_long_lines_utf8()
1148+
{
1149+
int64_t ret;
1150+
flb_ctx_t *ctx = NULL;
1151+
int in_ffd, out_ffd;
1152+
char path[PATH_MAX];
1153+
int fd;
1154+
1155+
const char *target = "truncate_long_lines_utf8";
1156+
int nExpected = 1;
1157+
1158+
struct flb_lib_out_cb cb;
1159+
int unused = 0;
1160+
int num = 0;
1161+
1162+
cb.cb = cb_count_msgpack;
1163+
cb.data = &unused;
1164+
1165+
clear_output_num();
1166+
1167+
ctx = flb_create();
1168+
TEST_CHECK_(ctx != NULL, "flb_create failed");
1169+
1170+
TEST_CHECK_(flb_service_set(ctx, "Log_Level", "error", NULL) == 0,
1171+
"setting service options");
1172+
1173+
in_ffd = flb_input(ctx, "tail", NULL);
1174+
TEST_CHECK(in_ffd >= 0);
1175+
TEST_CHECK(flb_input_set(ctx, in_ffd, "tag", "test", NULL) == 0);
1176+
1177+
snprintf(path, sizeof(path) - 1, DPATH "/log/%s.log", target);
1178+
fd = creat(path, S_IRWXU | S_IRGRP);
1179+
TEST_CHECK(fd >= 0);
1180+
1181+
TEST_CHECK(write_long_utf8_line(fd, 10 * 1024) == 0);
1182+
close(fd);
1183+
1184+
TEST_CHECK_(access(path, R_OK) == 0, "accessing log file: %s", path);
1185+
1186+
TEST_CHECK(flb_input_set(ctx, in_ffd,
1187+
"path", path,
1188+
"read_from_head", "true",
1189+
"truncate_long_lines", "on",
1190+
"skip_long_lines", "off",
1191+
"Buffer_Chunk_Size", "1k",
1192+
"Buffer_Max_Size", "4k",
1193+
NULL) == 0);
1194+
1195+
out_ffd = flb_output(ctx, "lib", &cb);
1196+
TEST_CHECK(out_ffd >= 0);
1197+
TEST_CHECK(flb_output_set(ctx, out_ffd,
1198+
"match", "test",
1199+
NULL) == 0);
1200+
1201+
TEST_CHECK(flb_service_set(ctx, "Flush", "0.5",
1202+
"Grace", "1",
1203+
NULL) == 0);
1204+
1205+
ret = flb_start(ctx);
1206+
TEST_CHECK_(ret == 0, "starting engine");
1207+
1208+
wait_num_with_timeout(5000, &num);
1209+
1210+
num = get_output_num();
1211+
TEST_CHECK(num == nExpected);
1212+
TEST_MSG("output count (truncate utf8): got=%d expected=%d", num, nExpected);
1213+
1214+
ret = flb_stop(ctx);
1215+
TEST_CHECK_(ret == 0, "stopping engine");
1216+
1217+
if (ctx) {
1218+
flb_destroy(ctx);
1219+
}
1220+
1221+
unlink(path);
1222+
}
1223+
9941224
/*
9951225
* test case for https://github.com/fluent/fluent-bit/issues/3943
9961226
*
@@ -2426,6 +2656,8 @@ TEST_LIST = {
24262656
{"issue_3943", flb_test_in_tail_issue_3943},
24272657
/* Properties */
24282658
{"skip_long_lines", flb_test_in_tail_skip_long_lines},
2659+
{"truncate_long_lines", flb_test_in_tail_truncate_long_lines},
2660+
{"truncate_long_lines_utf8", flb_test_in_tail_truncate_long_lines_utf8},
24292661
{"path_comma", flb_test_path_comma},
24302662
{"path_key", flb_test_path_key},
24312663
{"exclude_path", flb_test_exclude_path},

0 commit comments

Comments
 (0)