Skip to content

Commit 7ad0000

Browse files
cosmo0920edsiper
authored andcommitted
in_systemd: test: Add a format test case for duplicated keys
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 6790b26 commit 7ad0000

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

tests/runtime/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ if(FLB_OUT_LIB)
5858
FLB_RT_TEST(FLB_IN_FORWARD "in_forward.c")
5959
FLB_RT_TEST(FLB_IN_FLUENTBIT_METRICS "in_fluentbit_metrics.c")
6060
FLB_RT_TEST(FLB_IN_KUBERNETES_EVENTS "in_kubernetes_events.c")
61+
if (FLB_IN_SYSTEMD)
62+
FLB_RT_TEST(FLB_IN_SYSTEMD "in_systemd.c")
63+
endif ()
6164
endif()
6265

6366
if (FLB_CUSTOM_CALYPTIA)

tests/runtime/in_systemd.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2022 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.h>
21+
#include <fluent-bit/flb_pack.h>
22+
#include <fluent-bit/flb_record_accessor.h>
23+
#include <fluent-bit/flb_ra_key.h>
24+
25+
#include "flb_tests_runtime.h"
26+
27+
static void cb_check_cfl_variant_properties(void *ctx, int ffd,
28+
int res_ret, void *res_data, size_t res_size,
29+
void *data)
30+
{
31+
flb_sds_t output;
32+
char *result = NULL;
33+
34+
/* Convert from msgpack to JSON */
35+
output = flb_msgpack_raw_to_json_sds(res_data, res_size);
36+
TEST_CHECK(output != NULL);
37+
38+
result = strstr(output, "\"MESSAGE\":\"test native message with multiple values\"");
39+
if (TEST_CHECK(result != NULL)) {
40+
TEST_MSG("output:%s\n", output);
41+
}
42+
43+
result = strstr(output, "\"KEY\":[\"value1\",\"value4\",\"another\"]");
44+
if (TEST_CHECK(result != NULL)) {
45+
TEST_MSG("output:%s\n", output);
46+
}
47+
48+
result = strstr(output, "\"KEY2\":[\"value2\",\"value3\",\"value5\",\"value10\",\"final_field\"]");
49+
if (TEST_CHECK(result != NULL)) {
50+
TEST_MSG("output:%s\n", output);
51+
}
52+
53+
result = strstr(output, "\"KEY3\":[\"howdy\",\"prettygood\",\"wow\"]");
54+
if (TEST_CHECK(result != NULL)) {
55+
TEST_MSG("output:%s\n", output);
56+
}
57+
58+
flb_sds_destroy(output);
59+
}
60+
61+
void flb_test_duplicated_keys()
62+
{
63+
int ret;
64+
int in_ffd;
65+
int out_ffd;
66+
flb_ctx_t *ctx;
67+
char *message = "MESSAGE=test native message with multiple values\nKEY=value1\nKEY=value4\n"
68+
"KEY2=value2\nKEY=another\nKEY2=value3\nKEY2=value5\nKEY3=howdy\nKEY3=prettygood\nKEY2=value10\n"
69+
"KEY3=wow\nKEY2=final_field\n";
70+
71+
/* Create context, flush every second (some checks omitted here) */
72+
ctx = flb_create();
73+
flb_service_set(ctx,
74+
"flush", "2",
75+
"grace", "1",
76+
"Log_Level", "error",
77+
NULL);
78+
79+
/* Systemd */
80+
in_ffd = flb_input(ctx, (char *) "systemd", NULL);
81+
flb_input_set(ctx, in_ffd,
82+
"tag", "test",
83+
"Read_From_Tail", "On",
84+
NULL);
85+
86+
87+
out_ffd = flb_output(ctx, (char *) "null", NULL);
88+
flb_output_set(ctx, out_ffd,
89+
"match", "test",
90+
NULL);
91+
92+
/* Enable test mode */
93+
ret = flb_input_set_test(ctx, in_ffd, "formatter",
94+
cb_check_cfl_variant_properties,
95+
NULL);
96+
97+
/* Start */
98+
ret = flb_start(ctx);
99+
TEST_CHECK(ret == 0);
100+
101+
/* Ingest data sample to run test formatter */
102+
ret = flb_lib_push(ctx, in_ffd, message, strlen(message));
103+
TEST_CHECK(ret == 0);
104+
105+
sleep(2);
106+
flb_stop(ctx);
107+
flb_destroy(ctx);
108+
}
109+
110+
/* Test list */
111+
TEST_LIST = {
112+
{ "duplicated_keys", flb_test_duplicated_keys },
113+
{ NULL, NULL}
114+
};

0 commit comments

Comments
 (0)