From 377bf126fffd76c68de6937d33a5830447a8dd41 Mon Sep 17 00:00:00 2001 From: ryanohnemus Date: Wed, 3 Jan 2024 07:51:51 -0600 Subject: [PATCH] in_kubernetes_events: add tests for timestamp logic Signed-off-by: ryanohnemus --- tests/runtime/CMakeLists.txt | 1 + .../eventlist_v1_with_creationTimestamp.json | 55 +++ .../eventlist_v1_with_creationTimestamp.out | 1 + .../eventlist_v1_with_lastTimestamp.json | 55 +++ .../eventlist_v1_with_lastTimestamp.out | 1 + tests/runtime/data/in_kubernetes_events/token | 1 + tests/runtime/in_kubernetes_events.c | 349 ++++++++++++++++++ 7 files changed, 463 insertions(+) create mode 100644 tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.json create mode 100644 tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.out create mode 100644 tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.json create mode 100644 tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.out create mode 100644 tests/runtime/data/in_kubernetes_events/token create mode 100644 tests/runtime/in_kubernetes_events.c diff --git a/tests/runtime/CMakeLists.txt b/tests/runtime/CMakeLists.txt index f673a5fbe87..63d0fbebb9a 100644 --- a/tests/runtime/CMakeLists.txt +++ b/tests/runtime/CMakeLists.txt @@ -57,6 +57,7 @@ if(FLB_OUT_LIB) FLB_RT_TEST(FLB_IN_TCP "in_tcp.c") FLB_RT_TEST(FLB_IN_FORWARD "in_forward.c") FLB_RT_TEST(FLB_IN_FLUENTBIT_METRICS "in_fluentbit_metrics.c") + FLB_RT_TEST(FLB_IN_KUBERNETES_EVENTS "in_kubernetes_events.c") endif() # Filter Plugins diff --git a/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.json b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.json new file mode 100644 index 00000000000..4f13524ee57 --- /dev/null +++ b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.json @@ -0,0 +1,55 @@ +{ + "kind": "EventList", + "apiVersion": "v1", + "metadata": { + "resourceVersion": "177157" + }, + "items": [ + { + "metadata": { + "name": "fluent-bit-78945dccd8-2g7qg.17a3c80ba0453aee", + "namespace": "default", + "uid": "6e3013d5-a79b-4dc4-b6c0-6b652302672e", + "resourceVersion": "176761", + "creationTimestamp": "2023-12-24T13:37:16Z", + "managedFields": [ + { + "manager": "kube-scheduler", + "operation": "Update", + "apiVersion": "events.k8s.io/v1", + "time": "2023-12-24T13:37:16Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:action": {}, + "f:eventTime": {}, + "f:note": {}, + "f:reason": {}, + "f:regarding": {}, + "f:reportingController": {}, + "f:reportingInstance": {}, + "f:type": {} + } + } + ] + }, + "involvedObject": { + "kind": "Pod", + "namespace": "default", + "name": "fluent-bit-78945dccd8-2g7qg", + "uid": "ed7de8ff-61fb-40bb-9ecb-55a801a4cd89", + "apiVersion": "v1", + "resourceVersion": "176749" + }, + "reason": "FailedScheduling", + "message": "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..", + "source": {}, + "firstTimestamp": null, + "lastTimestamp": null, + "type": "Warning", + "eventTime": "2023-12-24T13:37:16.335172Z", + "action": "Scheduling", + "reportingComponent": "default-scheduler", + "reportingInstance": "default-scheduler-minikube" + } + ] +} \ No newline at end of file diff --git a/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.out b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.out new file mode 100644 index 00000000000..0ee4783201a --- /dev/null +++ b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_creationTimestamp.out @@ -0,0 +1 @@ +[1703425036.000000,{"metadata":{"name":"fluent-bit-78945dccd8-2g7qg.17a3c80ba0453aee","namespace":"default","uid":"6e3013d5-a79b-4dc4-b6c0-6b652302672e","resourceVersion":"176761","creationTimestamp":"2023-12-24T13:37:16Z","managedFields":[{"manager":"kube-scheduler","operation":"Update","apiVersion":"events.k8s.io/v1","time":"2023-12-24T13:37:16Z","fieldsType":"FieldsV1","fieldsV1":{"f:action":{},"f:eventTime":{},"f:note":{},"f:reason":{},"f:regarding":{},"f:reportingController":{},"f:reportingInstance":{},"f:type":{}}}]},"involvedObject":{"kind":"Pod","namespace":"default","name":"fluent-bit-78945dccd8-2g7qg","uid":"ed7de8ff-61fb-40bb-9ecb-55a801a4cd89","apiVersion":"v1","resourceVersion":"176749"},"reason":"FailedScheduling","message":"0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..","source":{},"firstTimestamp":null,"lastTimestamp":null,"type":"Warning","eventTime":"2023-12-24T13:37:16.335172Z","action":"Scheduling","reportingComponent":"default-scheduler","reportingInstance":"default-scheduler-minikube"}] \ No newline at end of file diff --git a/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.json b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.json new file mode 100644 index 00000000000..1019a13b835 --- /dev/null +++ b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.json @@ -0,0 +1,55 @@ +{ + "kind": "EventList", + "apiVersion": "v1", + "metadata": { + "resourceVersion": "177157" + }, + "items": [ + { + "metadata": { + "name": ".17a3ba8b4aa36c81", + "namespace": "default", + "uid": "ec5546b7-f1b9-4e61-a90c-a1f3b611edbc", + "resourceVersion": "174688", + "creationTimestamp": "2023-12-24T09:30:07Z", + "managedFields": [ + { + "manager": "storage-provisioner", + "operation": "Update", + "apiVersion": "v1", + "time": "2023-12-24T09:30:07Z", + "fieldsType": "FieldsV1", + "fieldsV1": { + "f:count": {}, + "f:firstTimestamp": {}, + "f:involvedObject": {}, + "f:lastTimestamp": {}, + "f:message": {}, + "f:reason": {}, + "f:source": { + "f:component": {} + }, + "f:type": {} + } + } + ] + }, + "involvedObject": { + "kind": "Endpoints", + "apiVersion": "v1" + }, + "reason": "LeaderElection", + "message": "minikube_31f5cdfb-29b0-4f84-9f9c-585088e9235f stopped leading", + "source": { + "component": "k8s.io/minikube-hostpath_minikube_31f5cdfb-29b0-4f84-9f9c-585088e9235f" + }, + "firstTimestamp": "2023-12-24T09:29:51Z", + "lastTimestamp": "2023-12-24T09:29:51Z", + "count": 1, + "type": "Normal", + "eventTime": null, + "reportingComponent": "", + "reportingInstance": "" + } + ] +} \ No newline at end of file diff --git a/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.out b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.out new file mode 100644 index 00000000000..35ff686ed31 --- /dev/null +++ b/tests/runtime/data/in_kubernetes_events/eventlist_v1_with_lastTimestamp.out @@ -0,0 +1 @@ +[1703410191.000000,{"metadata":{"name":".17a3ba8b4aa36c81","namespace":"default","uid":"ec5546b7-f1b9-4e61-a90c-a1f3b611edbc","resourceVersion":"174688","creationTimestamp":"2023-12-24T09:30:07Z","managedFields":[{"manager":"storage-provisioner","operation":"Update","apiVersion":"v1","time":"2023-12-24T09:30:07Z","fieldsType":"FieldsV1","fieldsV1":{"f:count":{},"f:firstTimestamp":{},"f:involvedObject":{},"f:lastTimestamp":{},"f:message":{},"f:reason":{},"f:source":{"f:component":{}},"f:type":{}}}]},"involvedObject":{"kind":"Endpoints","apiVersion":"v1"},"reason":"LeaderElection","message":"minikube_31f5cdfb-29b0-4f84-9f9c-585088e9235f stopped leading","source":{"component":"k8s.io/minikube-hostpath_minikube_31f5cdfb-29b0-4f84-9f9c-585088e9235f"},"firstTimestamp":"2023-12-24T09:29:51Z","lastTimestamp":"2023-12-24T09:29:51Z","count":1,"type":"Normal","eventTime":null,"reportingComponent":"","reportingInstance":""}] \ No newline at end of file diff --git a/tests/runtime/data/in_kubernetes_events/token b/tests/runtime/data/in_kubernetes_events/token new file mode 100644 index 00000000000..88a4c08cdf6 --- /dev/null +++ b/tests/runtime/data/in_kubernetes_events/token @@ -0,0 +1 @@ +fakeTokenFile \ No newline at end of file diff --git a/tests/runtime/in_kubernetes_events.c b/tests/runtime/in_kubernetes_events.c new file mode 100644 index 00000000000..31ee21a68ad --- /dev/null +++ b/tests/runtime/in_kubernetes_events.c @@ -0,0 +1,349 @@ +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ + +/* Fluent Bit + * ========== + * Copyright (C) 2019-2022 The Fluent Bit Authors + * Copyright (C) 2015-2018 Treasure Data Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "flb_tests_runtime.h" + +#define JSON_CONTENT_TYPE "application/json" + +#define KUBE_API_HOST "127.0.0.1" +#define KUBE_API_PORT 8449 + +#define V1_EVENTS "/v1/api/events" +#define IN_KUBERNETES_EVENTS_DATA_PATH FLB_TESTS_DATA_PATH "/data/in_kubernetes_events" +#define KUBE_TOKEN_FILE FLB_TESTS_DATA_PATH "/data/in_kubernetes_events/token" + +struct test_ctx { + flb_ctx_t *flb; /* Fluent Bit library context */ + int i_ffd; /* Input fd */ + int f_ffd; /* Filter fd (unused) */ + int o_ffd; /* Output fd */ + +}; + +struct test_k8s_server_ctx { + mk_ctx_t *ctx; /* Monkey HTTP Context */ + int vid; /* Virtual Host ID */ + int mq_id; /* Message Queue ID */ + struct mk_event_loop *evl; + char json_input_file[1024]; +}; + + +pthread_mutex_t result_mutex = PTHREAD_MUTEX_INITIALIZER; +int num_output = 0; +static int get_output_num() +{ + int ret; + pthread_mutex_lock(&result_mutex); + ret = num_output; + pthread_mutex_unlock(&result_mutex); + + return ret; +} + +static void set_output_num(int num) +{ + pthread_mutex_lock(&result_mutex); + num_output = num; + pthread_mutex_unlock(&result_mutex); +} + +static void clear_output_num() +{ + set_output_num(0); +} + +static flb_sds_t read_file(const char *filename) +{ + int fd = -1; + struct stat sb; + int ret; + flb_sds_t payload = NULL; + + fd = open(filename, O_RDONLY, 0); + if (fd != -1) { + if (fstat(fd, &sb) == 0) { + payload = flb_sds_create_size(sb.st_size+1); + if (!payload) { + flb_errno(); + } + else { + ret = read(fd, payload, sb.st_size); + if (ret != sb.st_size) { + flb_error("Problem reading file: %s", filename); + } + payload[sb.st_size] = '\0'; + } + } + close(fd); + } else { + flb_error("Unable to open test file: %s", filename); + } + + return payload; +} + +/* Callback to check expected results */ +static int cb_check_result_json(void *record, size_t size, void *data) +{ + char *p; + flb_sds_t expected; + char *result; + int num = get_output_num(); + const char *filename; + char full_filename[1024]; + + set_output_num(num+1); + + filename = (const char *) data; + result = (char *) record; + + sprintf(full_filename, "%s/%s.out", IN_KUBERNETES_EVENTS_DATA_PATH, filename); + expected = read_file(full_filename); + + p = strstr(result, expected); + TEST_CHECK(p != NULL); + + if (p == NULL) { + flb_error("Expected to find: '%s' in result '%s'", + expected, result); + } + + flb_free(record); + if (expected) { + flb_sds_destroy(expected); + } + return 0; +} + +static void cb_root(mk_request_t *request, void *data) +{ + flb_sds_t payload; + struct test_k8s_server_ctx *server = data; + payload = read_file(server->json_input_file); + + mk_http_status(request, 200); + mk_http_header(request, "Content-Type", 12, JSON_CONTENT_TYPE, 16); + mk_http_send(request, payload, strlen(payload), NULL); + mk_http_done(request); + + if (payload) { + flb_sds_destroy(payload); + } +} + +struct test_k8s_server_ctx *initialize_mock_k8s_api(const char* filename) +{ + int vid; + char tmp[32]; + struct test_k8s_server_ctx *server; + + server = flb_calloc(1, sizeof(struct test_k8s_server_ctx)); + if (!server) { + flb_errno(); + return NULL; + } + + sprintf(server->json_input_file, "%s/%s.json", + IN_KUBERNETES_EVENTS_DATA_PATH, filename); + + /* Create HTTP server context */ + server->ctx = mk_create(); + if (!server->ctx) { + flb_error("[http_server] could not create context"); + flb_free(server); + return NULL; + } + + /* Compose listen address */ + snprintf(tmp, sizeof(tmp) -1, "%s:%d", KUBE_API_HOST, KUBE_API_PORT); + mk_config_set(server->ctx, "Listen", tmp, NULL); + vid = mk_vhost_create(server->ctx, NULL); + server->vid = vid; + + /* Setup virtual host */ + mk_vhost_set(server->ctx, vid, "Name", "kubernetes-api", NULL); + + /* Root */ + mk_vhost_handler(server->ctx, vid, "/", cb_root, server); + + mk_start(server->ctx); + + return server; +} + +static struct test_ctx *test_ctx_create(struct flb_lib_out_cb *data) +{ + int i_ffd; + int o_ffd; + struct test_ctx *ctx = NULL; + char kube_url[512] = {0}; + + + ctx = flb_calloc(1, sizeof(struct test_ctx)); + if (!TEST_CHECK(ctx != NULL)) { + TEST_MSG("flb_calloc failed"); + flb_errno(); + return NULL; + } + + /* Service config */ + ctx->flb = flb_create(); + flb_service_set(ctx->flb, + "Flush", "0.200000000", + "Grace", "1", + "Log_Level", "debug", + NULL); + + /* Input */ + + i_ffd = flb_input(ctx->flb, (char *) "kubernetes_events", NULL); + TEST_CHECK(i_ffd >= 0); + ctx->i_ffd = i_ffd; + + sprintf(kube_url, "http://%s:%d", KUBE_API_HOST, KUBE_API_PORT); + TEST_CHECK(flb_input_set(ctx->flb, i_ffd, + "kube_url", kube_url, + "kube_token_file", KUBE_TOKEN_FILE, + "kube_retention_time", "365000d", + "tls", "off", + "interval_nsec", "0", + "interval_sec", "1", + NULL) == 0); + + /* Output */ + o_ffd = flb_output(ctx->flb, (char *) "lib", (void *) data); + ctx->o_ffd = o_ffd; + + flb_output_set(ctx->flb, ctx->o_ffd, + "match", "*", + "format", "json", + NULL); + + return ctx; +} + +static void test_ctx_destroy(struct test_ctx *ctx) +{ + TEST_CHECK(ctx != NULL); + + flb_stop(ctx->flb); + flb_destroy(ctx->flb); + flb_free(ctx); + +} + +static void mock_k8s_api_destroy(struct test_k8s_server_ctx* server) +{ + TEST_CHECK(server != NULL); + mk_stop(server->ctx); + mk_destroy(server->ctx); + flb_free(server); +} + +void flb_test_events_v1_with_lastTimestamp() +{ + struct flb_lib_out_cb cb_data; + struct test_ctx *ctx; + int ret; + int num; + const char *filename = "eventlist_v1_with_lastTimestamp"; + + clear_output_num(); + + cb_data.cb = cb_check_result_json; + cb_data.data = (void *)filename; + + ctx = test_ctx_create(&cb_data); + if (!TEST_CHECK(ctx != NULL)) { + TEST_MSG("test_ctx_create failed"); + exit(EXIT_FAILURE); + } + + struct test_k8s_server_ctx* k8s_server = initialize_mock_k8s_api( + filename + ); + + ret = flb_start(ctx->flb); + TEST_CHECK(ret == 0); + + // waiting to flush + flb_time_msleep(1500); + + num = get_output_num(); + if (!TEST_CHECK(num > 0)) { + TEST_MSG("no outputs"); + } + test_ctx_destroy(ctx); + mock_k8s_api_destroy(k8s_server); +} + +void flb_test_events_v1_with_creationTimestamp() +{ + struct flb_lib_out_cb cb_data; + struct test_ctx *ctx; + int ret; + int num; + const char *filename = "eventlist_v1_with_creationTimestamp"; + + clear_output_num(); + + cb_data.cb = cb_check_result_json; + cb_data.data = (void *)filename; + + ctx = test_ctx_create(&cb_data); + if (!TEST_CHECK(ctx != NULL)) { + TEST_MSG("test_ctx_create failed"); + exit(EXIT_FAILURE); + } + + struct test_k8s_server_ctx* k8s_server = initialize_mock_k8s_api( + filename + ); + + ret = flb_start(ctx->flb); + TEST_CHECK(ret == 0); + + // waiting to flush + flb_time_msleep(1500); + + num = get_output_num(); + if (!TEST_CHECK(num > 0)) { + TEST_MSG("no outputs"); + } + test_ctx_destroy(ctx); + mock_k8s_api_destroy(k8s_server); +} + +TEST_LIST = { + {"events_v1_with_lastTimestamp", flb_test_events_v1_with_lastTimestamp}, + {"events_v1_with_creationTimestamp", flb_test_events_v1_with_creationTimestamp}, + {NULL, NULL} +}; +