Skip to content

Commit

Permalink
tests: add calyptia machine id generation tests
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 5, 2024
1 parent 89c2ad0 commit 594767e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/custom_calyptia/calyptia.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct calyptia {

int set_fleet_input_properties(struct calyptia *ctx, struct flb_input_instance *fleet);
static flb_sds_t agent_config_filename(struct calyptia *ctx, char *fname);
static flb_sds_t get_machine_id(struct calyptia *ctx);

/* These are unique to the agent rather than the fleet */
#define machine_id_fleet_config_filename(a) agent_config_filename((a), "machine-id")
Expand Down
89 changes: 89 additions & 0 deletions tests/runtime/custom_calyptia_input_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,97 @@ void test_set_fleet_input_properties()
cleanup_test_context(t_ctx);
}

static struct test_context * update_config_dir(struct test_context * t_ctx, const char* new_config_dir) {
if (!t_ctx || !new_config_dir) {
return NULL;
}

if (t_ctx->ctx) {
if (t_ctx->ctx->fleet_config_dir) {
flb_free(t_ctx->ctx->fleet_config_dir);
}
t_ctx->ctx->fleet_config_dir = flb_strdup(new_config_dir);
return t_ctx;
}

cleanup_test_context(t_ctx);
return NULL;
}

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

/* Set config directory to default */
t_ctx = update_config_dir(FLEET_DEFAULT_CONFIG_DIR)
TEST_CHECK(t_ctx != NULL);

/* Test setting properties */
int ret = set_fleet_input_properties(t_ctx->ctx, t_ctx->fleet);
TEST_CHECK(ret == 0);

/* Verify properties were set correctly */
const char *value;

/* Check config_dir */
value = flb_input_get_property("config_dir", t_ctx->fleet);
TEST_CHECK(value != NULL);
TEST_MSG("config_dir expected=%s got=%s", FLEET_DEFAULT_CONFIG_DIR, value);
TEST_CHECK(value && strcmp(value, FLEET_DEFAULT_CONFIG_DIR) == 0);

/**
* Initial generation should create a new UUID
* Subsequent generation should reuse the previous one
* Repeat with custom directory to confirm that works too
*/
value = machine_id_fleet_config_filename(t_ctx->ctx);
const char* expectedValue = flb_sds_printf(expectedValue, "%s/machine-id.conf", FLEET_DEFAULT_CONFIG_DIR);
TEST_CHECK(value != NULL);
TEST_CHECK(expectedValue != NULL);
TEST_MSG("machine_id filename expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcmp(value, expectedValue) == 0);

/* generate a new machine ID and verify it is not null then store for later use */
const char* machine_id = get_machine_id(t_ctx->ctx);
TEST_CHECK(machine_id != NULL);

/* repeat to confirm existing UUID is maintained */
value = get_machine_id(t_ctx->ctx);
TEST_CHECK(value != NULL);
TEST_MSG("machine_id changed, expected=%s got=%s", machine_id, value);
TEST_CHECK(value && strcmp(value, machine_id) == 0);

/* repeat with new config directory */
t_ctx = update_config_dir("/test/config/fleet")
TEST_CHECK(t_ctx != NULL);

/* check we use the new directory for the filename */
value = machine_id_fleet_config_filename(t_ctx->ctx);
expectedValue = flb_sds_printf(expectedValue, "/test/config/fleet/machine-id.conf");
TEST_CHECK(value != NULL);
TEST_CHECK(expectedValue != NULL);
TEST_MSG("machine_id filename expected=%s got=%s", expectedValue, value);
TEST_CHECK(value && strcmp(value, expectedValue) == 0);

/* check we generate a new value */
value = get_machine_id(t_ctx->ctx);
TEST_CHECK(value != NULL);
TEST_MSG("machine_id did not change, expected!=%s got=%s", machine_id, value);
TEST_CHECK(value && strcmp(value, machine_id) != 0);

machine_id = value;
/* repeat to confirm existing UUID is maintained */
value = get_machine_id(t_ctx->ctx);
TEST_CHECK(value != NULL);
TEST_MSG("machine_id changed, expected=%s got=%s", machine_id, value);
TEST_CHECK(value && strcmp(value, machine_id) == 0);

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

0 comments on commit 594767e

Please sign in to comment.