|
23 | 23 | #include <fluent-bit/flb_sds.h>
|
24 | 24 | #include <stdio.h>
|
25 | 25 | #include <stdlib.h>
|
| 26 | +#include <unistd.h> |
| 27 | +#include <sys/stat.h> |
| 28 | +#include <fcntl.h> |
26 | 29 | #include "flb_tests_internal.h"
|
27 | 30 |
|
28 | 31 | /* https://github.com/fluent/fluent-bit/issues/6313 */
|
@@ -84,8 +87,340 @@ void test_translate_long_env()
|
84 | 87 | flb_env_destroy(env);
|
85 | 88 | }
|
86 | 89 |
|
| 90 | +/* Test file-based environment variable with refresh interval */ |
| 91 | +void test_file_env_var_basic() |
| 92 | +{ |
| 93 | + struct flb_env *env; |
| 94 | + flb_sds_t buf = NULL; |
| 95 | + char *test_file = "/tmp/flb_test_secret.txt"; |
| 96 | + char *test_content = "secret_value_123"; |
| 97 | + char *template = "${SECRET}"; |
| 98 | + int fd; |
| 99 | + int ret; |
| 100 | + |
| 101 | + /* Create test file */ |
| 102 | + fd = open(test_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 103 | + if (!TEST_CHECK(fd >= 0)) { |
| 104 | + TEST_MSG("Failed to create test file"); |
| 105 | + return; |
| 106 | + } |
| 107 | + ret = write(fd, test_content, strlen(test_content)); |
| 108 | + close(fd); |
| 109 | + if (!TEST_CHECK(ret == strlen(test_content))) { |
| 110 | + TEST_MSG("Failed to write test content"); |
| 111 | + unlink(test_file); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + /* Test environment variable loading */ |
| 116 | + env = flb_env_create(); |
| 117 | + if (!TEST_CHECK(env != NULL)) { |
| 118 | + TEST_MSG("flb_env_create failed"); |
| 119 | + unlink(test_file); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + /* Set file-based environment variable */ |
| 124 | + ret = flb_env_set_extended(env, "SECRET", NULL, "file:///tmp/flb_test_secret.txt", 0); |
| 125 | + if (!TEST_CHECK(ret >= 0)) { |
| 126 | + TEST_MSG("flb_env_set_extended failed"); |
| 127 | + flb_env_destroy(env); |
| 128 | + unlink(test_file); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + /* Test variable translation */ |
| 133 | + buf = flb_env_var_translate(env, template); |
| 134 | + if (!TEST_CHECK(buf != NULL)) { |
| 135 | + TEST_MSG("flb_env_var_translate failed"); |
| 136 | + flb_env_destroy(env); |
| 137 | + unlink(test_file); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + if (!TEST_CHECK(strcmp(buf, test_content) == 0)) { |
| 142 | + TEST_MSG("Content mismatch. Got=%s expect=%s", buf, test_content); |
| 143 | + } |
| 144 | + |
| 145 | + flb_sds_destroy(buf); |
| 146 | + flb_env_destroy(env); |
| 147 | + unlink(test_file); |
| 148 | +} |
| 149 | + |
| 150 | +/* Test file-based environment variable with refresh interval */ |
| 151 | +void test_file_env_var_refresh() |
| 152 | +{ |
| 153 | + struct flb_env *env; |
| 154 | + flb_sds_t buf = NULL; |
| 155 | + char *test_file = "/tmp/flb_test_refresh.txt"; |
| 156 | + char *initial_content = "initial_value"; |
| 157 | + char *updated_content = "updated_value"; |
| 158 | + char *template = "${REFRESH_VAR}"; |
| 159 | + int fd; |
| 160 | + int ret; |
| 161 | + |
| 162 | + /* Create test file with initial content */ |
| 163 | + fd = open(test_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 164 | + if (!TEST_CHECK(fd >= 0)) { |
| 165 | + TEST_MSG("Failed to create test file"); |
| 166 | + return; |
| 167 | + } |
| 168 | + ret = write(fd, initial_content, strlen(initial_content)); |
| 169 | + close(fd); |
| 170 | + if (!TEST_CHECK(ret == strlen(initial_content))) { |
| 171 | + TEST_MSG("Failed to write initial content"); |
| 172 | + unlink(test_file); |
| 173 | + return; |
| 174 | + } |
| 175 | + |
| 176 | + /* Test environment variable loading with refresh interval */ |
| 177 | + env = flb_env_create(); |
| 178 | + if (!TEST_CHECK(env != NULL)) { |
| 179 | + TEST_MSG("flb_env_create failed"); |
| 180 | + unlink(test_file); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + /* Set file-based environment variable with 1 second refresh interval */ |
| 185 | + ret = flb_env_set_extended(env, "REFRESH_VAR", NULL, "file:///tmp/flb_test_refresh.txt", 1); |
| 186 | + if (!TEST_CHECK(ret >= 0)) { |
| 187 | + TEST_MSG("flb_env_set_extended failed"); |
| 188 | + flb_env_destroy(env); |
| 189 | + unlink(test_file); |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + /* Test initial value */ |
| 194 | + buf = flb_env_var_translate(env, template); |
| 195 | + if (!TEST_CHECK(buf != NULL)) { |
| 196 | + TEST_MSG("flb_env_var_translate failed"); |
| 197 | + flb_env_destroy(env); |
| 198 | + unlink(test_file); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + if (!TEST_CHECK(strcmp(buf, initial_content) == 0)) { |
| 203 | + TEST_MSG("Initial content mismatch. Got=%s expect=%s", buf, initial_content); |
| 204 | + } |
| 205 | + flb_sds_destroy(buf); |
| 206 | + |
| 207 | + /* Update file content */ |
| 208 | + fd = open(test_file, O_WRONLY | O_TRUNC, 0644); |
| 209 | + if (!TEST_CHECK(fd >= 0)) { |
| 210 | + TEST_MSG("Failed to open test file for update"); |
| 211 | + flb_env_destroy(env); |
| 212 | + unlink(test_file); |
| 213 | + return; |
| 214 | + } |
| 215 | + ret = write(fd, updated_content, strlen(updated_content)); |
| 216 | + close(fd); |
| 217 | + if (!TEST_CHECK(ret == strlen(updated_content))) { |
| 218 | + TEST_MSG("Failed to write updated content"); |
| 219 | + flb_env_destroy(env); |
| 220 | + unlink(test_file); |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + /* Wait for refresh interval to pass */ |
| 225 | + sleep(2); |
| 226 | + |
| 227 | + /* Test updated value */ |
| 228 | + buf = flb_env_var_translate(env, template); |
| 229 | + if (!TEST_CHECK(buf != NULL)) { |
| 230 | + TEST_MSG("flb_env_var_translate failed after refresh"); |
| 231 | + flb_env_destroy(env); |
| 232 | + unlink(test_file); |
| 233 | + return; |
| 234 | + } |
| 235 | + |
| 236 | + if (!TEST_CHECK(strcmp(buf, updated_content) == 0)) { |
| 237 | + TEST_MSG("Updated content mismatch. Got=%s expect=%s", buf, updated_content); |
| 238 | + } |
| 239 | + |
| 240 | + flb_sds_destroy(buf); |
| 241 | + flb_env_destroy(env); |
| 242 | + unlink(test_file); |
| 243 | +} |
| 244 | + |
| 245 | +/* Test file-based environment variable with file:// URI */ |
| 246 | +void test_file_env_var_uri() |
| 247 | +{ |
| 248 | + struct flb_env *env; |
| 249 | + flb_sds_t buf = NULL; |
| 250 | + char *test_file = "/tmp/flb_test_uri.txt"; |
| 251 | + char *test_content = "uri_value_456"; |
| 252 | + char *file_uri = "file:///tmp/flb_test_uri.txt"; |
| 253 | + char *template = "${URI_VAR}"; |
| 254 | + int fd; |
| 255 | + int ret; |
| 256 | + |
| 257 | + /* Create test file */ |
| 258 | + fd = open(test_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 259 | + if (!TEST_CHECK(fd >= 0)) { |
| 260 | + TEST_MSG("Failed to create test file"); |
| 261 | + return; |
| 262 | + } |
| 263 | + ret = write(fd, test_content, strlen(test_content)); |
| 264 | + close(fd); |
| 265 | + if (!TEST_CHECK(ret == strlen(test_content))) { |
| 266 | + TEST_MSG("Failed to write test content"); |
| 267 | + unlink(test_file); |
| 268 | + return; |
| 269 | + } |
| 270 | + |
| 271 | + /* Test environment variable loading with file:// URI */ |
| 272 | + env = flb_env_create(); |
| 273 | + if (!TEST_CHECK(env != NULL)) { |
| 274 | + TEST_MSG("flb_env_create failed"); |
| 275 | + unlink(test_file); |
| 276 | + return; |
| 277 | + } |
| 278 | + |
| 279 | + /* Set file-based environment variable with file:// URI */ |
| 280 | + ret = flb_env_set_extended(env, "URI_VAR", NULL, file_uri, 0); |
| 281 | + if (!TEST_CHECK(ret >= 0)) { |
| 282 | + TEST_MSG("flb_env_set_extended failed"); |
| 283 | + flb_env_destroy(env); |
| 284 | + unlink(test_file); |
| 285 | + return; |
| 286 | + } |
| 287 | + |
| 288 | + /* Test variable translation */ |
| 289 | + buf = flb_env_var_translate(env, template); |
| 290 | + if (!TEST_CHECK(buf != NULL)) { |
| 291 | + TEST_MSG("flb_env_var_translate failed"); |
| 292 | + flb_env_destroy(env); |
| 293 | + unlink(test_file); |
| 294 | + return; |
| 295 | + } |
| 296 | + |
| 297 | + if (!TEST_CHECK(strcmp(buf, test_content) == 0)) { |
| 298 | + TEST_MSG("Content mismatch. Got=%s expect=%s", buf, test_content); |
| 299 | + } |
| 300 | + |
| 301 | + flb_sds_destroy(buf); |
| 302 | + flb_env_destroy(env); |
| 303 | + unlink(test_file); |
| 304 | +} |
| 305 | + |
| 306 | +/* Test mixed static and dynamic environment variables */ |
| 307 | +void test_mixed_env_vars() |
| 308 | +{ |
| 309 | + struct flb_env *env; |
| 310 | + flb_sds_t buf = NULL; |
| 311 | + char *test_file = "/tmp/flb_test_mixed.txt"; |
| 312 | + char *file_content = "dynamic_value"; |
| 313 | + char *template = "Static: ${STATIC_VAR}, Dynamic: ${DYNAMIC_VAR}"; |
| 314 | + char *expected = "Static: static_value, Dynamic: dynamic_value"; |
| 315 | + int fd; |
| 316 | + int ret; |
| 317 | + |
| 318 | + /* Create test file */ |
| 319 | + fd = open(test_file, O_CREAT | O_WRONLY | O_TRUNC, 0644); |
| 320 | + if (!TEST_CHECK(fd >= 0)) { |
| 321 | + TEST_MSG("Failed to create test file"); |
| 322 | + return; |
| 323 | + } |
| 324 | + ret = write(fd, file_content, strlen(file_content)); |
| 325 | + close(fd); |
| 326 | + if (!TEST_CHECK(ret == strlen(file_content))) { |
| 327 | + TEST_MSG("Failed to write test content"); |
| 328 | + unlink(test_file); |
| 329 | + return; |
| 330 | + } |
| 331 | + |
| 332 | + /* Test mixed environment variables */ |
| 333 | + env = flb_env_create(); |
| 334 | + if (!TEST_CHECK(env != NULL)) { |
| 335 | + TEST_MSG("flb_env_create failed"); |
| 336 | + unlink(test_file); |
| 337 | + return; |
| 338 | + } |
| 339 | + |
| 340 | + /* Set static environment variable */ |
| 341 | + ret = flb_env_set(env, "STATIC_VAR", "static_value"); |
| 342 | + if (!TEST_CHECK(ret >= 0)) { |
| 343 | + TEST_MSG("flb_env_set failed for static variable"); |
| 344 | + flb_env_destroy(env); |
| 345 | + unlink(test_file); |
| 346 | + return; |
| 347 | + } |
| 348 | + |
| 349 | + /* Set dynamic environment variable */ |
| 350 | + ret = flb_env_set_extended(env, "DYNAMIC_VAR", NULL, "file:///tmp/flb_test_mixed.txt", 0); |
| 351 | + if (!TEST_CHECK(ret >= 0)) { |
| 352 | + TEST_MSG("flb_env_set_extended failed for dynamic variable"); |
| 353 | + flb_env_destroy(env); |
| 354 | + unlink(test_file); |
| 355 | + return; |
| 356 | + } |
| 357 | + |
| 358 | + /* Test mixed variable translation */ |
| 359 | + buf = flb_env_var_translate(env, template); |
| 360 | + if (!TEST_CHECK(buf != NULL)) { |
| 361 | + TEST_MSG("flb_env_var_translate failed"); |
| 362 | + flb_env_destroy(env); |
| 363 | + unlink(test_file); |
| 364 | + return; |
| 365 | + } |
| 366 | + |
| 367 | + if (!TEST_CHECK(strcmp(buf, expected) == 0)) { |
| 368 | + TEST_MSG("Mixed content mismatch. Got=%s expect=%s", buf, expected); |
| 369 | + } |
| 370 | + |
| 371 | + flb_sds_destroy(buf); |
| 372 | + flb_env_destroy(env); |
| 373 | + unlink(test_file); |
| 374 | +} |
| 375 | + |
| 376 | +/* Test error handling for missing file */ |
| 377 | +void test_file_env_var_missing() |
| 378 | +{ |
| 379 | + struct flb_env *env; |
| 380 | + flb_sds_t buf = NULL; |
| 381 | + char *missing_file = "/tmp/flb_nonexistent_file.txt"; |
| 382 | + char *template = "${MISSING_VAR}"; |
| 383 | + int ret; |
| 384 | + |
| 385 | + /* Test environment variable loading with missing file */ |
| 386 | + env = flb_env_create(); |
| 387 | + if (!TEST_CHECK(env != NULL)) { |
| 388 | + TEST_MSG("flb_env_create failed"); |
| 389 | + return; |
| 390 | + } |
| 391 | + |
| 392 | + /* Set file-based environment variable with missing file */ |
| 393 | + ret = flb_env_set_extended(env, "MISSING_VAR", NULL, "file:///tmp/flb_nonexistent_file.txt", 0); |
| 394 | + if (!TEST_CHECK(ret == -1)) { |
| 395 | + TEST_MSG("flb_env_set_extended failed"); |
| 396 | + flb_env_destroy(env); |
| 397 | + return; |
| 398 | + } |
| 399 | + |
| 400 | + /* Test variable translation should return empty string for missing file */ |
| 401 | + buf = flb_env_var_translate(env, template); |
| 402 | + if (!TEST_CHECK(buf != NULL)) { |
| 403 | + TEST_MSG("flb_env_var_translate failed"); |
| 404 | + flb_env_destroy(env); |
| 405 | + return; |
| 406 | + } |
| 407 | + |
| 408 | + /* Should return empty string for missing file */ |
| 409 | + if (!TEST_CHECK(strlen(buf) == 0)) { |
| 410 | + TEST_MSG("Expected empty string for missing file. Got=%s", buf); |
| 411 | + } |
| 412 | + |
| 413 | + flb_sds_destroy(buf); |
| 414 | + flb_env_destroy(env); |
| 415 | +} |
| 416 | + |
87 | 417 |
|
88 | 418 | TEST_LIST = {
|
89 | 419 | { "translate_long_env" , test_translate_long_env},
|
| 420 | + { "file_env_var_basic" , test_file_env_var_basic}, |
| 421 | + { "file_env_var_refresh" , test_file_env_var_refresh}, |
| 422 | + { "file_env_var_uri" , test_file_env_var_uri}, |
| 423 | + { "mixed_env_vars" , test_mixed_env_vars}, |
| 424 | + { "file_env_var_missing" , test_file_env_var_missing}, |
90 | 425 | { NULL, NULL }
|
91 | 426 | };
|
0 commit comments