Skip to content

Commit

Permalink
tests: runtime: in_calyptia_fleet: add placeholders
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 5ef6296 commit 5d51ec8
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ init/fluent-bit.service
lib/chunkio/include/chunkio/cio_version.h
lib/monkey/monkey.service
lib/monkey/include/monkey/mk_core/mk_core_info.h
lib/cprofiles/include/cprofiles/cprof_info.h

packaging/.env
packaging/packages
Expand Down
11 changes: 8 additions & 3 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ static flb_sds_t generate_base_fleet_directory(struct flb_in_calyptia_fleet_conf
}
}

/* Ensure we have a valid value */
if (ctx->config_dir == NULL) {
ctx->config_dir = FLEET_DEFAULT_CONFIG_DIR;
}

if (ctx->fleet_name != NULL) {
return flb_sds_printf(fleet_dir, "%s" PATH_SEPARATOR "%s" PATH_SEPARATOR "%s",
ctx->config_dir, ctx->machine_id, ctx->fleet_name);
Expand All @@ -188,7 +193,7 @@ static flb_sds_t generate_base_fleet_directory(struct flb_in_calyptia_fleet_conf
ctx->config_dir, ctx->machine_id, ctx->fleet_id);
}

static flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname)
flb_sds_t fleet_config_filename(struct flb_in_calyptia_fleet_config *ctx, char *fname)
{
flb_sds_t cfgname = NULL;
flb_sds_t ret;
Expand Down Expand Up @@ -1921,7 +1926,7 @@ static int fleet_mkdir(struct flb_in_calyptia_fleet_config *ctx, time_t timestam
return ret;
}

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

static int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
int get_calyptia_files(struct flb_in_calyptia_fleet_config *ctx,
const char *url,
time_t timestamp)
{
Expand Down
6 changes: 3 additions & 3 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ struct reload_ctx {
flb_sds_t cfg_path;
};

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

#define new_fleet_config_filename(a) fleet_config_filename((a), "new")
#define cur_fleet_config_filename(a) fleet_config_filename((a), "cur")
#define old_fleet_config_filename(a) fleet_config_filename((a), "old")
#define hdr_fleet_config_filename(a) fleet_config_filename((a), "header")

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

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

#endif /* FLB_IN_CALYPTIA_FLEET_H */
4 changes: 3 additions & 1 deletion tests/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ if (FLB_CUSTOM_CALYPTIA)
"custom_calyptia_test.c"
"custom_calyptia_registration_retry_test.c"
"custom_calyptia_input_test.c"
"in_calyptia_fleet_test.c"
)

foreach(TEST_SOURCE ${CALYPTIA_TESTS})
Expand All @@ -82,6 +83,7 @@ if (FLB_CUSTOM_CALYPTIA)
add_executable(${TEST_TARGET}
${TEST_SOURCE}
"../../plugins/custom_calyptia/calyptia.c"
"../../plugins/in_calyptia_fleet/in_calyptia_fleet.c"
)

target_link_libraries(${TEST_TARGET}
Expand Down Expand Up @@ -113,7 +115,7 @@ if(FLB_IN_EBPF)
# Function to create eBPF handler tests
function(add_ebpf_handler_test name source_file handler_file)
set(TEST_TARGET "flb-rt-in_ebpf_${name}_handler")

# Add executable
add_executable(${TEST_TARGET}
${source_file}
Expand Down
35 changes: 0 additions & 35 deletions tests/runtime/custom_calyptia_input_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,44 +277,9 @@ static void test_calyptia_machine_id_generation() {
cleanup_test_context(t_ctx);
}

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

int ret = set_fleet_input_properties(t_ctx->ctx, t_ctx->fleet);
TEST_CHECK(ret == 0);

/* Verify properties were set correctly */
const char *value;
char *expectedValue = flb_strdup("on");

/* Default is true */
value = flb_input_get_property("fleet_config_legacy_format", t_ctx->fleet);
TEST_CHECK(value != NULL);
TEST_MSG("fleet_config_legacy_format default expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcasecmp(value, expectedValue) == 0);

/* Check for change to disabling */
t_ctx->ctx->fleet_config_legacy_format = FLB_FALSE;
ret = set_fleet_input_properties(t_ctx->ctx, t_ctx->fleet);
TEST_CHECK(ret == 0);

flb_free(expectedValue);
expectedValue = flb_strdup("off");

value = flb_input_get_property("fleet_config_legacy_format", t_ctx->fleet);
TEST_CHECK(value != NULL);
TEST_MSG("fleet_config_legacy_format changed expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcasecmp(value, expectedValue) == 0);

flb_free(expectedValue);
cleanup_test_context(t_ctx);
}

/* Define test list */
TEST_LIST = {
{"set_fleet_input_properties", test_set_fleet_input_properties},
{"machine_id_generation", test_calyptia_machine_id_generation},
{"set_fleet_config_format", test_calyptia_config_format},
{NULL, NULL}
};
130 changes: 130 additions & 0 deletions tests/runtime/in_calyptia_fleet_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#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);

/* Test context structure */
struct test_context {
struct flb_in_calyptia_fleet_config *ctx;
struct flb_config *config;
};

/* Initialize test context */
static struct test_context *init_test_context()
{
struct test_context *t_ctx = flb_calloc(1, sizeof(struct test_context));
if (!t_ctx) {
return NULL;
}

t_ctx->config = flb_config_init();
if (!t_ctx->config) {
flb_free(t_ctx);
return NULL;
}

t_ctx->ctx = flb_calloc(1, sizeof(struct flb_in_calyptia_fleet_config));
if (!t_ctx->ctx) {
flb_config_exit(t_ctx->config);
flb_free(t_ctx);
return NULL;
}

/* Initialize plugin instance for logging */
t_ctx->ctx->ins = flb_calloc(1, sizeof(struct flb_input_instance));
if (!t_ctx->ctx->ins) {
flb_free(t_ctx->ctx);
flb_config_exit(t_ctx->config);
flb_free(t_ctx);
return NULL;
}

/* Initialize test values in ctx */
t_ctx->ctx->api_key = flb_strdup("test_api_key");
t_ctx->ctx->fleet_id = flb_strdup("test_fleet_id");

t_ctx->ctx->fleet_name = flb_strdup("test_fleet");
t_ctx->ctx->machine_id = flb_strdup("test_machine_id");

t_ctx->ctx->fleet_config_legacy_format = FLB_TRUE;

return t_ctx;
}

static void cleanup_test_context(struct test_context *t_ctx)
{
if (!t_ctx) {
return;
}

if (t_ctx->ctx) {
if (t_ctx->ctx->api_key) flb_free(t_ctx->ctx->api_key);
if (t_ctx->ctx->fleet_id) flb_free(t_ctx->ctx->fleet_id);

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->ins) flb_free(t_ctx->ctx->ins);
flb_free(t_ctx->ctx);
}

if (t_ctx->config) {
/* Destroy the config which will cleanup any remaining instances */
flb_config_exit(t_ctx->config);
}

flb_free(t_ctx);
}

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

/* Ensure we create TOML files by default */
char expectedValue[CALYPTIA_MAX_DIR_SIZE];
int ret = sprintf(expectedValue, "%s/%s/%s/test.conf", 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" );
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);

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);

/* 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);
TEST_CHECK(ret > 0);

flb_sds_t 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);

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},
{NULL, NULL}
};

0 comments on commit 5d51ec8

Please sign in to comment.