Skip to content

Commit 7f663f2

Browse files
committed
test: runtime: out_stackdriver: add unit tests for k8s resource types
Signed-off-by: Jeff Luo <[email protected]>
1 parent 3810b7c commit 7f663f2

File tree

2 files changed

+301
-0
lines changed

2 files changed

+301
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2+
3+
/* k8s_container */
4+
#define K8S_CONTAINER_COMMON "[" \
5+
"1591649196," \
6+
"{" \
7+
"\"message\": \"K8S_CONTAINER_COMMON\"," \
8+
"\"logging.googleapis.com/local_resource_id\": \"k8s_container.testnamespace.testpod.testctr\"" \
9+
"}]"
10+
11+
/* k8s_node */
12+
#define K8S_NODE_COMMON "[" \
13+
"1591649196," \
14+
"{" \
15+
"\"message\": \"K8S_NODE_COMMON\"," \
16+
"\"logging.googleapis.com/local_resource_id\": \"k8s_node.testnode\"," \
17+
"\"PRIORITY\": 6," \
18+
"\"SYSLOG_FACILITY\": 3," \
19+
"\"_CAP_EFFECTIVE\": \"3fffffffff\"," \
20+
"\"_PID\": 1387," \
21+
"\"_SYSTEMD_UNIT\": \"docker.service\"," \
22+
"\"END_KEY\": \"JSON_END\"" \
23+
"}]"
24+
25+
/* k8s_pod */
26+
#define K8S_POD_COMMON "[" \
27+
"1591649196," \
28+
"{" \
29+
"\"message\": \"K8S_POD_COMMON\"," \
30+
"\"logging.googleapis.com/local_resource_id\": \"k8s_pod.testnamespace.testpod\"," \
31+
"\"key_0\": false," \
32+
"\"key_1\": true," \
33+
"\"key_2\": \"some string\"," \
34+
"\"key_3\": 0.12345678," \
35+
"\"key_4\": 5000," \
36+
"\"END_KEY\": \"JSON_END\"" \
37+
"}]"
38+

tests/runtime/out_stackdriver.c

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/* JSON payload example */
3232
#include "data/stackdriver/json.h"
3333
#include "data/stackdriver/stackdriver_test_operation.h"
34+
#include "data/stackdriver/stackdriver_test_k8s_resource.h"
3435

