Skip to content

Commit

Permalink
core: test: Add an internal test case for appending kv elements to re…
Browse files Browse the repository at this point in the history
…cords

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 authored and edsiper committed Mar 14, 2024
1 parent 5e8c7e3 commit 342c8e6
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(UNIT_TESTS_FILES
log_event_encoder.c
processor.c
uri.c
msgpack_append_message.c
)

# Config format
Expand Down
5 changes: 5 additions & 0 deletions tests/internal/data/msgpack_append_message/map1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"key1": 123456789,
"key2": 0.999887766,
"key3": "abcdefghijklmnopqrstuvwxyz",
"key4": [{"a": 10, "b": 20}, {"c": 30, "d": 40}]
}
113 changes: 113 additions & 0 deletions tests/internal/msgpack_append_message.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_msgpack_append_message.h>
#include <monkey/mk_core.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h> /* for NAN */

/* JSON tests data */
#define JSON_MAP1 FLB_TESTS_DATA_PATH "/data/msgpack_append_message/map1.json"

#include "flb_tests_internal.h"

struct msgpack_append_message_test {
char *msgpack;
char *json;
};

static inline int process_pack(char *pack, size_t size)
{
int ret;
msgpack_unpacked result;
char *appended_buffer = NULL;
size_t appended_size;
char *inject_message = "injected";
char *inject_key_name = "expanding";
flb_sds_t inject_key;
size_t off = 0;
size_t prev_off = 0;
flb_sds_t out_buf;
char *p = NULL;

inject_key = flb_sds_create_len(inject_key_name, strlen(inject_key_name));
if (!inject_key) {
flb_errno();
return -1;
}
msgpack_unpacked_init(&result);
while (msgpack_unpack_next(&result, pack, size, &off) == MSGPACK_UNPACK_SUCCESS) {
if (result.data.type == MSGPACK_OBJECT_MAP) {
ret = flb_msgpack_append_message_to_record(&appended_buffer,
&appended_size,
inject_key,
pack + prev_off,
size,
inject_message,
8,
MSGPACK_OBJECT_STR);
TEST_CHECK(ret == 0);

out_buf = flb_msgpack_raw_to_json_sds(appended_buffer, appended_size);
TEST_CHECK(out_buf != NULL);
p = strstr(out_buf, "\"expanding\":\"injected\"");
if (!TEST_CHECK(p != NULL)) {
TEST_MSG("\"expanding\":\"injected\" should be appended. out_buf=%s", out_buf);
}
if (out_buf) {
flb_sds_destroy(out_buf);
}
}
prev_off = off;
}

msgpack_unpacked_destroy(&result);

flb_sds_destroy(inject_key);
flb_free(appended_buffer);

return ret;
}

/* Append a single key-value pair into msgpack map */
void test_append_basic()
{
int ret;
size_t len;
char *data;
char *pack;
int out_size;
struct flb_pack_state state;

data = mk_file_to_buffer(JSON_MAP1);
TEST_CHECK(data != NULL);

len = strlen(data);

ret = flb_pack_state_init(&state);
TEST_CHECK(ret == 0);

ret = flb_pack_json_state(data, len, &pack, &out_size, &state);
TEST_CHECK(ret == 0);

ret = process_pack(pack, out_size);
TEST_CHECK(ret == 0);

flb_pack_state_reset(&state);
flb_free(data);
flb_free(pack);
}

TEST_LIST = {
{ "basic", test_append_basic },
{ 0 }
};

0 comments on commit 342c8e6

Please sign in to comment.