|
| 1 | +#include <assert.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include <fluent-bit/flb_input_plugin.h> |
| 6 | +#include <fluent-bit/flb_log_event_decoder.h> |
| 7 | +#include <fluent-bit/flb_log_event_encoder.h> |
| 8 | + |
| 9 | +#include "flb_tests_runtime.h" |
| 10 | + |
| 11 | +#include "../../plugins/in_ebpf/traces/includes/common/event_context.h" |
| 12 | +#include "../../plugins/in_ebpf/traces/includes/common/events.h" |
| 13 | +#include "../../plugins/in_ebpf/traces/sched/handler.h" |
| 14 | + |
| 15 | +struct test_context { |
| 16 | + struct trace_event_context event_ctx; |
| 17 | + struct flb_input_instance *ins; |
| 18 | + struct flb_log_event_decoder *decoder; |
| 19 | +}; |
| 20 | + |
| 21 | +static struct test_context *init_test_context() |
| 22 | +{ |
| 23 | + struct test_context *ctx; |
| 24 | + |
| 25 | + ctx = flb_calloc(1, sizeof(struct test_context)); |
| 26 | + if (!ctx) { |
| 27 | + return NULL; |
| 28 | + } |
| 29 | + |
| 30 | + ctx->ins = flb_calloc(1, sizeof(struct flb_input_instance)); |
| 31 | + if (!ctx->ins) { |
| 32 | + flb_free(ctx); |
| 33 | + return NULL; |
| 34 | + } |
| 35 | + |
| 36 | + ctx->event_ctx.log_encoder = flb_log_event_encoder_create(FLB_LOG_EVENT_FORMAT_DEFAULT); |
| 37 | + if (!ctx->event_ctx.log_encoder) { |
| 38 | + flb_free(ctx->ins); |
| 39 | + flb_free(ctx); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | + ctx->decoder = flb_log_event_decoder_create(NULL, 0); |
| 44 | + if (!ctx->decoder) { |
| 45 | + flb_log_event_encoder_destroy(ctx->event_ctx.log_encoder); |
| 46 | + flb_free(ctx->ins); |
| 47 | + flb_free(ctx); |
| 48 | + return NULL; |
| 49 | + } |
| 50 | + |
| 51 | + ctx->ins->context = &ctx->event_ctx; |
| 52 | + ctx->event_ctx.ins = ctx->ins; |
| 53 | + |
| 54 | + return ctx; |
| 55 | +} |
| 56 | + |
| 57 | +static void cleanup_test_context(struct test_context *ctx) |
| 58 | +{ |
| 59 | + if (!ctx) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (ctx->decoder) { |
| 64 | + flb_log_event_decoder_destroy(ctx->decoder); |
| 65 | + } |
| 66 | + |
| 67 | + if (ctx->event_ctx.log_encoder) { |
| 68 | + flb_log_event_encoder_destroy(ctx->event_ctx.log_encoder); |
| 69 | + } |
| 70 | + |
| 71 | + if (ctx->ins) { |
| 72 | + flb_free(ctx->ins); |
| 73 | + } |
| 74 | + |
| 75 | + flb_free(ctx); |
| 76 | +} |
| 77 | + |
| 78 | +static int key_matches(msgpack_object key, const char *str) |
| 79 | +{ |
| 80 | + if (key.type != MSGPACK_OBJECT_STR) { |
| 81 | + return 0; |
| 82 | + } |
| 83 | + |
| 84 | + return strncmp(key.via.str.ptr, str, key.via.str.size) == 0 && |
| 85 | + strlen(str) == key.via.str.size; |
| 86 | +} |
| 87 | + |
| 88 | +static void verify_decoded_values(struct flb_log_event *event, |
| 89 | + const struct event *original) |
| 90 | +{ |
| 91 | + msgpack_object_kv *kv; |
| 92 | + int i; |
| 93 | + |
| 94 | + TEST_CHECK(event != NULL); |
| 95 | + TEST_CHECK(event->body->type == MSGPACK_OBJECT_MAP); |
| 96 | + |
| 97 | + for (i = 0; i < event->body->via.map.size; i++) { |
| 98 | + kv = &event->body->via.map.ptr[i]; |
| 99 | + |
| 100 | + if (key_matches(kv->key, "event_type")) { |
| 101 | + TEST_CHECK(kv->val.type == MSGPACK_OBJECT_STR); |
| 102 | + TEST_CHECK(strncmp(kv->val.via.str.ptr, "sched", kv->val.via.str.size) == 0); |
| 103 | + } |
| 104 | + else if (key_matches(kv->key, "cpu")) { |
| 105 | + TEST_CHECK(kv->val.via.u64 == original->details.sched.cpu); |
| 106 | + } |
| 107 | + else if (key_matches(kv->key, "prev_pid")) { |
| 108 | + TEST_CHECK(kv->val.via.u64 == original->details.sched.prev_pid); |
| 109 | + } |
| 110 | + else if (key_matches(kv->key, "next_pid")) { |
| 111 | + TEST_CHECK(kv->val.via.u64 == original->details.sched.next_pid); |
| 112 | + } |
| 113 | + else if (key_matches(kv->key, "runq_latency_ns")) { |
| 114 | + TEST_CHECK(kv->val.via.u64 == original->details.sched.runq_latency_ns); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void test_sched_event_encoding() |
| 120 | +{ |
| 121 | + struct test_context *ctx; |
| 122 | + struct flb_log_event log_event; |
| 123 | + struct event test_event; |
| 124 | + int ret; |
| 125 | + |
| 126 | + ctx = init_test_context(); |
| 127 | + TEST_CHECK(ctx != NULL); |
| 128 | + |
| 129 | + memset(&test_event, 0, sizeof(struct event)); |
| 130 | + test_event.type = EVENT_TYPE_SCHED; |
| 131 | + test_event.common.pid = 4321; |
| 132 | + test_event.common.tid = 4321; |
| 133 | + memcpy(test_event.common.comm, "sched_test", strlen("sched_test")); |
| 134 | + |
| 135 | + test_event.details.sched.prev_pid = 1111; |
| 136 | + test_event.details.sched.prev_prio = 120; |
| 137 | + test_event.details.sched.prev_state = 1; |
| 138 | + test_event.details.sched.next_pid = 4321; |
| 139 | + test_event.details.sched.next_prio = 90; |
| 140 | + test_event.details.sched.cpu = 3; |
| 141 | + test_event.details.sched.runq_latency_ns = 125000; |
| 142 | + test_event.details.sched.wakeup_tracked = 1; |
| 143 | + |
| 144 | + ret = encode_sched_event(ctx->event_ctx.log_encoder, &test_event); |
| 145 | + TEST_CHECK(ret == 0); |
| 146 | + TEST_CHECK(ctx->event_ctx.log_encoder->output_length > 0); |
| 147 | + |
| 148 | + ret = flb_log_event_decoder_init(ctx->decoder, |
| 149 | + ctx->event_ctx.log_encoder->output_buffer, |
| 150 | + ctx->event_ctx.log_encoder->output_length); |
| 151 | + TEST_CHECK(ret == 0); |
| 152 | + |
| 153 | + ret = flb_log_event_decoder_next(ctx->decoder, &log_event); |
| 154 | + TEST_CHECK(ret == FLB_EVENT_DECODER_SUCCESS); |
| 155 | + |
| 156 | + verify_decoded_values(&log_event, &test_event); |
| 157 | + |
| 158 | + cleanup_test_context(ctx); |
| 159 | +} |
| 160 | + |
| 161 | +TEST_LIST = { |
| 162 | + {"sched_event_encoding", test_sched_event_encoding}, |
| 163 | + {NULL, NULL} |
| 164 | +}; |
0 commit comments