|
| 1 | +#include <fluent-bit/flb_input.h> |
| 2 | +#include <fluent-bit/flb_input_chunk.h> |
| 3 | +#include <fluent-bit/flb_mem.h> |
| 4 | +#include <chunkio/chunkio.h> |
| 5 | +#include <chunkio/cio_utils.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +#include "flb_tests_internal.h" |
| 9 | + |
| 10 | +#define TEST_STREAM_PATH "/tmp/flb-chunk-direct-test" |
| 11 | + |
| 12 | +static int write_legacy_chunk_metadata(struct cio_chunk *chunk, |
| 13 | + int event_type, |
| 14 | + const char *tag, |
| 15 | + int tag_len) |
| 16 | +{ |
| 17 | + int ret; |
| 18 | + int meta_size; |
| 19 | + char *meta; |
| 20 | + |
| 21 | + meta_size = FLB_INPUT_CHUNK_META_HEADER + tag_len; |
| 22 | + meta = flb_malloc(meta_size); |
| 23 | + if (!meta) { |
| 24 | + flb_errno(); |
| 25 | + return -1; |
| 26 | + } |
| 27 | + |
| 28 | + meta[0] = FLB_INPUT_CHUNK_MAGIC_BYTE_0; |
| 29 | + meta[1] = FLB_INPUT_CHUNK_MAGIC_BYTE_1; |
| 30 | + |
| 31 | + if (event_type == FLB_INPUT_LOGS) { |
| 32 | + meta[2] = FLB_INPUT_CHUNK_TYPE_LOGS; |
| 33 | + } |
| 34 | + else if (event_type == FLB_INPUT_METRICS) { |
| 35 | + meta[2] = FLB_INPUT_CHUNK_TYPE_METRICS; |
| 36 | + } |
| 37 | + else if (event_type == FLB_INPUT_TRACES) { |
| 38 | + meta[2] = FLB_INPUT_CHUNK_TYPE_TRACES; |
| 39 | + } |
| 40 | + else if (event_type == FLB_INPUT_PROFILES) { |
| 41 | + meta[2] = FLB_INPUT_CHUNK_TYPE_PROFILES; |
| 42 | + } |
| 43 | + else { |
| 44 | + meta[2] = FLB_INPUT_CHUNK_TYPE_LOGS; |
| 45 | + } |
| 46 | + |
| 47 | + meta[3] = 0; |
| 48 | + |
| 49 | + memcpy(meta + FLB_INPUT_CHUNK_META_HEADER, tag, tag_len); |
| 50 | + |
| 51 | + ret = cio_meta_write(chunk, meta, meta_size); |
| 52 | + |
| 53 | + flb_free(meta); |
| 54 | + |
| 55 | + return ret; |
| 56 | +} |
| 57 | + |
| 58 | +static void test_chunk_metadata_direct_routes() |
| 59 | +{ |
| 60 | + struct cio_options opts; |
| 61 | + struct cio_ctx *ctx; |
| 62 | + struct cio_stream *stream; |
| 63 | + struct cio_chunk *chunk; |
| 64 | + struct flb_input_chunk ic; |
| 65 | + struct flb_chunk_direct_route output_routes[2]; |
| 66 | + struct flb_chunk_direct_route *loaded_routes; |
| 67 | + char *content_buf; |
| 68 | + const char *tag_buf; |
| 69 | + const char *tag_string; |
| 70 | + const char payload[] = "direct route payload validation string"; |
| 71 | + int tag_len; |
| 72 | + int route_count; |
| 73 | + int ret; |
| 74 | + int err; |
| 75 | + int expected_tag_len; |
| 76 | + size_t content_size; |
| 77 | + size_t payload_size; |
| 78 | + |
| 79 | + payload_size = sizeof(payload) - 1; |
| 80 | + tag_string = "test.tag"; |
| 81 | + expected_tag_len = strlen(tag_string); |
| 82 | + |
| 83 | + cio_utils_recursive_delete(TEST_STREAM_PATH); |
| 84 | + memset(&opts, 0, sizeof(opts)); |
| 85 | + cio_options_init(&opts); |
| 86 | + opts.root_path = TEST_STREAM_PATH; |
| 87 | + opts.flags = CIO_OPEN; |
| 88 | + ctx = cio_create(&opts); |
| 89 | + TEST_CHECK(ctx != NULL); |
| 90 | + if (!ctx) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + stream = cio_stream_create(ctx, "direct", CIO_STORE_FS); |
| 95 | + TEST_CHECK(stream != NULL); |
| 96 | + if (!stream) { |
| 97 | + cio_destroy(ctx); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + chunk = cio_chunk_open(ctx, stream, "meta", CIO_OPEN, 1024, &err); |
| 102 | + TEST_CHECK(chunk != NULL); |
| 103 | + if (!chunk) { |
| 104 | + cio_destroy(ctx); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + ret = cio_chunk_is_up(chunk); |
| 109 | + if (ret == CIO_FALSE) { |
| 110 | + ret = cio_chunk_up_force(chunk); |
| 111 | + TEST_CHECK(ret == CIO_OK); |
| 112 | + } |
| 113 | + |
| 114 | + tag_len = expected_tag_len; |
| 115 | + ret = write_legacy_chunk_metadata(chunk, FLB_INPUT_LOGS, |
| 116 | + tag_string, tag_len); |
| 117 | + TEST_CHECK(ret == 0); |
| 118 | + |
| 119 | + ret = cio_chunk_write(chunk, payload, payload_size); |
| 120 | + TEST_CHECK(ret == 0); |
| 121 | + |
| 122 | + ret = cio_chunk_get_content(chunk, &content_buf, &content_size); |
| 123 | + TEST_CHECK(ret == 0); |
| 124 | + if (ret == 0) { |
| 125 | + TEST_CHECK(content_buf != NULL); |
| 126 | + TEST_CHECK(content_size == payload_size); |
| 127 | + if (content_size == payload_size) { |
| 128 | + TEST_CHECK(memcmp(content_buf, payload, payload_size) == 0); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + output_routes[0].id = 511; |
| 133 | + output_routes[0].label = "alpha"; |
| 134 | + output_routes[0].label_length = 5; |
| 135 | + output_routes[1].id = 70000; |
| 136 | + output_routes[1].label = "beta"; |
| 137 | + output_routes[1].label_length = 4; |
| 138 | + ret = flb_input_chunk_write_header_v2(chunk, |
| 139 | + FLB_INPUT_LOGS, |
| 140 | + (char *) tag_string, |
| 141 | + tag_len, |
| 142 | + output_routes, |
| 143 | + 2); |
| 144 | + TEST_CHECK(ret == 0); |
| 145 | + |
| 146 | + memset(&ic, 0, sizeof(ic)); |
| 147 | + ic.chunk = chunk; |
| 148 | + |
| 149 | + TEST_CHECK(flb_input_chunk_has_direct_routes(&ic) == FLB_TRUE); |
| 150 | + |
| 151 | + ret = cio_chunk_get_content(chunk, &content_buf, &content_size); |
| 152 | + TEST_CHECK(ret == 0); |
| 153 | + if (ret == 0) { |
| 154 | + TEST_CHECK(content_buf != NULL); |
| 155 | + TEST_CHECK(content_size == payload_size); |
| 156 | + if (content_size == payload_size) { |
| 157 | + TEST_CHECK(memcmp(content_buf, payload, payload_size) == 0); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + ret = flb_input_chunk_get_direct_routes(&ic, &loaded_routes, &route_count); |
| 162 | + TEST_CHECK(ret == 0); |
| 163 | + TEST_CHECK(route_count == 2); |
| 164 | + if (ret == 0 && route_count == 2) { |
| 165 | + TEST_CHECK(loaded_routes != NULL); |
| 166 | + if (loaded_routes) { |
| 167 | + TEST_CHECK(loaded_routes[0].id == 511); |
| 168 | + TEST_CHECK(loaded_routes[1].id == 70000); |
| 169 | + TEST_CHECK(loaded_routes[0].label != NULL); |
| 170 | + TEST_CHECK(loaded_routes[1].label != NULL); |
| 171 | + if (loaded_routes[0].label && loaded_routes[1].label) { |
| 172 | + TEST_CHECK(strcmp(loaded_routes[0].label, "alpha") == 0); |
| 173 | + TEST_CHECK(strcmp(loaded_routes[1].label, "beta") == 0); |
| 174 | + } |
| 175 | + flb_input_chunk_destroy_direct_routes(loaded_routes, route_count); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + ret = flb_input_chunk_get_tag(&ic, &tag_buf, &tag_len); |
| 180 | + TEST_CHECK(ret == 0); |
| 181 | + TEST_CHECK(tag_len == expected_tag_len); |
| 182 | + if (ret == 0 && tag_len == expected_tag_len) { |
| 183 | + TEST_CHECK(memcmp(tag_buf, tag_string, expected_tag_len) == 0); |
| 184 | + } |
| 185 | + |
| 186 | + cio_chunk_close(chunk, CIO_TRUE); |
| 187 | + cio_destroy(ctx); |
| 188 | + cio_utils_recursive_delete(TEST_STREAM_PATH); |
| 189 | +} |
| 190 | + |
| 191 | +TEST_LIST = { |
| 192 | + { "chunk_metadata_direct_routes", test_chunk_metadata_direct_routes }, |
| 193 | + { 0 } |
| 194 | +}; |
0 commit comments