Skip to content

Commit 6f20aa7

Browse files
committed
in_kubernetes_events: tests: missing db config options
Add db.locking, db.journal_mode, and db.sync configuration parameters with proper validation. Remove dns_retries and dns_wait_time options as they were never implemented in the plugin. - Add config_map entries for db.locking, db.journal_mode - Add db.sync property reading code and journal_mode validation - Remove dns_retries and dns_wait_time (unused, unlike filter_kubernetes) - Add test coverage for db.sync, db.journal_mode, and db.locking options and guard SQLDB config tests with FLB_HAVE_SQLDB Fixes #11243. Signed-off-by: Eric D. Schabell <[email protected]>
1 parent 10ebd3a commit 6f20aa7

File tree

4 files changed

+231
-3
lines changed

4 files changed

+231
-3
lines changed

plugins/in_kubernetes_events/kubernetes_events.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,18 @@ static struct flb_config_map config_map[] = {
10851085
0, FLB_FALSE, 0,
10861086
"set a database sync method. values: extra, full, normal and off."
10871087
},
1088+
{
1089+
FLB_CONFIG_MAP_BOOL, "db.locking", "false",
1090+
0, FLB_TRUE, offsetof(struct k8s_events, db_locking),
1091+
"set exclusive locking mode, increase performance but don't allow "
1092+
"external connections to the database file."
1093+
},
1094+
{
1095+
FLB_CONFIG_MAP_STR, "db.journal_mode", "WAL",
1096+
0, FLB_TRUE, offsetof(struct k8s_events, db_journal_mode),
1097+
"set the journal mode for the database. values: DELETE, TRUNCATE, "
1098+
"PERSIST, MEMORY, WAL, OFF."
1099+
},
10881100
#endif
10891101

10901102
/* EOF */

plugins/in_kubernetes_events/kubernetes_events.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ struct k8s_events {
6767
char *auth;
6868
size_t auth_len;
6969

70-
int dns_retries;
71-
int dns_wait_time;
72-
7370
struct flb_tls *tls;
7471

7572
struct flb_log_event_encoder *encoder;

plugins/in_kubernetes_events/kubernetes_events_conf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,45 @@ struct k8s_events *k8s_events_conf_create(struct flb_input_instance *ins)
232232
}
233233

234234
#ifdef FLB_HAVE_SQLDB
235+
/* Database sync mode (needs to be set before opening the database) */
236+
ctx->db_sync = 1; /* default: sqlite sync 'normal' */
237+
tmp = flb_input_get_property("db.sync", ins);
238+
if (tmp) {
239+
if (strcasecmp(tmp, "extra") == 0) {
240+
ctx->db_sync = 3;
241+
}
242+
else if (strcasecmp(tmp, "full") == 0) {
243+
ctx->db_sync = 2;
244+
}
245+
else if (strcasecmp(tmp, "normal") == 0) {
246+
ctx->db_sync = 1;
247+
}
248+
else if (strcasecmp(tmp, "off") == 0) {
249+
ctx->db_sync = 0;
250+
}
251+
else {
252+
flb_plg_error(ctx->ins, "invalid database 'db.sync' value: %s", tmp);
253+
k8s_events_conf_destroy(ctx);
254+
return NULL;
255+
}
256+
}
257+
258+
/* Journal mode validation */
259+
tmp = flb_input_get_property("db.journal_mode", ins);
260+
if (tmp) {
261+
if (strcasecmp(tmp, "DELETE") != 0 &&
262+
strcasecmp(tmp, "TRUNCATE") != 0 &&
263+
strcasecmp(tmp, "PERSIST") != 0 &&
264+
strcasecmp(tmp, "MEMORY") != 0 &&
265+
strcasecmp(tmp, "WAL") != 0 &&
266+
strcasecmp(tmp, "OFF") != 0) {
267+
268+
flb_plg_error(ctx->ins, "invalid db.journal_mode=%s", tmp);
269+
k8s_events_conf_destroy(ctx);
270+
return NULL;
271+
}
272+
}
273+
235274
/* Initialize database */
236275
tmp = flb_input_get_property("db", ins);
237276
if (tmp) {

tests/runtime/in_kubernetes_events.c

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,77 @@ static struct test_ctx *test_ctx_create(struct flb_lib_out_cb *data)
303303
return ctx;
304304
}
305305