3536
/*
3637
* Fluent Bit Stackdriver plugin, always set as payload a JSON strings contained in a
@@ -289,6 +290,7 @@ static void cb_check_global_resource(void *ctx, int ffd,
289290

290291
ret = mp_kv_cmp(res_data, res_size, "$resource['type']", "global");
291292
TEST_CHECK(ret == FLB_TRUE);
293+
292294
flb_sds_destroy(res_data);
293295
}
294296

@@ -320,6 +322,135 @@ static void cb_check_gce_instance(void *ctx, int ffd,
320322
flb_sds_destroy(res_data);
321323
}
322324

325+
static void cb_check_k8s_container_resource(void *ctx, int ffd,
326+
int res_ret, void *res_data, size_t res_size,
327+
void *data)
328+
{
329+
int ret;
330+
331+
/* resource type */
332+
ret = mp_kv_cmp(res_data, res_size, "$resource['type']", "k8s_container");
333+
TEST_CHECK(ret == FLB_TRUE);
334+
335+
/* project id */
336+
ret = mp_kv_cmp(res_data, res_size,
337+
"$resource['labels']['project_id']", "fluent-bit");
338+
TEST_CHECK(ret == FLB_TRUE);
339+
340+
/* location */
341+
ret = mp_kv_cmp(res_data, res_size,
342+
"$resource['labels']['location']", "test_cluster_location");
343+
TEST_CHECK(ret == FLB_TRUE);
344+
345+
/* cluster name */
346+
ret = mp_kv_cmp(res_data, res_size,
347+
"$resource['labels']['cluster_name']", "test_cluster_name");
348+
TEST_CHECK(ret == FLB_TRUE);
349+
350+
/* namespace name */
351+
ret = mp_kv_cmp(res_data, res_size,
352+
"$resource['labels']['namespace_name']", "testnamespace");
353+
TEST_CHECK(ret == FLB_TRUE);
354+
355+
/* pod name */
356+
ret = mp_kv_cmp(res_data, res_size,
357+
"$resource['labels']['pod_name']", "testpod");
358+
TEST_CHECK(ret == FLB_TRUE);
359+
360+
/* container name */
361+
ret = mp_kv_cmp(res_data, res_size,
362+
"$resource['labels']['container_name']", "testctr");
363+
TEST_CHECK(ret == FLB_TRUE);
364+
365+
/* check `local_resource_id` has been removed from jsonPayload */
366+
ret = mp_kv_exists(res_data, res_size,
367+
"$entries[0]['jsonPayload']['logging.googleapis.com/local_resource_id']");
368+
TEST_CHECK(ret == FLB_FALSE);
369+
370+
flb_sds_destroy(res_data);
371+
}
372+
373+
static void cb_check_k8s_node_resource(void *ctx, int ffd,
374+
int res_ret, void *res_data, size_t res_size,
375+
void *data)
376+
{
377+
int ret;
378+
379+
/* resource type */
380+
ret = mp_kv_cmp(res_data, res_size, "$resource['type']", "k8s_node");
381+
TEST_CHECK(ret == FLB_TRUE);
382+
383+
/* project id */
384+
ret = mp_kv_cmp(res_data, res_size,
385+
"$resource['labels']['project_id']", "fluent-bit");
386+
TEST_CHECK(ret == FLB_TRUE);
387+
388+
/* location */
389+
ret = mp_kv_cmp(res_data, res_size,
390+
"$resource['labels']['location']", "test_cluster_location");
391+
TEST_CHECK(ret == FLB_TRUE);
392+
393+
/* cluster name */
394+
ret = mp_kv_cmp(res_data, res_size,
395+
"$resource['labels']['cluster_name']", "test_cluster_name");
396+
TEST_CHECK(ret == FLB_TRUE);
397+
398+
/* node name */
399+
ret = mp_kv_cmp(res_data, res_size,
400+
"$resource['labels']['node_name']", "testnode");
401+
TEST_CHECK(ret == FLB_TRUE);
402+
403+
/* check `local_resource_id` has been removed from jsonPayload */
404+
ret = mp_kv_exists(res_data, res_size,
405+
"$entries[0]['jsonPayload']['logging.googleapis.com/local_resource_id']");
406+
TEST_CHECK(ret == FLB_FALSE);
407+
408+
flb_sds_destroy(res_data);
409+
}
410+
411+
static void cb_check_k8s_pod_resource(void *ctx, int ffd,
412+
int res_ret, void *res_data, size_t res_size,
413+
void *data)
414+
{
415+
int ret;
416+
417+
/* resource type */
418+
ret = mp_kv_cmp(res_data, res_size, "$resource['type']", "k8s_pod");
419+
TEST_CHECK(ret == FLB_TRUE);
420+
421+
/* project id */
422+
ret = mp_kv_cmp(res_data, res_size,
423+
"$resource['labels']['project_id']", "fluent-bit");
424+
TEST_CHECK(ret == FLB_TRUE);
425+
426+
/* location */
427+
ret = mp_kv_cmp(res_data, res_size,
428+
"$resource['labels']['location']", "test_cluster_location");
429+
TEST_CHECK(ret == FLB_TRUE);
430+
431+
/* cluster name */
432+
ret = mp_kv_cmp(res_data, res_size,
433+
"$resource['labels']['cluster_name']", "test_cluster_name");
434+
TEST_CHECK(ret == FLB_TRUE);
435+
436+
/* namespace name */
437+
ret = mp_kv_cmp(res_data, res_size,
438+
"$resource['labels']['namespace_name']", "testnamespace");
439+
TEST_CHECK(ret == FLB_TRUE);
440+
441+
/* pod name */
442+
ret = mp_kv_cmp(res_data, res_size,
443+
"$resource['labels']['pod_name']", "testpod");
444+
TEST_CHECK(ret == FLB_TRUE);
445+
446+
/* check `local_resource_id` has been removed from jsonPayload */
447+
ret = mp_kv_exists(res_data, res_size,
448+
"$entries[0]['jsonPayload']['logging.googleapis.com/local_resource_id']");
449+
TEST_CHECK(ret == FLB_FALSE);
450+
451+
flb_sds_destroy(res_data);
452+
}
453+
323454
static void cb_check_operation_common_case(void *ctx, int ffd,
324455
int res_ret, void *res_data, size_t res_size,
325456
void *data)
@@ -809,6 +940,135 @@ void flb_test_operation_extra_subfields()
809940
flb_destroy(ctx);
810941
}
811942

