Skip to content

Commit

Permalink
in_calyptia_fleet: refactor to expose functions
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 5d51ec8 commit 297795b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
17 changes: 10 additions & 7 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@

#define DEFAULT_MAX_HTTP_BUFFER_SIZE "10485760"

static int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx);
static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
time_t timestamp);

#ifndef FLB_SYSTEM_WINDOWS

static int is_link(const char *path) {
Expand Down Expand Up @@ -1664,7 +1668,7 @@ flb_sds_t fleet_config_get(struct flb_in_calyptia_fleet_config *ctx)
return NULL;
}

buf = flb_sds_create_size(2048);
buf = flb_sds_create_size(CALYPTIA_MAX_DIR_SIZE);

if (!buf) {
return NULL;
Expand Down Expand Up @@ -1754,7 +1758,7 @@ static int create_fleet_header(struct flb_in_calyptia_fleet_config *ctx)
return rc;
}

static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t cfgname;
flb_sds_t cfgnewname;
Expand Down Expand Up @@ -1804,7 +1808,7 @@ static int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx)

/* new file created! */
if (ret == 1) {
get_calyptia_files(ctx, ctx->fleet_files_url, time_last_modified);
get_calyptia_files(ctx, time_last_modified);

cfgname = time_fleet_config_filename(ctx, time_last_modified);

Expand Down Expand Up @@ -1926,7 +1930,7 @@ static int fleet_mkdir(struct flb_in_calyptia_fleet_config *ctx, time_t timestam
return ret;
}

int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx)
static int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx)
{
flb_sds_t fleetcurdir;
int ret;
Expand Down Expand Up @@ -2117,14 +2121,13 @@ static int create_fleet_files(struct flb_in_calyptia_fleet_config *ctx,
return 0;
}

int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
time_t timestamp)
{
struct flb_http_client *client;
int ret = -1;

if (ctx == NULL || url == NULL) {
if (ctx == NULL || ctx->fleet_files_url == NULL) {
return -1;
}

Expand Down
6 changes: 1 addition & 5 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *
#define old_fleet_config_filename(a) fleet_config_filename((a), "old")
#define hdr_fleet_config_filename(a) fleet_config_filename((a), "header")

int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
time_t timestamp);

int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx);
int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx);

#endif /* FLB_IN_CALYPTIA_FLEET_H */
33 changes: 14 additions & 19 deletions tests/runtime/in_calyptia_fleet_test.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

#include <fluent-bit.h>
#include <fluent-bit/calyptia/calyptia_constants.h>
#include "flb_tests_runtime.h"
#include "../../plugins/in_calyptia_fleet/in_calyptia_fleet.h"

flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname);

int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
time_t timestamp);

int fleet_cur_chdir(struct flb_in_calyptia_fleet_config *ctx);
int get_calyptia_fleet_config(struct flb_in_calyptia_fleet_config *ctx);

/* Test context structure */
struct test_context {
Expand Down Expand Up @@ -54,6 +51,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 All @@ -71,6 +72,7 @@ static void cleanup_test_context(struct test_context *t_ctx)

if (t_ctx->ctx->fleet_name) flb_free(t_ctx->ctx->fleet_name);
if (t_ctx->ctx->machine_id) flb_free(t_ctx->ctx->machine_id);
if (t_ctx->ctx->fleet_files_url) flb_free(t_ctx->ctx->fleet_files_url);

if (t_ctx->ctx->ins) flb_free(t_ctx->ctx->ins);
flb_free(t_ctx->ctx);
Expand All @@ -84,7 +86,7 @@ static void cleanup_test_context(struct test_context *t_ctx)
flb_free(t_ctx);
}

static void test_in_fleet_toml_format() {
static void test_in_fleet_format() {
struct test_context *t_ctx = init_test_context();
TEST_CHECK(t_ctx != NULL);

Expand All @@ -98,33 +100,26 @@ static void test_in_fleet_toml_format() {
TEST_MSG("fleet_config_filename expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcmp(value, expectedValue) == 0);
flb_sds_destroy(value);

cleanup_test_context(t_ctx);
}

static void test_in_fleet_yaml_format() {
struct test_context *t_ctx = init_test_context();
TEST_CHECK(t_ctx != NULL);
value = NULL;

/* Ensure we create YAML files if configured to do so */
t_ctx->ctx->fleet_config_legacy_format = FLB_FALSE;

char expectedValue[CALYPTIA_MAX_DIR_SIZE];
int ret = sprintf(expectedValue, "%s/%s/%s/test.yaml", FLEET_DEFAULT_CONFIG_DIR, t_ctx->ctx->machine_id, t_ctx->ctx->fleet_name);
ret = sprintf(expectedValue, "%s/%s/%s/test.yaml", FLEET_DEFAULT_CONFIG_DIR, t_ctx->ctx->machine_id, t_ctx->ctx->fleet_name);
TEST_CHECK(ret > 0);

flb_sds_t value = fleet_config_filename( t_ctx->ctx, "test" );
value = fleet_config_filename( t_ctx->ctx, "test" );
TEST_CHECK(value != NULL);
TEST_MSG("fleet_config_filename expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcmp(value, expectedValue) == 0);
flb_sds_destroy(value);
value = NULL;

cleanup_test_context(t_ctx);
}

/* Define test list */
TEST_LIST = {
{"in_calyptia_fleet_toml_format", test_in_fleet_toml_format},
{"in_calyptia_fleet_yaml_format", test_in_fleet_yaml_format},
{"in_calyptia_fleet_format", test_in_fleet_format},
{NULL, NULL}
};

0 comments on commit 297795b

Please sign in to comment.