summaryrefslogtreecommitdiffstats
path: root/fluent-bit/plugins/in_event_test/event_test.c
blob: 557017fe22f67d082b75ec0e367e9026c62cd2d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  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 <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_pipe.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_time_utils.h>

#define STATUS_OK        1
#define STATUS_ERROR     0
#define STATUS_PENDING  -1
#define CALLBACK_TIME    2 /* 2 seconds */

#define SERVER_PORT      "9092"
#define SERVER_IFACE     "0.0.0.0"

struct unit_test {
    int id;
    int coll_id;
    int status;
    char *desc;
};

struct unit_test tests[] = {
    {0, 0, STATUS_PENDING, "collector time"},
    {1, 0, STATUS_PENDING, "collector fd_event"},
    {2, 0, STATUS_PENDING, "collector fd_server | socket"},
    {3, 0, STATUS_PENDING, "plugin paused from engine"},
    {4, 0, STATUS_PENDING, "plugin resumed from engine"},
};

#define UNIT_TESTS_SIZE  (sizeof(tests) / sizeof(struct unit_test))

struct event_test {
    flb_pipefd_t pipe[2];
    int server_fd;
    int client_coll_id;
    struct flb_upstream *upstream;
    struct unit_test *tests;
    struct flb_input_instance *ins;
};

static void set_unit_test_status(struct event_test *ctx, int id, int status)
{
    struct unit_test *ut;

    ut = &ctx->tests[id];
    ut->status = status;
}

static int config_destroy(struct event_test *ctx)
{
    if (!ctx) {
        return 0;
    }

    if (ctx->tests) {
        flb_free(ctx->tests);
    }

    if (ctx->pipe[0] > 0) {
        flb_socket_close(ctx->pipe[0]);
    }
    if (ctx->pipe[1] > 0) {
        flb_socket_close(ctx->pipe[1]);
    }
    if (ctx->server_fd > 0) {
        flb_socket_close(ctx->server_fd);
    }

    if (ctx->upstream) {
        flb_upstream_destroy(ctx->upstream);
    }

    flb_free(ctx);
    return 0;
}

static int cb_collector_time(struct flb_input_instance *ins,
                            struct flb_config *config, void *in_context)
{
    int diff;
    int ret;
    uint64_t val;
    time_t now;
    struct unit_test *ut;
    struct event_test *ctx = (struct event_test *) in_context;

    now = time(NULL);
    diff = now - config->init_time;
    /* For macOS, we sometimes get the +1 longer time elapse.
     * To handle this, we simply add +1 as a delta for checking interval. */
    if (diff > (CALLBACK_TIME + 1)) {
        flb_plg_error(ins, "cb_collector_time difference failed: %i seconds", diff);
        set_unit_test_status(ctx, 0, STATUS_ERROR);
        flb_engine_exit(config);
    }

    /* disable the collector */
    ut = &ctx->tests[0];
    flb_input_collector_pause(ut->coll_id, ins);

    /*
     * before to return, trigger test 1 (collector_fd_event) by writing a byte
     * to our local pipe.
     */
    val = 1;
    ret = write(ctx->pipe[1], &val, sizeof(val));
    if (ret == -1) {
        flb_errno();
        set_unit_test_status(ctx, 0, STATUS_ERROR);
        flb_engine_exit(config);
    }

    set_unit_test_status(ctx, 0, STATUS_OK);
    flb_plg_info(ins, "[OK] collector_time");
    FLB_INPUT_RETURN(0);
}

static int cb_collector_fd(struct flb_input_instance *ins,
                           struct flb_config *config, void *in_context)
{
    uint64_t val = 0;
    size_t bytes;
    struct unit_test *ut;
    struct event_test *ctx = (struct event_test *) in_context;

    bytes = read(ctx->pipe[0], &val, sizeof(val));
    if (bytes <= 0) {
        flb_errno();
        set_unit_test_status(ctx, 1, STATUS_ERROR);
        flb_engine_exit(config);
    }
    else {
        flb_plg_info(ins, "[OK] collector_fd");
    }

    /* disable the collector */
    ut = &ctx->tests[1];
    flb_input_collector_pause(ut->coll_id, ins);
    set_unit_test_status(ctx, 1, STATUS_OK);

    FLB_INPUT_RETURN(0);
}

static int cb_collector_server_socket(struct flb_input_instance *ins,
                                      struct flb_config *config, void *in_context)
{
    int fd;
    struct unit_test *ut;
    struct event_test *ctx = in_context;

    /* Accept the new connection */
    fd = flb_net_accept(ctx->server_fd);
    if (fd == -1) {
        flb_plg_error(ins, "could not accept new connection");
        return -1;
    }

    /* sleep co-routine for 500ms */
    flb_time_sleep(500);
    flb_socket_close(fd);

    ut = &ctx->tests[2];
    flb_input_collector_pause(ut->coll_id, ins);
    set_unit_test_status(ctx, 2, STATUS_OK);

    flb_plg_info(ins, "[OK] collector_server_socket");

    /* tell the engine to deliver a pause request */
    flb_plg_info(ins, "test pause/resume in 5 seconds...");
    flb_input_test_pause_resume(ins, 5);

    /* return */
    FLB_INPUT_RETURN(0);
}

static int cb_collector_server_client(struct flb_input_instance *ins,
                                      struct flb_config *config, void *in_context)
{
    struct flb_connection *u_conn;
    struct event_test *ctx = (struct event_test *) in_context;

    /* get the upstream connection (localhost) */
    u_conn = flb_upstream_conn_get(ctx->upstream);
    if (!u_conn) {
        flb_plg_error(ins, "could not connect to socket server");
        return -1;
    }

