Skip to content

Commit

Permalink
in_calyptia_fleet: fix Windows failure
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Stephens <[email protected]>
  • Loading branch information
patrick-stephens committed Dec 16, 2024
1 parent 0d6fea1 commit 5c80ce7
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
Expand Down
124 changes: 124 additions & 0 deletions tests/runtime/in_calyptia_fleet_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

#include <fluent-bit.h>
#include <fluent-bit/calyptia/calyptia_constants.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_custom.h>

#include <monkey/mk_core.h>
#include <monkey/mk_lib.h>

#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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}
};

0 comments on commit 5c80ce7

Please sign in to comment.