|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +#include <fluent-bit.h> |
| 4 | +#include <fluent-bit/flb_output.h> |
| 5 | + |
| 6 | +#include "../../plugins/out_prometheus_exporter/prom.h" |
| 7 | +#include "../../plugins/out_prometheus_exporter/prom_http.h" |
| 8 | + |
| 9 | +#include "flb_tests_runtime.h" |
| 10 | + |
| 11 | +static int get_free_port(void) |
| 12 | +{ |
| 13 | + int ret; |
| 14 | + int port; |
| 15 | + flb_sockfd_t sock; |
| 16 | + socklen_t len; |
| 17 | + struct sockaddr_in addr; |
| 18 | + |
| 19 | +#ifdef _WIN32 |
| 20 | + WSADATA wsa_data; |
| 21 | + WSAStartup(0x0201, &wsa_data); |
| 22 | +#endif |
| 23 | + |
| 24 | + len = sizeof(addr); |
| 25 | + memset(&addr, 0, sizeof(addr)); |
| 26 | + |
| 27 | + sock = socket(AF_INET, SOCK_STREAM, 0); |
| 28 | + if (sock < 0) { |
| 29 | + return -1; |
| 30 | + } |
| 31 | + |
| 32 | + addr.sin_family = AF_INET; |
| 33 | + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 34 | + addr.sin_port = htons(0); |
| 35 | + |
| 36 | + ret = bind(sock, (struct sockaddr *) &addr, sizeof(addr)); |
| 37 | + if (ret != 0) { |
| 38 | + flb_socket_close(sock); |
| 39 | + return -1; |
| 40 | + } |
| 41 | + |
| 42 | + ret = getsockname(sock, (struct sockaddr *) &addr, &len); |
| 43 | + if (ret != 0) { |
| 44 | + flb_socket_close(sock); |
| 45 | + return -1; |
| 46 | + } |
| 47 | + |
| 48 | + port = ntohs(addr.sin_port); |
| 49 | + flb_socket_close(sock); |
| 50 | + |
| 51 | + return port; |
| 52 | +} |
| 53 | + |
| 54 | +static void test_http_server_options(void) |
| 55 | +{ |
| 56 | + int ret; |
| 57 | + int port; |
| 58 | + int in_ffd; |
| 59 | + int out_ffd; |
| 60 | + char port_string[16]; |
| 61 | + flb_ctx_t *ctx; |
| 62 | + struct prom_exporter *prom_ctx; |
| 63 | + struct prom_http *prom_http; |
| 64 | + struct flb_output_instance *out; |
| 65 | + |
| 66 | + /* Multi-worker HTTP servers require shared listener ports (SO_REUSEPORT), |
| 67 | + * which are not supported on Windows. Use a single worker there so the |
| 68 | + * server starts; the parsed options are still verified on both platforms. |
| 69 | + */ |
| 70 | +#ifdef _WIN32 |
| 71 | + const char *workers_str = "1"; |
| 72 | + int expected_workers = 1; |
| 73 | +#else |
| 74 | + const char *workers_str = "2"; |
| 75 | + int expected_workers = 2; |
| 76 | +#endif |
| 77 | + |
| 78 | + port = get_free_port(); |
| 79 | + TEST_CHECK(port > 0); |
| 80 | + snprintf(port_string, sizeof(port_string), "%d", port); |
| 81 | + |
| 82 | + ctx = flb_create(); |
| 83 | + TEST_CHECK(ctx != NULL); |
| 84 | + |
| 85 | + ret = flb_service_set(ctx, |
| 86 | + "Flush", "1", |
| 87 | + "Grace", "1", |
| 88 | + "Log_Level", "error", |
| 89 | + NULL); |
| 90 | + TEST_CHECK(ret == 0); |
| 91 | + |
| 92 | + in_ffd = flb_input(ctx, "fluentbit_metrics", NULL); |
| 93 | + TEST_CHECK(in_ffd >= 0); |
| 94 | + ret = flb_input_set(ctx, in_ffd, |
| 95 | + "tag", "metrics", |
| 96 | + "scrape_interval", "1", |
| 97 | + NULL); |
| 98 | + TEST_CHECK(ret == 0); |
| 99 | + |
| 100 | + out_ffd = flb_output(ctx, "prometheus_exporter", NULL); |
| 101 | + TEST_CHECK(out_ffd >= 0); |
| 102 | + ret = flb_output_set(ctx, out_ffd, |
| 103 | + "match", "*", |
| 104 | + "host", "127.0.0.1", |
| 105 | + "port", port_string, |
| 106 | + "http_server.buffer_chunk_size", "64K", |
| 107 | + "http_server.workers", workers_str, |
| 108 | + NULL); |
| 109 | + TEST_CHECK(ret == 0); |
| 110 | + |
| 111 | + ret = flb_start(ctx); |
| 112 | + TEST_CHECK(ret == 0); |
| 113 | + |
| 114 | + out = flb_output_get_instance(ctx->config, out_ffd); |
| 115 | + TEST_CHECK(out != NULL); |
| 116 | + |
| 117 | + prom_ctx = out->context; |
| 118 | + TEST_CHECK(prom_ctx != NULL); |
| 119 | + TEST_CHECK(prom_ctx->http != NULL); |
| 120 | + |
| 121 | + prom_http = prom_ctx->http; |
| 122 | + TEST_CHECK(prom_http->server.buffer_chunk_size == |
| 123 | + out->http_server_config->buffer_chunk_size); |
| 124 | + TEST_CHECK(prom_http->server.workers == expected_workers); |
| 125 | +#ifndef _WIN32 |
| 126 | + /* workers > 1 forces the server onto a dedicated downstream worker |
| 127 | + * runtime; a single worker runs on the caller event loop (runtime NULL). |
| 128 | + */ |
| 129 | + TEST_CHECK(prom_http->server.runtime != NULL); |
| 130 | +#endif |
| 131 | + |
| 132 | + flb_stop(ctx); |
| 133 | + flb_destroy(ctx); |
| 134 | +} |
| 135 | + |
| 136 | +TEST_LIST = { |
| 137 | + {"http_server_options", test_http_server_options}, |
| 138 | + {NULL, NULL} |
| 139 | +}; |
0 commit comments