Skip to content

Commit 30a04be

Browse files
braydonkedsiper
authored andcommitted
tests: runtime: filter_parser: adjust to updated parser API for system timezone
Signed-off-by: Eduardo Silva <[email protected]>
1 parent b857ac7 commit 30a04be

File tree

1 file changed

+139
-10
lines changed

1 file changed

+139
-10
lines changed

tests/runtime/filter_parser.c

Lines changed: 139 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void flb_test_filter_parser_extract_fields()
108108
/* Parser */
109109
parser = flb_parser_create("dummy_test", "regex", "^(?<INT>[^ ]+) (?<FLOAT>[^ ]+) (?<BOOL>[^ ]+) (?<STRING>.+)$",
110110
FLB_TRUE,
111-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, NULL, 0,
111+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE, NULL, 0,
112112
NULL, ctx->config);
113113
TEST_CHECK(parser != NULL);
114114

@@ -195,7 +195,7 @@ void flb_test_filter_parser_reserve_data_off()
195195
/* Parser */
196196
parser = flb_parser_create("dummy_test", "regex", "^(?<INT>[^ ]+) (?<FLOAT>[^ ]+) (?<BOOL>[^ ]+) (?<STRING>.+)$",
197197
FLB_TRUE,
198-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, NULL, 0,
198+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE, NULL, 0,
199199
NULL, ctx->config);
200200
TEST_CHECK(parser != NULL);
201201

@@ -273,7 +273,7 @@ void flb_test_filter_parser_handle_time_key()
273273
parser = flb_parser_create("timestamp", "regex", "^(?<time>.*)$", FLB_TRUE,
274274
"%Y-%m-%dT%H:%M:%S.%L",
275275
"time",
276-
NULL, MK_FALSE, MK_TRUE, FLB_FALSE,
276+
NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
277277
NULL, 0, NULL, ctx->config);
278278
TEST_CHECK(parser != NULL);
279279

@@ -351,7 +351,7 @@ void flb_test_filter_parser_handle_time_key_with_fractional_timestamp()
351351
parser = flb_parser_create("timestamp", "regex", "^(?<time>.*)$", FLB_TRUE,
352352
"%s.%L",
353353
"time",
354-
NULL, MK_FALSE, MK_TRUE, FLB_FALSE,
354+
NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
355355
NULL, 0, NULL, ctx->config);
356356
TEST_CHECK(parser != NULL);
357357

@@ -439,6 +439,7 @@ void flb_test_filter_parser_handle_time_key_with_time_zone()
439439
NULL, // time_offset
440440
MK_FALSE, // time_keep
441441
MK_TRUE, // time_strict
442+
FLB_FALSE, // time_system_timezone
442443
MK_FALSE, // logfmt_no_bare_keys
443444
NULL, // types
444445
0, // types_len
@@ -491,6 +492,133 @@ void flb_test_filter_parser_handle_time_key_with_time_zone()
491492
flb_destroy(ctx);
492493
}
493494

