Skip to content

Commit 80e0b57

Browse files
authored
[Copp]Refactor coppmgr tests (#3093)
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.
1 parent a464729 commit 80e0b57

File tree

4 files changed

+12
-30
lines changed

4 files changed

+12
-30
lines changed

cfgmgr/coppmgr.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ static set<string> g_copp_init_set;
2121

2222
void CoppMgr::parseInitFile(void)
2323
{
24-
std::ifstream ifs(COPP_INIT_FILE);
24+
std::ifstream ifs(m_coppCfgfile);
25+
2526
if (ifs.fail())
2627
{
27-
SWSS_LOG_ERROR("COPP init file %s not found", COPP_INIT_FILE);
28+
SWSS_LOG_ERROR("COPP init file %s not found", m_coppCfgfile.c_str());
2829
return;
2930
}
3031
json j = json::parse(ifs);
@@ -293,15 +294,16 @@ bool CoppMgr::isDupEntry(const std::string &key, std::vector<FieldValueTuple> &f
293294
return true;
294295
}
295296

296-
CoppMgr::CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames) :
297+
CoppMgr::CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, const vector<string> &tableNames, const string copp_init_file) :
297298
Orch(cfgDb, tableNames),
298299
m_cfgCoppTrapTable(cfgDb, CFG_COPP_TRAP_TABLE_NAME),
299300
m_cfgCoppGroupTable(cfgDb, CFG_COPP_GROUP_TABLE_NAME),
300301
m_cfgFeatureTable(cfgDb, CFG_FEATURE_TABLE_NAME),
301302
m_appCoppTable(appDb, APP_COPP_TABLE_NAME),
302303
m_stateCoppTrapTable(stateDb, STATE_COPP_TRAP_TABLE_NAME),
303304
m_stateCoppGroupTable(stateDb, STATE_COPP_GROUP_TABLE_NAME),
304-
m_coppTable(appDb, APP_COPP_TABLE_NAME)
305+
m_coppTable(appDb, APP_COPP_TABLE_NAME),
306+
m_coppCfgfile(copp_init_file)
305307
{
306308
SWSS_LOG_ENTER();
307309
parseInitFile();

cfgmgr/coppmgr.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CoppMgr : public Orch
6262
{
6363
public:
6464
CoppMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb,
65-
const std::vector<std::string> &tableNames);
65+
const std::vector<std::string> &tableNames, const std::string copp_init_file = COPP_INIT_FILE);
6666

6767
using Orch::doTask;
6868
private:
@@ -75,6 +75,7 @@ class CoppMgr : public Orch
7575
CoppCfg m_coppGroupInitCfg;
7676
CoppCfg m_coppTrapInitCfg;
7777
CoppCfg m_featuresCfgTable;
78+
std::string m_coppCfgfile;
7879

7980

8081
void doTask(Consumer &consumer);

tests/mock_tests/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ tests_SOURCES = aclorch_ut.cpp \
130130
$(top_srcdir)/orchagent/dash/dashvnetorch.cpp \
131131
$(top_srcdir)/cfgmgr/buffermgrdyn.cpp \
132132
$(top_srcdir)/warmrestart/warmRestartAssist.cpp \
133-
$(top_srcdir)/orchagent/dash/pbutils.cpp
133+
$(top_srcdir)/orchagent/dash/pbutils.cpp \
134+
$(top_srcdir)/cfgmgr/coppmgr.cpp
134135

135136
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
136137
tests_SOURCES += $(DEBUG_CTR_DIR)/debug_counter.cpp $(DEBUG_CTR_DIR)/drop_counter.cpp

tests/mock_tests/copp_ut.cpp

+2-24
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,14 @@
44
#include "warm_restart.h"
55
#include "ut_helper.h"
66
#include "coppmgr.h"
7-
#include "coppmgr.cpp"
87
#include <fstream>
98
#include <streambuf>
9+
1010
using namespace std;
1111
using namespace swss;
1212

13-
void create_init_file()
14-
{
15-
int status = system("sudo mkdir /etc/sonic/");
16-
ASSERT_EQ(status, 0);
17-
18-
status = system("sudo chmod 777 /etc/sonic/");
19-
ASSERT_EQ(status, 0);
20-
21-
status = system("sudo cp copp_cfg.json /etc/sonic/");
22-
ASSERT_EQ(status, 0);
23-
}
24-
25-
void cleanup()
26-
{
27-
int status = system("sudo rm -rf /etc/sonic/");
28-
ASSERT_EQ(status, 0);
29-
}
30-
3113
TEST(CoppMgrTest, CoppTest)
3214
{
33-
create_init_file();
34-
3515
const vector<string> cfg_copp_tables = {
3616
CFG_COPP_TRAP_TABLE_NAME,
3717
CFG_COPP_GROUP_TABLE_NAME,
@@ -65,12 +45,10 @@ TEST(CoppMgrTest, CoppTest)
6545
{"trap_ids", "ip2me"}
6646
});
6747

68-
CoppMgr coppmgr(&cfgDb, &appDb, &stateDb, cfg_copp_tables);
48+
CoppMgr coppmgr(&cfgDb, &appDb, &stateDb, cfg_copp_tables, "./copp_cfg.json");
6949

7050
string overide_val;
7151
coppTable.hget("queue1_group1", "cbs",overide_val);
7252
EXPECT_EQ( overide_val, "6000");
73-
74-
cleanup();
7553
}
7654

0 commit comments

Comments
 (0)