943+
void flb_test_resource_k8s_container_common()
944+
{
945+
int ret;
946+
int size = sizeof(K8S_CONTAINER_COMMON) - 1;
947+
flb_ctx_t *ctx;
948+
int in_ffd;
949+
int out_ffd;
950+
951+
/* Create context, flush every second (some checks omitted here) */
952+
ctx = flb_create();
953+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
954+
955+
/* Lib input mode */
956+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
957+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
958+
959+
/* Stackdriver output */
960+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
961+
flb_output_set(ctx, out_ffd,
962+
"match", "test",
963+
"resource", "k8s_container",
964+
"google_service_credentials", SERVICE_CREDENTIALS,
965+
"k8s_cluster_name", "test_cluster_name",
966+
"k8s_cluster_location", "test_cluster_location",
967+
NULL);
968+
969+
/* Enable test mode */
970+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
971+
cb_check_k8s_container_resource,
972+
NULL);
973+
974+
/* Start */
975+
ret = flb_start(ctx);
976+
TEST_CHECK(ret == 0);
977+
978+
/* Ingest data sample */
979+
flb_lib_push(ctx, in_ffd, (char *) K8S_CONTAINER_COMMON, size);
980+
981+
sleep(2);
982+
flb_stop(ctx);
983+
flb_destroy(ctx);
984+
}
985+
986+
void flb_test_resource_k8s_node_common()
987+
{
988+
int ret;
989+
int size = sizeof(K8S_NODE_COMMON) - 1;
990+
flb_ctx_t *ctx;
991+
int in_ffd;
992+
int out_ffd;
993+
994+
/* Create context, flush every second (some checks omitted here) */
995+
ctx = flb_create();
996+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
997+
998+
/* Lib input mode */
999+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
1000+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
1001+
1002+
/* Stackdriver output */
1003+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
1004+
flb_output_set(ctx, out_ffd,
1005+
"match", "test",
1006+
"resource", "k8s_node",
1007+
"google_service_credentials", SERVICE_CREDENTIALS,
1008+
"k8s_cluster_name", "test_cluster_name",
1009+
"k8s_cluster_location", "test_cluster_location",
1010+
NULL);
1011+
1012+
/* Enable test mode */
1013+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1014+
cb_check_k8s_node_resource,
1015+
NULL);
1016+
1017+
/* Start */
1018+
ret = flb_start(ctx);
1019+
TEST_CHECK(ret == 0);
1020+
1021+
/* Ingest data sample */
1022+
flb_lib_push(ctx, in_ffd, (char *) K8S_NODE_COMMON, size);
1023+
1024+
sleep(2);
1025+
flb_stop(ctx);
1026+
flb_destroy(ctx);
1027+
}
1028+
1029+
void flb_test_resource_k8s_pod_common()
1030+
{
1031+
int ret;
1032+
int size = sizeof(K8S_POD_COMMON) - 1;
1033+
flb_ctx_t *ctx;
1034+
int in_ffd;
1035+
int out_ffd;
1036+
1037+
/* Create context, flush every second (some checks omitted here) */
1038+
ctx = flb_create();
1039+
flb_service_set(ctx, "flush", "1", "grace", "1", NULL);
1040+
1041+
/* Lib input mode */
1042+
in_ffd = flb_input(ctx, (char *) "lib", NULL);
1043+
flb_input_set(ctx, in_ffd, "tag", "test", NULL);
1044+
1045+
/* Stackdriver output */
1046+
out_ffd = flb_output(ctx, (char *) "stackdriver", NULL);
1047+
flb_output_set(ctx, out_ffd,
1048+
"match", "test",
1049+
"resource", "k8s_pod",
1050+
"google_service_credentials", SERVICE_CREDENTIALS,
1051+
"k8s_cluster_name", "test_cluster_name",
1052+
"k8s_cluster_location", "test_cluster_location",
1053+
NULL);
1054+
1055+
/* Enable test mode */
1056+
ret = flb_output_set_test(ctx, out_ffd, "formatter",
1057+
cb_check_k8s_pod_resource,
1058+
NULL);
1059+
1060+
/* Start */
1061+
ret = flb_start(ctx);
1062+
TEST_CHECK(ret == 0);
1063+
1064+
/* Ingest data sample */
1065+
flb_lib_push(ctx, in_ffd, (char *) K8S_POD_COMMON, size);
1066+
1067+
sleep(2);
1068+
flb_stop(ctx);
1069+
flb_destroy(ctx);
1070+
}
1071+
8121072
/* Test list */
8131073
TEST_LIST = {
8141074
{"resource_global", flb_test_resource_global },
@@ -819,5 +1079,8 @@ TEST_LIST = {
8191079
{"operation_partial_subfields", flb_test_operation_partial_subfields},
8201080
{"operation_subfields_in_incorrect_type", flb_test_operation_incorrect_type_subfields},
8211081
{"operation_extra_subfields_exist", flb_test_operation_extra_subfields},
1082+
{"resource_k8s_container_common", flb_test_resource_k8s_container_common },
1083+
{"resource_k8s_node_common", flb_test_resource_k8s_node_common },
1084+
{"resource_k8s_pod_common", flb_test_resource_k8s_pod_common },
8221085
{NULL, NULL}
8231086
};

0 commit comments

Comments
 (0)