Skip to content

Commit a9fd4aa

Browse files
committed
tests: internal: ra: new 'translate_tag' test (#3290)
Signed-off-by: Eduardo Silva <[email protected]>
1 parent 491889b commit a9fd4aa

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/internal/record_accessor.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,61 @@ void cb_translate()
155155
msgpack_unpacked_destroy(&result);
156156
}
157157

158+
void cb_translate_tag()
159+
{
160+
int len;
161+
int ret;
162+
int type;
163+
size_t off = 0;
164+
char *out_buf;
165+
size_t out_size;
166+
char *json;
167+
char *fmt;
168+
flb_sds_t str;
169+
msgpack_unpacked result;
170+
msgpack_object map;
171+
struct flb_record_accessor *ra;
172+
173+
/* Sample JSON message */
174+
json =
175+
"{\"k1\": \"string\", \"k2\": true, \"k3\": false," \
176+
" \"k4\": 0.123456789, \"k5\": 123456789," \
177+
" \"k6\": {\"s1\": {\"s2\": \"nested\"}}}";
178+
179+
/* Convert to msgpack */
180+
len = strlen(json);
181+
ret = flb_pack_json(json, len, &out_buf, &out_size, &type);
182+
TEST_CHECK(ret == 0);
183+
if (ret == -1) {
184+
exit(EXIT_FAILURE);
185+
}
186+
187+
fmt = "$TAG";
188+
ra = flb_ra_create(fmt, FLB_TRUE);
189+
TEST_CHECK(ra != NULL);
190+
if (!ra) {
191+
exit(EXIT_FAILURE);
192+
}
193+
194+
/* Unpack msgpack object */
195+
msgpack_unpacked_init(&result);
196+
msgpack_unpack_next(&result, out_buf, out_size, &off);
197+
map = result.data;
198+
199+
/* Do translation */
200+
str = flb_ra_translate(ra, "testapp", 7, map, NULL);
201+
TEST_CHECK(str != NULL);
202+
if (!str) {
203+
exit(EXIT_FAILURE);
204+
}
205+
TEST_CHECK(flb_sds_len(str) == 7);
206+
207+
flb_sds_destroy(str);
208+
flb_ra_destroy(ra);
209+
flb_free(out_buf);
210+
msgpack_unpacked_destroy(&result);
211+
}
212+
158213
void cb_dots_subkeys()
159214
{
160215
int len;
@@ -357,6 +412,7 @@ void cb_get_kv_pair()
357412
TEST_LIST = {
358413
{ "keys" , cb_keys},
359414
{ "translate" , cb_translate},
415+
{ "translate_tag", cb_translate_tag},
360416
{ "dots_subkeys" , cb_dots_subkeys},
361417
{ "array_id" , cb_array_id},
362418
{ "get_kv_pair" , cb_get_kv_pair},

0 commit comments

Comments
 (0)