Skip to content

Commit 40f0901

Browse files
committed
tests: runtime: core_internal_logger: add new unit tests
Signed-off-by: Eduardo Silva <[email protected]>
1 parent be6967f commit 40f0901

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ endmacro()
2525

2626
# Core
2727
FLB_RT_CORE_TEST(FLB_COROUTINE_TIMEOUT "core-timeout.c")
28+
FLB_RT_CORE_TEST(FLB_INTERNAL_LOGGER "core_internal_logger.c")
2829

2930
FLB_RT_TEST(FLB_CHUNK_TRACE "core_chunk_trace.c")
3031

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
#include <fluent-bit.h>
4+
#include <fluent-bit/flb_http_client.h>
5+
#include <fluent-bit/flb_log.h>
6+
#include <fluent-bit/flb_regex.h>
7+
#include <fluent-bit/flb_time.h>
8+
#include <time.h>
9+
10+
#include "flb_tests_runtime.h"
11+
12+
struct http_client_ctx {
13+
struct flb_upstream *u;
14+
struct flb_connection *u_conn;
15+
struct flb_config *config;
16+
struct mk_event_loop *evl;
17+
};
18+
19+
struct http_client_ctx *http_client_ctx_create()
20+
{
21+
struct http_client_ctx *ret_ctx = NULL;
22+
struct mk_event_loop *evl = NULL;
23+
24+
ret_ctx = flb_calloc(1, sizeof(struct http_client_ctx));
25+
if (!TEST_CHECK(ret_ctx != NULL)) {
26+
flb_errno();
27+
TEST_MSG("flb_calloc(http_client_ctx) failed");
28+
return NULL;
29+
}
30+
31+
evl = mk_event_loop_create(16);
32+
if (!TEST_CHECK(evl != NULL)) {
33+
TEST_MSG("mk_event_loop failed");
34+
flb_free(ret_ctx);
35+
return NULL;
36+
}
37+
ret_ctx->evl = evl;
38+
flb_engine_evl_init();
39+
flb_engine_evl_set(evl);
40+
41+
ret_ctx->config = flb_config_init();
42+
if (!TEST_CHECK(ret_ctx->config != NULL)) {
43+
TEST_MSG("flb_config_init failed");
44+
mk_event_loop_destroy(evl);
45+
flb_free(ret_ctx);
46+
return NULL;
47+
}
48+
49+
ret_ctx->u = flb_upstream_create(ret_ctx->config, "127.0.0.1", 2020, 0, NULL);
50+
if (!TEST_CHECK(ret_ctx->u != NULL)) {
51+
TEST_MSG("flb_upstream_create failed");
52+
flb_config_exit(ret_ctx->config);
53+
mk_event_loop_destroy(evl);
54+
flb_free(ret_ctx);
55+
return NULL;
56+
}
57+
58+
ret_ctx->u_conn = flb_upstream_conn_get(ret_ctx->u);
59+
TEST_CHECK(ret_ctx->u_conn != NULL);
60+
61+
ret_ctx->u_conn->upstream = ret_ctx->u;
62+
63+
return ret_ctx;
64+
}
65+
66+
void http_client_ctx_destroy(struct http_client_ctx *http_ctx)
67+
{
68+
TEST_CHECK(flb_upstream_conn_release(http_ctx->u_conn) == 0);
69+
flb_upstream_destroy(http_ctx->u);
70+
mk_event_loop_destroy(http_ctx->evl);
71+
flb_config_exit(http_ctx->config);
72+
flb_free(http_ctx);
73+
}
74+
75+
/* Check for expected Prometheus metrics. If the prom scrape endpoints returns a non-200
76+
* and fail_test is FLB_FALSE, this returns without failing the test.
77+
* This returns 0 if all the assertions were checked and no retry is necessary.
78+
*/
79+
int assert_internal_log_metrics(struct http_client_ctx *http_ctx, int fail_test)
80+
{
81+
struct flb_http_client *http_client;
82+
size_t b_sent;
83+
struct flb_regex *regex;
84+
85+
http_client = flb_http_client(http_ctx->u_conn,
86+
FLB_HTTP_GET,
87+
"/api/v2/metrics/prometheus",
88+
"", /* body */
89+
0, /* len(body) */
90+
"127.0.0.1",
91+
2020,
92+
NULL,
93+
0);
94+
TEST_ASSERT(http_client != NULL);
95+
96+
TEST_ASSERT(flb_http_do(http_client, &b_sent) == 0);
97+
98+
if (http_client->resp.status != 200 && !fail_test) {
99+
flb_http_client_destroy(http_client);
100+
return -1;
101+
}
102+
103+
TEST_MSG(http_client->resp.payload);
104+
if (!TEST_CHECK(http_client->resp.status == 200)) {
105+
TEST_MSG("http response code error. expect: 200, got: %d\n",
106+
http_client->resp.status);
107+
}
108+
109+
/* There be no errors logged */
110+
if (!TEST_CHECK(
111+
strstr(http_client->resp.payload,
112+
"fluentbit_logger_logs_total{message_type=\"error\"} 0")
113+
!= NULL)) {
114+
TEST_MSG("response payload: %s", http_client->resp.payload);
115+
}
116+
117+
/* The process startup should have logged at least 1 info log */
118+
regex = flb_regex_create(
119+
"fluentbit_logger_logs_total\\{message_type=\"info\"\\} [1-9]+[0-9]*");
120+
if (!TEST_CHECK(regex != NULL)) {
121+
TEST_MSG("Failed to create regex for info log count check");
122+
flb_http_client_destroy(http_client);
123+
return -1;
124+
}
125+
if (!TEST_CHECK(flb_regex_match(
126+
regex, http_client->resp.payload, http_client->resp.payload_size))) {
127+
TEST_MSG("response payload: %s\n", http_client->resp.payload);
128+
}
129+
130+
flb_regex_destroy(regex);
131+
flb_http_client_destroy(http_client);
132+
133+
return 0;
134+
}
135+
136+
/*
137+
* Test that internal logs (i.e. those created by flb_info, flb_error, etc)
138+
* tick internal v2 metrics.
139+
*/
140+
static void test_internal_log_metrics()
141+
{
142+
flb_ctx_t *ctx;
143+
int ret;
144+
struct http_client_ctx *http_ctx;
145+
struct flb_http_client *http_client;
146+
size_t b_sent;
147+
struct flb_regex *regex;
148+
int i;
149+
int attempt_count = 30;
150+
151+
ctx = flb_create();
152+
TEST_ASSERT(ctx != NULL);
153+
154+
TEST_ASSERT(flb_service_set(ctx,
155+
"HTTP_Server",
156+
"On",
157+
"HTTP_Listen",
158+
"127.0.0.1",
159+
"HTTP_Port",
160+
"2020",
161+
NULL) == 0);
162+
163+
ret = flb_start(ctx);
164+
TEST_ASSERT(ret == 0);
165+
166+
http_ctx = http_client_ctx_create();
167+
TEST_ASSERT(http_ctx != NULL);
168+
169+
/* If the assertion fails, retry in a sleep loop since the fluent-bit's HTTP server
170+
* may not be ready yet */
171+
for (i = 0; i < attempt_count; i++) {
172+
if (assert_internal_log_metrics(http_ctx, i == (attempt_count - 1)) ) {
173+
break;
174+
}
175+
flb_time_msleep(100);
176+
}
177+
178+
http_client_ctx_destroy(http_ctx);
179+
TEST_CHECK(flb_stop(ctx) == 0);
180+
flb_destroy(ctx);
181+
}
182+
183+
/* Test list */
184+
TEST_LIST = {
185+
{"internal_log_metrics", test_internal_log_metrics},
186+
{NULL, NULL},
187+
};

0 commit comments

Comments
 (0)