1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 
3 #include <fluent-bit.h>
4 #include "flb_tests_runtime.h"
5 
6 /* Test data */
7 #include "data/common/json_invalid.h" /* JSON_INVALID */
8 #include "data/common/json_long.h"    /* JSON_LONG    */
9 #include "data/common/json_small.h"   /* JSON_SMALL   */
10 
11 /* Test functions */
12 void flb_test_flowcounter_json_invalid(void);
13 void flb_test_flowcounter_json_long(void);
14 void flb_test_flowcounter_json_small(void);
15 void flb_test_flowcounter_unit_second(void);
16 void flb_test_flowcounter_unit_minute(void);
17 void flb_test_flowcounter_unit_hour(void);
18 void flb_test_flowcounter_unit_day(void);
19 void flb_test_flowcounter_unit_invalid(void);
20 
21 /* Test list */
22 TEST_LIST = {
23     {"json_invalid",    flb_test_flowcounter_json_invalid },
24     {"json_long",       flb_test_flowcounter_json_long    },
25     {"json_small",      flb_test_flowcounter_json_small   },
26     {"unit_second",     flb_test_flowcounter_unit_second  },
27     {"unit_minute",     flb_test_flowcounter_unit_minute  },
28     {"unit_hour",       flb_test_flowcounter_unit_hour    },
29     {"unit_day",        flb_test_flowcounter_unit_day     },
30     {"unit_invalid",    flb_test_flowcounter_unit_invalid },
31     {NULL, NULL}
32 };
33 
34 
35 void flb_test_flowcounter_json_invalid(void)
36 {
37     int i;
38     int ret;
39     int bytes;
40     char *p = (char *) JSON_INVALID;
41     flb_ctx_t *ctx;
42     int in_ffd;
43     int out_ffd;
44 
45     ctx = flb_create();
46     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
47 
48     in_ffd = flb_input(ctx, (char *) "lib", NULL);
49     TEST_CHECK(in_ffd >= 0);
50     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
51 
52     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
53     TEST_CHECK(out_ffd >= 0);
54     flb_output_set(ctx, out_ffd, "match", "test", NULL);
55 
56     ret = flb_start(ctx);
57     TEST_CHECK(ret == 0);
58 
59     for (i = 0; i < (int) sizeof(JSON_INVALID) - 1; i++) {
60         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
61         TEST_CHECK(bytes == 1);
62     }
63 
64     flb_stop(ctx);
65     flb_destroy(ctx);
66 }
67 
68 /* It writes a very long JSON map (> 100KB) byte by byte */
69 void flb_test_flowcounter_json_long(void)
70 {
71     int i;
72     int ret;
73     int bytes;
74     char *p = (char *) JSON_LONG;
75     flb_ctx_t *ctx;
76     int in_ffd;
77     int out_ffd;
78 
79     ctx = flb_create();
80     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
81 
82     in_ffd = flb_input(ctx, (char *) "lib", NULL);
83     TEST_CHECK(in_ffd >= 0);
84     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
85 
86     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
87     TEST_CHECK(out_ffd >= 0);
88     flb_output_set(ctx, out_ffd, "match", "test", NULL);
89 
90     ret = flb_start(ctx);
91     TEST_CHECK(ret == 0);
92 
93     for (i = 0; i < (int) sizeof(JSON_LONG) - 1; i++) {
94         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
95         TEST_CHECK(bytes == 1);
96     }
97 
98     sleep(1); /* waiting flush */
99 
100     flb_stop(ctx);
101     flb_destroy(ctx);
102 }
103 
104 void flb_test_flowcounter_json_small(void)
105 {
106     int i;
107     int ret;
108     int bytes;
109     char *p = (char *) JSON_SMALL;
110     flb_ctx_t *ctx;
111     int in_ffd;
112     int out_ffd;
113 
114     ctx = flb_create();
115     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
116 
117     in_ffd = flb_input(ctx, (char *) "lib", NULL);
118     TEST_CHECK(in_ffd >= 0);
119     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
120 
121     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
122     TEST_CHECK(out_ffd >= 0);
123     flb_output_set(ctx, out_ffd, "match", "test", NULL);
124 
125     ret = flb_start(ctx);
126     TEST_CHECK(ret == 0);
127 
128     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
129         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
130         TEST_CHECK(bytes == 1);
131     }
132 
133     sleep(1); /* waiting flush */
134 
135     flb_stop(ctx);
136     flb_destroy(ctx);
137 }
138 
139 void flb_test_flowcounter_unit_second(void)
140 {
141     int i;
142     int ret;
143     int bytes;
144     char *p = (char *) JSON_SMALL;
145     flb_ctx_t *ctx;
146     int in_ffd;
147     int out_ffd;
148 
149     ctx = flb_create();
150     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
151 
152     in_ffd = flb_input(ctx, (char *) "lib", NULL);
153     TEST_CHECK(in_ffd >= 0);
154     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
155 
156     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
157     TEST_CHECK(out_ffd >= 0);
158     flb_output_set(ctx, out_ffd, "match", "test", NULL);
159     flb_output_set(ctx, out_ffd, "Unit", "second", NULL);
160 
161     ret = flb_start(ctx);
162     TEST_CHECK(ret == 0);
163 
164     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
165         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
166         TEST_CHECK(bytes == 1);
167     }
168 
169     sleep(1); /* waiting flush */
170 
171     flb_stop(ctx);
172     flb_destroy(ctx);
173 }
174 
175 void flb_test_flowcounter_unit_minute(void)
176 {
177     int i;
178     int ret;
179     int bytes;
180     char *p = (char *) JSON_SMALL;
181     flb_ctx_t *ctx;
182     int in_ffd;
183     int out_ffd;
184 
185     ctx = flb_create();
186     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
187 
188     in_ffd = flb_input(ctx, (char *) "lib", NULL);
189     TEST_CHECK(in_ffd >= 0);
190     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
191 
192     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
193     TEST_CHECK(out_ffd >= 0);
194     flb_output_set(ctx, out_ffd, "match", "test", NULL);
195     flb_output_set(ctx, out_ffd, "Unit", "minute", NULL);
196 
197     ret = flb_start(ctx);
198     TEST_CHECK(ret == 0);
199 
200     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
201         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
202         TEST_CHECK(bytes == 1);
203     }
204 
205     sleep(1); /* waiting flush */
206 
207     flb_stop(ctx);
208     flb_destroy(ctx);
209 }
210 
211 void flb_test_flowcounter_unit_hour(void)
212 {
213     int i;
214     int ret;
215     int bytes;
216     char *p = (char *) JSON_SMALL;
217     flb_ctx_t *ctx;
218     int in_ffd;
219     int out_ffd;
220 
221     ctx = flb_create();
222     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
223 
224     in_ffd = flb_input(ctx, (char *) "lib", NULL);
225     TEST_CHECK(in_ffd >= 0);
226     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
227 
228     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
229     TEST_CHECK(out_ffd >= 0);
230     flb_output_set(ctx, out_ffd, "match", "test", NULL);
231     flb_output_set(ctx, out_ffd, "Unit", "hour", NULL);
232 
233     ret = flb_start(ctx);
234     TEST_CHECK(ret == 0);
235 
236     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
237         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
238         TEST_CHECK(bytes == 1);
239     }
240 
241     sleep(1); /* waiting flush */
242 
243     flb_stop(ctx);
244     flb_destroy(ctx);
245 }
246 
247 void flb_test_flowcounter_unit_day(void)
248 {
249     int i;
250     int ret;
251     int bytes;
252     char *p = (char *) JSON_SMALL;
253     flb_ctx_t *ctx;
254     int in_ffd;
255     int out_ffd;
256 
257     ctx = flb_create();
258     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
259 
260     in_ffd = flb_input(ctx, (char *) "lib", NULL);
261     TEST_CHECK(in_ffd >= 0);
262     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
263 
264     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
265     TEST_CHECK(out_ffd >= 0);
266     flb_output_set(ctx, out_ffd, "match", "test", NULL);
267     flb_output_set(ctx, out_ffd, "Unit", "day", NULL);
268 
269     ret = flb_start(ctx);
270     TEST_CHECK(ret == 0);
271 
272     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
273         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
274         TEST_CHECK(bytes == 1);
275     }
276 
277     sleep(1); /* waiting flush */
278 
279     flb_stop(ctx);
280     flb_destroy(ctx);
281 }
282 
283 void flb_test_flowcounter_unit_invalid(void)
284 {
285     int i;
286     int ret;
287     int bytes;
288     char *p = (char *) JSON_SMALL;
289     flb_ctx_t *ctx;
290     int in_ffd;
291     int out_ffd;
292 
293     ctx = flb_create();
294     flb_service_set(ctx, "Flush", "1", "Grace", "1", "Log_Level", "error", NULL);
295 
296     in_ffd = flb_input(ctx, (char *) "lib", NULL);
297     TEST_CHECK(in_ffd >= 0);
298     flb_input_set(ctx, in_ffd, "tag", "test", NULL);
299 
300     out_ffd = flb_output(ctx, (char *) "flowcounter", NULL);
301     TEST_CHECK(out_ffd >= 0);
302     flb_output_set(ctx, out_ffd, "match", "test", NULL);
303     flb_output_set(ctx, out_ffd, "Unit", "xxx", NULL);
304 
305     ret = flb_start(ctx);
306     TEST_CHECK(ret == 0);
307 
308     for (i = 0; i < (int) sizeof(JSON_SMALL) - 1; i++) {
309         bytes = flb_lib_push(ctx, in_ffd, p + i, 1);
310         TEST_CHECK(bytes == 1);
311     }
312 
313     sleep(1); /* waiting flush */
314 
315     flb_stop(ctx);
316     flb_destroy(ctx);
317 }
318