@cryptotaxi247 / netdata-1 / commits / e3f153505

Fix tests so that the actual metadata database is not accessed (#13439)

* Add simple ctx_unittest under -W unittest * Skip un needed initialization if running unittests -- make sure the context database is initialized in memory mode * Remove tests (no metadata is available at this point)

Stelios Fragkakis committed Aug 2, 2022 at 18:38 UTC e3f1535053a2a556d9651ac9bb24d69282592b90
3 files changed +24 -64
daemon/main.c
+2
@@ -1008,6 +1008,8 @@ int main(int argc, char **argv) {
1008 return 1;
1009 if (rrdlabels_unittest())
1010 return 1;
1011 + if (ctx_unittest())
1012 + return 1;
1013 fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
1014 return 0;
1015 }
database/rrdhost.c
+10 -5
@@ -768,10 +768,13 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
768 info("Skipping SQLITE metadata initialization since memory mode is not dbengine");
769 }
770
771 - if (unlikely(sql_init_context_database(0))) {
771 + if (unlikely(sql_init_context_database(system_info ? 0 : 1))) {
772 error_report("Failed to initialize context metadata database");
773 }
774
775 + if (unlikely(!system_info))
776 + goto unittest;
777 +
778 #ifdef ENABLE_DBENGINE
779 storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers);
780 if(storage_tiers < 1) {
@@ -891,6 +894,7 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
894 health_init();
895 rrdpush_init();
896
897 +unittest:
898 debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname);
899 rrd_wrlock();
900 localhost = rrdhost_create(
@@ -921,12 +925,13 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) {
925 return 1;
926 }
927
924 - if (likely(system_info))
925 - migrate_localhost(&localhost->host_uuid);
928 rrd_unlock();
927 - sql_aclk_sync_init();
929
929 - web_client_api_v1_management_init();
930 + if (likely(system_info)) {
931 + migrate_localhost(&localhost->host_uuid);
932 + sql_aclk_sync_init();
933 + web_client_api_v1_management_init();
934 + }
935 return localhost==NULL;
936 }
937
database/sqlite/sqlite_context.c
+12 -59
@@ -87,7 +87,11 @@ int sql_init_context_database(int memory)
87 snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version);
88 if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
89
90 - snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir);
90 + if (likely(!memory))
91 + snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir);
92 + else
93 + snprintfz(buf, 1024, "ATTACH DATABASE ':memory:' as meta;");
94 +
95 if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1;
96
97 if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_config[0]))
@@ -436,37 +440,6 @@ skip_delete:
440 //
441 // TESTING FUNCTIONS
442 //
439 -static void dict_ctx_get_label_list_cb(SQL_CLABEL_DATA *label_data_ptr, void *data)
440 -{
441 - (void)data;
442 - SQL_CLABEL_DATA *label_data = label_data_ptr;
443 -
444 - info(" LABEL %d %s = %s", label_data->label_source, label_data->label_key, label_data->label_value);
445 -}
446 -
447 -static void dict_ctx_get_dimension_list_cb(SQL_DIMENSION_DATA *dimension_data_ptr, void *data)
448 -{
449 - (void)data;
450 -
451 - SQL_DIMENSION_DATA *dimension_data = dimension_data_ptr;
452 -
453 - char uuid_str[UUID_STR_LEN];
454 - uuid_unparse_lower(dimension_data->dim_id, uuid_str);
455 -
456 - info(" Dimension %s = %s", uuid_str, dimension_data->id);
457 -}
458 -
459 -
460 -static void dict_ctx_get_chart_list_cb(SQL_CHART_DATA *chart_data, void *data)
461 -{
462 - (void)data;
463 -
464 - char uuid_str[UUID_STR_LEN];
465 - uuid_unparse_lower(chart_data->chart_id, uuid_str);
466 - info("OK GOT %s ID = %s NAME = %s CONTEXT = %s", uuid_str, chart_data->id, chart_data->name, chart_data->context);
467 - ctx_get_label_list(&chart_data->chart_id, dict_ctx_get_label_list_cb, NULL);
468 - ctx_get_dimension_list(&chart_data->chart_id, dict_ctx_get_dimension_list_cb, NULL);
469 -}
443
444 static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, void *data)
445 {
@@ -479,7 +452,7 @@ static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, v
452 "priority = %lu "
453 "first time = %lu "
454 "last time = %lu "
482 - "deleted = %d"
455 + "deleted = %d "
456 "family = %s",
457 context_data->id,
458 context_data->version,
@@ -493,35 +466,15 @@ static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, v
466 context_data->family);
467 }
468
496 -static int localhost_uuid_cb(void *data, int argc, char **argv, char **column)
497 -{
498 - uuid_t *uuid = data;
499 - UNUSED(argc);
500 - UNUSED(column);
501 - uuid_copy(*uuid, * (uuid_t *) argv[0]);
502 - return 0;
503 -}
504 -
505 -
506 -#define SQL_FIND_LOCALHOST "SELECT host_id FROM meta.host WHERE hops = 0;"
469 int ctx_unittest(void)
470 {
471 uuid_t host_uuid;
510 - uuid_t host_uuid1;
511 -
512 - uuid_generate(host_uuid1);
472 + uuid_generate(host_uuid);
473
514 - char *err_msg;
474 + int rc = sql_init_context_database(1);
475
516 - sql_init_context_database(1);
517 -
518 - int rc = sqlite3_exec(db_context_meta, SQL_FIND_LOCALHOST, localhost_uuid_cb, (void *) &host_uuid, &err_msg);
519 - if (rc != SQLITE_OK) {
520 - info("Failed to discover localhost UUID rc = %d -- %s", rc, err_msg);
521 - sqlite3_free(err_msg);
522 - }
523 -
524 - ctx_get_chart_list(&host_uuid, dict_ctx_get_chart_list_cb, NULL);
476 + if (rc != SQLITE_OK)
477 + return 1;
478
479 // Store a context
480 VERSIONED_CONTEXT_DATA context_data;
@@ -542,7 +495,7 @@ int ctx_unittest(void)
495 else
496 info("Entry %s not inserted", context_data.id);
497
545 - if (likely(!ctx_store_context(&host_uuid1, &context_data)))
498 + if (likely(!ctx_store_context(&host_uuid, &context_data)))
499 info("Entry %s inserted", context_data.id);
500 else
501 info("Entry %s not inserted", context_data.id);
@@ -572,7 +525,7 @@ int ctx_unittest(void)
525 info("List context end after insert");
526
527 info("List context start after insert");
575 - ctx_get_context_list(&host_uuid1, dict_ctx_get_context_list_cb, NULL);
528 + ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL);
529 info("List context end after insert");
530
531 // This will delete the entry