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

[sonic-db-cli] Fix sonic-db-cli crash when database config file not ready issue. #701

Merged
merged 6 commits into from
Oct 31, 2022
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
14 changes: 12 additions & 2 deletions sonic-db-cli/sonic-db-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ int sonic_db_cli(
}
else
{
// SonicDBConfig may initialized when run cli with UT
initializeConfig();
// Use the tcp connectivity if namespace is local and unixsocket cmd_option is present.
isTcpConn = true;
netns = "";
Expand All @@ -297,6 +295,12 @@ int sonic_db_cli(
if (options.m_cmd.size() != 0)
{
auto commands = options.m_cmd;

if (netns.empty())
{
initializeConfig();
}

return executeCommands(dbOrOperation, commands, netns, isTcpConn);
}
else if (dbOrOperation == "PING"
Expand All @@ -307,6 +311,12 @@ int sonic_db_cli(
// sonic-db-cli catch all possible exceptions and handle it as a failure case which not return 'OK' or 'PONG'
try
{
if (netns.empty())
{
// When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
initializeConfig();
}

return handleAllInstances(netns, dbOrOperation, isTcpConn);
}
catch (const exception& e)
Expand Down
50 changes: 50 additions & 0 deletions tests/cli_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using namespace swss;
using namespace std;

const string not_exist_config_file = "./tests/redis_multi_db_ut_config/database_config_not_exist.json";
const string config_file = "./tests/redis_multi_db_ut_config/database_config.json";
const string global_config_file = "./tests/redis_multi_db_ut_config/database_global.json";

Expand Down Expand Up @@ -279,6 +280,55 @@ TEST(sonic_db_cli, test_cli_ping_cmd)
EXPECT_EQ("True\n", output);
}

TEST(sonic_db_cli, test_cli_ping_cmd_no_config)
{
char *args[3];
args[0] = "sonic-db-cli";
args[1] = "PING";

// data base file does not exist, will throw exception
auto initializeGlobalConfig = []()
{
SonicDBConfig::initializeGlobalConfig(not_exist_config_file);
};

auto initializeConfig = []()
{
SonicDBConfig::initialize(not_exist_config_file);
};

optind = 0;
int exit_code = sonic_db_cli(
2,
args,
initializeGlobalConfig,
initializeConfig);

EXPECT_EQ(1, exit_code);

// When ping with DB name, exception will happen
args[0] = "sonic-db-cli";
args[1] = "TEST_DB";
args[2] = "PING";

bool exception_happen = false;
try
{
optind = 0;
exit_code = sonic_db_cli(
3,
args,
initializeGlobalConfig,
initializeConfig);
}
catch (const exception& e)
{
exception_happen = true;
}

EXPECT_EQ(true, exception_happen);
}

TEST(sonic_db_cli, test_cli_save_cmd)
{
char *args[2];
Expand Down