495+
void test_parser_timestamp_timezone(char *tz,
496+
char *time_fmt,
497+
char *timestamp,
498+
char *expected_epoch,
499+
int use_system_timezone)
500+
{
501+
int ret;
502+
int bytes;
503+
char *output, *original_tz;
504+
char p[256];
505+
char expected[12];
506+
flb_ctx_t *ctx;
507+
int in_ffd;
508+
int out_ffd;
509+
int filter_ffd;
510+
struct flb_parser *parser;
511+
512+
struct flb_lib_out_cb cb;
513+
cb.cb = callback_test;
514+
cb.data = NULL;
515+
516+
clear_output();
517+
518+
ctx = flb_create();
519+
520+
/* Configure service */
521+
flb_service_set(ctx, "Flush", FLUSH_INTERVAL, "Grace", "1", "Log_Level", "debug", NULL);
522+
523+
/* Input */
524+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
525+
TEST_CHECK(in_ffd >= 0);
526+
flb_input_set(ctx, in_ffd,
527+
"Tag", "test",
528+
NULL);
529+
530+
/* Parser */
531+
parser = flb_parser_create("timestamp", // name
532+
"regex", // format
533+
"^(?<time>.*)$", // regex
534+
FLB_TRUE, // skip_empty
535+
time_fmt, // time_fmt
536+
"time", // time_key
537+
NULL, // time_offset
538+
MK_FALSE, // time_keep
539+
MK_TRUE, // time_strict
540+
use_system_timezone, // time_system_timezone
541+
MK_FALSE, // logfmt_no_bare_keys
542+
NULL, // types
543+
0, // types_len
544+
NULL, // decoders
545+
ctx->config); // config
546+
TEST_CHECK(parser != NULL);
547+
548+
/* Filter */
549+
filter_ffd = flb_filter(ctx, (char *) "parser", NULL);
550+
TEST_CHECK(filter_ffd >= 0);
551+
ret = flb_filter_set(ctx, filter_ffd,
552+
"Match", "test",
553+
"Key_Name", "@timestamp",
554+
"Parser", "timestamp",
555+
"Reserve_Data", "On",
556+
NULL);
557+
TEST_CHECK(ret == 0);
558+
559+
/* Output */
560+
out_ffd = flb_output(ctx, (char *) "lib", &cb);
561+
TEST_CHECK(out_ffd >= 0);
562+
flb_output_set(ctx, out_ffd,
563+
"Match", "*",
564+
"format", "json",
565+
NULL);
566+
567+
/* Temporarily set the system timezone for this test */
568+
if (tz) {
569+
original_tz = getenv("TZ");
570+
ret = setenv("TZ", tz, 1);
571+
TEST_ASSERT(ret == 0);
572+
}
573+
574+
/* Start the engine */
575+
ret = flb_start(ctx);
576+
TEST_CHECK(ret == 0);
577+
if (ret != 0) {
578+
flb_destroy(ctx);
579+
return;
580+
}
581+
582+
/* Ingest data. */
583+
snprintf(p, 256, "[1448403340, {\"@timestamp\":\"%s\", \"message\":\"This is an example\"}]", timestamp);
584+
bytes = flb_lib_push(ctx, in_ffd, p, strlen(p));
585+
TEST_CHECK(bytes == strlen(p));
586+
587+
wait_with_timeout(2000, &output); /* waiting flush and ensuring data flush */
588+
TEST_CHECK_(output != NULL, "Expected output to not be NULL");
589+
if (output != NULL) {
590+
snprintf(expected, 12, "[%s", expected_epoch);
591+
TEST_CHECK_(strstr(output, expected) != NULL, "Expected output to contain '%s', got '%s'", expected, output);
592+
}
593+
594+
/* Reset original timezone */
595+
if (original_tz) {
596+
ret = setenv("TZ", original_tz, 1);
597+
TEST_CHECK(ret == 0);
598+
}
599+
600+
flb_stop(ctx);
601+
flb_destroy(ctx);
602+
}
603+
604+
void flb_test_filter_parser_use_system_timezone()
605+
{
606+
test_parser_timestamp_timezone("EST5EDT", /* char *tz */
607+
"%Y-%m-%d:%H:%M:%S", /* char *time_fmt */
608+
"2023-10-17:05:00:00", /* char *timestamp */
609+
"1697536800", /* char *expected_epoch */
610+
FLB_TRUE); /* int use_system_timezone */
611+
}
612+
613+
void flb_test_filter_parser_use_system_timezone_zone_in_timestamp()
614+
{
615+
test_parser_timestamp_timezone("EST5EDT", /* char *tz */
616+
"%Y-%m-%d:%H:%M:%S%z", /* char *time_fmt */
617+
"2023-10-17:05:00:00-0700", /* char *timestamp */
618+
"1697536800", /* char *expected_epoch */
619+
FLB_TRUE); /* int use_system_timezone */
620+
}
621+
494622
void flb_test_filter_parser_ignore_malformed_time()
495623
{
496624
int ret;
@@ -524,7 +652,7 @@ void flb_test_filter_parser_ignore_malformed_time()
524652
parser = flb_parser_create("timestamp", "regex",
525653
"^(?<time>.*)$", FLB_TRUE,
526654
"%Y-%m-%dT%H:%M:%S.%L", "time",
527-
NULL, FLB_FALSE, MK_TRUE, FLB_FALSE,
655+
NULL, FLB_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
528656
NULL, 0, NULL, ctx->config);
529657
TEST_CHECK(parser != NULL);
530658

@@ -602,7 +730,7 @@ void flb_test_filter_parser_preserve_original_field()
602730
/* Parser */
603731
parser = flb_parser_create("dummy_test", "regex", "^(?<INT>[^ ]+) (?<FLOAT>[^ ]+) (?<BOOL>[^ ]+) (?<STRING>.+)$",
604732
FLB_TRUE,
605-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, NULL, 0,
733+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE, NULL, 0,
606734
NULL, ctx->config);
607735
TEST_CHECK(parser != NULL);
608736

@@ -687,13 +815,13 @@ void flb_test_filter_parser_first_matched_when_mutilple_parser()
687815
/* Parser */
688816
parser = flb_parser_create("one", "regex", "^(?<one>.+?)$",
689817
FLB_TRUE,
690-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE,
818+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
691819
NULL, 0, NULL, ctx->config);
692820
TEST_CHECK(parser != NULL);
693821

694822
parser = flb_parser_create("two", "regex", "^(?<two>.+?)$",
695823
FLB_TRUE,
696-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE,
824+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
697825
NULL, 0, NULL, ctx->config);
698826
TEST_CHECK(parser != NULL);
699827

@@ -774,7 +902,7 @@ void flb_test_filter_parser_skip_empty_values_false()
774902
/* Parser */
775903
parser = flb_parser_create("one", "regex", "^(?<one>.+?)$",
776904
FLB_FALSE,
777-
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE,
905+
NULL, NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
778906
NULL, 0, NULL, ctx->config);
779907
TEST_CHECK(parser != NULL);
780908

@@ -818,12 +946,13 @@ void flb_test_filter_parser_skip_empty_values_false()
818946
flb_destroy(ctx);
819947
}
820948

821-
822949
TEST_LIST = {
823950
{"filter_parser_extract_fields", flb_test_filter_parser_extract_fields },
824951
{"filter_parser_reserve_data_off", flb_test_filter_parser_reserve_data_off },
825952
{"filter_parser_handle_time_key", flb_test_filter_parser_handle_time_key },
826953
{"filter_parser_handle_time_key_with_time_zone", flb_test_filter_parser_handle_time_key_with_time_zone },
954+
{"filter_parser_use_system_timezone", flb_test_filter_parser_use_system_timezone },
955+
{"filter_parser_use_system_timezone_zone_in_timestamp",flb_test_filter_parser_use_system_timezone_zone_in_timestamp },
827956
{"filter_parser_ignore_malformed_time", flb_test_filter_parser_ignore_malformed_time },
828957
{"filter_parser_preserve_original_field", flb_test_filter_parser_preserve_original_field },
829958
{"filter_parser_first_matched_when_multiple_parser", flb_test_filter_parser_first_matched_when_mutilple_parser },

0 commit comments

Comments
 (0)