Skip to content

Commit cfa533b

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

File tree

1 file changed

+92
-12
lines changed

1 file changed

+92
-12
lines changed

tests/internal/input_chunk_routes.c

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#undef flb_plg_debug
1818
#undef flb_plg_trace
1919

20+
#include <fluent-bit/flb_mem.h>
21+
#include <fluent-bit/flb_str.h>
2022
#include <fluent-bit/flb_output_plugin.h>
2123
#include <fluent-bit/flb_routes_mask.h>
2224
#include <fluent-bit/flb_router.h>
@@ -27,12 +29,13 @@
2729
#include <chunkio/chunkio.h>
2830
#include <chunkio/cio_utils.h>
2931
#include <string.h>
32+
#include <stdlib.h>
3033
#include <cmetrics/cmetrics.h>
3134

3235

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"
36+
#define TEST_STREAM_PATH "/flb-chunk-direct-test"
37+
#define TEST_STREAM_PATH_MATCH "/flb-chunk-direct-test-match"
38+
#define TEST_STREAM_PATH_NULL "/flb-chunk-direct-test-null"
3639

3740
static int write_test_log_payload(struct cio_chunk *chunk)
3841
{
@@ -332,6 +335,54 @@ static int write_legacy_chunk_metadata(struct cio_chunk *chunk,
332335
return ret;
333336
}
334337

338+
static char* env_tmpdir()
339+
{
340+
char *env;
341+
342+
/* Unix */
343+
env = getenv("TMPDIR");
344+
if (env) {
345+
return flb_strdup(env);
346+
}
347+
348+
/* Windows */
349+
env = getenv("TMP");
350+
if (env) {
351+
return flb_strdup(env);
352+
}
353+
354+
/* Fallback */
355+
return flb_strdup("/tmp");
356+
}
357+
358+
static char* tmpdir_cat(const char *postfix)
359+
{
360+
char *tmpdir;
361+
char *ret;
362+
size_t tmpdir_len;
363+
size_t postfix_len;
364+
365+
tmpdir = env_tmpdir();
366+
if (!tmpdir) {
367+
return NULL;
368+
}
369+
370+
tmpdir_len = strlen(tmpdir);
371+
postfix_len = strlen(postfix);
372+
ret = (char *) flb_malloc(tmpdir_len + postfix_len + 1);
373+
if (!ret) {
374+
flb_free(tmpdir);
375+
return NULL;
376+
}
377+
378+
memcpy(ret, tmpdir, tmpdir_len);
379+
flb_free(tmpdir);
380+
memcpy(ret + tmpdir_len, postfix, postfix_len);
381+
ret[tmpdir_len + postfix_len] = '\0';
382+
return ret;
383+
}
384+
385+
335386
static void test_chunk_metadata_direct_routes()
336387
{
337388
struct cio_options opts;
@@ -341,6 +392,7 @@ static void test_chunk_metadata_direct_routes()
341392
struct flb_input_chunk ic;
342393
struct flb_chunk_direct_route output_routes[2];
343394
struct flb_chunk_direct_route *loaded_routes;
395+
char *stream_path;
344396
char *content_buf;
345397
const char *tag_buf;
346398
const char *tag_string;
@@ -353,32 +405,41 @@ static void test_chunk_metadata_direct_routes()
353405
size_t content_size;
354406
size_t payload_size;
355407

408+
stream_path = tmpdir_cat(TEST_STREAM_PATH);
409+
TEST_CHECK(stream_path != NULL);
410+
if (!stream_path) {
411+
return;
412+
}
413+
356414
payload_size = sizeof(payload) - 1;
357415
tag_string = "test.tag";
358416
expected_tag_len = strlen(tag_string);
359417

360-
cio_utils_recursive_delete(TEST_STREAM_PATH);
418+
cio_utils_recursive_delete(stream_path);
361419
memset(&opts, 0, sizeof(opts));
362420
cio_options_init(&opts);
363-
opts.root_path = TEST_STREAM_PATH;
421+
opts.root_path = stream_path;
364422
opts.flags = CIO_OPEN;
365423
ctx = cio_create(&opts);
366424
TEST_CHECK(ctx != NULL);
367425
if (!ctx) {
426+
flb_free(stream_path);
368427
return;
369428
}
370429

371430
stream = cio_stream_create(ctx, "direct", CIO_STORE_FS);
372431
TEST_CHECK(stream != NULL);
373432
if (!stream) {
374433
cio_destroy(ctx);
434+
flb_free(stream_path);
375435
return;
376436
}
377437

378438
chunk = cio_chunk_open(ctx, stream, "meta", CIO_OPEN, 1024, &err);
379439
TEST_CHECK(chunk != NULL);
380440
if (!chunk) {
381441
cio_destroy(ctx);
442+
flb_free(stream_path);
382443
return;
383444
}
384445

@@ -478,7 +539,8 @@ static void test_chunk_metadata_direct_routes()
478539

479540
cio_chunk_close(chunk, CIO_TRUE);
480541
cio_destroy(ctx);
481-
cio_utils_recursive_delete(TEST_STREAM_PATH);
542+
cio_utils_recursive_delete(stream_path);
543+
flb_free(stream_path);
482544
}
483545

484546
static void test_chunk_restore_alias_plugin_match_multiple()
@@ -497,12 +559,19 @@ static void test_chunk_restore_alias_plugin_match_multiple()
497559
struct flb_output_plugin stdout_plugin;
498560
struct flb_output_plugin http_plugin;
499561
struct flb_chunk_direct_route route;
562+
char *stream_path;
500563
const char *tag_string;
501564
int tag_len;
502565
int ret;
503566
int err;
504567
int config_ready;
505568

569+
stream_path = tmpdir_cat(TEST_STREAM_PATH_MATCH);
570+
TEST_CHECK(stream_path != NULL);
571+
if (!stream_path) {
572+
return;
573+
}
574+
506575
ctx = NULL;
507576
stream = NULL;
508577
chunk = NULL;
@@ -511,15 +580,16 @@ static void test_chunk_restore_alias_plugin_match_multiple()
511580
tag_string = "test.tag";
512581
tag_len = (int) strlen(tag_string);
513582

514-
cio_utils_recursive_delete(TEST_STREAM_PATH_MATCH);
583+
cio_utils_recursive_delete(stream_path);
515584
memset(&opts, 0, sizeof(opts));
516585
cio_options_init(&opts);
517-
opts.root_path = TEST_STREAM_PATH_MATCH;
586+
opts.root_path = stream_path;
518587
opts.flags = CIO_OPEN;
519588

520589
ctx = cio_create(&opts);
521590
TEST_CHECK(ctx != NULL);
522591
if (!ctx) {
592+
flb_free(stream_path);
523593
return;
524594
}
525595

@@ -639,7 +709,8 @@ static void test_chunk_restore_alias_plugin_match_multiple()
639709
cleanup:
640710
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
641711
&in, &config, chunk, ctx, config_ready,
642-
TEST_STREAM_PATH_MATCH);
712+
stream_path);
713+
flb_free(stream_path);
643714
}
644715

