Skip to content

Commit

Permalink
Class Init and reading from cfg_db ut's added
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy Karri <vkarri@nvidia.com>
  • Loading branch information
vivekrnv committed Apr 28, 2022
1 parent dc384cd commit bc7528a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 10 deletions.
7 changes: 0 additions & 7 deletions portsyncd/linksync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ const string LAG_PREFIX = "PortChannel";
set<string> g_portSet;
bool g_init;

struct if_nameindex
{
unsigned int if_index;
char *if_name;
};
extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }

LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) :
m_portTableProducer(appl_db, APP_PORT_TABLE_NAME),
m_portTable(appl_db, APP_PORT_TABLE_NAME),
Expand Down
7 changes: 7 additions & 0 deletions portsyncd/linksync.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ class LinkSync : public NetMsg
void handlePortConfigFromConfigDB(swss::ProducerStateTable &, swss::DBConnector &, bool );
void handlePortConfig(swss::ProducerStateTable &, std::map<std::string, swss::KeyOpFieldsValuesTuple> &);

struct if_nameindex
{
unsigned int if_index;
char *if_name;
};
extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; }

#endif
3 changes: 2 additions & 1 deletion tests/mock_tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \
mock_hiredis.cpp \
mock_redisreply.cpp

tests_portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) -Wl,-wrap,if_nameindex
tests_portsyncd_CXXFLAGS = -Wl,-wrap,if_nameindex
tests_portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST)
tests_portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(tests_portsyncd_INCLUDES)
tests_portsyncd_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis -lpthread \
-lswsscommon -lswsscommon -lgtest -lgtest_main -lnl-3 -lnl-route-3
121 changes: 119 additions & 2 deletions tests/mock_tests/portsyncd/portsyncd_ut.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,127 @@
#include "gtest/gtest.h"
#include "producerstatetable.h"
#include "../mock_table.h"
#define private public
#include "linksync.h"
#undef private

TEST(LinkSyncInitialization, test_initialization)
struct if_nameindex *if_ni_mock = NULL;

/* Mock if_nameindex() call */
extern "C" {
struct if_nameindex *__wrap_if_nameindex()
{
return if_ni_mock;
}
}

extern std::string mockCmdStdcout;
extern std::vector<std::string> mockCallArgs;
extern std::set<std::string> g_portSet;
/*
Test Fixture
*/
namespace portsyncd_ut
{
struct PortSyncdTest : public ::testing::Test
{
std::shared_ptr<swss::DBConnector> m_config_db;
std::shared_ptr<swss::DBConnector> m_app_db;
std::shared_ptr<swss::DBConnector> m_state_db;
std::shared_ptr<swss::Table> m_portCfgTable;
std::shared_ptr<swss::Table> m_portAppTable;

virtual void SetUp() override
{
testing_db::reset();

m_config_db = std::make_shared<swss::DBConnector>("CONFIG_DB", 0);
m_app_db = std::make_shared<swss::DBConnector>("APPL_DB", 0);
m_state_db = std::make_shared<swss::DBConnector>("STATE_DB", 0);
m_portCfgTable = std::make_shared<swss::Table>(m_config_db.get(), CFG_PORT_TABLE_NAME);
m_portAppTable = std::make_shared<swss::Table>(m_app_db.get(), APP_PORT_TABLE_NAME);

/* Construct a mock if_nameindex array */
if_ni_mock = (struct if_nameindex*) calloc(3, sizeof(struct if_nameindex));

if_ni_mock[2].if_index = 0;
if_ni_mock[2].if_name = NULL;

if_ni_mock[1].if_index = 16222;
if_ni_mock[1].if_name = "eth0";

if_ni_mock[0].if_index = 1;
if_ni_mock[0].if_name = "lo";
}

virtual void TearDown() override {
free(if_ni_mock);
if_ni_mock = NULL;
}
};

/* Helper Methods */
void populateCfgDb(swss::Table* tbl){
/* populate config db with Eth0 and Eth4 objects */
std::vector<swss::FieldValueTuple> vec;
vec.emplace_back("admin_status", "down");
vec.emplace_back("index", "2");
vec.emplace_back("lanes", "4,5,6,7");
vec.emplace_back("mtu", "9100");
vec.emplace_back("speed", "10000");
vec.emplace_back("alias", "etp1");
tbl->set("Ethernet0", vec);
vec.pop_back();
vec.emplace_back("alias", "etp1");
tbl->set("Ethernet4", vec);
}
}

namespace portsyncd_ut
{
ASSERT_EQ(0, 0);
TEST_F(PortSyncdTest, test_linkSyncInit)
{
mockCmdStdcout = "up\n";
swss::LinkSync sync(m_app_db.get(), m_state_db.get());
std::vector<std::string> keys;
sync.m_stateMgmtPortTable.getKeys(keys);
ASSERT_EQ(keys.size(), 1);
ASSERT_EQ(keys.back(), "eth0");
ASSERT_EQ(mockCallArgs.back(), "cat /sys/class/net/\"eth0\"/operstate");
}

TEST_F(PortSyncdTest, test_handlePortConfigFromConfigDB)
{
swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME);
populateCfgDb(m_portCfgTable.get());
swss::DBConnector cfg_db_conn("CONFIG_DB", 0);
handlePortConfigFromConfigDB(p, cfg_db_conn, false);
ASSERT_EQ(g_portSet.size(), 2);
ASSERT_NE(g_portSet.find("Ethernet0"), g_portSet.end());
ASSERT_NE(g_portSet.find("Ethernet4"), g_portSet.end());
std::vector<std::string> keys_to_app_db;
m_portAppTable->getKeys(keys_to_app_db);
ASSERT_EQ(keys_to_app_db.size(), 3);
std::sort(keys_to_app_db.begin(), keys_to_app_db.end());
ASSERT_EQ(keys_to_app_db[0], "Ethernet0");
ASSERT_EQ(keys_to_app_db[1], "Ethernet4");
ASSERT_EQ(keys_to_app_db[2], "PortConfigDone");
std::string count;
ASSERT_EQ(m_portAppTable->hget("PortConfigDone", "count", count), true);
ASSERT_EQ(count, "2");
}

TEST_F(PortSyncdTest, test_handlePortConfigFromConfigDBWarmBoot)
{
swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME);
populateCfgDb(m_portCfgTable.get());
swss::DBConnector cfg_db_conn("CONFIG_DB", 0);
handlePortConfigFromConfigDB(p, cfg_db_conn, true);
ASSERT_EQ(g_portSet.size(), 2);
ASSERT_NE(g_portSet.find("Ethernet0"), g_portSet.end());
ASSERT_NE(g_portSet.find("Ethernet4"), g_portSet.end());
std::vector<std::string> keys_to_app_db;
m_portAppTable->getKeys(keys_to_app_db);
ASSERT_EQ(keys_to_app_db.size(), 0);
}
}

0 comments on commit bc7528a

Please sign in to comment.