|
| 1 | +#include <fluent-bit.h> |
| 2 | +#include <fluent-bit/flb_time.h> |
| 3 | +#include "flb_tests_runtime.h" |
| 4 | + |
| 5 | +#define DPATH_PROM_TEXTFILE FLB_TESTS_DATA_PATH "/data/prometheus_textfile" |
| 6 | + |
| 7 | +static pthread_mutex_t result_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 8 | +static int num_output = 0; |
| 9 | + |
| 10 | +static int cb_count(void *record, size_t size, void *data) |
| 11 | +{ |
| 12 | + (void)record; |
| 13 | + (void)size; |
| 14 | + (void)data; |
| 15 | + |
| 16 | + pthread_mutex_lock(&result_mutex); |
| 17 | + num_output++; |
| 18 | + pthread_mutex_unlock(&result_mutex); |
| 19 | + flb_free(record); |
| 20 | + return 0; |
| 21 | +} |
| 22 | + |
| 23 | +static void clear_output() |
| 24 | +{ |
| 25 | + pthread_mutex_lock(&result_mutex); |
| 26 | + num_output = 0; |
| 27 | + pthread_mutex_unlock(&result_mutex); |
| 28 | +} |
| 29 | + |
| 30 | +struct test_ctx { |
| 31 | + flb_ctx_t *flb; |
| 32 | + int i_ffd; |
| 33 | + int o_ffd; |
| 34 | +}; |
| 35 | + |
| 36 | +static struct test_ctx *test_ctx_create(struct flb_lib_out_cb *cb) |
| 37 | +{ |
| 38 | + struct test_ctx *ctx = flb_malloc(sizeof(struct test_ctx)); |
| 39 | + if (!TEST_CHECK(ctx != NULL)) { |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + ctx->flb = flb_create(); |
| 44 | + flb_service_set(ctx->flb, |
| 45 | + "Flush", "0.2", |
| 46 | + "Grace", "1", |
| 47 | + "Log_Level", "error", |
| 48 | + NULL); |
| 49 | + |
| 50 | + ctx->o_ffd = flb_output(ctx->flb, (char *)"lib", cb); |
| 51 | + if (!TEST_CHECK(ctx->o_ffd >= 0)) { |
| 52 | + flb_destroy(ctx->flb); |
| 53 | + flb_free(ctx); |
| 54 | + return NULL; |
| 55 | + } |
| 56 | + |
| 57 | + return ctx; |
| 58 | +} |
| 59 | + |
| 60 | +static void test_ctx_destroy(struct test_ctx *ctx) |
| 61 | +{ |
| 62 | + TEST_CHECK(ctx != NULL); |
| 63 | + flb_stop(ctx->flb); |
| 64 | + flb_destroy(ctx->flb); |
| 65 | + flb_free(ctx); |
| 66 | +} |
| 67 | + |
| 68 | +static void test_prometheus_textfile(void) |
| 69 | +{ |
| 70 | + struct flb_lib_out_cb cb = {0}; |
| 71 | + struct test_ctx *ctx; |
| 72 | + int ret; |
| 73 | + int count = 0; |
| 74 | + struct flb_time start, end, diff; |
| 75 | + uint64_t ms = 0; |
| 76 | + |
| 77 | + cb.cb = cb_count; |
| 78 | + cb.data = NULL; |
| 79 | + |
| 80 | + clear_output(); |
| 81 | + |
| 82 | + ctx = test_ctx_create(&cb); |
| 83 | + TEST_CHECK(ctx != NULL); |
| 84 | + |
| 85 | + ctx->i_ffd = flb_input(ctx->flb, (char *)"prometheus_textfile", NULL); |
| 86 | + TEST_CHECK(ctx->i_ffd >= 0); |
| 87 | + ret = flb_input_set(ctx->flb, ctx->i_ffd, |
| 88 | + "scrape_interval", "1s", |
| 89 | + "path", DPATH_PROM_TEXTFILE "/metrics.prom", |
| 90 | + NULL); |
| 91 | + TEST_CHECK(ret == 0); |
| 92 | + |
| 93 | + ret = flb_start(ctx->flb); |
| 94 | + TEST_CHECK(ret == 0); |
| 95 | + |
| 96 | + flb_time_get(&start); |
| 97 | + while (ms < 5000) { |
| 98 | + pthread_mutex_lock(&result_mutex); |
| 99 | + count = num_output; |
| 100 | + pthread_mutex_unlock(&result_mutex); |
| 101 | + if (count > 0) { |
| 102 | + break; |
| 103 | + } |
| 104 | + flb_time_msleep(200); |
| 105 | + flb_time_get(&end); |
| 106 | + flb_time_diff(&end, &start, &diff); |
| 107 | + ms = flb_time_to_nanosec(&diff) / 1000000; |
| 108 | + } |
| 109 | + |
| 110 | + TEST_CHECK(count > 0); |
| 111 | + |
| 112 | + test_ctx_destroy(ctx); |
| 113 | +} |
| 114 | + |
| 115 | +TEST_LIST = { |
| 116 | + {"prometheus_textfile", test_prometheus_textfile}, |
| 117 | + {NULL, NULL} |
| 118 | +}; |
| 119 | + |
0 commit comments