Skip to content

Commit 4bd3d49

Browse files
dzhangalibabaabdosi
authored andcommitted
[MultiDB]: throwing exception when database_config.json doesn't exist (#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
1 parent a4a1e10 commit 4bd3d49

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

common/dbconnector.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <system_error>
77
#include <fstream>
88
#include "json.hpp"
9+
#include "logger.h"
910

1011
#include "common/dbconnector.h"
1112
#include "common/redisreply.h"
@@ -17,8 +18,14 @@ namespace swss {
1718

1819
void SonicDBConfig::initialize(const string &file)
1920
{
21+
22+
SWSS_LOG_ENTER();
23+
2024
if (m_init)
25+
{
26+
SWSS_LOG_ERROR("SonicDBConfig already initialized");
2127
throw runtime_error("SonicDBConfig already initialized");
28+
}
2229

2330
ifstream i(file);
2431
if (i.good())
@@ -47,48 +54,55 @@ void SonicDBConfig::initialize(const string &file)
4754
}
4855
catch (domain_error& e)
4956
{
57+
SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
5058
throw runtime_error("key doesn't exist in json object, NULL value has no iterator >> " + string(e.what()));
5159
}
5260
catch (exception &e)
5361
{
62+
SWSS_LOG_ERROR("Sonic database config file syntax error >> %s\n", e.what());
5463
throw runtime_error("Sonic database config file syntax error >> " + string(e.what()));
5564
}
5665
}
66+
else
67+
{
68+
SWSS_LOG_ERROR("Sonic database config file doesn't exist at %s\n", file.c_str());
69+
throw runtime_error("Sonic database config file doesn't exist at " + file);
70+
}
5771
}
5872

5973
string SonicDBConfig::getDbInst(const string &dbName)
6074
{
6175
if (!m_init)
6276
initialize();
63-
return m_db_info[dbName].first;
77+
return m_db_info.at(dbName).first;
6478
}
6579

6680
int SonicDBConfig::getDbId(const string &dbName)
6781
{
6882
if (!m_init)
6983
initialize();
70-
return m_db_info[dbName].second;
84+
return m_db_info.at(dbName).second;
7185
}
7286

7387
string SonicDBConfig::getDbSock(const string &dbName)
7488
{
7589
if (!m_init)
7690
initialize();
77-
return m_inst_info[getDbInst(dbName)].first;
91+
return m_inst_info.at(getDbInst(dbName)).first;
7892
}
7993

8094
string SonicDBConfig::getDbHostname(const string &dbName)
8195
{
8296
if (!m_init)
8397
initialize();
84-
return m_inst_info[getDbInst(dbName)].second.first;
98+
return m_inst_info.at(getDbInst(dbName)).second.first;
8599
}
86100

87101
int SonicDBConfig::getDbPort(const string &dbName)
88102
{
89103
if (!m_init)
90104
initialize();
91-
return m_inst_info[getDbInst(dbName)].second.second;
105+
return m_inst_info.at(getDbInst(dbName)).second.second;
92106
}
93107

94108
constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE;

tests/redis_multi_db_ut.cpp

+46-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,56 @@ using json = nlohmann::json;
1313
TEST(DBConnector, multi_db_test)
1414
{
1515
string file = "./tests/redis_multi_db_ut_config/database_config.json";
16+
string nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json";
17+
1618
// by default , init should be false
1719
cout<<"Default : isInit = "<<SonicDBConfig::isInit()<<endl;
18-
EXPECT_EQ(SonicDBConfig::isInit(), false);
20+
EXPECT_FALSE(SonicDBConfig::isInit());
21+
22+
23+
// load nonexisting file, should throw exception with NO file existing
24+
try
25+
{
26+
cout<<"INIT: loading nonexisting db config file"<<endl;
27+
SonicDBConfig::initialize(nonexisting_file);
28+
}
29+
catch (exception &e)
30+
{
31+
EXPECT_TRUE(strstr(e.what(), "Sonic database config file doesn't exist"));
32+
}
33+
EXPECT_FALSE(SonicDBConfig::isInit());
34+
1935
// load local config file, init should be true
2036
SonicDBConfig::initialize(file);
21-
cout<<"INIT : load local db config file, isInit = "<<SonicDBConfig::isInit()<<endl;
22-
EXPECT_EQ(SonicDBConfig::isInit(), true);
37+
cout<<"INIT: load local db config file, isInit = "<<SonicDBConfig::isInit()<<endl;
38+
EXPECT_TRUE(SonicDBConfig::isInit());
39+
40+
// load local config file again, should throw exception with init already done
41+
try
42+
{
43+
cout<<"INIT: loading local config file again"<<endl;
44+
SonicDBConfig::initialize(file);
45+
}
46+
catch (exception &e)
47+
{
48+
EXPECT_TRUE(strstr(e.what(), "SonicDBConfig already initialized"));
49+
}
50+
51+
//Invalid DB name input
52+
cout<<"GET: invalid dbname input for getDbInst()"<<endl;
53+
EXPECT_THROW(SonicDBConfig::getDbInst("INVALID_DBNAME"), out_of_range);
54+
55+
cout<<"GET: invalid dbname input for getDbId()"<<endl;
56+
EXPECT_THROW(SonicDBConfig::getDbId("INVALID_DBNAME"), out_of_range);
57+
58+
cout<<"GET: invalid dbname input for getDbSock()"<<endl;
59+
EXPECT_THROW(SonicDBConfig::getDbSock("INVALID_DBNAME"), out_of_range);
60+
61+
cout<<"GET: invalid dbname input for getDbHostname()"<<endl;
62+
EXPECT_THROW(SonicDBConfig::getDbHostname("INVALID_DBNAME"), out_of_range);
63+
64+
cout<<"GET: invalid dbname input for getDbPort()"<<endl;
65+
EXPECT_THROW(SonicDBConfig::getDbPort("INVALID_DBNAME"), out_of_range);
2366

2467
// parse config file
2568
ifstream i(file);

0 commit comments

Comments
 (0)