diff options
author | Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> | 2022-08-02 18:38:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-02 18:38:42 +0300 |
commit | e3f1535053a2a556d9651ac9bb24d69282592b90 (patch) | |
tree | 86bae97b7c7b6c55a1ffb5349a655e4b049530d4 | |
parent | 6d2fd8886124fe64534636f2c1093bd19a06701b (diff) |
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)
-rw-r--r-- | daemon/main.c | 2 | ||||
-rw-r--r-- | database/rrdhost.c | 15 | ||||
-rw-r--r-- | database/sqlite/sqlite_context.c | 71 |
3 files changed, 24 insertions, 64 deletions
diff --git a/daemon/main.c b/daemon/main.c index a2317c1b72..ada3c14f2a 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -1008,6 +1008,8 @@ int main(int argc, char **argv) { return 1; if (rrdlabels_unittest()) return 1; + if (ctx_unittest()) + return 1; fprintf(stderr, "\n\nALL TESTS PASSED\n\n"); return 0; } diff --git a/database/rrdhost.c b/database/rrdhost.c index 19997af3be..7f4bd95ba6 100644 --- a/database/rrdhost.c +++ b/database/rrdhost.c @@ -768,10 +768,13 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) { info("Skipping SQLITE metadata initialization since memory mode is not dbengine"); } - if (unlikely(sql_init_context_database(0))) { + if (unlikely(sql_init_context_database(system_info ? 0 : 1))) { error_report("Failed to initialize context metadata database"); } + if (unlikely(!system_info)) + goto unittest; + #ifdef ENABLE_DBENGINE storage_tiers = config_get_number(CONFIG_SECTION_DB, "storage tiers", storage_tiers); if(storage_tiers < 1) { @@ -891,6 +894,7 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) { health_init(); rrdpush_init(); +unittest: debug(D_RRDHOST, "Initializing localhost with hostname '%s'", hostname); rrd_wrlock(); localhost = rrdhost_create( @@ -921,12 +925,13 @@ int rrd_init(char *hostname, struct rrdhost_system_info *system_info) { return 1; } - if (likely(system_info)) - migrate_localhost(&localhost->host_uuid); rrd_unlock(); - sql_aclk_sync_init(); - web_client_api_v1_management_init(); + if (likely(system_info)) { + migrate_localhost(&localhost->host_uuid); + sql_aclk_sync_init(); + web_client_api_v1_management_init(); + } return localhost==NULL; } diff --git a/database/sqlite/sqlite_context.c b/database/sqlite/sqlite_context.c index 8d2fa2a087..88818579ba 100644 --- a/database/sqlite/sqlite_context.c +++ b/database/sqlite/sqlite_context.c @@ -87,7 +87,11 @@ int sql_init_context_database(int memory) snprintfz(buf, 1024, "PRAGMA user_version=%d;", target_version); if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1; - snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir); + if (likely(!memory)) + snprintfz(buf, 1024, "ATTACH DATABASE \"%s/netdata-meta.db\" as meta;", netdata_configured_cache_dir); + else + snprintfz(buf, 1024, "ATTACH DATABASE ':memory:' as meta;"); + if(init_database_batch(db_context_meta, DB_CHECK_NONE, 0, list)) return 1; if (init_database_batch(db_context_meta, DB_CHECK_NONE, 0, &database_context_config[0])) @@ -436,37 +440,6 @@ skip_delete: // // TESTING FUNCTIONS // -static void dict_ctx_get_label_list_cb(SQL_CLABEL_DATA *label_data_ptr, void *data) -{ - (void)data; - SQL_CLABEL_DATA *label_data = label_data_ptr; - - info(" LABEL %d %s = %s", label_data->label_source, label_data->label_key, label_data->label_value); -} - -static void dict_ctx_get_dimension_list_cb(SQL_DIMENSION_DATA *dimension_data_ptr, void *data) -{ - (void)data; - - SQL_DIMENSION_DATA *dimension_data = dimension_data_ptr; - - char uuid_str[UUID_STR_LEN]; - uuid_unparse_lower(dimension_data->dim_id, uuid_str); - - info(" Dimension %s = %s", uuid_str, dimension_data->id); -} - - -static void dict_ctx_get_chart_list_cb(SQL_CHART_DATA *chart_data, void *data) -{ - (void)data; - - char uuid_str[UUID_STR_LEN]; - uuid_unparse_lower(chart_data->chart_id, uuid_str); - info("OK GOT %s ID = %s NAME = %s CONTEXT = %s", uuid_str, chart_data->id, chart_data->name, chart_data->context); - ctx_get_label_list(&chart_data->chart_id, dict_ctx_get_label_list_cb, NULL); - ctx_get_dimension_list(&chart_data->chart_id, dict_ctx_get_dimension_list_cb, NULL); -} static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, void *data) { @@ -479,7 +452,7 @@ static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, v "priority = %lu " "first time = %lu " "last time = %lu " - "deleted = %d" + "deleted = %d " "family = %s", context_data->id, context_data->version, @@ -493,35 +466,15 @@ static void dict_ctx_get_context_list_cb(VERSIONED_CONTEXT_DATA *context_data, v context_data->family); } -static int localhost_uuid_cb(void *data, int argc, char **argv, char **column) -{ - uuid_t *uuid = data; - UNUSED(argc); - UNUSED(column); - uuid_copy(*uuid, * (uuid_t *) argv[0]); - return 0; -} - - -#define SQL_FIND_LOCALHOST "SELECT host_id FROM meta.host WHERE hops = 0;" int ctx_unittest(void) { uuid_t host_uuid; - uuid_t host_uuid1; - - uuid_generate(host_uuid1); + uuid_generate(host_uuid); - char *err_msg; + int rc = sql_init_context_database(1); - sql_init_context_database(1); - - int rc = sqlite3_exec(db_context_meta, SQL_FIND_LOCALHOST, localhost_uuid_cb, (void *) &host_uuid, &err_msg); - if (rc != SQLITE_OK) { - info("Failed to discover localhost UUID rc = %d -- %s", rc, err_msg); - sqlite3_free(err_msg); - } - - ctx_get_chart_list(&host_uuid, dict_ctx_get_chart_list_cb, NULL); + if (rc != SQLITE_OK) + return 1; // Store a context VERSIONED_CONTEXT_DATA context_data; @@ -542,7 +495,7 @@ int ctx_unittest(void) else info("Entry %s not inserted", context_data.id); - if (likely(!ctx_store_context(&host_uuid1, &context_data))) + if (likely(!ctx_store_context(&host_uuid, &context_data))) info("Entry %s inserted", context_data.id); else info("Entry %s not inserted", context_data.id); @@ -572,7 +525,7 @@ int ctx_unittest(void) info("List context end after insert"); info("List context start after insert"); - ctx_get_context_list(&host_uuid1, dict_ctx_get_context_list_cb, NULL); + ctx_get_context_list(&host_uuid, dict_ctx_get_context_list_cb, NULL); info("List context end after insert"); // This will delete the entry |