Skip to content

Commit

Permalink
[sonic-db-cli] Fix sonic-db-cli crash when database config file not r…
Browse files Browse the repository at this point in the history
…eady issue. (#701)

#### Why I did it
Fix sonic-db-cli  PING/SAVE/FLUSHALL command crash when database config file not ready issue:
sonic-net/sonic-buildimage#12047

#### How I did it
When run PING/SAVE/FLUSHALL command, catch database initialize failed exception and return 1.

#### How to verify it
Pass all existing UT and E2E test.
Add new UT to cover changed code.
Manually test, sonic-db-cli will return 1 when run PING command and can't find config file:

azureuser@a7f66d2b794c:/sonic/src/sonic-swss-common$ ./sonic-db-cli/sonic-db-cli PING
An exception of type Sonic database config file doesn't exist at /var/run/redis/sonic-db/database_config.json occurred. Arguments:
/sonic/src/sonic-swss-common/sonic-db-cli/.libs/sonic-db-cli PING
azureuser@a7f66d2b794c:/sonic/src/sonic-swss-common$ echo $?
1


#### Which release branch to backport (provide reason below if selected)

<!--
- Note we only backport fixes to a release branch, *not* features!
- Please also provide a reason for the backporting below.
- e.g.
- [x] 202006
-->

- [ ] 201811
- [ ] 201911
- [ ] 202006
- [ ] 202012
- [ ] 202106
- [x] 202111
- [x] 202205

#### Description for the changelog
Fix sonic-db-cli  PING/SAVE/FLUSHALL command crash when database config file not ready issue.

#### Link to config_db schema for YANG module changes
<!--
Provide a link to config_db schema for the table for which YANG model
is defined
Link should point to correct section on https://github.com/Azure/SONiC/wiki/Configuration.
-->

#### A picture of a cute animal (not mandatory but encouraged)
  • Loading branch information
liuh-80 authored Oct 31, 2022
1 parent abda263 commit 276f47c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
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

0 comments on commit 276f47c

Please sign in to comment.