From 9993672c5df78952e75a02bd294c9196c97e8693 Mon Sep 17 00:00:00 2001 From: dzhang Date: Mon, 2 Dec 2019 17:01:03 -0800 Subject: [PATCH 1/6] [MultiDB]: throwing exception when database_config.json doesn't exist and using at() to get map entry --- common/dbconnector.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index d4969c458..3854cb3b8 100644 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -53,6 +53,8 @@ void SonicDBConfig::initialize(const string &file) { throw runtime_error("Sonic database config file syntax error >> " + string(e.what())); } + } else { + throw runtime_error("Sonic database config file doesn't exist at " + string(DEFAULT_SONIC_DB_CONFIG_FILE)); } } @@ -60,35 +62,35 @@ string SonicDBConfig::getDbInst(const string &dbName) { if (!m_init) initialize(); - return m_db_info[dbName].first; + return m_db_info.at(dbName).first; } int SonicDBConfig::getDbId(const string &dbName) { if (!m_init) initialize(); - return m_db_info[dbName].second; + return m_db_info.at(dbName).second; } string SonicDBConfig::getDbSock(const string &dbName) { if (!m_init) initialize(); - return m_inst_info[getDbInst(dbName)].first; + return m_inst_info.at(getDbInst(dbName)).first; } string SonicDBConfig::getDbHostname(const string &dbName) { if (!m_init) initialize(); - return m_inst_info[getDbInst(dbName)].second.first; + return m_inst_info.at(getDbInst(dbName)).second.first; } int SonicDBConfig::getDbPort(const string &dbName) { if (!m_init) initialize(); - return m_inst_info[getDbInst(dbName)].second.second; + return m_inst_info.at(getDbInst(dbName)).second.second; } constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE; From 727a538f30a3c17a6ec6704b2c331a03cb1912da Mon Sep 17 00:00:00 2001 From: dzhang Date: Mon, 2 Dec 2019 17:04:26 -0800 Subject: [PATCH 2/6] update file name --- common/dbconnector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index 3854cb3b8..d6b93a6d7 100644 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -54,7 +54,7 @@ void SonicDBConfig::initialize(const string &file) throw runtime_error("Sonic database config file syntax error >> " + string(e.what())); } } else { - throw runtime_error("Sonic database config file doesn't exist at " + string(DEFAULT_SONIC_DB_CONFIG_FILE)); + throw runtime_error("Sonic database config file doesn't exist at " + file); } } From cf3fac9cc855bcfe3a5483589333272819763acb Mon Sep 17 00:00:00 2001 From: dzhang Date: Mon, 2 Dec 2019 17:36:29 -0800 Subject: [PATCH 3/6] address code format --- common/dbconnector.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index d6b93a6d7..e0bc0f61b 100644 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -53,7 +53,9 @@ void SonicDBConfig::initialize(const string &file) { throw runtime_error("Sonic database config file syntax error >> " + string(e.what())); } - } else { + } + else + { throw runtime_error("Sonic database config file doesn't exist at " + file); } } From 5c18e595563d105b39c9734dcc4d946973b3630d Mon Sep 17 00:00:00 2001 From: dzhang Date: Tue, 3 Dec 2019 16:30:27 -0800 Subject: [PATCH 4/6] add UT and logs --- common/dbconnector.cpp | 10 ++++++++ tests/redis_multi_db_ut.cpp | 47 +++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index e0bc0f61b..d5b08eeba 100644 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -6,6 +6,7 @@ #include #include #include "json.hpp" +#include "logger.h" #include "common/dbconnector.h" #include "common/redisreply.h" @@ -17,8 +18,14 @@ namespace swss { void SonicDBConfig::initialize(const string &file) { + + SWSS_LOG_ENTER(); + if (m_init) + { + SWSS_LOG_ERROR("SonicDBConfig already initialized"); throw runtime_error("SonicDBConfig already initialized"); + } ifstream i(file); if (i.good()) @@ -47,15 +54,18 @@ void SonicDBConfig::initialize(const string &file) } catch (domain_error& e) { + SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what()); throw runtime_error("key doesn't exist in json object, NULL value has no iterator >> " + string(e.what())); } catch (exception &e) { + SWSS_LOG_ERROR("Sonic database config file syntax error >> %s\n", e.what()); throw runtime_error("Sonic database config file syntax error >> " + string(e.what())); } } else { + SWSS_LOG_ERROR("Sonic database config file doesn't exist at %s\n", file.c_str()); throw runtime_error("Sonic database config file doesn't exist at " + file); } } diff --git a/tests/redis_multi_db_ut.cpp b/tests/redis_multi_db_ut.cpp index 69029b276..1488efaa4 100644 --- a/tests/redis_multi_db_ut.cpp +++ b/tests/redis_multi_db_ut.cpp @@ -13,13 +13,56 @@ using json = nlohmann::json; TEST(DBConnector, multi_db_test) { string file = "./tests/redis_multi_db_ut_config/database_config.json"; + string nonexist_file = "./tests/redis_multi_db_ut_config/database_config_noexist.json"; + // by default , init should be false cout<<"Default : isInit = "< Date: Wed, 4 Dec 2019 13:34:26 -0800 Subject: [PATCH 5/6] fix typo --- tests/redis_multi_db_ut.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/redis_multi_db_ut.cpp b/tests/redis_multi_db_ut.cpp index 1488efaa4..91df21250 100644 --- a/tests/redis_multi_db_ut.cpp +++ b/tests/redis_multi_db_ut.cpp @@ -13,18 +13,18 @@ using json = nlohmann::json; TEST(DBConnector, multi_db_test) { string file = "./tests/redis_multi_db_ut_config/database_config.json"; - string nonexist_file = "./tests/redis_multi_db_ut_config/database_config_noexist.json"; + string nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json"; // by default , init should be false cout<<"Default : isInit = "< Date: Wed, 4 Dec 2019 13:42:58 -0800 Subject: [PATCH 6/6] fix typo --- tests/redis_multi_db_ut.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/redis_multi_db_ut.cpp b/tests/redis_multi_db_ut.cpp index 91df21250..9698526ee 100644 --- a/tests/redis_multi_db_ut.cpp +++ b/tests/redis_multi_db_ut.cpp @@ -34,7 +34,7 @@ TEST(DBConnector, multi_db_test) // load local config file, init should be true SonicDBConfig::initialize(file); - cout<<"INIT : load local db config file, isInit = "<