Skip to content

Commit 8a43a3e

Browse files
cosmo0920edsiper
authored andcommitted
out_chronicle: tests: Add runtime tests for formatting records
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 1365417 commit 8a43a3e

File tree

2 files changed

+271
-0
lines changed

2 files changed

+271
-0
lines changed

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ if(FLB_IN_LIB)
236236
FLB_RT_TEST(FLB_OUT_S3 "out_s3.c")
237237
FLB_RT_TEST(FLB_OUT_TD "out_td.c")
238238
FLB_RT_TEST(FLB_OUT_INFLUXDB "out_influxdb.c")
239+
FLB_RT_TEST(FLB_OUT_CHRONICLE "out_chronicle.c")
239240

240241
endif()
241242

tests/runtime/out_chronicle.c

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
#include <fluent-bit.h>
4+
#include "flb_tests_runtime.h"
5+
6+
7+
pthread_mutex_t result_mutex = PTHREAD_MUTEX_INITIALIZER;
8+
int num_invoked = 0;
9+
static int get_output_invoked()
10+
{
11+
int ret;
12+
pthread_mutex_lock(&result_mutex);
13+
ret = num_invoked;
14+
pthread_mutex_unlock(&result_mutex);
15+
16+
return ret;
17+
}
18+
19+
static void set_output_invoked(int num)
20+
{
21+
pthread_mutex_lock(&result_mutex);
22+
num_invoked = num;
23+
pthread_mutex_unlock(&result_mutex);
24+
}
25+
26+
static void clear_output_invoked()
27+
{
28+
set_output_invoked(0);
29+
}
30+
31+
static void cb_check_format_no_log_key(void *ctx, int ffd,
32+
int res_ret, void *res_data,
33+
size_t res_size, void *data)
34+
{
35+
char *out_json = res_data;
36+
char *p;
37+
38+
set_output_invoked(1);
39+
40+
p = strstr(out_json, "\"customer_id\":\"test-customer\"");
41+
if (!TEST_CHECK(p != NULL)) {
42+
TEST_MSG("Expected customer_id not found. Got: %s", out_json);
43+
}
44+
45+
p = strstr(out_json, "\"log_type\":\"TEST_LOG\"");
46+
if (!TEST_CHECK(p != NULL)) {
47+
TEST_MSG("Expected log_type not found. Got: %s", out_json);
48+
}
49+
50+
p = strstr(out_json, "\"entries\":[");
51+
if (!TEST_CHECK(p != NULL)) {
52+
TEST_MSG("Entries array not found. Got: %s", out_json);
53+
}
54+
55+
p = strstr(out_json, "\"log_text\":\"{\\\"message\\\":\\\"hello world\\\"}\"");
56+
if (!TEST_CHECK(p != NULL)) {
57+
TEST_MSG("Expected log_text not found. Got: %s", out_json);
58+
}
59+
60+
p = strstr(out_json, "\"ts_rfc3339\":");
61+
if (!TEST_CHECK(p != NULL)) {
62+
TEST_MSG("Expected ts_rfc3339 key not found. Got: %s", out_json);
63+
}
64+
65+
flb_sds_destroy(res_data);
66+
}
67+
68+
static void cb_check_format_with_log_key(void *ctx, int ffd,
69+
int res_ret, void *res_data,
70+
size_t res_size, void *data)
71+
{
72+
char *out_json = res_data;
73+
char *p;
74+
75+
if (out_json == NULL) {
76+
return;
77+
}
78+
79+
set_output_invoked(1);
80+
81+
p = strstr(out_json, "\"log_text\":\"This is the target message.\"");
82+
if (!TEST_CHECK(p != NULL)) {
83+
TEST_MSG("Expected log_text with specific value not found. Got: %s", out_json);
84+
}
85+
86+
p = strstr(out_json, "other_key");
87+
TEST_CHECK(p == NULL);
88+
89+
flb_sds_destroy(res_data);
90+
}
91+
92+
static void cb_check_format_multiple_records(void *ctx, int ffd,
93+
int res_ret, void *res_data,
94+
size_t res_size, void *data)
95+
{
96+
char *out_json = res_data;
97+
char *p1, *p2;
98+
99+
set_output_invoked(1);
100+
101+
p1 = strstr(out_json, "\"log_text\":\"{\\\"message\\\":\\\"record one\\\"}\"");
102+
if (!TEST_CHECK(p1 != NULL)) {
103+
TEST_MSG("First record not found. Got: %s", out_json);
104+
}
105+
106+
p2 = strstr(out_json, "\"log_text\":\"{\\\"message\\\":\\\"record two\\\"}\"");
107+
if (!TEST_CHECK(p2 != NULL)) {
108+
TEST_MSG("Second record not found. Got: %s", out_json);
109+
}
110+
111+
flb_sds_destroy(res_data);
112+
}
113+
114+
void test_format_no_log_key()
115+
{
116+
flb_ctx_t *ctx;
117+
int in_ffd, out_ffd;
118+
char record[1024];
119+
120+
ctx = flb_create();
121+
flb_service_set(ctx, "flush", "0.2", "grace", "1", "log_level", "error", NULL);
122+
123+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
124+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
125+
126+
out_ffd = flb_output(ctx, (char *) "chronicle", NULL);
127+
flb_output_set(ctx, out_ffd,
128+
"match", "test",
129+
"customer_id", "test-customer",
130+
"project_id", "TESTING_FORMAT",
131+
"log_type", "TEST_LOG",
132+
NULL);
133+
134+
flb_output_set_test(ctx, out_ffd, "formatter", cb_check_format_no_log_key, NULL, NULL);
135+
136+
flb_start(ctx);
137+
clear_output_invoked();
138+
139+
snprintf(record, sizeof(record) - 1, "[%ld, {\"message\": \"hello world\"}]", (long) time(NULL));
140+
flb_lib_push(ctx, in_ffd, record, strlen(record));
141+
142+
sleep(1);
143+
144+
TEST_CHECK(get_output_invoked() == 1);
145+
flb_stop(ctx);
146+
flb_destroy(ctx);
147+
}
148+
149+
void test_format_with_log_key_found()
150+
{
151+
flb_ctx_t *ctx;
152+
int in_ffd, out_ffd;
153+
char record[1024];
154+
155+
ctx = flb_create();
156+
flb_service_set(ctx, "flush", "0.2", "grace", "1", "log_level", "error", NULL);
157+
158+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
159+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
160+
161+
out_ffd = flb_output(ctx, (char *) "chronicle", NULL);
162+
flb_output_set(ctx, out_ffd,
163+
"match", "test",
164+
"customer_id", "test-customer",
165+
"project_id", "TESTING_FORMAT",
166+
"log_type", "TEST_LOG",
167+
"log_key", "message",
168+
NULL);
169+
170+
flb_output_set_test(ctx, out_ffd, "formatter", cb_check_format_with_log_key, NULL, NULL);
171+
172+
flb_start(ctx);
173+
clear_output_invoked();
174+
175+
snprintf(record, sizeof(record) - 1,
176+
"[%ld, {\"other_key\": \"some value\", \"message\": \"This is the target message.\"}]",
177+
(long) time(NULL));
178+
flb_lib_push(ctx, in_ffd, record, strlen(record));
179+
180+
sleep(1);
181+
182+
TEST_CHECK(get_output_invoked() == 1);
183+
flb_stop(ctx);
184+
flb_destroy(ctx);
185+
}
186+
187+
void test_format_with_log_key_not_found()
188+
{
189+
flb_ctx_t *ctx;
190+
int in_ffd, out_ffd;
191+
char record[1024];
192+
193+
ctx = flb_create();
194+
flb_service_set(ctx, "flush", "0.2", "grace", "1", "log_level", "error", NULL);
195+
196+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
197+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
198+
199+
out_ffd = flb_output(ctx, (char *) "chronicle", NULL);
200+
flb_output_set(ctx, out_ffd,
201+
"match", "test",
202+
"customer_id", "test-customer",
203+
"project_id", "TESTING_FORMAT",
204+
"log_type", "TEST_LOG",
205+
"log_key", "non_existent_key",
206+
NULL);
207+
208+
flb_output_set_test(ctx, out_ffd, "formatter", cb_check_format_with_log_key, NULL, NULL);
209+
210+
flb_start(ctx);
211+
clear_output_invoked();
212+
213+
snprintf(record, sizeof(record) - 1, "[%ld, {\"some_other_key\": \"some_value\"}]", (long) time(NULL));
214+
flb_lib_push(ctx, in_ffd, record, strlen(record));
215+
216+
sleep(1);
217+
218+
TEST_CHECK(get_output_invoked() == 0);
219+
flb_stop(ctx);
220+
flb_destroy(ctx);
221+
}
222+
223+
224+
void test_format_multiple_records()
225+
{
226+
flb_ctx_t *ctx;
227+
int in_ffd, out_ffd;
228+
char record1[1024];
229+
char record2[1024];
230+
time_t now = time(NULL);
231+
232+
ctx = flb_create();
233+
flb_service_set(ctx, "flush", "0.2", "grace", "1", "log_level", "error", NULL);
234+
235+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
236+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
237+
238+
out_ffd = flb_output(ctx, (char *) "chronicle", NULL);
239+
flb_output_set(ctx, out_ffd,
240+
"match", "test",
241+
"customer_id", "test-customer",
242+
"project_id", "TESTING_FORMAT",
243+
"log_type", "TEST_LOG",
244+
NULL);
245+
246+
flb_output_set_test(ctx, out_ffd, "formatter", cb_check_format_multiple_records, NULL, NULL);
247+
248+
flb_start(ctx);
249+
clear_output_invoked();
250+
251+
snprintf(record1, sizeof(record1) - 1, "[%ld, {\"message\": \"record one\"}]", (long) now);
252+
snprintf(record2, sizeof(record2) - 1, "[%ld, {\"message\": \"record two\"}]", (long) now + 1);
253+
254+
flb_lib_push(ctx, in_ffd, record1, strlen(record1));
255+
flb_lib_push(ctx, in_ffd, record2, strlen(record2));
256+
257+
sleep(1);
258+
259+
TEST_CHECK(get_output_invoked() == 1);
260+
flb_stop(ctx);
261+
flb_destroy(ctx);
262+
}
263+
264+
TEST_LIST = {
265+
{ "format_no_log_key", test_format_no_log_key },
266+
{ "format_with_log_key_found", test_format_with_log_key_found },
267+
{ "format_with_log_key_not_found", test_format_with_log_key_not_found },
268+
{ "format_multiple_records", test_format_multiple_records },
269+
{ NULL, NULL }
270+
};

0 commit comments

Comments
 (0)