Skip to content

Commit ffe6fb0

Browse files
committed
tests: runtime: Add out_gcs runtime test cases
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
1 parent c690ef2 commit ffe6fb0

3 files changed

Lines changed: 120 additions & 13 deletions

File tree

plugins/out_gcs/gcs.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,16 @@ static int gcs_upload_object(struct flb_gcs *ctx,
662662
struct flb_aws_header *encoding_header;
663663
char final_body_md5[25];
664664

665+
if (gcs_under_test_mode() == FLB_TRUE) {
666+
mock_gcs_call_increment_counter("UploadObject");
667+
668+
if (getenv("TEST_GCS_UPLOAD_ERROR") != NULL) {
669+
return -1;
670+
}
671+
672+
return 0;
673+
}
674+
665675
u_conn = flb_upstream_conn_get(ctx->u);
666676
if (!u_conn) {
667677
return -1;
@@ -708,19 +718,6 @@ static int gcs_upload_object(struct flb_gcs *ctx,
708718
}
709719
}
710720

711-
if (gcs_under_test_mode() == FLB_TRUE) {
712-
mock_gcs_call_increment_counter("UploadObject");
713-
if (getenv("TEST_GCS_UPLOAD_ERROR") != NULL) {
714-
ret = -1;
715-
}
716-
else {
717-
ret = 0;
718-
}
719-
flb_http_client_destroy(c);
720-
flb_upstream_conn_release(u_conn);
721-
return ret;
722-
}
723-
724721
ret = flb_http_do(c, &bytes);
725722
flb_http_client_destroy(c);
726723
flb_upstream_conn_release(u_conn);

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ if(FLB_IN_LIB)
265265
FLB_RT_TEST(FLB_OUT_FILE "out_file.c")
266266
endif()
267267
FLB_RT_TEST(FLB_OUT_S3 "out_s3.c")
268+
FLB_RT_TEST(FLB_OUT_GCS "out_gcs.c")
268269
if (FLB_IN_OPENTELEMETRY AND FLB_OUT_S3)
269270
FLB_RT_TEST(FLB_OUT_S3 "out_s3_otlp_json.c")
270271
endif()

tests/runtime/out_gcs.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
#include <fluent-bit.h>
3+
#include "flb_tests_runtime.h"
4+
5+
#include "data/td/json_td.h"
6+
7+
/* Local 'test' credentials file */
8+
#define SERVICE_CREDENTIALS \
9+
FLB_TESTS_DATA_PATH "/data/stackdriver/stackdriver-credentials.json"
10+
11+
void flb_test_gcs_upload_success(void)
12+
{
13+
int ret;
14+
flb_ctx_t *ctx;
15+
int in_ffd;
16+
int out_ffd;
17+
char *call_count_str;
18+
int call_count;
19+
char store_dir[] = "/tmp/flb-gcs-test-success-XXXXXX";
20+
21+
TEST_CHECK(mkdtemp(store_dir) != NULL);
22+
23+
setenv("FLB_GCS_PLUGIN_UNDER_TEST", "true", 1);
24+
25+
ctx = flb_create();
26+
27+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
28+
TEST_CHECK(in_ffd >= 0);
29+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
30+
31+
out_ffd = flb_output(ctx, (char *) "gcs", NULL);
32+
TEST_CHECK(out_ffd >= 0);
33+
flb_output_set(ctx, out_ffd, "match", "*", NULL);
34+
flb_output_set(ctx, out_ffd, "bucket", "fluent", NULL);
35+
flb_output_set(ctx, out_ffd, "google_service_credentials", SERVICE_CREDENTIALS, NULL);
36+
flb_output_set(ctx, out_ffd, "upload_timeout", "3s", NULL);
37+
flb_output_set(ctx, out_ffd, "store_dir", store_dir, NULL);
38+
39+
ret = flb_start(ctx);
40+
TEST_CHECK(ret == 0);
41+
42+
flb_lib_push(ctx, in_ffd, (char *) JSON_TD, (int) sizeof(JSON_TD) - 1);
43+
sleep(5);
44+
45+
call_count_str = getenv("TEST_GCS_UploadObject_CALL_COUNT");
46+
call_count = call_count_str ? atoi(call_count_str) : 0;
47+
TEST_CHECK_(call_count == 1,
48+
"Expected 1 UploadObject call, got %d", call_count);
49+
50+
flb_stop(ctx);
51+
flb_destroy(ctx);
52+
53+
unsetenv("FLB_GCS_PLUGIN_UNDER_TEST");
54+
unsetenv("TEST_GCS_UploadObject_CALL_COUNT");
55+
}
56+
57+
void flb_test_gcs_upload_error(void)
58+
{
59+
int ret;
60+
flb_ctx_t *ctx;
61+
int in_ffd;
62+
int out_ffd;
63+
char *call_count_str;
64+
int call_count;
65+
char store_dir[] = "/tmp/flb-gcs-test-error-XXXXXX";
66+
67+
TEST_CHECK(mkdtemp(store_dir) != NULL);
68+
69+
setenv("FLB_GCS_PLUGIN_UNDER_TEST", "true", 1);
70+
setenv("TEST_GCS_UPLOAD_ERROR", "true", 1);
71+
72+
ctx = flb_create();
73+
74+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
75+
TEST_CHECK(in_ffd >= 0);
76+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
77+
78+
out_ffd = flb_output(ctx, (char *) "gcs", NULL);
79+
TEST_CHECK(out_ffd >= 0);
80+
flb_output_set(ctx, out_ffd, "match", "*", NULL);
81+
flb_output_set(ctx, out_ffd, "bucket", "fluent", NULL);
82+
flb_output_set(ctx, out_ffd, "google_service_credentials", SERVICE_CREDENTIALS, NULL);
83+
flb_output_set(ctx, out_ffd, "upload_timeout", "3s", NULL);
84+
flb_output_set(ctx, out_ffd, "store_dir", store_dir, NULL);
85+
86+
ret = flb_start(ctx);
87+
TEST_CHECK(ret == 0);
88+
89+
flb_lib_push(ctx, in_ffd, (char *) JSON_TD, (int) sizeof(JSON_TD) - 1);
90+
sleep(6);
91+
92+
call_count_str = getenv("TEST_GCS_UploadObject_CALL_COUNT");
93+
call_count = call_count_str ? atoi(call_count_str) : 0;
94+
TEST_CHECK_(call_count >= 1,
95+
"Expected >=1 UploadObject calls, got %d", call_count);
96+
97+
flb_stop(ctx);
98+
flb_destroy(ctx);
99+
100+
unsetenv("FLB_GCS_PLUGIN_UNDER_TEST");
101+
unsetenv("TEST_GCS_UPLOAD_ERROR");
102+
unsetenv("TEST_GCS_UploadObject_CALL_COUNT");
103+
}
104+
105+
TEST_LIST = {
106+
{"upload_success", flb_test_gcs_upload_success},
107+
{"upload_error", flb_test_gcs_upload_error},
108+
{NULL, NULL}
109+
};

0 commit comments

Comments
 (0)