Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MultiDB]: throwing exception when database_config.json doesn't exist #319

Merged
merged 6 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDbInst [](start = 22, length = 9)

Add a unit test case for non existing dbName #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE. Added all five API test cases.

{
if (!m_init)
initialize();
return m_db_info[dbName].first;
return m_db_info.at(dbName).first;
}

int SonicDBConfig::getDbId(const string &dbName)
Copy link
Contributor

@qiluo-msft qiluo-msft Dec 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDbId [](start = 19, length = 7)

Add a unit test case for non existing dbName #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE. Added all five API test cases.

{
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