Skip to content

Commit 4cfe096

Browse files
committed
tests: internal: input_chunk_routes: Windows support
Signed-off-by: Marat Abrarov <abrarov@gmail.com>
1 parent 1dcc0b7 commit 4cfe096

File tree

1 file changed

+95
-12
lines changed

1 file changed

+95
-12
lines changed

tests/internal/input_chunk_routes.c

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <fluent-bit/flb_input_plugin.h>
99
#include <fluent-bit/flb_log.h>
1010
#include <fluent-bit/flb_mem.h>
11+
#include <fluent-bit/flb_str.h>
1112
#include <fluent-bit/flb_output.h>
1213

1314
/* Dummy workaround: undefine input plugin macros to avoid redefinition warnings */
@@ -27,12 +28,13 @@
2728
#include <chunkio/chunkio.h>
2829
#include <chunkio/cio_utils.h>
2930
#include <string.h>
31+
#include <stdlib.h>
3032
#include <cmetrics/cmetrics.h>
3133

3234

33-
#define TEST_STREAM_PATH "/tmp/flb-chunk-direct-test"
34-
#define TEST_STREAM_PATH_MATCH "/tmp/flb-chunk-direct-test-match"
35-
#define TEST_STREAM_PATH_NULL "/tmp/flb-chunk-direct-test-null"
35+
#define TEST_STREAM_PATH "/flb-chunk-direct-test"
36+
#define TEST_STREAM_PATH_MATCH "/flb-chunk-direct-test-match"
37+
#define TEST_STREAM_PATH_NULL "/flb-chunk-direct-test-null"
3638

3739
static int write_test_log_payload(struct cio_chunk *chunk)
3840
{
@@ -332,6 +334,58 @@ static int write_legacy_chunk_metadata(struct cio_chunk *chunk,
332334
return ret;
333335
}
334336

337+
static char* env_tmpdir()
338+
{
339+
char *env;
340+
341+
/* Unix */
342+
env = getenv("TMPDIR");
343+
if (env) {
344+
return flb_strdup(env);
345+
}
346+
347+
/* Windows */
348+
env = getenv("TEMP");
349+
if (env) {
350+
return flb_strdup(env);
351+
}
352+
env = getenv("TMP");
353+
if (env) {
354+
return flb_strdup(env);
355+
}
356+
357+
/* Fallback */
358+
return flb_strdup("/tmp");
359+
}
360+
361+
static char* tmpdir_cat(const char *postfix)
362+
{
363+
char *tmpdir;
364+
char *ret;
365+
size_t tmpdir_len;
366+
size_t postfix_len;
367+
368+
tmpdir = env_tmpdir();
369+
if (!tmpdir) {
370+
return NULL;
371+
}
372+
373+
tmpdir_len = strlen(tmpdir);
374+
postfix_len = strlen(postfix);
375+
ret = (char *) flb_malloc(tmpdir_len + postfix_len + 1);
376+
if (!ret) {
377+
flb_free(tmpdir);
378+
return NULL;
379+
}
380+
381+
memcpy(ret, tmpdir, tmpdir_len);
382+
flb_free(tmpdir);
383+
memcpy(ret + tmpdir_len, postfix, postfix_len);
384+
ret[tmpdir_len + postfix_len] = '\0';
385+
return ret;
386+
}
387+
388+
335389
static void test_chunk_metadata_direct_routes()
336390
{
337391
struct cio_options opts;
@@ -341,6 +395,7 @@ static void test_chunk_metadata_direct_routes()
341395
struct flb_input_chunk ic;
342396
struct flb_chunk_direct_route output_routes[2];
343397
struct flb_chunk_direct_route *loaded_routes;
398+
char *stream_path;
344399
char *content_buf;
345400
const char *tag_buf;
346401
const char *tag_string;
@@ -353,32 +408,41 @@ static void test_chunk_metadata_direct_routes()
353408
size_t content_size;
354409
size_t payload_size;
355410

411+
stream_path = tmpdir_cat(TEST_STREAM_PATH);
412+
TEST_CHECK(stream_path != NULL);
413+
if (!stream_path) {
414+
return;
415+
}
416+
356417
payload_size = sizeof(payload) - 1;
357418
tag_string = "test.tag";
358419
expected_tag_len = strlen(tag_string);
359420

360-
cio_utils_recursive_delete(TEST_STREAM_PATH);
421+
cio_utils_recursive_delete(stream_path);
361422
memset(&opts, 0, sizeof(opts));
362423
cio_options_init(&opts);
363-
opts.root_path = TEST_STREAM_PATH;
424+
opts.root_path = stream_path;
364425
opts.flags = CIO_OPEN;
365426
ctx = cio_create(&opts);
366427
TEST_CHECK(ctx != NULL);
367428
if (!ctx) {
429+
flb_free(stream_path);
368430
return;
369431
}
370432

371433
stream = cio_stream_create(ctx, "direct", CIO_STORE_FS);
372434
TEST_CHECK(stream != NULL);
373435
if (!stream) {
374436
cio_destroy(ctx);
437+
flb_free(stream_path);
375438
return;
376439
}
377440

378441
chunk = cio_chunk_open(ctx, stream, "meta", CIO_OPEN, 1024, &err);
379442
TEST_CHECK(chunk != NULL);
380443
if (!chunk) {
381444
cio_destroy(ctx);
445+
flb_free(stream_path);
382446
return;
383447
}
384448

@@ -478,7 +542,8 @@ static void test_chunk_metadata_direct_routes()
478542

479543
cio_chunk_close(chunk, CIO_TRUE);
480544
cio_destroy(ctx);
481-
cio_utils_recursive_delete(TEST_STREAM_PATH);
545+
cio_utils_recursive_delete(stream_path);
546+
flb_free(stream_path);
482547
}
483548

484549
static void test_chunk_restore_alias_plugin_match_multiple()
@@ -497,12 +562,19 @@ static void test_chunk_restore_alias_plugin_match_multiple()
497562
struct flb_output_plugin stdout_plugin;
498563
struct flb_output_plugin http_plugin;
499564
struct flb_chunk_direct_route route;
565+
char *stream_path;
500566
const char *tag_string;
501567
int tag_len;
502568
int ret;
503569
int err;
504570
int config_ready;
505571

572+
stream_path = tmpdir_cat(TEST_STREAM_PATH_MATCH);
573+
TEST_CHECK(stream_path != NULL);
574+
if (!stream_path) {
575+
return;
576+
}
577+
506578
ctx = NULL;
507579
stream = NULL;
508580
chunk = NULL;
@@ -511,15 +583,16 @@ static void test_chunk_restore_alias_plugin_match_multiple()
511583
tag_string = "test.tag";
512584
tag_len = (int) strlen(tag_string);
513585

514-
cio_utils_recursive_delete(TEST_STREAM_PATH_MATCH);
586+
cio_utils_recursive_delete(stream_path);
515587
memset(&opts, 0, sizeof(opts));
516588
cio_options_init(&opts);
517-
opts.root_path = TEST_STREAM_PATH_MATCH;
589+
opts.root_path = stream_path;
518590
opts.flags = CIO_OPEN;
519591

520592
ctx = cio_create(&opts);
521593
TEST_CHECK(ctx != NULL);
522594
if (!ctx) {
595+
flb_free(stream_path);
523596
return;
524597
}
525598

@@ -639,7 +712,8 @@ static void test_chunk_restore_alias_plugin_match_multiple()
639712
cleanup:
640713
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
641714
&in, &config, chunk, ctx, config_ready,
642-
TEST_STREAM_PATH_MATCH);
715+
stream_path);
716+
flb_free(stream_path);
643717
}
644718

