|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2019-2021 The Fluent Bit Authors |
| 6 | + * Copyright (C) 2015-2018 Treasure Data Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <fluent-bit.h> |
| 22 | +#include <sys/types.h> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <fcntl.h> |
| 25 | +#include "flb_tests_runtime.h" |
| 26 | + |
| 27 | +#define TMP_LUA_PATH "a.lua" |
| 28 | + |
| 29 | +pthread_mutex_t result_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 30 | +char *output = NULL; |
| 31 | + |
| 32 | +void set_output(char *val) |
| 33 | +{ |
| 34 | + pthread_mutex_lock(&result_mutex); |
| 35 | + output = val; |
| 36 | + pthread_mutex_unlock(&result_mutex); |
| 37 | +} |
| 38 | + |
| 39 | +char *get_output(void) |
| 40 | +{ |
| 41 | + char *val; |
| 42 | + |
| 43 | + pthread_mutex_lock(&result_mutex); |
| 44 | + val = output; |
| 45 | + pthread_mutex_unlock(&result_mutex); |
| 46 | + |
| 47 | + return val; |
| 48 | +} |
| 49 | + |
| 50 | +int callback_test(void* data, size_t size, void* cb_data) |
| 51 | +{ |
| 52 | + if (size > 0) { |
| 53 | + flb_debug("[test_filter_lua] received message: %s", data); |
| 54 | + set_output(data); /* success */ |
| 55 | + } |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +void delete_script() |
| 60 | +{ |
| 61 | + unlink(TMP_LUA_PATH); |
| 62 | + flb_debug("remove script\n"); |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +int create_script(char *script_body, size_t body_size) |
| 67 | +{ |
| 68 | + FILE *fp = NULL; |
| 69 | + fp = fopen(TMP_LUA_PATH, "w+"); |
| 70 | + if (fp == NULL) { |
| 71 | + TEST_MSG("fopen error\n"); |
| 72 | + return -1; |
| 73 | + } |
| 74 | + fwrite(script_body, body_size, 1, fp); |
| 75 | + fflush(fp); |
| 76 | + fclose(fp); |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +void flb_test_type_int_key(void) |
| 81 | +{ |
| 82 | + int ret; |
| 83 | + flb_ctx_t *ctx; |
| 84 | + int in_ffd; |
| 85 | + int out_ffd; |
| 86 | + int filter_ffd; |
| 87 | + char *output = NULL; |
| 88 | + char *input = "[0, {\"key\":\"val\"}]"; |
| 89 | + char *result; |
| 90 | + struct flb_lib_out_cb cb_data; |
| 91 | + |
| 92 | + char *script_body = "" |
| 93 | + "function lua_main(tag, timestamp, record)\n" |
| 94 | + " new_record = record\n" |
| 95 | + " new_record[\"lua_int\"] = 10\n" |
| 96 | + " return 1, timestamp, new_record\n" |
| 97 | + "end\n"; |
| 98 | + |
| 99 | + /* Create context, flush every second (some checks omitted here) */ |
| 100 | + ctx = flb_create(); |
| 101 | + flb_service_set(ctx, "flush", "1", "grace", "1", NULL); |
| 102 | + |
| 103 | + /* Prepare output callback context*/ |
| 104 | + cb_data.cb = callback_test; |
| 105 | + cb_data.data = NULL; |
| 106 | + |
| 107 | + ret = create_script(script_body, strlen(script_body)); |
| 108 | + TEST_CHECK(ret == 0); |
| 109 | + /* Filter */ |
| 110 | + filter_ffd = flb_filter(ctx, (char *) "lua", NULL); |
| 111 | + TEST_CHECK(filter_ffd >= 0); |
| 112 | + ret = flb_filter_set(ctx, filter_ffd, |
| 113 | + "Match", "*", |
| 114 | + "call", "lua_main", |
| 115 | + "type_int_key", "lua_int", |
| 116 | + "script", TMP_LUA_PATH, |
| 117 | + NULL); |
| 118 | + |
| 119 | + /* Input */ |
| 120 | + in_ffd = flb_input(ctx, (char *) "lib", NULL); |
| 121 | + flb_input_set(ctx, in_ffd, "tag", "test", NULL); |
| 122 | + TEST_CHECK(in_ffd >= 0); |
| 123 | + |
| 124 | + /* Lib output */ |
| 125 | + out_ffd = flb_output(ctx, (char *) "lib", (void *)&cb_data); |
| 126 | + TEST_CHECK(out_ffd >= 0); |
| 127 | + flb_output_set(ctx, out_ffd, |
| 128 | + "match", "test", |
| 129 | + "format", "json", |
| 130 | + NULL); |
| 131 | + |
| 132 | + ret = flb_start(ctx); |
| 133 | + TEST_CHECK(ret==0); |
| 134 | + |
| 135 | + flb_lib_push(ctx, in_ffd, input, strlen(input)); |
| 136 | + sleep(1); |
| 137 | + output = get_output(); |
| 138 | + result = strstr(output, "\"lua_int\":10."); |
| 139 | + TEST_CHECK(result == NULL); |
| 140 | + result = strstr(output, "\"lua_int\":10"); |
| 141 | + TEST_CHECK(result != NULL); |
| 142 | + |
| 143 | + /* clean up */ |
| 144 | + flb_lib_free(output); |
| 145 | + delete_script(); |
| 146 | + |
| 147 | + flb_stop(ctx); |
| 148 | + flb_destroy(ctx); |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | +void flb_test_append_tag(void) |
| 153 | +{ |
| 154 | + int ret; |
| 155 | + flb_ctx_t *ctx; |
| 156 | + int in_ffd; |
| 157 | + int out_ffd; |
| 158 | + int filter_ffd; |
| 159 | + char *output = NULL; |
| 160 | + char *input = "[0, {\"key\":\"val\"}]"; |
| 161 | + char *result; |
| 162 | + struct flb_lib_out_cb cb_data; |
| 163 | + |
| 164 | + char *script_body = "" |
| 165 | + "function lua_main(tag, timestamp, record)\n" |
| 166 | + " new_record = record\n" |
| 167 | + " new_record[\"tag\"] = tag\n" |
| 168 | + " return 1, timestamp, new_record\n" |
| 169 | + "end\n"; |
| 170 | + |
| 171 | + /* Create context, flush every second (some checks omitted here) */ |
| 172 | + ctx = flb_create(); |
| 173 | + flb_service_set(ctx, "flush", "1", "grace", "1", NULL); |
| 174 | + |
| 175 | + /* Prepare output callback context*/ |
| 176 | + cb_data.cb = callback_test; |
| 177 | + cb_data.data = NULL; |
| 178 | + |
| 179 | + ret = create_script(script_body, strlen(script_body)); |
| 180 | + TEST_CHECK(ret == 0); |
| 181 | + /* Filter */ |
| 182 | + filter_ffd = flb_filter(ctx, (char *) "lua", NULL); |
| 183 | + TEST_CHECK(filter_ffd >= 0); |
| 184 | + ret = flb_filter_set(ctx, filter_ffd, |
| 185 | + "Match", "*", |
| 186 | + "call", "lua_main", |
| 187 | + "script", TMP_LUA_PATH, |
| 188 | + NULL); |
| 189 | + |
| 190 | + /* Input */ |
| 191 | + in_ffd = flb_input(ctx, (char *) "lib", NULL); |
| 192 | + flb_input_set(ctx, in_ffd, "tag", "test", NULL); |
| 193 | + TEST_CHECK(in_ffd >= 0); |
| 194 | + |
| 195 | + /* Lib output */ |
| 196 | + out_ffd = flb_output(ctx, (char *) "lib", (void *)&cb_data); |
| 197 | + TEST_CHECK(out_ffd >= 0); |
| 198 | + flb_output_set(ctx, out_ffd, |
| 199 | + "match", "test", |
| 200 | + "format", "json", |
| 201 | + NULL); |
| 202 | + |
| 203 | + ret = flb_start(ctx); |
| 204 | + TEST_CHECK(ret==0); |
| 205 | + |
| 206 | + flb_lib_push(ctx, in_ffd, input, strlen(input)); |
| 207 | + sleep(1); |
| 208 | + output = get_output(); |
| 209 | + result = strstr(output, "\"tag\":\"test\""); |
| 210 | + TEST_CHECK(result != NULL); |
| 211 | + |
| 212 | + /* clean up */ |
| 213 | + flb_lib_free(output); |
| 214 | + delete_script(); |
| 215 | + |
| 216 | + flb_stop(ctx); |
| 217 | + flb_destroy(ctx); |
| 218 | +} |
| 219 | + |
| 220 | + |
| 221 | +void flb_test_helloworld(void) |
| 222 | +{ |
| 223 | + int ret; |
| 224 | + flb_ctx_t *ctx; |
| 225 | + int in_ffd; |
| 226 | + int out_ffd; |
| 227 | + int filter_ffd; |
| 228 | + |
| 229 | + char *script_body = "" |
| 230 | + "function lua_main(tag, timestamp, record)\n" |
| 231 | + " print(\"hello world\")\n" |
| 232 | + " return 0, timestamp, record\n" |
| 233 | + "end\n"; |
| 234 | + |
| 235 | + /* Create context, flush every second (some checks omitted here) */ |
| 236 | + ctx = flb_create(); |
| 237 | + flb_service_set(ctx, "flush", "1", "grace", "1", NULL); |
| 238 | + |
| 239 | + ret = create_script(script_body, strlen(script_body)); |
| 240 | + TEST_CHECK(ret == 0); |
| 241 | + /* Filter */ |
| 242 | + filter_ffd = flb_filter(ctx, (char *) "lua", NULL); |
| 243 | + TEST_CHECK(filter_ffd >= 0); |
| 244 | + ret = flb_filter_set(ctx, filter_ffd, |
| 245 | + "Match", "*", |
| 246 | + "call", "lua_main", |
| 247 | + "script", TMP_LUA_PATH, |
| 248 | + NULL); |
| 249 | + |
| 250 | + /* Input */ |
| 251 | + in_ffd = flb_input(ctx, (char *) "dummy", NULL); |
| 252 | + flb_input_set(ctx, in_ffd, "tag", "test", NULL); |
| 253 | + TEST_CHECK(in_ffd >= 0); |
| 254 | + |
| 255 | + /* Lib output */ |
| 256 | + out_ffd = flb_output(ctx, (char *) "stdout", NULL); |
| 257 | + TEST_CHECK(out_ffd >= 0); |
| 258 | + flb_output_set(ctx, out_ffd, |
| 259 | + "match", "test", |
| 260 | + NULL); |
| 261 | + |
| 262 | + ret = flb_start(ctx); |
| 263 | + TEST_CHECK(ret==0); |
| 264 | + |
| 265 | + delete_script(); |
| 266 | + flb_stop(ctx); |
| 267 | + flb_destroy(ctx); |
| 268 | +} |
| 269 | + |
| 270 | +TEST_LIST = { |
| 271 | + {"hello_world", flb_test_helloworld}, |
| 272 | + {"append_tag", flb_test_append_tag}, |
| 273 | + {"type_int_key", flb_test_type_int_key}, |
| 274 | + {NULL, NULL} |
| 275 | +}; |
0 commit comments