306+
#ifdef FLB_HAVE_SQLDB
307+
/* Create test context with additional config options for config parameter testing */
308+
static struct test_ctx *test_ctx_create_with_config(struct flb_lib_out_cb *data,
309+
const char *db_sync,
310+
const char *db_locking,
311+
const char *db_journal_mode)
312+
{
313+
int i_ffd;
314+
int o_ffd;
315+
int ret;
316+
struct test_ctx *ctx = NULL;
317+
char kube_url[512] = {0};
318+
319+
ctx = flb_calloc(1, sizeof(struct test_ctx));
320+
if (!TEST_CHECK(ctx != NULL)) {
321+
TEST_MSG("flb_calloc failed");
322+
flb_errno();
323+
return NULL;
324+
}
325+
326+
/* Service config */
327+
ctx->flb = flb_create();
328+
flb_service_set(ctx->flb,
329+
"Flush", "0.200000000",
330+
"Grace", "3",
331+
"Log_Level", "debug",
332+
NULL);
333+
334+
/* Input */
335+
i_ffd = flb_input(ctx->flb, (char *) "kubernetes_events", NULL);
336+
TEST_CHECK(i_ffd >= 0);
337+
ctx->i_ffd = i_ffd;
338+
339+
sprintf(kube_url, "http://%s:%d", KUBE_API_HOST, KUBE_API_PORT);
340+
ret = flb_input_set(ctx->flb, i_ffd,
341+
"kube_url", kube_url,
342+
"kube_token_file", KUBE_TOKEN_FILE,
343+
"kube_retention_time", "365000d",
344+
"tls", "off",
345+
"interval_sec", "1",
346+
"interval_nsec", "0",
347+
NULL);
348+
TEST_CHECK(ret == 0);
349+
350+
/* Set optional config parameters if provided */
351+
if (db_sync) {
352+
ret = flb_input_set(ctx->flb, i_ffd, "db.sync", db_sync, NULL);
353+
TEST_CHECK(ret == 0);
354+
}
355+
if (db_locking) {
356+
ret = flb_input_set(ctx->flb, i_ffd, "db.locking", db_locking, NULL);
357+
TEST_CHECK(ret == 0);
358+
}
359+
if (db_journal_mode) {
360+
ret = flb_input_set(ctx->flb, i_ffd, "db.journal_mode", db_journal_mode, NULL);
361+
TEST_CHECK(ret == 0);
362+
}
363+
364+
/* Output */
365+
o_ffd = flb_output(ctx->flb, (char *) "lib", (void *) data);
366+
ctx->o_ffd = o_ffd;
367+
368+
flb_output_set(ctx->flb, ctx->o_ffd,
369+
"match", "*",
370+
"format", "json",
371+
NULL);
372+
373+
return ctx;
374+
}
375+
#endif
376+
306377
static void test_ctx_destroy(struct test_ctx *ctx)
307378
{
308379
TEST_CHECK(ctx != NULL);
@@ -444,10 +515,119 @@ void flb_test_events_with_chunkedrecv()
444515
test_ctx_destroy(ctx);
445516
}
446517

