Skip to content

Commit e0de49c

Browse files
nokute78edsiper
authored andcommitted
tests: internal: log_event_decoder: add test for log_event_decoder
Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent d743f7c commit e0de49c

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed

tests/internal/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ set(UNIT_TESTS_FILES
3838
parser_logfmt.c
3939
env.c
4040
log.c
41+
log_event_decoder.c
4142
processor.c
4243
)
4344

tests/internal/log_event_decoder.c

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2023 The Fluent Bit Authors
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#include <fluent-bit/flb_info.h>
21+
#include <fluent-bit/flb_time.h>
22+
#include <fluent-bit/flb_pack.h>
23+
#include <fluent-bit/flb_log_event_decoder.h>
24+
#include <msgpack.h>
25+
#include <string.h>
26+
27+
#include "flb_tests_internal.h"
28+
29+
static int pack_event_time(msgpack_packer *pck, struct flb_time *tm)
30+
{
31+
char ext_data[8] = {0};
32+
uint32_t tmp;
33+
34+
/* event time */
35+
tmp = htonl((uint32_t)tm->tm.tv_sec); /* second from epoch */
36+
memcpy(&ext_data, &tmp, 4);
37+
tmp = htonl((uint32_t)tm->tm.tv_nsec);/* nanosecond */
38+
memcpy(&ext_data[4], &tmp, 4);
39+
40+
msgpack_pack_ext(pck, 8, 0);
41+
msgpack_pack_ext_body(pck, ext_data, sizeof(ext_data));
42+
43+
return 0;
44+
}
45+
46+
void create_destroy()
47+
{
48+
struct flb_log_event_decoder *dec = NULL;
49+
char buf[256] = {0};
50+
51+
dec = flb_log_event_decoder_create(&buf[0], sizeof(buf));
52+
if (!TEST_CHECK(dec != NULL)) {
53+
TEST_MSG("flb_log_event_decoder_create failed");
54+
return;
55+
}
56+
57+
flb_log_event_decoder_destroy(dec);
58+
}
59+
60+
void init_destroy()
61+
{
62+
struct flb_log_event_decoder dec;
63+
char buf[256] = {0};
64+
int ret;
65+
66+
ret = flb_log_event_decoder_init(&dec, &buf[0], sizeof(buf));
67+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
68+
TEST_MSG("flb_log_event_decoder_init failed. ret=%s",
69+
flb_log_event_decoder_get_error_description(ret));
70+
return;
71+
}
72+
73+
flb_log_event_decoder_destroy(&dec);
74+
}
75+
76+
void decode_timestamp()
77+
{
78+
struct flb_time tm;
79+
msgpack_sbuffer sbuf;
80+
msgpack_packer pck;
81+
msgpack_unpacked result;
82+
size_t offset = 0;
83+
84+
int ret;
85+
86+
msgpack_sbuffer_init(&sbuf);
87+
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
88+
msgpack_pack_int64(&pck, 123456);
89+
90+
msgpack_unpacked_init(&result);
91+
msgpack_unpack_next(&result, sbuf.data, sbuf.size, &offset);
92+
93+
ret = flb_log_event_decoder_decode_timestamp(&result.data, &tm);
94+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
95+
TEST_MSG("flb_log_event_decoder_timestamp failed. ret=%s",
96+
flb_log_event_decoder_get_error_description(ret));
97+
return;
98+
}
99+
if (!TEST_CHECK(tm.tm.tv_sec == 123456 && tm.tm.tv_nsec == 0)) {
100+
TEST_MSG("timestamp error. tv_sec=%ld tv_nsec=%lu", tm.tm.tv_sec, tm.tm.tv_nsec);
101+
return;
102+
}
103+
104+
msgpack_unpacked_init(&result);
105+
msgpack_sbuffer_clear(&sbuf);
106+
107+
/* event time */
108+
flb_time_set(&tm, 123456, 123456);
109+
pack_event_time(&pck, &tm);
110+
111+
offset = 0;
112+
msgpack_unpack_next(&result, sbuf.data, sbuf.size, &offset);
113+
114+
flb_time_zero(&tm);
115+
ret = flb_log_event_decoder_decode_timestamp(&result.data, &tm);
116+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
117+
TEST_MSG("flb_log_event_decoder_timestamp failed. ret=%s",
118+
flb_log_event_decoder_get_error_description(ret));
119+
return;
120+
}
121+
if (!TEST_CHECK(tm.tm.tv_sec == 123456 && tm.tm.tv_nsec == 123456)) {
122+
TEST_MSG("timestamp error. tv_sec=%ld tv_nsec=%lu", tm.tm.tv_sec, tm.tm.tv_nsec);
123+
return;
124+
}
125+
126+
msgpack_unpacked_destroy(&result);
127+
msgpack_sbuffer_destroy(&sbuf);
128+
}
129+
130+
void decode_object()
131+
{
132+
struct flb_log_event_decoder dec;
133+
struct flb_log_event event;
134+
int ret;
135+
struct flb_time tm;
136+
msgpack_sbuffer sbuf;
137+
msgpack_packer pck;
138+
msgpack_unpacked result;
139+
size_t offset = 0;
140+
char *json = NULL;
141+
142+
flb_time_set(&tm, 123456, 123456);
143+
144+
msgpack_sbuffer_init(&sbuf);
145+
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
146+
147+
/* [[123456.123456, {}], {"key1":"val1", "key2":"val2"}] */
148+
msgpack_pack_array(&pck, 2);
149+
msgpack_pack_array(&pck, 2);
150+
pack_event_time(&pck, &tm);
151+
msgpack_pack_map(&pck, 0);
152+
msgpack_pack_map(&pck, 2);
153+
154+
msgpack_pack_str(&pck, 4);
155+
msgpack_pack_str_body(&pck, "key1", 4);
156+
msgpack_pack_str(&pck, 4);
157+
msgpack_pack_str_body(&pck, "val1", 4);
158+
159+
msgpack_pack_str(&pck, 4);
160+
msgpack_pack_str_body(&pck, "key2", 4);
161+
msgpack_pack_str(&pck, 4);
162+
msgpack_pack_str_body(&pck, "val2", 4);
163+
164+
msgpack_unpacked_init(&result);
165+
ret = msgpack_unpack_next(&result, sbuf.data, sbuf.size, &offset);
166+
if (!TEST_CHECK(ret == MSGPACK_UNPACK_SUCCESS)) {
167+
TEST_MSG("msgpack_unpack_next failed");
168+
return;
169+
}
170+
171+
ret = flb_event_decoder_decode_object(&dec, &event, &result.data);
172+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
173+
TEST_MSG("flb_log_event_decoder_decode_object failed. ret=%s",
174+
flb_log_event_decoder_get_error_description(ret));
175+
return;
176+
}
177+
178+
if (!TEST_CHECK(flb_time_equal(&tm, &event.timestamp))) {
179+
TEST_MSG("timestamp mismatch");
180+
return;
181+
}
182+
183+
json = flb_msgpack_to_json_str(4096, event.body);
184+
if (!TEST_CHECK(json != NULL)) {
185+
TEST_MSG("flb_msgpack_to_json_str error");
186+
return;
187+
}
188+
if (!TEST_CHECK(strstr(json, "\"key1\":\"val1\"") != NULL)) {
189+
TEST_MSG("\"key1\":\"val1\" is missing. json=%s", json);
190+
return;
191+
}
192+
if (!TEST_CHECK(strstr(json, "\"key2\":\"val2\"") != NULL)) {
193+
TEST_MSG("\"key2\":\"val2\" is missing. json=%s", json);
194+
return;
195+
}
196+
197+
flb_free(json);
198+
msgpack_unpacked_destroy(&result);
199+
msgpack_sbuffer_destroy(&sbuf);
200+
}
201+
202+
void decoder_next()
203+
{
204+
struct flb_log_event_decoder dec;
205+
struct flb_log_event event;
206+
int ret;
207+
struct flb_time tm;
208+
msgpack_sbuffer sbuf;
209+
msgpack_packer pck;
210+
char *json = NULL;
211+
212+
flb_time_set(&tm, 123456, 123456);
213+
214+
msgpack_sbuffer_init(&sbuf);
215+
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
216+
217+
/* [[123456.123456, {}], {"key1":"val1", "key2":"val2"}] */
218+
msgpack_pack_array(&pck, 2);
219+
msgpack_pack_array(&pck, 2);
220+
pack_event_time(&pck, &tm);
221+
msgpack_pack_map(&pck, 0);
222+
msgpack_pack_map(&pck, 2);
223+
224+
msgpack_pack_str(&pck, 4);
225+
msgpack_pack_str_body(&pck, "key1", 4);
226+
msgpack_pack_str(&pck, 4);
227+
msgpack_pack_str_body(&pck, "val1", 4);
228+
229+
msgpack_pack_str(&pck, 4);
230+
msgpack_pack_str_body(&pck, "key2", 4);
231+
msgpack_pack_str(&pck, 4);
232+
msgpack_pack_str_body(&pck, "val2", 4);
233+
234+
235+
ret = flb_log_event_decoder_init(&dec, (char *)sbuf.data, sbuf.size);
236+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
237+
TEST_MSG("flb_log_event_decoder_init failed. ret=%s",
238+
flb_log_event_decoder_get_error_description(ret));
239+
return;
240+
}
241+
242+
ret = flb_log_event_decoder_next(&dec, &event);
243+
if (!TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS)) {
244+
TEST_MSG("flb_log_event_decoder_next failed. ret=%s",
245+
flb_log_event_decoder_get_error_description(ret));
246+
return;
247+
}
248+
if (!TEST_CHECK(flb_time_equal(&tm, &event.timestamp))) {
249+
TEST_MSG("timestamp mismatch");
250+
return;
251+
}
252+
253+
json = flb_msgpack_to_json_str(4096, event.body);
254+
if (!TEST_CHECK(json != NULL)) {
255+
TEST_MSG("flb_msgpack_to_json_str error");
256+
return;
257+
}
258+
if (!TEST_CHECK(strstr(json, "\"key1\":\"val1\"") != NULL)) {
259+
TEST_MSG("\"key1\":\"val1\" is missing. json=%s", json);
260+
return;
261+
}
262+
if (!TEST_CHECK(strstr(json, "\"key2\":\"val2\"") != NULL)) {
263+
TEST_MSG("\"key2\":\"val2\" is missing. json=%s", json);
264+
return;
265+
}
266+
267+
flb_free(json);
268+
flb_log_event_decoder_destroy(&dec);
269+
msgpack_sbuffer_destroy(&sbuf);
270+
}
271+
272+
273+
274+
TEST_LIST = {
275+
{ "create_destroy", create_destroy },
276+
{ "init_destroy", init_destroy },
277+
{ "decode_timestamp", decode_timestamp },
278+
{ "decode_object", decode_object },
279+
{ "decoder_next", decoder_next },
280+
{ 0 }
281+
};

0 commit comments

Comments
 (0)