Skip to content

Commit 85d54b9

Browse files
committed
tests: runtime: azure blob compression coverage
Signed-off-by: Nico Berlee <[email protected]>
1 parent 87c47e4 commit 85d54b9

File tree

2 files changed

+303
-0
lines changed

2 files changed

+303
-0
lines changed

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ if(FLB_IN_LIB)
204204
FLB_RT_TEST(FLB_OUT_LIB "core_routes.c")
205205
FLB_RT_TEST(FLB_OUT_LIB "config_map_opts.c")
206206
FLB_RT_TEST(FLB_OUT_COUNTER "out_counter.c")
207+
FLB_RT_TEST(FLB_OUT_AZURE_BLOB "out_azure_blob_compression.c")
207208
FLB_RT_TEST(FLB_OUT_AZURE_KUSTO "out_azure_kusto.c")
208209
FLB_RT_TEST(FLB_OUT_DATADOG "out_datadog.c")
209210
FLB_RT_TEST(FLB_OUT_SKYWALKING "out_skywalking.c")
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* Fluent Bit
4+
* ==========
5+
* Copyright (C) 2015-2024 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 <string.h>
21+
22+
#include <fluent-bit/flb_info.h>
23+
#include <fluent-bit/flb_mem.h>
24+
#include <fluent-bit/flb_sds.h>
25+
#include <fluent-bit/flb_config.h>
26+
#include <fluent-bit/flb_upstream.h>
27+
#include <fluent-bit/flb_http_client.h>
28+
#include <fluent-bit/flb_compression.h>
29+
30+
#include "flb_tests_runtime.h"
31+
32+
#include "../../plugins/out_azure_blob/azure_blob.h"
33+
#include "../../plugins/out_azure_blob/azure_blob_blockblob.h"
34+
#include "../../plugins/out_azure_blob/azure_blob_http.h"
35+
36+
struct azure_blob_fixture {
37+
struct flb_config *config;
38+
struct flb_upstream *upstream;
39+
struct flb_connection *connection;
40+
};
41+
42+
static int azure_blob_fixture_init(struct azure_blob_fixture *fixture)
43+
{
44+
memset(fixture, 0, sizeof(*fixture));
45+
46+
fixture->config = flb_config_init();
47+
if (!TEST_CHECK(fixture->config != NULL)) {
48+
TEST_MSG("flb_config_init failed");
49+
return -1;
50+
}
51+
52+
fixture->upstream = flb_upstream_create(fixture->config, "127.0.0.1",
53+
80, FLB_IO_TCP, NULL);
54+
if (!TEST_CHECK(fixture->upstream != NULL)) {
55+
TEST_MSG("flb_upstream_create failed");
56+
flb_config_exit(fixture->config);
57+
fixture->config = NULL;
58+
return -1;
59+
}
60+
61+
fixture->connection = flb_calloc(1, sizeof(struct flb_connection));
62+
if (!TEST_CHECK(fixture->connection != NULL)) {
63+
flb_errno();
64+
TEST_MSG("flb_calloc(flb_connection) failed");
65+
flb_upstream_destroy(fixture->upstream);
66+
fixture->upstream = NULL;
67+
flb_config_exit(fixture->config);
68+
fixture->config = NULL;
69+
return -1;
70+
}
71+
72+
fixture->connection->upstream = fixture->upstream;
73+
return 0;
74+
}
75+
76+
static void azure_blob_fixture_destroy(struct azure_blob_fixture *fixture)
77+
{
78+
if (fixture->connection != NULL) {
79+
flb_free(fixture->connection);
80+
}
81+
82+
if (fixture->upstream != NULL) {
83+
flb_upstream_destroy(fixture->upstream);
84+
}
85+
86+
if (fixture->config != NULL) {
87+
flb_config_exit(fixture->config);
88+
}
89+
}
90+
91+
static int azure_blob_ctx_init(struct flb_azure_blob *ctx)
92+
{
93+
memset(ctx, 0, sizeof(*ctx));
94+
ctx->base_uri = flb_sds_create("/");
95+
if (!TEST_CHECK(ctx->base_uri != NULL)) {
96+
TEST_MSG("flb_sds_create failed for base_uri");
97+
return -1;
98+
}
99+
ctx->container_name = flb_sds_create("container");
100+
if (!TEST_CHECK(ctx->container_name != NULL)) {
101+
TEST_MSG("flb_sds_create failed for container_name");
102+
flb_sds_destroy(ctx->base_uri);
103+
ctx->base_uri = NULL;
104+
return -1;
105+
}
106+
ctx->btype = AZURE_BLOB_BLOCKBLOB;
107+
ctx->atype = AZURE_BLOB_AUTH_KEY;
108+
109+
return 0;
110+
}
111+
112+
static void azure_blob_ctx_destroy(struct flb_azure_blob *ctx)
113+
{
114+
if (ctx->base_uri != NULL) {
115+
flb_sds_destroy(ctx->base_uri);
116+
}
117+
118+
if (ctx->container_name != NULL) {
119+
flb_sds_destroy(ctx->container_name);
120+
}
121+
}
122+
123+
static void test_block_blob_extension_zstd()
124+
{
125+
struct flb_azure_blob ctx;
126+
flb_sds_t uri;
127+
int ret;
128+
129+
ret = azure_blob_ctx_init(&ctx);
130+
if (ret != 0) {
131+
return;
132+
}
133+
ctx.compress_blob = FLB_TRUE;
134+
ctx.compression = FLB_COMPRESSION_ALGORITHM_ZSTD;
135+
136+
uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand");
137+
TEST_CHECK(uri != NULL);
138+
TEST_CHECK(strstr(uri, ".zst?blockid=") != NULL);
139+
140+
flb_sds_destroy(uri);
141+
azure_blob_ctx_destroy(&ctx);
142+
}
143+
144+
static void test_block_blob_extension_gzip_default()
145+
{
146+
struct flb_azure_blob ctx;
147+
flb_sds_t uri;
148+
int ret;
149+
150+
ret = azure_blob_ctx_init(&ctx);
151+
if (ret != 0) {
152+
return;
153+
}
154+
ctx.compress_blob = FLB_TRUE;
155+
ctx.compression = FLB_COMPRESSION_ALGORITHM_NONE;
156+
157+
/* When no explicit algorithm is configured, gzip remains the
158+
* fallback to preserve legacy behavior. */
159+
160+
uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand");
161+
TEST_CHECK(uri != NULL);
162+
TEST_CHECK(strstr(uri, ".gz?blockid=") != NULL);
163+
164+
flb_sds_destroy(uri);
165+
azure_blob_ctx_destroy(&ctx);
166+
}
167+
168+
static void test_block_blob_extension_disabled()
169+
{
170+
struct flb_azure_blob ctx;
171+
flb_sds_t uri;
172+
int ret;
173+
174+
ret = azure_blob_ctx_init(&ctx);
175+
if (ret != 0) {
176+
return;
177+
}
178+
ctx.compress_blob = FLB_FALSE;
179+
ctx.compression = FLB_COMPRESSION_ALGORITHM_ZSTD;
180+
181+
uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand");
182+
TEST_CHECK(uri != NULL);
183+
TEST_CHECK(strstr(uri, ".gz?blockid=") == NULL);
184+
TEST_CHECK(strstr(uri, ".zst?blockid=") == NULL);
185+
186+
flb_sds_destroy(uri);
187+
azure_blob_ctx_destroy(&ctx);
188+
}
189+
190+
static void test_http_headers_zstd_encoding()
191+
{
192+
struct azure_blob_fixture fixture;
193+
struct flb_http_client *client;
194+
struct flb_azure_blob ctx;
195+
struct flb_output_instance ins;
196+
flb_sds_t header;
197+
int ret;
198+
199+
ret = azure_blob_fixture_init(&fixture);
200+
if (!TEST_CHECK(ret == 0)) {
201+
return;
202+
}
203+
204+
client = flb_http_client(fixture.connection, FLB_HTTP_PUT, "/resource",
205+
NULL, 0, "localhost", 80, NULL, 0);
206+
TEST_CHECK(client != NULL);
207+
if (client == NULL) {
208+
azure_blob_fixture_destroy(&fixture);
209+
return;
210+
}
211+
212+
memset(&ins, 0, sizeof(ins));
213+
ret = azure_blob_ctx_init(&ctx);
214+
if (ret != 0) {
215+
flb_http_client_destroy(client);
216+
azure_blob_fixture_destroy(&fixture);
217+
return;
218+
}
219+
ctx.ins = &ins;
220+
ctx.atype = AZURE_BLOB_AUTH_SAS;
221+
222+
ret = azb_http_client_setup(&ctx, client, 1, FLB_FALSE,
223+
AZURE_BLOB_CT_JSON,
224+
AZURE_BLOB_CE_ZSTD);
225+
TEST_CHECK(ret == 0);
226+
227+
header = flb_http_get_header(client, "Content-Encoding", 16);
228+
TEST_CHECK(header != NULL);
229+
if (header != NULL) {
230+
TEST_CHECK(strcmp(header, "zstd") == 0);
231+
flb_sds_destroy(header);
232+
}
233+
234+
header = flb_http_get_header(client, "Content-Type", 12);
235+
TEST_CHECK(header != NULL);
236+
if (header != NULL) {
237+
TEST_CHECK(strcmp(header, "application/json") == 0);
238+
flb_sds_destroy(header);
239+
}
240+
241+
flb_http_client_destroy(client);
242+
azure_blob_ctx_destroy(&ctx);
243+
azure_blob_fixture_destroy(&fixture);
244+
}
245+
246+
static void test_http_headers_zstd_content_type()
247+
{
248+
struct azure_blob_fixture fixture;
249+
struct flb_http_client *client;
250+
struct flb_azure_blob ctx;
251+
struct flb_output_instance ins;
252+
flb_sds_t header;
253+
int ret;
254+
255+
ret = azure_blob_fixture_init(&fixture);
256+
if (!TEST_CHECK(ret == 0)) {
257+
return;
258+
}
259+
260+
client = flb_http_client(fixture.connection, FLB_HTTP_PUT, "/resource",
261+
NULL, 0, "localhost", 80, NULL, 0);
262+
TEST_CHECK(client != NULL);
263+
if (client == NULL) {
264+
azure_blob_fixture_destroy(&fixture);
265+
return;
266+
}
267+
268+
memset(&ins, 0, sizeof(ins));
269+
ret = azure_blob_ctx_init(&ctx);
270+
if (ret != 0) {
271+
flb_http_client_destroy(client);
272+
azure_blob_fixture_destroy(&fixture);
273+
return;
274+
}
275+
ctx.ins = &ins;
276+
ctx.atype = AZURE_BLOB_AUTH_SAS;
277+
278+
ret = azb_http_client_setup(&ctx, client, 1, FLB_FALSE,
279+
AZURE_BLOB_CT_ZSTD,
280+
AZURE_BLOB_CE_NONE);
281+
TEST_CHECK(ret == 0);
282+
283+
header = flb_http_get_header(client, "Content-Type", 12);
284+
TEST_CHECK(header != NULL);
285+
if (header != NULL) {
286+
TEST_CHECK(strcmp(header, "application/zstd") == 0);
287+
flb_sds_destroy(header);
288+
}
289+
290+
flb_http_client_destroy(client);
291+
azure_blob_ctx_destroy(&ctx);
292+
azure_blob_fixture_destroy(&fixture);
293+
}
294+
295+
TEST_LIST = {
296+
{"block_blob_extension_zstd", test_block_blob_extension_zstd},
297+
{"block_blob_extension_gzip_default", test_block_blob_extension_gzip_default},
298+
{"block_blob_extension_disabled", test_block_blob_extension_disabled},
299+
{"http_headers_zstd_encoding", test_http_headers_zstd_encoding},
300+
{"http_headers_zstd_content_type", test_http_headers_zstd_content_type},
301+
{ 0 }
302+
};

0 commit comments

Comments
 (0)