518+
#ifdef FLB_HAVE_SQLDB
519+
/* Test valid db.sync values */
520+
void flb_test_config_db_sync_values()
521+
{
522+
struct flb_lib_out_cb cb_data;
523+
struct test_ctx *ctx;
524+
int ret;
525+
const char *sync_values[] = {"extra", "full", "normal", "off", NULL};
526+
int i;
527+
528+
cb_data.cb = NULL;
529+
cb_data.data = NULL;
530+
531+
for (i = 0; sync_values[i] != NULL; i++) {
532+
ctx = test_ctx_create_with_config(&cb_data,
533+
sync_values[i], /* db.sync */
534+
NULL, /* db.locking */
535+
NULL); /* db.journal_mode */
536+
if (!TEST_CHECK(ctx != NULL)) {
537+
TEST_MSG("test_ctx_create_with_config failed for db.sync=%s", sync_values[i]);
538+
continue;
539+
}
540+
541+
ret = flb_start(ctx->flb);
542+
TEST_CHECK(ret == 0);
543+
if (ret != 0) {
544+
TEST_MSG("flb_start failed for db.sync=%s", sync_values[i]);
545+
}
546+
547+
flb_stop(ctx->flb);
548+
flb_destroy(ctx->flb);
549+
flb_free(ctx);
550+
}
551+
}
552+
553+
/* Test valid db.journal_mode values */
554+
void flb_test_config_db_journal_mode_values()
555+
{
556+
struct flb_lib_out_cb cb_data;
557+
struct test_ctx *ctx;
558+
int ret;
559+
const char *journal_modes[] = {"DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF", NULL};
560+
int i;
561+
562+
cb_data.cb = NULL;
563+
cb_data.data = NULL;
564+
565+
for (i = 0; journal_modes[i] != NULL; i++) {
566+
ctx = test_ctx_create_with_config(&cb_data,
567+
NULL, /* db.sync */
568+
NULL, /* db.locking */
569+
journal_modes[i]); /* db.journal_mode */
570+
if (!TEST_CHECK(ctx != NULL)) {
571+
TEST_MSG("test_ctx_create_with_config failed for db.journal_mode=%s", journal_modes[i]);
572+
continue;
573+
}
574+
575+
ret = flb_start(ctx->flb);
576+
TEST_CHECK(ret == 0);
577+
if (ret != 0) {
578+
TEST_MSG("flb_start failed for db.journal_mode=%s", journal_modes[i]);
579+
}
580+
581+
flb_stop(ctx->flb);
582+
flb_destroy(ctx->flb);
583+
flb_free(ctx);
584+
}
585+
}
586+
587+
/* Test valid db.locking values */
588+
void flb_test_config_db_locking_values()
589+
{
590+
struct flb_lib_out_cb cb_data;
591+
struct test_ctx *ctx;
592+
int ret;
593+
const char *locking_values[] = {"true", "false", NULL};
594+
int i;
595+
596+
cb_data.cb = NULL;
597+
cb_data.data = NULL;
598+
599+
for (i = 0; locking_values[i] != NULL; i++) {
600+
ctx = test_ctx_create_with_config(&cb_data,
601+
NULL, /* db.sync */
602+
locking_values[i], /* db.locking */
603+
NULL); /* db.journal_mode */
604+
if (!TEST_CHECK(ctx != NULL)) {
605+
TEST_MSG("test_ctx_create_with_config failed for db.locking=%s", locking_values[i]);
606+
continue;
607+
}
608+
609+
ret = flb_start(ctx->flb);
610+
TEST_CHECK(ret == 0);
611+
if (ret != 0) {
612+
TEST_MSG("flb_start failed for db.locking=%s", locking_values[i]);
613+
}
614+
615+
flb_stop(ctx->flb);
616+
flb_destroy(ctx->flb);
617+
flb_free(ctx);
618+
}
619+
}
620+
#endif
621+
447622
TEST_LIST = {
448623
{"events_v1_with_lastTimestamp", flb_test_events_v1_with_lastTimestamp},
449624
{"events_v1_with_creationTimestamp", flb_test_events_v1_with_creationTimestamp},
450625
//{"events_v1_with_chunkedrecv", flb_test_events_with_chunkedrecv},
626+
#ifdef FLB_HAVE_SQLDB
627+
{"config_db_sync_values", flb_test_config_db_sync_values},
628+
{"config_db_journal_mode_values", flb_test_config_db_journal_mode_values},
629+
{"config_db_locking_values", flb_test_config_db_locking_values},
630+
#endif
451631
{NULL, NULL}
452632
};
453633

0 commit comments

Comments
 (0)