Skip to content

Commit

Permalink
out_stackdriver: add k8s_cluster resource test
Browse files Browse the repository at this point in the history
Signed-off-by: ryanohnemus <[email protected]>
  • Loading branch information
ryanohnemus committed Jan 22, 2024
1 parent 34bc58e commit a1cf43e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@
"\"END_KEY\": \"JSON_END\"" \
"}]"

#define K8S_CLUSTER_NO_LOCAL_RESOURCE_ID "[" \
"1591649196," \
"{" \
"\"message\": \"K8S_CLUSTER_NO_LOCAL_RESOURCE_ID\"" \
"}]"

73 changes: 73 additions & 0 deletions tests/runtime/out_stackdriver.c
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,34 @@ static void cb_check_log_name_no_override(void *ctx, int ffd,
flb_sds_destroy(res_data);
}

static void cb_check_k8s_cluster_resource(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
{
int ret;

/* resource type */
ret = mp_kv_cmp(res_data, res_size, "$resource['type']", "k8s_cluster");
TEST_CHECK(ret == FLB_TRUE);

/* project id */
ret = mp_kv_cmp(res_data, res_size,
"$resource['labels']['project_id']", "fluent-bit");
TEST_CHECK(ret == FLB_TRUE);

/* location */
ret = mp_kv_cmp(res_data, res_size,
"$resource['labels']['location']", "test_cluster_location");
TEST_CHECK(ret == FLB_TRUE);

/* cluster name */
ret = mp_kv_cmp(res_data, res_size,
"$resource['labels']['cluster_name']", "test_cluster_name");
TEST_CHECK(ret == FLB_TRUE);

flb_sds_destroy(res_data);
}

static void cb_check_project_key_override(void *ctx, int ffd,
int res_ret, void *res_data, size_t res_size,
void *data)
Expand Down Expand Up @@ -3777,6 +3805,50 @@ void flb_test_resource_k8s_container_custom_k8s_regex_custom_prefix()
flb_destroy(ctx);
}

void flb_test_resource_k8s_cluster_no_local_resource_id()
{
int ret;
int size = sizeof(K8S_CLUSTER_NO_LOCAL_RESOURCE_ID) - 1;
flb_ctx_t *ctx;
int in_ffd;
int out_ffd;

/* Create context, flush every second (some checks omitted here) */
ctx = flb_create();
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);

/* Lib input mode */
in_ffd = flb_input(ctx, (char *) "lib", NULL);
flb_input_set(ctx, in_ffd, "tag", "test", NULL);

/* Stackdriver output */
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
flb_output_set(ctx, out_ffd,
"match", "test",
"resource", "k8s_cluster",
"google_service_credentials", SERVICE_CREDENTIALS,
"k8s_cluster_name", "test_cluster_name",
"k8s_cluster_location", "test_cluster_location",
NULL);

/* Enable test mode */
ret = flb_output_set_test(ctx, out_ffd, "formatter",
cb_check_k8s_cluster_resource,
NULL, NULL);

/* Start */
ret = flb_start(ctx);
TEST_CHECK(ret == 0);

/* Ingest data sample */
flb_lib_push(ctx, in_ffd, (char *) K8S_CLUSTER_NO_LOCAL_RESOURCE_ID, size);

sleep(2);
flb_stop(ctx);
flb_destroy(ctx);
}


void flb_test_resource_k8s_node_common()
{
int ret;
Expand Down Expand Up @@ -6293,6 +6365,7 @@ TEST_LIST = {
{"resource_k8s_container_default_tag_regex", flb_test_resource_k8s_container_default_tag_regex },
{"resource_k8s_container_custom_k8s_regex", flb_test_resource_k8s_container_custom_k8s_regex },
{"resource_k8s_container_custom_k8s_regex_custom_prefix", flb_test_resource_k8s_container_custom_k8s_regex_custom_prefix },
{"resource_k8s_cluster_no_local_resource_id", flb_test_resource_k8s_cluster_no_local_resource_id },
{"resource_k8s_node_common", flb_test_resource_k8s_node_common },
{"resource_k8s_node_no_local_resource_id", flb_test_resource_k8s_node_no_local_resource_id },
{"resource_k8s_node_custom_k8s_regex_with_dot", flb_test_resource_k8s_node_custom_k8s_regex_with_dot },
Expand Down

0 comments on commit a1cf43e

Please sign in to comment.