Skip to content

Commit

Permalink
code-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadalihussnain committed Nov 28, 2024
1 parent 610311e commit 158d51b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
10 changes: 5 additions & 5 deletions cfgmgr/portmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ bool PortMgr::setPortDHCPMitigationRate(const string &alias, const string &dhcp_
cmd_str = cmd.str();
ret = swss::exec(cmd_str, res);
if (ret)
{
// Log the failure and return false to indicate an issue
SWSS_LOG_WARN("Failed to delete ingress qdisc for alias:%s with cmd:%s, rc:%d, error:%s", alias.c_str(), cmd_str.c_str(), ret, res.c_str());
return false;
}
{
// Log the failure and return false to indicate an issue
SWSS_LOG_WARN("Failed to delete ingress qdisc ");
return false;
}
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def _verify_db_contents():
return (True, None)

# Verify that ASIC DB has been fully initialized
init_polling_config = PollingConfig(2, 60, strict=True)
init_polling_config = PollingConfig(2, 40, strict=True)
wait_for_result(_verify_db_contents, init_polling_config)

def _generate_oid_to_interface_mapping(self) -> None:
Expand Down
63 changes: 39 additions & 24 deletions tests/mock_tests/portmgr_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,34 +93,49 @@ namespace portmgr_ut
ASSERT_EQ("up", value_opt.get());
}

// Define a MockPortMgr class to mock necessary methods.
class MockPortMgr : public PortMgr {
public:
MOCK_METHOD(int, exec, (const std::string& cmd_str, std::string& res), (override)); // Mock exec function
MOCK_METHOD(bool, writeConfigToAppDb, (const std::string& alias, const std::string& key, const std::string& value), (override));
MOCK_METHOD(bool, isPortStateOk, (const std::string& alias), (override));
};

TEST_F(PortMgrTest, DeleteIngressQdiscFailure)
{
Table cfg_port_table(m_config_db.get(), CFG_PORT_TABLE_NAME);
Table app_port_table(m_app_db.get(), APP_PORT_TABLE_NAME);
Table state_port_table(m_state_db.get(), STATE_PORT_TABLE_NAME);

// Set port state to ok
state_port_table.set("Ethernet0", {
{"state", "ok"}
});

// Configure the port with dhcp_rate_limit = 0 to trigger qdisc deletion
cfg_port_table.set("Ethernet0", {
{"dhcp_rate_limit", "0"}
});
m_portMgr->addExistingData(&cfg_port_table);

// Mock command execution to simulate failure
mockCallArgs.clear();
mockSetCommandReturnValue("/sbin/tc qdisc del dev \"Ethernet0\" handle ffff: ingress", 1, "Some error message");

// Execute the doTask function
m_portMgr->doTask();

// Verify the log message for the failure
ASSERT_TRUE(mockCallArgs.empty());
// Prepare mock objects
MockPortMgr mockPortMgr;
Table cfg_port_table(m_config_db.get(), CFG_PORT_TABLE_NAME);
Table app_port_table(m_app_db.get(), APP_PORT_TABLE_NAME);
Table state_port_table(m_state_db.get(), STATE_PORT_TABLE_NAME);

// Set port state to ok
state_port_table.set("Ethernet0", {
{"state", "ok"}
});

// Configure the port with dhcp_rate_limit = 0 to trigger qdisc deletion
cfg_port_table.set("Ethernet0", {
{"dhcp_rate_limit", "0"}
});
m_portMgr->addExistingData(&cfg_port_table);

// Prepare to mock command execution to simulate failure
std::string cmd_str;
std::string res;

// Mock the exec method to simulate a command failure (non-zero return code)
EXPECT_CALL(mockPortMgr, exec("/sbin/tc qdisc del dev \"Ethernet0\" handle ffff: ingress", _))
.WillOnce(::testing::DoAll(::testing::SetArgReferee<1>("Some error message"), ::testing::Return(1)));

// Execute the doTask function
m_portMgr->doTask();

// Verify that the command was executed correctly and failure occurred
ASSERT_TRUE(mockCallArgs.empty()); // Verify that the call arguments are as expected after failure
}


TEST_F(PortMgrTest, ConfigureDuringRetry)
{
Table state_port_table(m_state_db.get(), STATE_PORT_TABLE_NAME);
Expand Down

0 comments on commit 158d51b

Please sign in to comment.