From 5c80ce717e819ad7e31640ef2b8b82640c09dda2 Mon Sep 17 00:00:00 2001 From: Patrick Stephens Date: Mon, 16 Dec 2024 15:51:09 +0000 Subject: [PATCH] in_calyptia_fleet: fix Windows failure Signed-off-by: Patrick Stephens --- plugins/in_calyptia_fleet/in_calyptia_fleet.c | 6 + tests/runtime/in_calyptia_fleet_test.c | 124 ++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index 8f2e4484bcb..089ec83545c 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -525,8 +525,13 @@ static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx, return FLB_FALSE; } + /* Prevent undefined references due to use of readlink */ +#ifdef FLB_SYSTEM_WINDOWS + strncpy(realname, cfgpath, sizeof(realname)-1); +#else switch (is_link(cfgpath)) { case FLB_TRUE: + len = readlink(cfgpath, realname, sizeof(realname)); if (len > sizeof(realname)) { @@ -540,6 +545,7 @@ static int parse_config_name_timestamp(struct flb_in_calyptia_fleet_config *ctx, flb_errno(); return FLB_FALSE; } +#endif fname = basename(realname); flb_plg_debug(ctx->ins, "parsing configuration timestamp from path: %s", fname); diff --git a/tests/runtime/in_calyptia_fleet_test.c b/tests/runtime/in_calyptia_fleet_test.c index c9cd8cb1da2..e8645acddeb 100644 --- a/tests/runtime/in_calyptia_fleet_test.c +++ b/tests/runtime/in_calyptia_fleet_test.c @@ -2,12 +2,24 @@ #include #include +#include +#include + +#include +#include + #include "flb_tests_runtime.h" #include "../../plugins/in_calyptia_fleet/in_calyptia_fleet.h" +#define MOCK_SERVER_HOST "127.0.0.1" +#define MOCK_SERVER_PORT "9876" + flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname); int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx); +static int tomlRequests = 0; +static int yamlRequests = 0; + /* Test context structure */ struct test_context { struct flb_in_calyptia_fleet_config *ctx; @@ -51,6 +63,10 @@ static struct test_context *init_test_context() t_ctx->ctx->fleet_name = flb_strdup("test_fleet"); t_ctx->ctx->machine_id = flb_strdup("test_machine_id"); + char mock_url[256] = {0}; + snprintf(mock_url, sizeof(mock_url) - 1, "%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT); + t_ctx->ctx->fleet_files_url = flb_strdup(mock_url); + t_ctx->ctx->fleet_config_legacy_format = FLB_TRUE; return t_ctx; @@ -114,8 +130,116 @@ static void test_in_fleet_format() { cleanup_test_context(t_ctx); } +static void mock_server_fleet_files_toml(mk_request_t *request, void *data) +{ + tomlRequests++; + /* Use a local buffer with correct size */ + char *response = "{\"id\":\"test-id\"}"; + size_t response_len = strlen(response); + + mk_http_status(request, 200); + mk_http_header(request, "Content-Type", sizeof("Content-Type") - 1, + "application/json", sizeof("application/json") - 1); + mk_http_send(request, response, response_len, NULL); + mk_http_done(request); +} + +static void mock_server_fleet_files_yaml(mk_request_t *request, void *data) +{ + yamlRequests++; + /* Use a local buffer with correct size */ + char *response = "{\"id\":\"test-id\"}"; + size_t response_len = strlen(response); + + mk_http_status(request, 200); + mk_http_header(request, "Content-Type", sizeof("Content-Type") - 1, + "application/json", sizeof("application/json") - 1); + mk_http_send(request, response, response_len, NULL); + mk_http_done(request); +} + +static void test_in_fleet_get_calyptia_files() { + char tmp[256] = {0}; + struct test_context *t_ctx = init_test_context(); + TEST_CHECK(t_ctx != NULL); + + /* Init mock server */ + mk_ctx_t *mock_ctx = mk_create(); + TEST_CHECK(mock_ctx != NULL); + + /* Compose listen address */ + snprintf(tmp, sizeof(tmp) - 1, "%s:%s", MOCK_SERVER_HOST, MOCK_SERVER_PORT); + int ret = mk_config_set(mock_ctx, "Listen", tmp, NULL); + TEST_CHECK(ret == 0); + + int vid = mk_vhost_create(mock_ctx, NULL); + TEST_CHECK(vid >= 0); + + sprintf(tmp, CALYPTIA_ENDPOINT_FLEET_CONFIG_INI, t_ctx->ctx->fleet_id); + ret = mk_vhost_handler(mock_ctx, vid, tmp, mock_server_fleet_files_toml, NULL); + TEST_CHECK(ret == 0); + + sprintf(tmp, CALYPTIA_ENDPOINT_FLEET_CONFIG_YAML, t_ctx->ctx->fleet_id); + ret = mk_vhost_handler(mock_ctx, vid, tmp, mock_server_fleet_files_yaml, NULL); + TEST_CHECK(ret == 0); + + ret = mk_start(mock_ctx); + TEST_CHECK(ret == 0); + + /* Allow the mock server to initialize */ + flb_time_msleep(500); + + tomlRequests = 0; + yamlRequests = 0; + + /* Init Fluent Bit context */ + flb_ctx_t *ctx = flb_create(); + TEST_CHECK(ctx != NULL); + + ret = flb_service_set(ctx, + "Log_Level", "debug", + NULL); + TEST_CHECK(ret == 0); + + /* Create dummy input */ + int in_ffd = flb_input(ctx, (char *)"dummy", NULL); + TEST_CHECK(in_ffd >= 0); + + /* Create custom Calyptia plugin */ + struct flb_custom_instance *calyptia = flb_custom_new(ctx->config, (char *)"calyptia", NULL); + TEST_CHECK(calyptia != NULL); + + /* Set custom plugin properties */ + flb_custom_set_property(calyptia, "api_key", "test-key"); + flb_custom_set_property(calyptia, "log_level", "debug"); + flb_custom_set_property(calyptia, "calyptia_host", MOCK_SERVER_HOST); + flb_custom_set_property(calyptia, "calyptia_port", MOCK_SERVER_PORT); + flb_custom_set_property(calyptia, "calyptia_tls", "off"); + flb_custom_set_property(calyptia, "calyptia_tls.verify", "off"); + + /* Start the engine */ + ret = flb_start(ctx); + TEST_CHECK(ret == 0); + + /* TOML */ + ret = get_calyptia_fleet_config(t_ctx->ctx); + TEST_CHECK(ret == 0); + TEST_CHECK(tomlRequests == 1); + TEST_CHECK(yamlRequests == 0); + + /* YAML */ + t_ctx->ctx->fleet_config_legacy_format = FLB_FALSE; + ret = get_calyptia_fleet_config(t_ctx->ctx); + TEST_CHECK(ret == 0); + TEST_CHECK(tomlRequests == 1); + TEST_CHECK(yamlRequests == 1); + + cleanup_test_context(t_ctx); +} + /* Define test list */ TEST_LIST = { {"in_calyptia_fleet_format", test_in_fleet_format}, + {"in_calyptia_fleet_get_files", test_in_fleet_get_calyptia_files}, {NULL, NULL} }; \ No newline at end of file