Skip to content

Commit

Permalink
[Copp]Refactor coppmgr tests (sonic-net#3093)
Browse files Browse the repository at this point in the history
What I did
Refactoring coppmgr mock tests

Why I did it
After migration to bookworm, coppmgr tests started failing due to the use of sudo commands.
  • Loading branch information
dgsudharsan authored Mar 27, 2024
1 parent a464729 commit 80e0b57
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 30 deletions.
10 changes: 6 additions & 4 deletions cfgmgr/coppmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ static set<string> g_copp_init_set;

void CoppMgr::parseInitFile(void)
{
std::ifstream ifs(COPP_INIT_FILE);
std::ifstream ifs(m_coppCfgfile);

if (ifs.fail())
{
SWSS_LOG_ERROR("COPP init file %s not found", COPP_INIT_FILE);
SWSS_LOG_ERROR("COPP init file %s not found", m_coppCfgfile.c_str());
return;
}
json j = json::parse(ifs);
Expand Down Expand Up @@ -293,15 +294,16 @@ bool CoppMgr::isDupEntry(const std::string &key, std::vector<FieldValueTuple> &f
return true;
}

CoppMgr::CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) :
CoppMgr::CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames, const string copp_init_file) :
Orch(cfgDb, tableNames),
m_cfgCoppTrapTable(cfgDb, CFG_COPP_TRAP_TABLE_NAME),
m_cfgCoppGroupTable(cfgDb, CFG_COPP_GROUP_TABLE_NAME),
m_cfgFeatureTable(cfgDb, CFG_FEATURE_TABLE_NAME),
m_appCoppTable(appDb, APP_COPP_TABLE_NAME),
m_stateCoppTrapTable(stateDb, STATE_COPP_TRAP_TABLE_NAME),
m_stateCoppGroupTable(stateDb, STATE_COPP_GROUP_TABLE_NAME),
m_coppTable(appDb, APP_COPP_TABLE_NAME)
m_coppTable(appDb, APP_COPP_TABLE_NAME),
m_coppCfgfile(copp_init_file)
{
SWSS_LOG_ENTER();
parseInitFile();
Expand Down
3 changes: 2 additions & 1 deletion cfgmgr/coppmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class CoppMgr : public Orch
{
public:
CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb,
const std::vector<std::string> &tableNames);
const std::vector<std::string> &tableNames, const std::string copp_init_file = COPP_INIT_FILE);

using Orch::doTask;
private:
Expand All @@ -75,6 +75,7 @@ class CoppMgr : public Orch
CoppCfg m_coppGroupInitCfg;
CoppCfg m_coppTrapInitCfg;
CoppCfg m_featuresCfgTable;
std::string m_coppCfgfile;


void doTask(Consumer &consumer);
Expand Down
3 changes: 2 additions & 1 deletion tests/mock_tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ tests_SOURCES = aclorch_ut.cpp \
$(top_srcdir)/orchagent/dash/dashvnetorch.cpp \
$(top_srcdir)/cfgmgr/buffermgrdyn.cpp \
$(top_srcdir)/warmrestart/warmRestartAssist.cpp \
$(top_srcdir)/orchagent/dash/pbutils.cpp
$(top_srcdir)/orchagent/dash/pbutils.cpp \
$(top_srcdir)/cfgmgr/coppmgr.cpp

tests_SOURCES += $(FLEX_CTR_DIR)/flex_counter_manager.cpp $(FLEX_CTR_DIR)/flex_counter_stat_manager.cpp $(FLEX_CTR_DIR)/flow_counter_handler.cpp $(FLEX_CTR_DIR)/flowcounterrouteorch.cpp
tests_SOURCES += $(DEBUG_CTR_DIR)/debug_counter.cpp $(DEBUG_CTR_DIR)/drop_counter.cpp
Expand Down
26 changes: 2 additions & 24 deletions tests/mock_tests/copp_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,14 @@
#include "warm_restart.h"
#include "ut_helper.h"
#include "coppmgr.h"
#include "coppmgr.cpp"
#include <fstream>
#include <streambuf>

using namespace std;
using namespace swss;

void create_init_file()
{
int status = system("sudo mkdir /etc/sonic/");
ASSERT_EQ(status, 0);

status = system("sudo chmod 777 /etc/sonic/");
ASSERT_EQ(status, 0);

status = system("sudo cp copp_cfg.json /etc/sonic/");
ASSERT_EQ(status, 0);
}

void cleanup()
{
int status = system("sudo rm -rf /etc/sonic/");
ASSERT_EQ(status, 0);
}

TEST(CoppMgrTest, CoppTest)
{
create_init_file();

const vector<string> cfg_copp_tables = {
CFG_COPP_TRAP_TABLE_NAME,
CFG_COPP_GROUP_TABLE_NAME,
Expand Down Expand Up @@ -65,12 +45,10 @@ TEST(CoppMgrTest, CoppTest)
{"trap_ids", "ip2me"}
});

CoppMgr coppmgr(&cfgDb, &appDb, &stateDb, cfg_copp_tables);
CoppMgr coppmgr(&cfgDb, &appDb, &stateDb, cfg_copp_tables, "./copp_cfg.json");

string overide_val;
coppTable.hget("queue1_group1", "cbs",overide_val);
EXPECT_EQ( overide_val, "6000");

cleanup();
}

0 comments on commit 80e0b57

Please sign in to comment.