    flb_time_sleep(200);
    flb_upstream_conn_release(u_conn);

    /* disable this collector */
    flb_input_collector_pause(ctx->client_coll_id, ins);
    FLB_INPUT_RETURN(0);
}

static struct event_test *config_create(struct flb_input_instance *ins)
{
    size_t size;
    struct event_test *ctx;

    /* Allocate space for the configuration */
    ctx = flb_calloc(1, sizeof(struct event_test));
    if (!ctx) {
        flb_errno();
        return NULL;
    }
    ctx->ins = ins;

    size = sizeof(struct unit_test) * UNIT_TESTS_SIZE;
    ctx->tests = flb_malloc(size);
    if (!ctx->tests) {
        flb_errno();
        flb_free(ctx);
        return NULL;
    }
    memcpy(ctx->tests, &tests, size);
    return ctx;
}

/* Initialize plugin */
static int cb_event_test_init(struct flb_input_instance *ins,
                              struct flb_config *config, void *data)
{
    int fd;
    int ret;
    struct unit_test *ut;
    struct event_test *ctx = NULL;
    struct flb_upstream *upstream;

    /* Allocate space for the configuration */
    ctx = config_create(ins);
    if (!ctx) {
        return -1;
    }
    flb_input_set_context(ins, ctx);

    /* unit test 0: collector_time */
    ret = flb_input_set_collector_time(ins, cb_collector_time,
                                       CALLBACK_TIME, 0, config);
    if (ret < 0) {
        config_destroy(ctx);
        return -1;
    }
    ut = &ctx->tests[0];
    ut->coll_id = ret;

    /* unit test 1: collector_fd_event */
    ret = flb_pipe_create(ctx->pipe);
    if (ret == -1) {
        flb_errno();
        config_destroy(ctx);
        return -1;
    }
    ret = flb_input_set_collector_event(ins,
                                        cb_collector_fd,
                                        ctx->pipe[0],
                                        config);
    if (ret < 0) {
        config_destroy(ctx);
        return -1;
    }
    ut = &ctx->tests[1];
    ut->coll_id = ret;

    /* unit test 2: collector_socket */
    fd = flb_net_server(SERVER_PORT, SERVER_IFACE);
    if (fd < 0) {
        flb_errno();
        config_destroy(ctx);
        return -1;
    }
    flb_net_socket_nonblocking(fd);
        ctx->server_fd = fd;

    /* socket server */
    ret = flb_input_set_collector_socket(ins,
                                         cb_collector_server_socket,
                                         ctx->server_fd,
                                         config);
    if (ret == -1) {
        config_destroy(ctx);
        return -1;
    }
    ut = &ctx->tests[2];
    ut->coll_id = ret;

    /* socket client: connect to socket server to trigger the event */
    ret = flb_input_set_collector_time(ins, cb_collector_server_client,
                                       CALLBACK_TIME * 2, 0, config);
    if (ret < 0) {
        config_destroy(ctx);
        return -1;
    }
    ctx->client_coll_id = ret;

    /* upstream context for socket client */
    upstream = flb_upstream_create(config, "127.0.0.1", atoi(SERVER_PORT),
                                   FLB_IO_TCP, NULL);
    if (!upstream) {
        config_destroy(ctx);
        return -1;
    }
    ctx->upstream = upstream;
    flb_input_upstream_set(ctx->upstream, ins);

    return 0;
}

static int cb_event_test_pre_run(struct flb_input_instance *ins,
                                 struct flb_config *config, void *in_context)
{
    flb_plg_info(ins, "pre run OK");
    return -1;
}

static void cb_event_test_pause(void *data, struct flb_config *config)
{
    struct event_test *ctx = data;

    set_unit_test_status(ctx, 3, STATUS_OK);
    flb_plg_info(ctx->ins, "[OK] engine has paused the plugin");
}

static void cb_event_test_resume(void *data, struct flb_config *config)
{
    struct event_test *ctx = data;

    set_unit_test_status(ctx, 4, STATUS_OK);
    flb_plg_info(ctx->ins, "[OK] engine has resumed the plugin");

    flb_engine_exit(config);
}

static int in_event_test_exit(void *data, struct flb_config *config)
{
    int i;
    int failed = FLB_FALSE;
    struct event_test *ctx = data;
    struct unit_test *ut;
    (void) *config;

    /* check tests */
    for (i = 0; i < UNIT_TESTS_SIZE; i++) {
        ut = &ctx->tests[i];
        if (ut->status != STATUS_OK) {
            flb_plg_error(ctx->ins, "unit test #%i '%s' failed",
                          i, ut->desc);
            failed = FLB_TRUE;
        }
        else {
            flb_plg_info(ctx->ins, "unit test #%i '%s' succeeded",
                         i, ut->desc);
        }
    }

    /* if one test failed, perform an abrupt exit with proper error */
    if (failed) {
        exit(EXIT_FAILURE);
    }

    config_destroy(ctx);
    return 0;
}

/* Configuration properties map */
static struct flb_config_map config_map[] = {
   /* EOF */
   {0}
};

struct flb_input_plugin in_event_test_plugin = {
    .name         = "event_test",
    .description  = "Event tests for input plugins",
    .cb_init      = cb_event_test_init,
    .cb_pre_run   = cb_event_test_pre_run,
    .cb_collect   = NULL,
    .cb_flush_buf = NULL,
    .cb_pause     = cb_event_test_pause,
    .cb_resume    = cb_event_test_resume,
    .cb_exit      = in_event_test_exit,
    .config_map   = config_map,
    .flags        = FLB_INPUT_CORO | FLB_INPUT_THREADED
};