diff --git a/tests/runtime/data/stackdriver/stackdriver_test_k8s_resource.h b/tests/runtime/data/stackdriver/stackdriver_test_k8s_resource.h index 9fe1604a964..f259c72d0e7 100644 --- a/tests/runtime/data/stackdriver/stackdriver_test_k8s_resource.h +++ b/tests/runtime/data/stackdriver/stackdriver_test_k8s_resource.h @@ -86,4 +86,9 @@ "\"END_KEY\": \"JSON_END\"" \ "}]" +#define K8S_CLUSTER_NO_LOCAL_RESOURCE_ID "[" \ + "1591649196," \ + "{" \ + "\"message\": \"K8S_CLUSTER_NO_LOCAL_RESOURCE_ID\"" \ + "}]" \ No newline at end of file diff --git a/tests/runtime/out_stackdriver.c b/tests/runtime/out_stackdriver.c index 643fc7d468a..585e734478d 100644 --- a/tests/runtime/out_stackdriver.c +++ b/tests/runtime/out_stackdriver.c @@ -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) @@ -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; @@ -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 },