645719
static void test_chunk_restore_alias_plugin_null_matches_all()
@@ -658,12 +732,19 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
658732
struct flb_output_plugin stdout_plugin;
659733
struct flb_output_plugin http_plugin;
660734
struct flb_chunk_direct_route route;
735+
char *stream_path;
661736
const char *tag_string;
662737
int tag_len;
663738
int ret;
664739
int err;
665740
int config_ready;
666741

742+
stream_path = tmpdir_cat(TEST_STREAM_PATH_NULL);
743+
TEST_CHECK(stream_path != NULL);
744+
if (!stream_path) {
745+
return;
746+
}
747+
667748
ctx = NULL;
668749
stream = NULL;
669750
chunk = NULL;
@@ -672,15 +753,16 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
672753
tag_string = "test.tag";
673754
tag_len = (int) strlen(tag_string);
674755

675-
cio_utils_recursive_delete(TEST_STREAM_PATH_NULL);
756+
cio_utils_recursive_delete(stream_path);
676757
memset(&opts, 0, sizeof(opts));
677758
cio_options_init(&opts);
678-
opts.root_path = TEST_STREAM_PATH_NULL;
759+
opts.root_path = stream_path;
679760
opts.flags = CIO_OPEN;
680761

681762
ctx = cio_create(&opts);
682763
TEST_CHECK(ctx != NULL);
683764
if (!ctx) {
765+
flb_free(stream_path);
684766
return;
685767
}
686768

@@ -800,7 +882,8 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
800882
cleanup:
801883
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
802884
&in, &config, chunk, ctx, config_ready,
803-
TEST_STREAM_PATH_NULL);
885+
stream_path);
886+
flb_free(stream_path);
804887
}
805888

806889
TEST_LIST = {

0 commit comments

Comments
 (0)