|
| 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