645716
static void test_chunk_restore_alias_plugin_null_matches_all()
@@ -658,12 +729,19 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
658729
struct flb_output_plugin stdout_plugin;
659730
struct flb_output_plugin http_plugin;
660731
struct flb_chunk_direct_route route;
732+
char *stream_path;
661733
const char *tag_string;
662734
int tag_len;
663735
int ret;
664736
int err;
665737
int config_ready;
666738

739+
stream_path = tmpdir_cat(TEST_STREAM_PATH_NULL);
740+
TEST_CHECK(stream_path != NULL);
741+
if (!stream_path) {
742+
return;
743+
}
744+
667745
ctx = NULL;
668746
stream = NULL;
669747
chunk = NULL;
@@ -672,15 +750,16 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
672750
tag_string = "test.tag";
673751
tag_len = (int) strlen(tag_string);
674752

675-
cio_utils_recursive_delete(TEST_STREAM_PATH_NULL);
753+
cio_utils_recursive_delete(stream_path);
676754
memset(&opts, 0, sizeof(opts));
677755
cio_options_init(&opts);
678-
opts.root_path = TEST_STREAM_PATH_NULL;
756+
opts.root_path = stream_path;
679757
opts.flags = CIO_OPEN;
680758

681759
ctx = cio_create(&opts);
682760
TEST_CHECK(ctx != NULL);
683761
if (!ctx) {
762+
flb_free(stream_path);
684763
return;
685764
}
686765

@@ -800,7 +879,8 @@ static void test_chunk_restore_alias_plugin_null_matches_all()
800879
cleanup:
801880
cleanup_test_routing_scenario(ic, &stdout_one, &stdout_two, &http_out,
802881
&in, &config, chunk, ctx, config_ready,
803-
TEST_STREAM_PATH_NULL);
882+
stream_path);
883+
flb_free(stream_path);
804884
}
805885

806886
TEST_LIST = {

0 commit comments

Comments
 (0)