Skip to content

Commit

Permalink
[MultiDB]: throwing exception when database_config.json doesn't exist (
Browse files Browse the repository at this point in the history
…#319)

* [MultiDB]: throwing exception when database_config.json doesn't exist and using at() to get map entry
* update file name
* address code format
* add UT and logs
* fix typo
  • Loading branch information
dzhangalibaba authored and qiluo-msft committed Dec 4, 2019
1 parent ccd3db8 commit eb01b68
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
24 changes: 19 additions & 5 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <system_error>
#include <fstream>
#include "json.hpp"
#include "logger.h"

#include "common/dbconnector.h"
#include "common/redisreply.h"
Expand All @@ -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())
Expand Down Expand Up @@ -47,48 +54,55 @@ 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);
}
}

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;
Expand Down
49 changes: 46 additions & 3 deletions tests/redis_multi_db_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json";

// by default , init should be false
cout<<"Default : isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_EQ(SonicDBConfig::isInit(), false);
EXPECT_FALSE(SonicDBConfig::isInit());


// load nonexisting file, should throw exception with NO file existing
try
{
cout<<"INIT: loading nonexisting db config file"<<endl;
SonicDBConfig::initialize(nonexisting_file);
}
catch (exception &e)
{
EXPECT_TRUE(strstr(e.what(), "Sonic database config file doesn't exist"));
}
EXPECT_FALSE(SonicDBConfig::isInit());

// load local config file, init should be true
SonicDBConfig::initialize(file);
cout<<"INIT : load local db config file, isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_EQ(SonicDBConfig::isInit(), true);
cout<<"INIT: load local db config file, isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_TRUE(SonicDBConfig::isInit());

// load local config file again, should throw exception with init already done
try
{
cout<<"INIT: loading local config file again"<<endl;
SonicDBConfig::initialize(file);
}
catch (exception &e)
{
EXPECT_TRUE(strstr(e.what(), "SonicDBConfig already initialized"));
}

//Invalid DB name input
cout<<"GET: invalid dbname input for getDbInst()"<<endl;
EXPECT_THROW(SonicDBConfig::getDbInst("INVALID_DBNAME"), out_of_range);

cout<<"GET: invalid dbname input for getDbId()"<<endl;
EXPECT_THROW(SonicDBConfig::getDbId("INVALID_DBNAME"), out_of_range);

cout<<"GET: invalid dbname input for getDbSock()"<<endl;
EXPECT_THROW(SonicDBConfig::getDbSock("INVALID_DBNAME"), out_of_range);

cout<<"GET: invalid dbname input for getDbHostname()"<<endl;
EXPECT_THROW(SonicDBConfig::getDbHostname("INVALID_DBNAME"), out_of_range);

cout<<"GET: invalid dbname input for getDbPort()"<<endl;
EXPECT_THROW(SonicDBConfig::getDbPort("INVALID_DBNAME"), out_of_range);

// parse config file
ifstream i(file);
Expand Down

0 comments on commit eb01b68

Please sign in to comment.