|
| 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 void azure_blob_ctx_init(struct flb_azure_blob *ctx) |
| 92 | +{ |
| 93 | + memset(ctx, 0, sizeof(*ctx)); |
| 94 | + ctx->base_uri = flb_sds_create("/"); |
| 95 | + ctx->container_name = flb_sds_create("container"); |
| 96 | + ctx->btype = AZURE_BLOB_BLOCKBLOB; |
| 97 | + ctx->atype = AZURE_BLOB_AUTH_KEY; |
| 98 | +} |
| 99 | + |
| 100 | +static void azure_blob_ctx_destroy(struct flb_azure_blob *ctx) |
| 101 | +{ |
| 102 | + if (ctx->base_uri != NULL) { |
| 103 | + flb_sds_destroy(ctx->base_uri); |
| 104 | + } |
| 105 | + |
| 106 | + if (ctx->container_name != NULL) { |
| 107 | + flb_sds_destroy(ctx->container_name); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static void test_block_blob_extension_zstd() |
| 112 | +{ |
| 113 | + struct flb_azure_blob ctx; |
| 114 | + flb_sds_t uri; |
| 115 | + |
| 116 | + azure_blob_ctx_init(&ctx); |
| 117 | + ctx.compress_blob = FLB_TRUE; |
| 118 | + ctx.compression = FLB_COMPRESSION_ALGORITHM_ZSTD; |
| 119 | + |
| 120 | + uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand"); |
| 121 | + TEST_CHECK(uri != NULL); |
| 122 | + TEST_CHECK(strstr(uri, ".zst?blockid=") != NULL); |
| 123 | + |
| 124 | + flb_sds_destroy(uri); |
| 125 | + azure_blob_ctx_destroy(&ctx); |
| 126 | +} |
| 127 | + |
| 128 | +static void test_block_blob_extension_gzip_default() |
| 129 | +{ |
| 130 | + struct flb_azure_blob ctx; |
| 131 | + flb_sds_t uri; |
| 132 | + |
| 133 | + azure_blob_ctx_init(&ctx); |
| 134 | + ctx.compress_blob = FLB_TRUE; |
| 135 | + ctx.compression = FLB_COMPRESSION_ALGORITHM_NONE; |
| 136 | + |
| 137 | + /* When no explicit algorithm is configured, gzip remains the |
| 138 | + * fallback to preserve legacy behavior. */ |
| 139 | + |
| 140 | + uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand"); |
| 141 | + TEST_CHECK(uri != NULL); |
| 142 | + TEST_CHECK(strstr(uri, ".gz?blockid=") != NULL); |
| 143 | + |
| 144 | + flb_sds_destroy(uri); |
| 145 | + azure_blob_ctx_destroy(&ctx); |
| 146 | +} |
| 147 | + |
| 148 | +static void test_block_blob_extension_disabled() |
| 149 | +{ |
| 150 | + struct flb_azure_blob ctx; |
| 151 | + flb_sds_t uri; |
| 152 | + |
| 153 | + azure_blob_ctx_init(&ctx); |
| 154 | + ctx.compress_blob = FLB_FALSE; |
| 155 | + ctx.compression = FLB_COMPRESSION_ALGORITHM_ZSTD; |
| 156 | + |
| 157 | + uri = azb_block_blob_uri(&ctx, "file", "block", 123, "rand"); |
| 158 | + TEST_CHECK(uri != NULL); |
| 159 | + TEST_CHECK(strstr(uri, ".gz?blockid=") == NULL); |
| 160 | + TEST_CHECK(strstr(uri, ".zst?blockid=") == NULL); |
| 161 | + |
| 162 | + flb_sds_destroy(uri); |
| 163 | + azure_blob_ctx_destroy(&ctx); |
| 164 | +} |
| 165 | + |
| 166 | +static void test_http_headers_zstd_encoding() |
| 167 | +{ |
| 168 | + struct azure_blob_fixture fixture; |
| 169 | + struct flb_http_client *client; |
| 170 | + struct flb_azure_blob ctx; |
| 171 | + struct flb_output_instance ins; |
| 172 | + flb_sds_t header; |
| 173 | + int ret; |
| 174 | + |
| 175 | + ret = azure_blob_fixture_init(&fixture); |
| 176 | + if (!TEST_CHECK(ret == 0)) { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + client = flb_http_client(fixture.connection, FLB_HTTP_PUT, "/resource", |
| 181 | + NULL, 0, "localhost", 80, NULL, 0); |
| 182 | + TEST_CHECK(client != NULL); |
| 183 | + if (client == NULL) { |
| 184 | + azure_blob_fixture_destroy(&fixture); |
| 185 | + return; |
| 186 | + } |
| 187 | + |
| 188 | + memset(&ins, 0, sizeof(ins)); |
| 189 | + azure_blob_ctx_init(&ctx); |
| 190 | + ctx.ins = &ins; |
| 191 | + ctx.atype = AZURE_BLOB_AUTH_SAS; |
| 192 | + |
| 193 | + ret = azb_http_client_setup(&ctx, client, 1, FLB_FALSE, |
| 194 | + AZURE_BLOB_CT_JSON, |
| 195 | + AZURE_BLOB_CE_ZSTD); |
| 196 | + TEST_CHECK(ret == 0); |
| 197 | + |
| 198 | + header = flb_http_get_header(client, "Content-Encoding", 16); |
| 199 | + TEST_CHECK(header != NULL); |
| 200 | + if (header != NULL) { |
| 201 | + TEST_CHECK(strcmp(header, "zstd") == 0); |
| 202 | + flb_sds_destroy(header); |
| 203 | + } |
| 204 | + |
| 205 | + header = flb_http_get_header(client, "Content-Type", 12); |
| 206 | + TEST_CHECK(header != NULL); |
| 207 | + if (header != NULL) { |
| 208 | + TEST_CHECK(strcmp(header, "application/json") == 0); |
| 209 | + flb_sds_destroy(header); |
| 210 | + } |
| 211 | + |
| 212 | + flb_http_client_destroy(client); |
| 213 | + azure_blob_ctx_destroy(&ctx); |
| 214 | + azure_blob_fixture_destroy(&fixture); |
| 215 | +} |
| 216 | + |
| 217 | +static void test_http_headers_zstd_content_type() |
| 218 | +{ |
| 219 | + struct azure_blob_fixture fixture; |
| 220 | + struct flb_http_client *client; |
| 221 | + struct flb_azure_blob ctx; |
| 222 | + struct flb_output_instance ins; |
| 223 | + flb_sds_t header; |
| 224 | + int ret; |
| 225 | + |
| 226 | + ret = azure_blob_fixture_init(&fixture); |
| 227 | + if (!TEST_CHECK(ret == 0)) { |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + client = flb_http_client(fixture.connection, FLB_HTTP_PUT, "/resource", |
| 232 | + NULL, 0, "localhost", 80, NULL, 0); |
| 233 | + TEST_CHECK(client != NULL); |
| 234 | + if (client == NULL) { |
| 235 | + azure_blob_fixture_destroy(&fixture); |
| 236 | + return; |
| 237 | + } |
| 238 | + |
| 239 | + memset(&ins, 0, sizeof(ins)); |
| 240 | + azure_blob_ctx_init(&ctx); |
| 241 | + ctx.ins = &ins; |
| 242 | + ctx.atype = AZURE_BLOB_AUTH_SAS; |
| 243 | + |
| 244 | + ret = azb_http_client_setup(&ctx, client, 1, FLB_FALSE, |
| 245 | + AZURE_BLOB_CT_ZSTD, |
| 246 | + AZURE_BLOB_CE_NONE); |
| 247 | + TEST_CHECK(ret == 0); |
| 248 | + |
| 249 | + header = flb_http_get_header(client, "Content-Type", 12); |
| 250 | + TEST_CHECK(header != NULL); |
| 251 | + if (header != NULL) { |
| 252 | + TEST_CHECK(strcmp(header, "application/zstd") == 0); |
| 253 | + flb_sds_destroy(header); |
| 254 | + } |
| 255 | + |
| 256 | + flb_http_client_destroy(client); |
| 257 | + azure_blob_ctx_destroy(&ctx); |
| 258 | + azure_blob_fixture_destroy(&fixture); |
| 259 | +} |
| 260 | + |
| 261 | +TEST_LIST = { |
| 262 | + {"block_blob_extension_zstd", test_block_blob_extension_zstd}, |
| 263 | + {"block_blob_extension_gzip_default", test_block_blob_extension_gzip_default}, |
| 264 | + {"block_blob_extension_disabled", test_block_blob_extension_disabled}, |
| 265 | + {"http_headers_zstd_encoding", test_http_headers_zstd_encoding}, |
| 266 | + {"http_headers_zstd_content_type", test_http_headers_zstd_content_type}, |
| 267 | + { 0 } |
| 268 | +}; |
0 commit comments