Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions tests/include/flb_tests_tmpdir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2015-2026 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_TESTS_TMPDIR_H
#define FLB_TESTS_TMPDIR_H

#include <string.h>
#include <stdlib.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_str.h>

static inline char* flb_test_env_tmpdir()
{
char *env;

/* Unix */
env = getenv("TMPDIR");
if (env) {
return flb_strdup(env);
}

/* Windows */
env = getenv("TEMP");
if (env) {
return flb_strdup(env);
}
env = getenv("TMP");
if (env) {
return flb_strdup(env);
}

/* Fallback */
return flb_strdup("/tmp");
}

static inline char* flb_test_tmpdir_cat(const char *postfix)
{
char *tmpdir;
char *ret;
size_t tmpdir_len;
size_t postfix_len;

tmpdir = flb_test_env_tmpdir();
if (!tmpdir) {
return NULL;
}

tmpdir_len = strlen(tmpdir);
postfix_len = strlen(postfix);
ret = (char *) flb_malloc(tmpdir_len + postfix_len + 1);
if (!ret) {
flb_free(tmpdir);
return NULL;
}

memcpy(ret, tmpdir, tmpdir_len);
flb_free(tmpdir);
memcpy(ret + tmpdir_len, postfix, postfix_len);
ret[tmpdir_len + postfix_len] = '\0';
return ret;
}

#endif
2 changes: 2 additions & 0 deletions tests/internal/flb_tests_internal.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ static inline void fini_nop() {};
#define TEST_FINI { flb_test_env_config_destroy(); }
#endif

#include "../include/flb_tests_tmpdir.h"

#include "../lib/acutest/acutest.h"
#define FLB_TESTS_DATA_PATH "@FLB_TESTS_DATA_PATH@"

Expand Down
42 changes: 37 additions & 5 deletions tests/internal/input_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,18 @@ void do_test(char *system, const char *target, ...)
int out_ffd;
char path[PATH_MAX];
struct tail_test_result result = {0};
char *tmpdir;
char storage_path[PATH_MAX];

tmpdir = flb_test_env_tmpdir();
TEST_CHECK(tmpdir != NULL);
if (!tmpdir) {
return;
}
snprintf(storage_path, sizeof(storage_path) - 1, "%s/input-chunk-test-%s",
tmpdir, target);
flb_free(tmpdir);

result.nMatched = 0;
result.target = target;

Expand All @@ -176,8 +186,6 @@ void do_test(char *system, const char *target, ...)

ctx = flb_create();

snprintf(storage_path, sizeof(storage_path) - 1, "/tmp/input-chunk-test-%s", target);

/* create chunks in /tmp folder */
ret = flb_service_set(ctx,
"Parsers_File", DPATH "parser.conf",
Expand Down Expand Up @@ -258,14 +266,21 @@ void flb_test_input_chunk_dropping_chunks()
struct mk_list *head;
struct flb_input_chunk *ic;
struct flb_task *task;
char *storage_path;

storage_path = flb_test_tmpdir_cat("/input-chunk-test/");
TEST_CHECK(storage_path != NULL);
if (!storage_path) {
return;
}

/* Create context, flush every second (some checks omitted here) */
ctx = flb_create();

/* create chunks in /tmp folder */
ret = flb_service_set(ctx,
"flush", "2", "grace", "1",
"storage.path", "/tmp/input-chunk-test/",
"storage.path", storage_path,
"Log_Level", "error",
NULL);

Expand Down Expand Up @@ -307,6 +322,7 @@ void flb_test_input_chunk_dropping_chunks()
flb_time_msleep(2100);
flb_stop(ctx);
flb_destroy(ctx);
flb_free(storage_path);
}

static int gen_buf(msgpack_sbuffer *mp_sbuf, char *buf, size_t buf_size)
Expand Down Expand Up @@ -365,6 +381,13 @@ void flb_test_input_chunk_fs_chunks_size_real()
char buf[262144];
struct mk_event_loop *evl;
struct cio_options opts = {0};
char *root_path;

