Skip to content

Commit 1f3110f

Browse files
cosmo0920edsiper
authored andcommitted
tests: internal: Make Windows compatible on error_reporter cases
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent 714b2a3 commit 1f3110f

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

tests/internal/error_reporter.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
22
#include <stdio.h>
33
#include <stdlib.h>
4-
#include <unistd.h>
54
#include <time.h>
65
#include <monkey/mk_core/mk_list.h>
76

7+
#include <fluent-bit/flb_compat.h>
88
#include <fluent-bit/flb_mem.h>
99
#include <fluent-bit/aws/flb_aws_error_reporter.h>
1010

1111
#include "flb_tests_internal.h"
1212

13-
const char* file_path = "/tmp/error.log";
13+
#ifdef FLB_SYSTEM_WINDOWS
14+
#define TEST_ERROR_FILE_PATH "error.log"
15+
#define TEST_INVALID_ERROR_FILE_PATH "NUL\\error.log"
16+
17+
static int test_setenv(const char *name, const char *value, int overwrite)
18+
{
19+
if (overwrite == 0 && getenv(name) != NULL) {
20+
return 0;
21+
}
22+
23+
return _putenv_s(name, value);
24+
}
25+
#else
26+
#define TEST_ERROR_FILE_PATH "/tmp/error.log"
27+
#define TEST_INVALID_ERROR_FILE_PATH "/dev/null/error.log"
28+
#define test_setenv(name, value, overwrite) setenv(name, value, overwrite)
29+
#endif
30+
31+
const char* file_path = TEST_ERROR_FILE_PATH;
1432
const char* error_message_1 = "[engine] scheduler could not start";
1533
const char* error_message_2 = "[engine] scheduler could not stop";
1634

1735

1836
void test_flb_aws_error_reporter_create() {
1937

20-
setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
38+
test_setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
2139
struct flb_aws_error_reporter *error_reporter = flb_aws_error_reporter_create();
2240
TEST_CHECK((void*) error_reporter != NULL);
2341
TEST_CHECK((void*)error_reporter->file_path != NULL);
@@ -36,7 +54,7 @@ void test_flb_aws_error_reporter_write() {
3654
}
3755
error_message_3[1040] = '\0';
3856

39-
setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
57+
test_setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
4058

4159
struct flb_aws_error_reporter *error_reporter = flb_aws_error_reporter_create();
4260
flb_aws_error_reporter_write(error_reporter, error_message_1);
@@ -62,7 +80,7 @@ void test_flb_aws_error_reporter_write() {
6280

6381
void test_flb_aws_error_reporter_clean() {
6482

65-
setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
83+
test_setenv(STATUS_MESSAGE_FILE_PATH_ENV, file_path, 1);
6684
struct flb_aws_error_reporter *error_reporter = flb_aws_error_reporter_create();
6785
flb_aws_error_reporter_write(error_reporter, error_message_1);
6886
time_t start = time(NULL);
@@ -79,8 +97,8 @@ void test_flb_aws_error_reporter_write_open_failure()
7997
int ret;
8098
struct flb_aws_error_reporter *error_reporter;
8199

82-
setenv(STATUS_MESSAGE_FILE_PATH_ENV,
83-
"/dev/null/error.log", 1);
100+
test_setenv(STATUS_MESSAGE_FILE_PATH_ENV,
101+
TEST_INVALID_ERROR_FILE_PATH, 1);
84102

85103
error_reporter = flb_aws_error_reporter_create();
86104
TEST_CHECK(error_reporter != NULL);

0 commit comments

Comments
 (0)