root_path = flb_test_tmpdir_cat("/input-chunk-fs_chunks-size_real");
TEST_CHECK(root_path != NULL);
if (!root_path) {
return;
}

flb_init_env();
cfg = flb_config_init();
Expand All @@ -380,7 +403,7 @@ void flb_test_input_chunk_fs_chunks_size_real()

cio_options_init(&opts);

opts.root_path = "/tmp/input-chunk-fs_chunks-size_real";
opts.root_path = root_path;
opts.log_cb = log_cb;
opts.log_level = CIO_LOG_DEBUG;
opts.flags = CIO_OPEN;
Expand Down Expand Up @@ -455,6 +478,7 @@ void flb_test_input_chunk_fs_chunks_size_real()
flb_input_exit_all(cfg);
flb_output_exit(cfg);
flb_config_exit(cfg);
flb_free(root_path);
}

/* This tests uses the subsystems of the engine directly
Expand All @@ -475,6 +499,13 @@ void flb_test_input_chunk_correct_total_records(void)
char buf[262144];
struct mk_event_loop *evl;
struct cio_options opts = {0};
char *root_path;

root_path = flb_test_tmpdir_cat("/input-chunk-fs_chunks-size_real");
TEST_CHECK(root_path != NULL);
if (!root_path) {
return;
}

flb_init_env();
cfg = flb_config_init();
Expand All @@ -490,7 +521,7 @@ void flb_test_input_chunk_correct_total_records(void)

cio_options_init(&opts);

opts.root_path = "/tmp/input-chunk-fs_chunks-size_real";
opts.root_path = root_path;
opts.log_cb = log_cb;
opts.log_level = CIO_LOG_DEBUG;
opts.flags = CIO_OPEN;
Expand Down Expand Up @@ -541,6 +572,7 @@ void flb_test_input_chunk_correct_total_records(void)
flb_input_exit_all(cfg);
flb_output_exit(cfg);
flb_config_exit(cfg);
flb_free(root_path);
}


Expand Down
53 changes: 41 additions & 12 deletions tests/internal/input_chunk_routes.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
#include <cmetrics/cmetrics.h>


#define TEST_STREAM_PATH "/tmp/flb-chunk-direct-test"
#define TEST_STREAM_PATH_MATCH "/tmp/flb-chunk-direct-test-match"
#define TEST_STREAM_PATH_NULL "/tmp/flb-chunk-direct-test-null"
#define TEST_STREAM_PATH "/flb-chunk-direct-test"
#define TEST_STREAM_PATH_MATCH "/flb-chunk-direct-test-match"
#define TEST_STREAM_PATH_NULL "/flb-chunk-direct-test-null"

static int write_test_log_payload(struct cio_chunk *chunk)
{
Expand Down Expand Up @@ -341,6 +341,7 @@ static void test_chunk_metadata_direct_routes()
struct flb_input_chunk ic;
struct flb_chunk_direct_route output_routes[2];
struct flb_chunk_direct_route *loaded_routes;
char *stream_path;
char *content_buf;
const char *tag_buf;
const char *tag_string;
Expand All @@ -353,32 +354,41 @@ static void test_chunk_metadata_direct_routes()
size_t content_size;
size_t payload_size;

stream_path = flb_test_tmpdir_cat(TEST_STREAM_PATH);
TEST_CHECK(stream_path != NULL);
if (!stream_path) {
return;
}

payload_size = sizeof(payload) - 1;
tag_string = "test.tag";
expected_tag_len = strlen(tag_string);

cio_utils_recursive_delete(TEST_STREAM_PATH);
cio_utils_recursive_delete(stream_path);
memset(&opts, 0, sizeof(opts));
cio_options_init(&opts);
opts.root_path = TEST_STREAM_PATH;
opts.root_path = stream_path;
opts.flags = CIO_OPEN;
ctx = cio_create(&opts);
TEST_CHECK(ctx != NULL);
if (!ctx) {
flb_free(stream_path);
return;
}

stream = cio_stream_create(ctx, "direct", CIO_STORE_FS);
TEST_CHECK(stream != NULL);
if (!stream) {
cio_destroy(ctx);
flb_free(stream_path);
return;
}

chunk = cio_chunk_open(ctx, stream, "meta", CIO_OPEN, 1024, &err);
TEST_CHECK(chunk != NULL);
if (!chunk) {
cio_destroy(ctx);
flb_free(stream_path);
return;
}

Expand Down Expand Up @@ -478,7 +488,8 @@ static void test_chunk_metadata_direct_routes()

cio_chunk_close(chunk, CIO_TRUE);
cio_destroy(ctx);
cio_utils_recursive_delete(TEST_STREAM_PATH);
cio_utils_recursive_delete(stream_path);
flb_free(stream_path);
}

static void test_chunk_restore_alias_plugin_match_multiple()
Expand All @@ -497,12 +508,19 @@ static void test_chunk_restore_alias_plugin_match_multiple()
struct flb_output_plugin stdout_plugin;
struct flb_output_plugin http_plugin;
struct flb_chunk_direct_route route;
char *stream_path;
const char *tag_string;
int tag_len;
int ret;
int err;
int config_ready;

stream_path = flb_test_tmpdir_cat(TEST_STREAM_PATH_MATCH);
TEST_CHECK(stream_path != NULL);
if (!stream_path) {
return;
}

ctx = NULL;
stream = NULL;
chunk = NULL;
Expand All @@ -511,15 +529,16 @@ static void test_chunk_restore_alias_plugin_match_multiple()
tag_string = "test.tag";
tag_len = (int) strlen(tag_string);

cio_utils_recursive_delete(TEST_STREAM_PATH_MATCH);
cio_utils_recursive_delete(stream_path);
memset(&opts, 0, sizeof(opts));
cio_options_init(&opts);
opts.root_path = TEST_STREAM_PATH_MATCH;
opts.root_path = stream_path;
opts.flags = CIO_OPEN;

ctx = cio_create(&opts);
TEST_CHECK(ctx != NULL);
if (!ctx) {
flb_free(stream_path);
return;
}

Expand Down Expand Up @@ -639,7 +658,8 @@ static void test_chunk_restore_alias_plugin_match_multiple()
cleanup:
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
&in, &config, chunk, ctx, config_ready,
TEST_STREAM_PATH_MATCH);
stream_path);
flb_free(stream_path);
}

static void test_chunk_restore_alias_plugin_null_matches_all()
Expand All @@ -658,12 +678,19 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
struct flb_output_plugin stdout_plugin;
struct flb_output_plugin http_plugin;
struct flb_chunk_direct_route route;
char *stream_path;
const char *tag_string;
int tag_len;
int ret;
int err;
int config_ready;

stream_path = flb_test_tmpdir_cat(TEST_STREAM_PATH_NULL);
TEST_CHECK(stream_path != NULL);
if (!stream_path) {
return;
}

ctx = NULL;
stream = NULL;
chunk = NULL;
Expand All @@ -672,15 +699,16 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
tag_string = "test.tag";
tag_len = (int) strlen(tag_string);

cio_utils_recursive_delete(TEST_STREAM_PATH_NULL);
cio_utils_recursive_delete(stream_path);
memset(&opts, 0, sizeof(opts));
cio_options_init(&opts);
opts.root_path = TEST_STREAM_PATH_NULL;
opts.root_path = stream_path;
opts.flags = CIO_OPEN;

ctx = cio_create(&opts);
TEST_CHECK(ctx != NULL);
if (!ctx) {
flb_free(stream_path);
return;
}

Expand Down Expand Up @@ -800,7 +828,8 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
cleanup:
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
&in, &config, chunk, ctx, config_ready,
TEST_STREAM_PATH_NULL);
stream_path);
flb_free(stream_path);
}

TEST_LIST = {
Expand Down
Loading