From 1c6cd64135ebd0d25c08b3f07f7656e638bf711e Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Wed, 27 Apr 2022 18:37:56 +0000 Subject: [PATCH 01/22] Refactored portsyncd structure to improve testability Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 80 ++++++++++++++++++++++++++++++- portsyncd/linksync.h | 9 ++++ portsyncd/portsyncd.cpp | 101 +--------------------------------------- 3 files changed, 89 insertions(+), 101 deletions(-) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index 4a2b351ee0..f00ae53a3c 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -8,6 +8,7 @@ #include "netmsg.h" #include "dbconnector.h" #include "producerstatetable.h" +#include "subscriberstatetable.h" #include "tokenize.h" #include "exec.h" @@ -31,8 +32,8 @@ const string MGMT_PREFIX = "eth"; const string INTFS_PREFIX = "Ethernet"; const string LAG_PREFIX = "PortChannel"; -extern set g_portSet; -extern bool g_init; +set g_portSet; +bool g_init; struct if_nameindex { @@ -268,3 +269,78 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj) SWSS_LOG_NOTICE("Cannot find %s in port table", key.c_str()); } } + +static void notifyPortConfigDone(ProducerStateTable &p) +{ + /* Notify that all ports added */ + FieldValueTuple finish_notice("count", to_string(g_portSet.size())); + vector attrs = { finish_notice }; + p.set("PortConfigDone", attrs); +} + +void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) +{ + SWSS_LOG_ENTER(); + + SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); + + Table table(&cfgDb, CFG_PORT_TABLE_NAME); + std::vector ovalues; + std::vector keys; + table.getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_NOTICE("ConfigDB does not have port information, " + "however ports can be added later on, continuing..."); + } + + for ( auto &k : keys ) + { + table.get(k, ovalues); + vector attrs; + for ( auto &v : ovalues ) + { + FieldValueTuple attr(v.first, v.second); + attrs.push_back(attr); + } + if (!warm) + { + p.set(k, attrs); + } + g_portSet.insert(k); + } + if (!warm) + { + notifyPortConfigDone(p); + } + +} + +void handlePortConfig(ProducerStateTable &p, map &port_cfg_map) +{ + auto it = port_cfg_map.begin(); + while (it != port_cfg_map.end()) + { + KeyOpFieldsValuesTuple entry = it->second; + string key = kfvKey(entry); + string op = kfvOp(entry); + auto values = kfvFieldsValues(entry); + + /* only push down port config when port is not in hostif create pending state */ + if (g_portSet.find(key) == g_portSet.end()) + { + /* No support for port delete yet */ + if (op == SET_COMMAND) + { + p.set(key, values); + } + + it = port_cfg_map.erase(it); + } + else + { + it++; + } + } +} \ No newline at end of file diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index d72e1ba124..41347c0433 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -4,8 +4,14 @@ #include "dbconnector.h" #include "producerstatetable.h" #include "netmsg.h" +#include "exec.h" #include +#include +#include +#include +#include +#include namespace swss { @@ -28,4 +34,7 @@ class LinkSync : public NetMsg } +void handlePortConfigFromConfigDB(swss::ProducerStateTable &, swss::DBConnector &, bool ); +void handlePortConfig(swss::ProducerStateTable &, std::map &); + #endif diff --git a/portsyncd/portsyncd.cpp b/portsyncd/portsyncd.cpp index 37e0c4232f..5e159867c0 100644 --- a/portsyncd/portsyncd.cpp +++ b/portsyncd/portsyncd.cpp @@ -2,19 +2,13 @@ #include #include #include -#include -#include -#include -#include #include #include "dbconnector.h" #include "select.h" #include "netdispatcher.h" #include "netlink.h" -#include "producerstatetable.h" #include "portsyncd/linksync.h" #include "subscriberstatetable.h" -#include "exec.h" #include "warm_restart.h" using namespace std; @@ -22,18 +16,8 @@ using namespace swss; #define DEFAULT_SELECT_TIMEOUT 1000 /* ms */ -/* - * This g_portSet contains all the front panel ports that the corresponding - * host interfaces needed to be created. When this LinkSync class is - * initialized, we check the database to see if some of the ports' host - * interfaces are already created and remove them from this set. We will - * remove the rest of the ports in the set when receiving the first netlink - * message indicating that the host interfaces are created. After the set - * is empty, we send out the signal PortInitDone. g_init is used to limit the - * command to be run only once. - */ -set g_portSet; -bool g_init = false; +extern set g_portSet; +extern bool g_init; void usage() { @@ -42,12 +26,6 @@ void usage() cout << " this program will exit if configDB does not contain that info" << endl; } -void handlePortConfigFile(ProducerStateTable &p, string file, bool warm); -void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm); -void handleVlanIntfFile(string file); -void handlePortConfig(ProducerStateTable &p, map &port_cfg_map); -void checkPortInitDone(DBConnector *appl_db); - int main(int argc, char **argv) { Logger::linkToDbNative("portsyncd"); @@ -178,78 +156,3 @@ int main(int argc, char **argv) return 1; } - -static void notifyPortConfigDone(ProducerStateTable &p) -{ - /* Notify that all ports added */ - FieldValueTuple finish_notice("count", to_string(g_portSet.size())); - vector attrs = { finish_notice }; - p.set("PortConfigDone", attrs); -} - -void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) -{ - SWSS_LOG_ENTER(); - - SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); - - Table table(&cfgDb, CFG_PORT_TABLE_NAME); - std::vector ovalues; - std::vector keys; - table.getKeys(keys); - - if (keys.empty()) - { - SWSS_LOG_NOTICE("ConfigDB does not have port information, " - "however ports can be added later on, continuing..."); - } - - for ( auto &k : keys ) - { - table.get(k, ovalues); - vector attrs; - for ( auto &v : ovalues ) - { - FieldValueTuple attr(v.first, v.second); - attrs.push_back(attr); - } - if (!warm) - { - p.set(k, attrs); - } - g_portSet.insert(k); - } - if (!warm) - { - notifyPortConfigDone(p); - } - -} - -void handlePortConfig(ProducerStateTable &p, map &port_cfg_map) -{ - auto it = port_cfg_map.begin(); - while (it != port_cfg_map.end()) - { - KeyOpFieldsValuesTuple entry = it->second; - string key = kfvKey(entry); - string op = kfvOp(entry); - auto values = kfvFieldsValues(entry); - - /* only push down port config when port is not in hostif create pending state */ - if (g_portSet.find(key) == g_portSet.end()) - { - /* No support for port delete yet */ - if (op == SET_COMMAND) - { - p.set(key, values); - } - - it = port_cfg_map.erase(it); - } - else - { - it++; - } - } -} From 7395544bdcc596bd29e2ba5da270c9267a63d56e Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Wed, 27 Apr 2022 19:27:47 +0000 Subject: [PATCH 02/22] Skeleton for portsyncd ut added Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 32 +++++++++++++++++---- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 10 +++++++ 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 tests/mock_tests/portsyncd/portsyncd_ut.cpp diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index 761aba19c6..b5a70b7495 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -2,13 +2,11 @@ FLEX_CTR_DIR = $(top_srcdir)/orchagent/flex_counter DEBUG_CTR_DIR = $(top_srcdir)/orchagent/debug_counter P4_ORCH_DIR = $(top_srcdir)/orchagent/p4orch -INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib - CFLAGS_SAI = -I /usr/include/sai -TESTS = tests +TESTS = tests tests_portsyncd -noinst_PROGRAMS = tests +noinst_PROGRAMS = tests tests_portsyncd LDADD_SAI = -lsaimeta -lsaimetadata -lsaivs -lsairedis @@ -21,6 +19,10 @@ endif CFLAGS_GTEST = LDADD_GTEST = -L/usr/src/gtest +## Orchagent Unit Tests + +tests_INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib -I$(top_srcdir)/orchagent + tests_SOURCES = aclorch_ut.cpp \ portsorch_ut.cpp \ routeorch_ut.cpp \ @@ -32,6 +34,7 @@ tests_SOURCES = aclorch_ut.cpp \ mock_orchagent_main.cpp \ mock_dbconnector.cpp \ mock_consumerstatetable.cpp \ + common/mock_shell_command.cpp \ mock_table.cpp \ mock_hiredis.cpp \ mock_redisreply.cpp \ @@ -109,7 +112,24 @@ tests_SOURCES += $(P4_ORCH_DIR)/p4orch.cpp \ $(P4_ORCH_DIR)/wcmp_manager.cpp \ $(P4_ORCH_DIR)/mirror_session_manager.cpp -tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -I$(top_srcdir)/orchagent +tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) +tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_INCLUDES) tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthread \ -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 + +## portsyncd unit tests + +tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr + +tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ + $(top_srcdir)/portsyncd/linksync.cpp \ + mock_dbconnector.cpp \ + common/mock_shell_command.cpp \ + mock_table.cpp \ + mock_hiredis.cpp \ + mock_redisreply.cpp + +tests_portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) -Wl,-wrap,if_nameindex +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 \ No newline at end of file diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp new file mode 100644 index 0000000000..eb4647b009 --- /dev/null +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -0,0 +1,10 @@ +#include "gtest/gtest.h" +#include "../mock_table.h" +#define private public +#include "linksync.h" +#undef private + +TEST(LinkSyncInitialization, test_initialization) +{ + ASSERT_EQ(0, 0); +} \ No newline at end of file From dc384cddb81cdf38eadcd70bc3afa7ba6bff3b57 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Wed, 27 Apr 2022 19:30:24 +0000 Subject: [PATCH 03/22] Minor update Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index b5a70b7495..58e7492db0 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -132,4 +132,4 @@ tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ tests_portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) -Wl,-wrap,if_nameindex 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 \ No newline at end of file + -lswsscommon -lswsscommon -lgtest -lgtest_main -lnl-3 -lnl-route-3 From bc7528aa6cd5b601757d37aca5da75485afcb1c6 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Thu, 28 Apr 2022 18:32:39 +0000 Subject: [PATCH 04/22] Class Init and reading from cfg_db ut's added Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 7 -- portsyncd/linksync.h | 7 ++ tests/mock_tests/Makefile.am | 3 +- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 121 +++++++++++++++++++- 4 files changed, 128 insertions(+), 10 deletions(-) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index f00ae53a3c..1e56d92b1d 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -35,13 +35,6 @@ const string LAG_PREFIX = "PortChannel"; set 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), diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index 41347c0433..1760e29b1e 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -37,4 +37,11 @@ class LinkSync : public NetMsg void handlePortConfigFromConfigDB(swss::ProducerStateTable &, swss::DBConnector &, bool ); void handlePortConfig(swss::ProducerStateTable &, std::map &); +struct if_nameindex +{ + unsigned int if_index; + char *if_name; +}; +extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; } + #endif diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index 58e7492db0..d711698ed9 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -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 diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index eb4647b009..909eb5bbd8 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -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 mockCallArgs; +extern std::set g_portSet; +/* +Test Fixture +*/ +namespace portsyncd_ut +{ + struct PortSyncdTest : public ::testing::Test + { + std::shared_ptr m_config_db; + std::shared_ptr m_app_db; + std::shared_ptr m_state_db; + std::shared_ptr m_portCfgTable; + std::shared_ptr m_portAppTable; + + virtual void SetUp() override + { + testing_db::reset(); + + m_config_db = std::make_shared("CONFIG_DB", 0); + m_app_db = std::make_shared("APPL_DB", 0); + m_state_db = std::make_shared("STATE_DB", 0); + m_portCfgTable = std::make_shared(m_config_db.get(), CFG_PORT_TABLE_NAME); + m_portAppTable = std::make_shared(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 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 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 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 keys_to_app_db; + m_portAppTable->getKeys(keys_to_app_db); + ASSERT_EQ(keys_to_app_db.size(), 0); + } } \ No newline at end of file From e052e201c5239adebd15394121b2a46040bb8f8b Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Thu, 28 Apr 2022 18:34:31 +0000 Subject: [PATCH 05/22] Added missing comment Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index 1e56d92b1d..0b65fd26e9 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -32,6 +32,16 @@ const string MGMT_PREFIX = "eth"; const string INTFS_PREFIX = "Ethernet"; const string LAG_PREFIX = "PortChannel"; +/* + * This g_portSet contains all the front panel ports that the corresponding + * host interfaces needed to be created. When this LinkSync class is + * initialized, we check the database to see if some of the ports' host + * interfaces are already created and remove them from this set. We will + * remove the rest of the ports in the set when receiving the first netlink + * message indicating that the host interfaces are created. After the set + * is empty, we send out the signal PortInitDone. g_init is used to limit the + * command to be run only once. + */ set g_portSet; bool g_init; From e267fd9513a7ad700e557852fdadbc6fe9f43ce8 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Thu, 28 Apr 2022 21:54:36 +0000 Subject: [PATCH 06/22] onMsg test added Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 2 +- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 162 +++++++++++++++++--- 2 files changed, 144 insertions(+), 20 deletions(-) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index 0b65fd26e9..e058355969 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -101,7 +101,7 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) : if (!WarmStart::isWarmStart()) { - /* See the comments for g_portSet in portsyncd.cpp */ + /* See the comments for g_portSet */ for (auto port_iter = g_portSet.begin(); port_iter != g_portSet.end();) { string port = *port_iter; diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index 909eb5bbd8..b7c9c02dbc 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -1,5 +1,6 @@ #include "gtest/gtest.h" -#include "producerstatetable.h" +#include +#include #include "../mock_table.h" #define private public #include "linksync.h" @@ -18,6 +19,7 @@ extern "C" { extern std::string mockCmdStdcout; extern std::vector mockCallArgs; extern std::set g_portSet; + /* Test Fixture */ @@ -34,30 +36,17 @@ namespace portsyncd_ut virtual void SetUp() override { testing_db::reset(); - m_config_db = std::make_shared("CONFIG_DB", 0); m_app_db = std::make_shared("APPL_DB", 0); m_state_db = std::make_shared("STATE_DB", 0); m_portCfgTable = std::make_shared(m_config_db.get(), CFG_PORT_TABLE_NAME); m_portAppTable = std::make_shared(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 (if_ni_mock != NULL) free(if_ni_mock); if_ni_mock = NULL; - } + } }; /* Helper Methods */ @@ -75,12 +64,97 @@ namespace portsyncd_ut vec.emplace_back("alias", "etp1"); tbl->set("Ethernet4", vec); } + + /* Create internal ds holding netdev ifaces for eth0 & lo */ + inline struct if_nameindex * populateNetDev(){ + struct if_nameindex *if_ni_temp; + /* Construct a mock if_nameindex array */ + if_ni_temp = (struct if_nameindex*) calloc(3, sizeof(struct if_nameindex)); + + if_ni_temp[2].if_index = 0; + if_ni_temp[2].if_name = NULL; + + if_ni_temp[1].if_index = 16222; + if_ni_temp[1].if_name = "eth0"; + + if_ni_temp[0].if_index = 1; + if_ni_temp[0].if_name = "lo"; + + return if_ni_temp; + } + + /* Create internal ds holding netdev ifaces for lo & Ethernet0 */ + inline struct if_nameindex * populateNetDevAdvanced(){ + struct if_nameindex *if_ni_temp; + /* Construct a mock if_nameindex array */ + if_ni_temp = (struct if_nameindex*) calloc(3, sizeof(struct if_nameindex)); + + if_ni_temp[2].if_index = 0; + if_ni_temp[2].if_name = NULL; + + if_ni_temp[1].if_index = 142; + if_ni_temp[1].if_name = "Ethernet0"; + + if_ni_temp[0].if_index = 1; + if_ni_temp[0].if_name = "lo"; + + return if_ni_temp; + } + + /* Draft a rtnl_link msg */ + struct nl_object* draft_nlmsg(const std::string& name, + std::vector flags, + const std::string& type, + const std::string& ll_add, + int ifindex, + unsigned int mtu, + int master_ifindex = 0){ + + struct rtnl_link* nl_obj = rtnl_link_alloc(); + if (!nl_obj){ + throw std::runtime_error("netlink: rtnl_link object allocation failed"); + } + /* Set name for rtnl link object */ + rtnl_link_set_name(nl_obj, name.c_str()); + + /* Set flags */ + for (auto nlflag : flags){ + rtnl_link_set_flags(nl_obj, nlflag); + } + + /* Set type */ + if (!type.empty()){ + rtnl_link_set_type(nl_obj, type.c_str()); + } + + /* Set Link layer Address */ + struct nl_addr * ll_addr; + int result = nl_addr_parse(ll_add.c_str(), AF_LLC, &ll_addr); + if (result < 0){ + throw std::runtime_error("netlink: Link layer address allocation failed"); + } + rtnl_link_set_addr(nl_obj, ll_addr); + + /* Set ifindex */ + rtnl_link_set_ifindex(nl_obj, ifindex); + + /* Set mtu */ + rtnl_link_set_mtu(nl_obj, mtu); + + /* Set master_ifindex if any */ + if (master_ifindex){ + rtnl_link_set_master(nl_obj, master_ifindex); + } + + return (struct nl_object*)nl_obj; + } } namespace portsyncd_ut { TEST_F(PortSyncdTest, test_linkSyncInit) - { + { + if_ni_mock = populateNetDev(); mockCmdStdcout = "up\n"; swss::LinkSync sync(m_app_db.get(), m_state_db.get()); std::vector keys; @@ -91,7 +165,7 @@ namespace portsyncd_ut } 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); @@ -124,4 +198,54 @@ namespace portsyncd_ut m_portAppTable->getKeys(keys_to_app_db); ASSERT_EQ(keys_to_app_db.size(), 0); } -} \ No newline at end of file + + TEST_F(PortSyncdTest, test_cacheOldIfaces) + { + if_ni_mock = populateNetDevAdvanced(); + swss::LinkSync sync(m_app_db.get(), m_state_db.get()); + ASSERT_EQ(mockCallArgs.back(), "ip link set \"Ethernet0\" down"); + ASSERT_NE(sync.m_ifindexOldNameMap.find(142), sync.m_ifindexOldNameMap.end()); + ASSERT_EQ(sync.m_ifindexOldNameMap[142], "Ethernet0"); + } + + TEST_F(PortSyncdTest, test_onMsg) + { + swss::LinkSync sync(m_app_db.get(), m_state_db.get()); + /* Write config to Config DB */ + populateCfgDb(m_portCfgTable.get()); + swss::DBConnector cfg_db_conn("CONFIG_DB", 0); + + /* Handle CFG DB notifs and Write them to APPL_DB */ + swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME); + handlePortConfigFromConfigDB(p, cfg_db_conn, false); + + /* Generate a netlink notification about the netdev iface */ + std::vector flags = {IFF_UP, IFF_RUNNING}; + struct nl_object* msg = draft_nlmsg("Ethernet0", + flags, + "sx_netdev", + "1c:34:da:1c:9f:00", + 142, + 9100, + 0); + sync.onMsg(RTM_NEWLINK, msg); + + /* Verify if the update has been written to State DB */ + std::vector ovalues; + ASSERT_EQ(sync.m_statePortTable.get("Ethernet0", ovalues), true); + for (auto value : ovalues){ + if (fvField(value) == "state") {ASSERT_EQ(fvValue(value), "ok");} + if (fvField(value) == "mtu") {ASSERT_EQ(fvValue(value), "9100");} + if (fvField(value) == "netdev_oper_status") {ASSERT_EQ(fvValue(value), "up");} + if (fvField(value) == "admin_status") {ASSERT_EQ(fvValue(value), "up");} + if (fvField(value) == "speed") {ASSERT_EQ(fvValue(value), "10000");} + } + + /* Verify if the internal strctures are updated as expected */ + ASSERT_NE(sync.m_ifindexNameMap.find(142), sync.m_ifindexNameMap.end()); + ASSERT_EQ(sync.m_ifindexNameMap[142], "Ethernet0"); + + /* Free Nl_object */ + nl_object_free(msg); + } +} From 67a086d549bf50880627f2a6b2ac9fa9657aa041 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 29 Apr 2022 03:08:17 +0000 Subject: [PATCH 07/22] 2 new test cases added Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 2 +- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 89 +++++++++++++++++++-- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index e058355969..e3f125f23d 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -346,4 +346,4 @@ void handlePortConfig(ProducerStateTable &p, map it++; } } -} \ No newline at end of file +} diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index b7c9c02dbc..85c0bca30d 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -103,12 +103,12 @@ namespace portsyncd_ut /* Draft a rtnl_link msg */ struct nl_object* draft_nlmsg(const std::string& name, - std::vector flags, - const std::string& type, - const std::string& ll_add, - int ifindex, - unsigned int mtu, - int master_ifindex = 0){ + std::vector flags, + const std::string& type, + const std::string& ll_add, + int ifindex, + unsigned int mtu, + int master_ifindex = 0){ struct rtnl_link* nl_obj = rtnl_link_alloc(); if (!nl_obj){ @@ -148,6 +148,10 @@ namespace portsyncd_ut return (struct nl_object*)nl_obj; } + + inline void free_nlobj(struct nl_object* msg){ + nl_object_free(msg); + } } namespace portsyncd_ut @@ -208,7 +212,7 @@ namespace portsyncd_ut ASSERT_EQ(sync.m_ifindexOldNameMap[142], "Ethernet0"); } - TEST_F(PortSyncdTest, test_onMsg) + TEST_F(PortSyncdTest, test_onMsgNewLink) { swss::LinkSync sync(m_app_db.get(), m_state_db.get()); /* Write config to Config DB */ @@ -246,6 +250,75 @@ namespace portsyncd_ut ASSERT_EQ(sync.m_ifindexNameMap[142], "Ethernet0"); /* Free Nl_object */ - nl_object_free(msg); + free_nlobj(msg); + } + + TEST_F(PortSyncdTest, test_onMsgDelLink){ + + swss::LinkSync sync(m_app_db.get(), m_state_db.get()); + + /* Write config to Config DB */ + populateCfgDb(m_portCfgTable.get()); + swss::DBConnector cfg_db_conn("CONFIG_DB", 0); + + /* Handle CFG DB notifs and Write them to APPL_DB */ + swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME); + handlePortConfigFromConfigDB(p, cfg_db_conn, false); + + /* Generate a netlink notification about the netdev iface */ + std::vector flags = {IFF_UP, IFF_RUNNING}; + struct nl_object* msg = draft_nlmsg("Ethernet0", + flags, + "sx_netdev", + "1c:34:da:1c:9f:00", + 142, + 9100, + 0); + sync.onMsg(RTM_NEWLINK, msg); + + /* Verify if the update has been written to State DB */ + std::vector ovalues; + ASSERT_EQ(sync.m_statePortTable.get("Ethernet0", ovalues), true); + + /* Free Nl_object */ + free_nlobj(msg); + + /* Generate a DELLINK Notif */ + msg = draft_nlmsg("Ethernet0", + flags, + "sx_netdev", + "1c:34:da:1c:9f:00", + 142, + 9100, + 0); + + sync.onMsg(RTM_DELLINK, msg); + ovalues.clear(); + + /* Verify if the state_db entry is cleared */ + ASSERT_EQ(sync.m_statePortTable.get("Ethernet0", ovalues), false); + } + + TEST_F(PortSyncdTest, test_onMsgMgmtIface){ + swss::LinkSync sync(m_app_db.get(), m_state_db.get()); + + /* Generate a netlink notification about the eth0 netdev iface */ + std::vector flags = {IFF_UP}; + struct nl_object* msg = draft_nlmsg("eth0", + flags, + "", + "00:50:56:28:0e:4a", + 16222, + 9100, + 0); + sync.onMsg(RTM_NEWLINK, msg); + + /* Verify if the update has been written to State DB */ + std::string oper_status; + ASSERT_EQ(sync.m_stateMgmtPortTable.hget("eth0", "oper_status", oper_status), true); + ASSERT_EQ(oper_status, "down"); + + /* Free Nl_object */ + free_nlobj(msg); } } From 64a8ee1cecf020dce23317dfca18168a46fda7e4 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 29 Apr 2022 03:36:28 +0000 Subject: [PATCH 08/22] Another case added Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index 85c0bca30d..298eed04cf 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -321,4 +321,27 @@ namespace portsyncd_ut /* Free Nl_object */ free_nlobj(msg); } + + TEST_F(PortSyncdTest, test_onMsgIgnoreOldNetDev){ + if_ni_mock = populateNetDevAdvanced(); + swss::LinkSync sync(m_app_db.get(), m_state_db.get()); + ASSERT_EQ(mockCallArgs.back(), "ip link set \"Ethernet0\" down"); + ASSERT_NE(sync.m_ifindexOldNameMap.find(142), sync.m_ifindexOldNameMap.end()); + ASSERT_EQ(sync.m_ifindexOldNameMap[142], "Ethernet0"); + + /* Generate a netlink notification about the netdev iface */ + std::vector flags; + struct nl_object* msg = draft_nlmsg("Ethernet0", + flags, + "sx_netdev", + "1c:34:da:1c:9f:00", + 142, + 9100, + 0); + sync.onMsg(RTM_NEWLINK, msg); + + /* Verify if nothing is written to state_db */ + std::vector ovalues; + ASSERT_EQ(sync.m_statePortTable.get("Ethernet0", ovalues), false); + } } From 6909333fce9be4fbafa9dfbc889ba9c0feb11bde Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 29 Apr 2022 18:40:18 +0000 Subject: [PATCH 09/22] Push infra modifications Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 3 +- .../mock_tests/common/mock_shell_command.cpp | 15 ++++++ tests/mock_tests/mock_table.cpp | 50 ++++++++++++++++++- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tests/mock_tests/common/mock_shell_command.cpp diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index d711698ed9..1913f9154c 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -119,8 +119,6 @@ tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthr ## portsyncd unit tests -tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr - tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ $(top_srcdir)/portsyncd/linksync.cpp \ mock_dbconnector.cpp \ @@ -129,6 +127,7 @@ tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ mock_hiredis.cpp \ mock_redisreply.cpp +tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr 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) diff --git a/tests/mock_tests/common/mock_shell_command.cpp b/tests/mock_tests/common/mock_shell_command.cpp new file mode 100644 index 0000000000..f3ccfbfe5e --- /dev/null +++ b/tests/mock_tests/common/mock_shell_command.cpp @@ -0,0 +1,15 @@ +#include +#include + +int mockCmdReturn = 0; +std::string mockCmdStdcout = ""; +std::vector mockCallArgs; + +namespace swss { + int exec(const std::string &cmd, std::string &stdout) + { + mockCallArgs.push_back(cmd); + stdout = mockCmdStdcout; + return mockCmdReturn; + } +} diff --git a/tests/mock_tests/mock_table.cpp b/tests/mock_tests/mock_table.cpp index 29011c30a0..732bac54a6 100644 --- a/tests/mock_tests/mock_table.cpp +++ b/tests/mock_tests/mock_table.cpp @@ -1,4 +1,6 @@ #include "table.h" +#include "producerstatetable.h" +#include using TableDataT = std::map>; using TablesT = std::map; @@ -62,6 +64,12 @@ namespace swss table[key] = values; } + void Table::del(const std::string &key, const std::string& /* op */, const std::string& /*prefix*/) + { + auto &table = gDB[m_pipe->getDbId()][getTableName()]; + table.erase(key); + } + void Table::getKeys(std::vector &keys) { keys.clear(); @@ -71,4 +79,44 @@ namespace swss keys.push_back(it.first); } } -} + + void ProducerStateTable::set(const std::string &key, + const std::vector &values, + const std::string &op, + const std::string &prefix) + { + auto &table = gDB[m_pipe->getDbId()][getTableName()]; + auto iter = table.find(key); + if (iter == table.end()) + { + table[key] = values; + } + else + { + std::vector new_values(values); + std::set field_set; + for (auto &value : values) + { + field_set.insert(fvField(value)); + } + for (auto &value : iter->second) + { + auto &field = fvField(value); + if (field_set.find(field) != field_set.end()) + { + continue; + } + new_values.push_back(value); + } + iter->second.swap(new_values); + } + } + + void ProducerStateTable::del(const std::string &key, + const std::string &op, + const std::string &prefix) + { + auto &table = gDB[m_pipe->getDbId()][getTableName()]; + table.erase(key); + } +} \ No newline at end of file From 7f26e83bbf9717c01ba3b42dbce3cf1c643b2e81 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Tue, 3 May 2022 17:32:49 +0000 Subject: [PATCH 10/22] Moved common methods/ds to a different file Signed-off-by: Vivek Reddy Karri --- portsyncd/Makefile.am | 2 +- portsyncd/linksync.cpp | 91 +-------------------------------- portsyncd/linksync.h | 2 + portsyncd/portsyncd_helper.cpp | 92 ++++++++++++++++++++++++++++++++++ tests/mock_tests/Makefile.am | 1 + 5 files changed, 98 insertions(+), 90 deletions(-) create mode 100644 portsyncd/portsyncd_helper.cpp diff --git a/portsyncd/Makefile.am b/portsyncd/Makefile.am index 5bba269ab2..cedc779ae8 100644 --- a/portsyncd/Makefile.am +++ b/portsyncd/Makefile.am @@ -8,7 +8,7 @@ else DBGFLAGS = -g endif -portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h +portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp portsyncd_helper.cpp $(top_srcdir)/cfgmgr/shellcmd.h portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index e3f125f23d..4224e464ce 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -6,9 +6,6 @@ #include #include "logger.h" #include "netmsg.h" -#include "dbconnector.h" -#include "producerstatetable.h" -#include "subscriberstatetable.h" #include "tokenize.h" #include "exec.h" @@ -32,18 +29,8 @@ const string MGMT_PREFIX = "eth"; const string INTFS_PREFIX = "Ethernet"; const string LAG_PREFIX = "PortChannel"; -/* - * This g_portSet contains all the front panel ports that the corresponding - * host interfaces needed to be created. When this LinkSync class is - * initialized, we check the database to see if some of the ports' host - * interfaces are already created and remove them from this set. We will - * remove the rest of the ports in the set when receiving the first netlink - * message indicating that the host interfaces are created. After the set - * is empty, we send out the signal PortInitDone. g_init is used to limit the - * command to be run only once. - */ -set g_portSet; -bool g_init; +extern set g_portSet; +extern bool g_init; LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) : m_portTableProducer(appl_db, APP_PORT_TABLE_NAME), @@ -273,77 +260,3 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj) } } -static void notifyPortConfigDone(ProducerStateTable &p) -{ - /* Notify that all ports added */ - FieldValueTuple finish_notice("count", to_string(g_portSet.size())); - vector attrs = { finish_notice }; - p.set("PortConfigDone", attrs); -} - -void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) -{ - SWSS_LOG_ENTER(); - - SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); - - Table table(&cfgDb, CFG_PORT_TABLE_NAME); - std::vector ovalues; - std::vector keys; - table.getKeys(keys); - - if (keys.empty()) - { - SWSS_LOG_NOTICE("ConfigDB does not have port information, " - "however ports can be added later on, continuing..."); - } - - for ( auto &k : keys ) - { - table.get(k, ovalues); - vector attrs; - for ( auto &v : ovalues ) - { - FieldValueTuple attr(v.first, v.second); - attrs.push_back(attr); - } - if (!warm) - { - p.set(k, attrs); - } - g_portSet.insert(k); - } - if (!warm) - { - notifyPortConfigDone(p); - } - -} - -void handlePortConfig(ProducerStateTable &p, map &port_cfg_map) -{ - auto it = port_cfg_map.begin(); - while (it != port_cfg_map.end()) - { - KeyOpFieldsValuesTuple entry = it->second; - string key = kfvKey(entry); - string op = kfvOp(entry); - auto values = kfvFieldsValues(entry); - - /* only push down port config when port is not in hostif create pending state */ - if (g_portSet.find(key) == g_portSet.end()) - { - /* No support for port delete yet */ - if (op == SET_COMMAND) - { - p.set(key, values); - } - - it = port_cfg_map.erase(it); - } - else - { - it++; - } - } -} diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index 1760e29b1e..e13f7eea9e 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -5,6 +5,8 @@ #include "producerstatetable.h" #include "netmsg.h" #include "exec.h" +#include "warm_restart.h" +#include "shellcmd.h" #include #include diff --git a/portsyncd/portsyncd_helper.cpp b/portsyncd/portsyncd_helper.cpp new file mode 100644 index 0000000000..845fb40212 --- /dev/null +++ b/portsyncd/portsyncd_helper.cpp @@ -0,0 +1,92 @@ +#include "portsyncd/linksync.h" + +using namespace std; +using namespace swss; + +/* + * This g_portSet contains all the front panel ports that the corresponding + * host interfaces needed to be created. When this LinkSync class is + * initialized, we check the database to see if some of the ports' host + * interfaces are already created and remove them from this set. We will + * remove the rest of the ports in the set when receiving the first netlink + * message indicating that the host interfaces are created. After the set + * is empty, we send out the signal PortInitDone. g_init is used to limit the + * command to be run only once. + */ +set g_portSet; +bool g_init; + +static void notifyPortConfigDone(ProducerStateTable &p) +{ + /* Notify that all ports added */ + FieldValueTuple finish_notice("count", to_string(g_portSet.size())); + vector attrs = { finish_notice }; + p.set("PortConfigDone", attrs); +} + +void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) +{ + SWSS_LOG_ENTER(); + + SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); + + Table table(&cfgDb, CFG_PORT_TABLE_NAME); + std::vector ovalues; + std::vector keys; + table.getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_NOTICE("ConfigDB does not have port information, " + "however ports can be added later on, continuing..."); + } + + for ( auto &k : keys ) + { + table.get(k, ovalues); + vector attrs; + for ( auto &v : ovalues ) + { + FieldValueTuple attr(v.first, v.second); + attrs.push_back(attr); + } + if (!warm) + { + p.set(k, attrs); + } + g_portSet.insert(k); + } + if (!warm) + { + notifyPortConfigDone(p); + } + +} + +void handlePortConfig(ProducerStateTable &p, map &port_cfg_map) +{ + auto it = port_cfg_map.begin(); + while (it != port_cfg_map.end()) + { + KeyOpFieldsValuesTuple entry = it->second; + string key = kfvKey(entry); + string op = kfvOp(entry); + auto values = kfvFieldsValues(entry); + + /* only push down port config when port is not in hostif create pending state */ + if (g_portSet.find(key) == g_portSet.end()) + { + /* No support for port delete yet */ + if (op == SET_COMMAND) + { + p.set(key, values); + } + + it = port_cfg_map.erase(it); + } + else + { + it++; + } + } +} diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index 1913f9154c..58100d4aa1 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -121,6 +121,7 @@ tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthr tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ $(top_srcdir)/portsyncd/linksync.cpp \ + $(top_srcdir)/portsyncd/portsyncd_helper.cpp \ mock_dbconnector.cpp \ common/mock_shell_command.cpp \ mock_table.cpp \ From 04023301b5e6faab9f162c778ad4019c60a62eed Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Mon, 9 May 2022 18:06:09 +0000 Subject: [PATCH 11/22] bullseye link issue handled Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index 58100d4aa1..c7a7a83592 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -132,5 +132,5 @@ tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr 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 +tests_portsyncd_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis \ + -lswsscommon -lswsscommon -lgtest -lgtest_main -lnl-3 -lnl-route-3 -lpthread From 8a3c138ce799f4248587db2a8db42c2613567634 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 20 May 2022 23:18:12 +0000 Subject: [PATCH 12/22] Update UT to include ASAN changes Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.h | 7 ------- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 10 +++++++++- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index e13f7eea9e..5115963b8c 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -39,11 +39,4 @@ class LinkSync : public NetMsg void handlePortConfigFromConfigDB(swss::ProducerStateTable &, swss::DBConnector &, bool ); void handlePortConfig(swss::ProducerStateTable &, std::map &); -struct if_nameindex -{ - unsigned int if_index; - char *if_name; -}; -extern "C" { extern struct if_nameindex *if_nameindex (void) __THROW; } - #endif diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index 298eed04cf..46726c10d2 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -1,5 +1,5 @@ #include "gtest/gtest.h" -#include +#include #include #include "../mock_table.h" #define private public @@ -16,6 +16,14 @@ extern "C" { } } +/* Mock if_freenameindex() call */ +extern "C" { + void __wrap_if_freenameindex(struct if_nameindex *ptr) + { + return ; + } +} + extern std::string mockCmdStdcout; extern std::vector mockCallArgs; extern std::set g_portSet; From dfcba645f586458067c8c8debb4f66560b09718f Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 20 May 2022 23:21:18 +0000 Subject: [PATCH 13/22] Restored original del func on mock_table Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/mock_table.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/mock_tests/mock_table.cpp b/tests/mock_tests/mock_table.cpp index fb98ed301b..4d512a9835 100644 --- a/tests/mock_tests/mock_table.cpp +++ b/tests/mock_tests/mock_table.cpp @@ -64,12 +64,6 @@ namespace swss table[key] = values; } - void Table::del(const std::string &key, const std::string& /* op */, const std::string& /*prefix*/) - { - auto &table = gDB[m_pipe->getDbId()][getTableName()]; - table.erase(key); - } - void Table::getKeys(std::vector &keys) { keys.clear(); @@ -80,6 +74,14 @@ namespace swss } } + void Table::del(const std::string &key, const std::string& /* op */, const std::string& /*prefix*/) + { + auto table = gDB[m_pipe->getDbId()].find(getTableName()); + if (table != gDB[m_pipe->getDbId()].end()){ + table->second.erase(key); + } + } + void ProducerStateTable::set(const std::string &key, const std::vector &values, const std::string &op, From 1bc2d97b0263b922bf85abb3f1376f709f2e2664 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Sat, 21 May 2022 00:56:44 +0000 Subject: [PATCH 14/22] Refactored mock shell cmd Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 26 ++++++++++--------- .../mock_tests/common/mock_shell_command.cpp | 18 ++++++++++--- .../intfmgrd/add_ipv6_prefix_ut.cpp | 16 +++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index a32c32c149..fec9cbdc8f 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -141,18 +141,20 @@ tests_portsyncd_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis \ ## intfmgrd unit tests tests_intfmgrd_SOURCES = intfmgrd/add_ipv6_prefix_ut.cpp \ - $(top_srcdir)/cfgmgr/intfmgr.cpp \ - $(top_srcdir)/orchagent/orch.cpp \ - $(top_srcdir)/orchagent/request_parser.cpp \ - $(top_srcdir)/lib/subintf.cpp \ - mock_orchagent_main.cpp \ - mock_dbconnector.cpp \ - mock_table.cpp \ - mock_hiredis.cpp \ - fake_response_publisher.cpp \ - mock_redisreply.cpp - + $(top_srcdir)/cfgmgr/intfmgr.cpp \ + $(top_srcdir)/lib/subintf.cpp \ + $(top_srcdir)/orchagent/orch.cpp \ + $(top_srcdir)/orchagent/request_parser.cpp \ + mock_orchagent_main.cpp \ + mock_dbconnector.cpp \ + mock_table.cpp \ + mock_hiredis.cpp \ + fake_response_publisher.cpp \ + mock_redisreply.cpp \ + common/mock_shell_command.cpp + +tests_intfmgrd_INCLUDES = $(tests_INCLUDES) -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/lib tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -I $(top_srcdir)/cfgmgr -I $(top_srcdir)/orchagent/ +tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_intfmgrd_INCLUDES) tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis \ -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread diff --git a/tests/mock_tests/common/mock_shell_command.cpp b/tests/mock_tests/common/mock_shell_command.cpp index f3ccfbfe5e..f410cf0ad8 100644 --- a/tests/mock_tests/common/mock_shell_command.cpp +++ b/tests/mock_tests/common/mock_shell_command.cpp @@ -1,6 +1,9 @@ #include #include +/* Override this pointer for custom behavior */ +int (*callback)(const std::string &cmd, std::string &stdout) = nullptr; + int mockCmdReturn = 0; std::string mockCmdStdcout = ""; std::vector mockCallArgs; @@ -8,8 +11,15 @@ std::vector mockCallArgs; namespace swss { int exec(const std::string &cmd, std::string &stdout) { - mockCallArgs.push_back(cmd); - stdout = mockCmdStdcout; - return mockCmdReturn; + if (callback != nullptr) + { + return callback(cmd, stdout); + } + else + { + mockCallArgs.push_back(cmd); + stdout = mockCmdStdcout; + return mockCmdReturn; + } } -} +} \ No newline at end of file diff --git a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp index f07fa9ccbd..2ed1a1b6af 100644 --- a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp +++ b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp @@ -5,25 +5,17 @@ #include #include "../mock_table.h" #include "warm_restart.h" -#define private public +#define private public #include "intfmgr.h" #undef private -/* Override this pointer for custom behavior */ -int (*callback)(const std::string &cmd, std::string &stdout) = nullptr; -std::vector mockCallArgs; - -namespace swss { - int exec(const std::string &cmd, std::string &stdout) - { - mockCallArgs.push_back(cmd); - return callback(cmd, stdout); - } -} +extern int (*callback)(const std::string &cmd, std::string &stdout); +extern std::vector mockCallArgs; bool Ethernet0IPv6Set = false; int cb(const std::string &cmd, std::string &stdout){ + mockCallArgs.push_back(cmd); if (cmd == "sysctl -w net.ipv6.conf.\"Ethernet0\".disable_ipv6=0") Ethernet0IPv6Set = true; else if (cmd.find("/sbin/ip -6 address \"add\"") == 0) { return Ethernet0IPv6Set ? 0 : 2; From 6134e80d92670066363c909fd45745d7af3429ed Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Sat, 21 May 2022 00:57:44 +0000 Subject: [PATCH 15/22] Minor fix Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/common/mock_shell_command.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mock_tests/common/mock_shell_command.cpp b/tests/mock_tests/common/mock_shell_command.cpp index f410cf0ad8..f56bf4ddd9 100644 --- a/tests/mock_tests/common/mock_shell_command.cpp +++ b/tests/mock_tests/common/mock_shell_command.cpp @@ -22,4 +22,4 @@ namespace swss { return mockCmdReturn; } } -} \ No newline at end of file +} From 06edeb9b2bfbc3e07b4a7f6f05aa6530336f0998 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Tue, 2 Aug 2022 08:10:29 +0000 Subject: [PATCH 16/22] Refactored mock_tests/ folder Signed-off-by: Vivek Reddy Karri --- tests/mock_tests/Makefile.am | 76 +++++++++---------- .../{ => common}/fake_response_publisher.cpp | 0 .../{ => common}/mock_consumerstatetable.cpp | 0 .../{ => common}/mock_dbconnector.cpp | 0 .../mock_tests/{ => common}/mock_hiredis.cpp | 0 .../{ => common}/mock_orchagent_main.cpp | 0 .../{ => common}/mock_redisreply.cpp | 0 tests/mock_tests/{ => common}/mock_table.cpp | 0 .../mock_tests/{ => common}/ut_saihelper.cpp | 0 .../fdborch/flush_syncd_notif_ut.cpp | 6 +- tests/mock_tests/{ => includes}/check.h | 0 .../{ => includes}/mock_orchagent_main.h | 0 tests/mock_tests/{ => includes}/mock_table.h | 0 tests/mock_tests/{ => includes}/portal.h | 0 tests/mock_tests/{ => includes}/saispy.h | 0 tests/mock_tests/{ => includes}/ut_helper.h | 0 .../intfmgrd/add_ipv6_prefix_ut.cpp | 2 +- tests/mock_tests/mock_shell_command.cpp | 15 ---- tests/mock_tests/portsyncd/portsyncd_ut.cpp | 2 +- 19 files changed, 40 insertions(+), 61 deletions(-) rename tests/mock_tests/{ => common}/fake_response_publisher.cpp (100%) rename tests/mock_tests/{ => common}/mock_consumerstatetable.cpp (100%) rename tests/mock_tests/{ => common}/mock_dbconnector.cpp (100%) rename tests/mock_tests/{ => common}/mock_hiredis.cpp (100%) rename tests/mock_tests/{ => common}/mock_orchagent_main.cpp (100%) rename tests/mock_tests/{ => common}/mock_redisreply.cpp (100%) rename tests/mock_tests/{ => common}/mock_table.cpp (100%) rename tests/mock_tests/{ => common}/ut_saihelper.cpp (100%) rename tests/mock_tests/{ => includes}/check.h (100%) rename tests/mock_tests/{ => includes}/mock_orchagent_main.h (100%) rename tests/mock_tests/{ => includes}/mock_table.h (100%) rename tests/mock_tests/{ => includes}/portal.h (100%) rename tests/mock_tests/{ => includes}/saispy.h (100%) rename tests/mock_tests/{ => includes}/ut_helper.h (100%) delete mode 100644 tests/mock_tests/mock_shell_command.cpp diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index bc13e2f3ad..7521bedb7b 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -9,6 +9,7 @@ TESTS = tests tests_intfmgrd tests_portsyncd noinst_PROGRAMS = tests tests_intfmgrd tests_portsyncd LDADD_SAI = -lsaimeta -lsaimetadata -lsaivs -lsairedis +LDADD_COMMON = -lnl-genl-3 -lhiredis -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread if DEBUG DBGFLAGS = -ggdb -DDEBUG @@ -19,9 +20,21 @@ endif CFLAGS_GTEST = LDADD_GTEST = -L/usr/src/gtest +mock_swsscommon_SOURCES = common/mock_shell_command.cpp \ + common/mock_redisreply.cpp \ + common/mock_dbconnector.cpp \ + common/mock_hiredis.cpp \ + common/mock_table.cpp \ + common/mock_consumerstatetable.cpp + +mock_swss_SOURCES = common/mock_orchagent_main.cpp \ + common/fake_response_publisher.cpp + +mock_sai_SOURCES = common/ut_saihelper.cpp + ## Orchagent Unit Tests -tests_INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/orchagent +tests_INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/orchagent -Iincludes tests_SOURCES = aclorch_ut.cpp \ portsorch_ut.cpp \ @@ -34,18 +47,12 @@ tests_SOURCES = aclorch_ut.cpp \ saispy_ut.cpp \ consumer_ut.cpp \ sfloworh_ut.cpp \ - ut_saihelper.cpp \ - mock_orchagent_main.cpp \ - mock_dbconnector.cpp \ - mock_consumerstatetable.cpp \ - common/mock_shell_command.cpp \ - mock_table.cpp \ - mock_hiredis.cpp \ - mock_redisreply.cpp \ bulker_ut.cpp \ portmgr_ut.cpp \ - fake_response_publisher.cpp \ swssnet_ut.cpp \ + $(mock_swsscommon_SOURCES) \ + $(mock_sai_SOURCES) \ + $(mock_swss_SOURCES) \ flowcounterrouteorch_ut.cpp \ $(top_srcdir)/lib/gearboxutils.cpp \ $(top_srcdir)/lib/subintf.cpp \ @@ -122,44 +129,31 @@ tests_SOURCES += $(P4_ORCH_DIR)/p4orch.cpp \ tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_INCLUDES) -tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthread \ - -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 +tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) $(LDADD_COMMON) + +## intfmgrd unit tests + +tests_intfmgrd_SOURCES = intfmgrd/add_ipv6_prefix_ut.cpp \ + $(top_srcdir)/cfgmgr/intfmgr.cpp \ + $(top_srcdir)/lib/subintf.cpp \ + $(top_srcdir)/orchagent/orch.cpp \ + $(top_srcdir)/orchagent/request_parser.cpp \ + $(mock_swsscommon_SOURCES) \ + $(mock_swss_SOURCES) + +tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) +tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_INCLUDES) +tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) $(LDADD_COMMON) ## portsyncd unit tests tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ $(top_srcdir)/portsyncd/linksync.cpp \ $(top_srcdir)/portsyncd/portsyncd_helper.cpp \ - mock_dbconnector.cpp \ - common/mock_shell_command.cpp \ - mock_table.cpp \ - mock_hiredis.cpp \ - mock_redisreply.cpp + $(mock_swsscommon_SOURCES) -tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr +tests_portsyncd_INCLUDES = -I $(top_srcdir)/lib -I$(top_srcdir)/portsyncd -I$(top_srcdir)/cfgmgr -Iincludes tests_portsyncd_CXXFLAGS = -Wl,-wrap,if_nameindex -Wl,-wrap,if_freenameindex 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 \ - -lswsscommon -lswsscommon -lgtest -lgtest_main -lnl-3 -lnl-route-3 -lpthread - -## intfmgrd unit tests - -tests_intfmgrd_SOURCES = intfmgrd/add_ipv6_prefix_ut.cpp \ - $(top_srcdir)/cfgmgr/intfmgr.cpp \ - $(top_srcdir)/lib/subintf.cpp \ - $(top_srcdir)/orchagent/orch.cpp \ - $(top_srcdir)/orchagent/request_parser.cpp \ - mock_orchagent_main.cpp \ - mock_dbconnector.cpp \ - mock_table.cpp \ - mock_hiredis.cpp \ - fake_response_publisher.cpp \ - mock_redisreply.cpp \ - common/mock_shell_command.cpp - -tests_intfmgrd_INCLUDES = $(tests_INCLUDES) -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/lib -tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_intfmgrd_INCLUDES) -tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis \ - -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread +tests_portsyncd_LDADD = $(LDADD_GTEST) $(LDADD_COMMON) diff --git a/tests/mock_tests/fake_response_publisher.cpp b/tests/mock_tests/common/fake_response_publisher.cpp similarity index 100% rename from tests/mock_tests/fake_response_publisher.cpp rename to tests/mock_tests/common/fake_response_publisher.cpp diff --git a/tests/mock_tests/mock_consumerstatetable.cpp b/tests/mock_tests/common/mock_consumerstatetable.cpp similarity index 100% rename from tests/mock_tests/mock_consumerstatetable.cpp rename to tests/mock_tests/common/mock_consumerstatetable.cpp diff --git a/tests/mock_tests/mock_dbconnector.cpp b/tests/mock_tests/common/mock_dbconnector.cpp similarity index 100% rename from tests/mock_tests/mock_dbconnector.cpp rename to tests/mock_tests/common/mock_dbconnector.cpp diff --git a/tests/mock_tests/mock_hiredis.cpp b/tests/mock_tests/common/mock_hiredis.cpp similarity index 100% rename from tests/mock_tests/mock_hiredis.cpp rename to tests/mock_tests/common/mock_hiredis.cpp diff --git a/tests/mock_tests/mock_orchagent_main.cpp b/tests/mock_tests/common/mock_orchagent_main.cpp similarity index 100% rename from tests/mock_tests/mock_orchagent_main.cpp rename to tests/mock_tests/common/mock_orchagent_main.cpp diff --git a/tests/mock_tests/mock_redisreply.cpp b/tests/mock_tests/common/mock_redisreply.cpp similarity index 100% rename from tests/mock_tests/mock_redisreply.cpp rename to tests/mock_tests/common/mock_redisreply.cpp diff --git a/tests/mock_tests/mock_table.cpp b/tests/mock_tests/common/mock_table.cpp similarity index 100% rename from tests/mock_tests/mock_table.cpp rename to tests/mock_tests/common/mock_table.cpp diff --git a/tests/mock_tests/ut_saihelper.cpp b/tests/mock_tests/common/ut_saihelper.cpp similarity index 100% rename from tests/mock_tests/ut_saihelper.cpp rename to tests/mock_tests/common/ut_saihelper.cpp diff --git a/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp b/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp index 7bf22373c1..ccdb832d5e 100644 --- a/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp +++ b/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp @@ -1,6 +1,6 @@ -#include "../ut_helper.h" -#include "../mock_orchagent_main.h" -#include "../mock_table.h" +#include "ut_helper.h" +#include "mock_orchagent_main.h" +#include "mock_table.h" #include "port.h" #define private public // Need to modify internal cache #include "portsorch.h" diff --git a/tests/mock_tests/check.h b/tests/mock_tests/includes/check.h similarity index 100% rename from tests/mock_tests/check.h rename to tests/mock_tests/includes/check.h diff --git a/tests/mock_tests/mock_orchagent_main.h b/tests/mock_tests/includes/mock_orchagent_main.h similarity index 100% rename from tests/mock_tests/mock_orchagent_main.h rename to tests/mock_tests/includes/mock_orchagent_main.h diff --git a/tests/mock_tests/mock_table.h b/tests/mock_tests/includes/mock_table.h similarity index 100% rename from tests/mock_tests/mock_table.h rename to tests/mock_tests/includes/mock_table.h diff --git a/tests/mock_tests/portal.h b/tests/mock_tests/includes/portal.h similarity index 100% rename from tests/mock_tests/portal.h rename to tests/mock_tests/includes/portal.h diff --git a/tests/mock_tests/saispy.h b/tests/mock_tests/includes/saispy.h similarity index 100% rename from tests/mock_tests/saispy.h rename to tests/mock_tests/includes/saispy.h diff --git a/tests/mock_tests/ut_helper.h b/tests/mock_tests/includes/ut_helper.h similarity index 100% rename from tests/mock_tests/ut_helper.h rename to tests/mock_tests/includes/ut_helper.h diff --git a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp index 2ed1a1b6af..83b68800e8 100644 --- a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp +++ b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp @@ -3,7 +3,7 @@ #include #include #include -#include "../mock_table.h" +#include "mock_table.h" #include "warm_restart.h" #define private public #include "intfmgr.h" diff --git a/tests/mock_tests/mock_shell_command.cpp b/tests/mock_tests/mock_shell_command.cpp deleted file mode 100644 index f3ccfbfe5e..0000000000 --- a/tests/mock_tests/mock_shell_command.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -int mockCmdReturn = 0; -std::string mockCmdStdcout = ""; -std::vector mockCallArgs; - -namespace swss { - int exec(const std::string &cmd, std::string &stdout) - { - mockCallArgs.push_back(cmd); - stdout = mockCmdStdcout; - return mockCmdReturn; - } -} diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index 46726c10d2..aeb1915ec1 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -1,7 +1,7 @@ #include "gtest/gtest.h" #include #include -#include "../mock_table.h" +#include "mock_table.h" #define private public #include "linksync.h" #undef private From 98b63585196925ffff32e6a28da05fa73bbd802e Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Tue, 2 Aug 2022 08:23:25 +0000 Subject: [PATCH 17/22] p4orch tests updated Signed-off-by: Vivek Reddy Karri --- orchagent/p4orch/tests/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orchagent/p4orch/tests/Makefile.am b/orchagent/p4orch/tests/Makefile.am index daeb484136..1c02bda759 100644 --- a/orchagent/p4orch/tests/Makefile.am +++ b/orchagent/p4orch/tests/Makefile.am @@ -42,7 +42,7 @@ p4orch_tests_SOURCES = $(ORCHAGENT_DIR)/orch.cpp \ $(P4ORCH_DIR)/acl_rule_manager.cpp \ $(P4ORCH_DIR)/wcmp_manager.cpp \ $(P4ORCH_DIR)/mirror_session_manager.cpp \ - $(top_srcdir)/tests/mock_tests/fake_response_publisher.cpp \ + $(top_srcdir)/tests/mock_tests/common/fake_response_publisher.cpp \ fake_portorch.cpp \ fake_crmorch.cpp \ fake_flexcounterorch.cpp \ From d436281c79a2820a67a008e511fca364c1dbc90a Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 5 Aug 2022 17:07:30 +0000 Subject: [PATCH 18/22] Revert "p4orch tests updated" This reverts commit 98b63585196925ffff32e6a28da05fa73bbd802e. --- orchagent/p4orch/tests/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/orchagent/p4orch/tests/Makefile.am b/orchagent/p4orch/tests/Makefile.am index 1c02bda759..daeb484136 100644 --- a/orchagent/p4orch/tests/Makefile.am +++ b/orchagent/p4orch/tests/Makefile.am @@ -42,7 +42,7 @@ p4orch_tests_SOURCES = $(ORCHAGENT_DIR)/orch.cpp \ $(P4ORCH_DIR)/acl_rule_manager.cpp \ $(P4ORCH_DIR)/wcmp_manager.cpp \ $(P4ORCH_DIR)/mirror_session_manager.cpp \ - $(top_srcdir)/tests/mock_tests/common/fake_response_publisher.cpp \ + $(top_srcdir)/tests/mock_tests/fake_response_publisher.cpp \ fake_portorch.cpp \ fake_crmorch.cpp \ fake_flexcounterorch.cpp \ From 70099b43b898eb3d2ec0ba7c508361100823a685 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Fri, 5 Aug 2022 17:07:46 +0000 Subject: [PATCH 19/22] Revert "Refactored mock_tests/ folder" This reverts commit 06edeb9b2bfbc3e07b4a7f6f05aa6530336f0998. --- tests/mock_tests/Makefile.am | 76 ++++++++++--------- tests/mock_tests/{includes => }/check.h | 0 .../{common => }/fake_response_publisher.cpp | 0 .../fdborch/flush_syncd_notif_ut.cpp | 6 +- .../intfmgrd/add_ipv6_prefix_ut.cpp | 2 +- .../{common => }/mock_consumerstatetable.cpp | 0 .../{common => }/mock_dbconnector.cpp | 0 .../mock_tests/{common => }/mock_hiredis.cpp | 0 .../{common => }/mock_orchagent_main.cpp | 0 .../{includes => }/mock_orchagent_main.h | 0 .../{common => }/mock_redisreply.cpp | 0 tests/mock_tests/mock_shell_command.cpp | 15 ++++ tests/mock_tests/{common => }/mock_table.cpp | 0 tests/mock_tests/{includes => }/mock_table.h | 0 tests/mock_tests/{includes => }/portal.h | 0 tests/mock_tests/portsyncd/portsyncd_ut.cpp | 2 +- tests/mock_tests/{includes => }/saispy.h | 0 tests/mock_tests/{includes => }/ut_helper.h | 0 .../mock_tests/{common => }/ut_saihelper.cpp | 0 19 files changed, 61 insertions(+), 40 deletions(-) rename tests/mock_tests/{includes => }/check.h (100%) rename tests/mock_tests/{common => }/fake_response_publisher.cpp (100%) rename tests/mock_tests/{common => }/mock_consumerstatetable.cpp (100%) rename tests/mock_tests/{common => }/mock_dbconnector.cpp (100%) rename tests/mock_tests/{common => }/mock_hiredis.cpp (100%) rename tests/mock_tests/{common => }/mock_orchagent_main.cpp (100%) rename tests/mock_tests/{includes => }/mock_orchagent_main.h (100%) rename tests/mock_tests/{common => }/mock_redisreply.cpp (100%) create mode 100644 tests/mock_tests/mock_shell_command.cpp rename tests/mock_tests/{common => }/mock_table.cpp (100%) rename tests/mock_tests/{includes => }/mock_table.h (100%) rename tests/mock_tests/{includes => }/portal.h (100%) rename tests/mock_tests/{includes => }/saispy.h (100%) rename tests/mock_tests/{includes => }/ut_helper.h (100%) rename tests/mock_tests/{common => }/ut_saihelper.cpp (100%) diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index 7521bedb7b..bc13e2f3ad 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -9,7 +9,6 @@ TESTS = tests tests_intfmgrd tests_portsyncd noinst_PROGRAMS = tests tests_intfmgrd tests_portsyncd LDADD_SAI = -lsaimeta -lsaimetadata -lsaivs -lsairedis -LDADD_COMMON = -lnl-genl-3 -lhiredis -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread if DEBUG DBGFLAGS = -ggdb -DDEBUG @@ -20,21 +19,9 @@ endif CFLAGS_GTEST = LDADD_GTEST = -L/usr/src/gtest -mock_swsscommon_SOURCES = common/mock_shell_command.cpp \ - common/mock_redisreply.cpp \ - common/mock_dbconnector.cpp \ - common/mock_hiredis.cpp \ - common/mock_table.cpp \ - common/mock_consumerstatetable.cpp - -mock_swss_SOURCES = common/mock_orchagent_main.cpp \ - common/fake_response_publisher.cpp - -mock_sai_SOURCES = common/ut_saihelper.cpp - ## Orchagent Unit Tests -tests_INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/orchagent -Iincludes +tests_INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/orchagent tests_SOURCES = aclorch_ut.cpp \ portsorch_ut.cpp \ @@ -47,12 +34,18 @@ tests_SOURCES = aclorch_ut.cpp \ saispy_ut.cpp \ consumer_ut.cpp \ sfloworh_ut.cpp \ + ut_saihelper.cpp \ + mock_orchagent_main.cpp \ + mock_dbconnector.cpp \ + mock_consumerstatetable.cpp \ + common/mock_shell_command.cpp \ + mock_table.cpp \ + mock_hiredis.cpp \ + mock_redisreply.cpp \ bulker_ut.cpp \ portmgr_ut.cpp \ + fake_response_publisher.cpp \ swssnet_ut.cpp \ - $(mock_swsscommon_SOURCES) \ - $(mock_sai_SOURCES) \ - $(mock_swss_SOURCES) \ flowcounterrouteorch_ut.cpp \ $(top_srcdir)/lib/gearboxutils.cpp \ $(top_srcdir)/lib/subintf.cpp \ @@ -129,31 +122,44 @@ tests_SOURCES += $(P4_ORCH_DIR)/p4orch.cpp \ tests_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) tests_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_INCLUDES) -tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) $(LDADD_COMMON) - -## intfmgrd unit tests - -tests_intfmgrd_SOURCES = intfmgrd/add_ipv6_prefix_ut.cpp \ - $(top_srcdir)/cfgmgr/intfmgr.cpp \ - $(top_srcdir)/lib/subintf.cpp \ - $(top_srcdir)/orchagent/orch.cpp \ - $(top_srcdir)/orchagent/request_parser.cpp \ - $(mock_swsscommon_SOURCES) \ - $(mock_swss_SOURCES) - -tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) -tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_INCLUDES) -tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) $(LDADD_COMMON) +tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthread \ + -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 ## portsyncd unit tests tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ $(top_srcdir)/portsyncd/linksync.cpp \ $(top_srcdir)/portsyncd/portsyncd_helper.cpp \ - $(mock_swsscommon_SOURCES) + mock_dbconnector.cpp \ + common/mock_shell_command.cpp \ + mock_table.cpp \ + mock_hiredis.cpp \ + mock_redisreply.cpp -tests_portsyncd_INCLUDES = -I $(top_srcdir)/lib -I$(top_srcdir)/portsyncd -I$(top_srcdir)/cfgmgr -Iincludes +tests_portsyncd_INCLUDES = -I $(top_srcdir)/portsyncd -I $(top_srcdir)/cfgmgr tests_portsyncd_CXXFLAGS = -Wl,-wrap,if_nameindex -Wl,-wrap,if_freenameindex 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) $(LDADD_COMMON) +tests_portsyncd_LDADD = $(LDADD_GTEST) -lnl-genl-3 -lhiredis -lhiredis \ + -lswsscommon -lswsscommon -lgtest -lgtest_main -lnl-3 -lnl-route-3 -lpthread + +## intfmgrd unit tests + +tests_intfmgrd_SOURCES = intfmgrd/add_ipv6_prefix_ut.cpp \ + $(top_srcdir)/cfgmgr/intfmgr.cpp \ + $(top_srcdir)/lib/subintf.cpp \ + $(top_srcdir)/orchagent/orch.cpp \ + $(top_srcdir)/orchagent/request_parser.cpp \ + mock_orchagent_main.cpp \ + mock_dbconnector.cpp \ + mock_table.cpp \ + mock_hiredis.cpp \ + fake_response_publisher.cpp \ + mock_redisreply.cpp \ + common/mock_shell_command.cpp + +tests_intfmgrd_INCLUDES = $(tests_INCLUDES) -I$(top_srcdir)/cfgmgr -I$(top_srcdir)/lib +tests_intfmgrd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) +tests_intfmgrd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_GTEST) $(CFLAGS_SAI) $(tests_intfmgrd_INCLUDES) +tests_intfmgrd_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis \ + -lswsscommon -lswsscommon -lgtest -lgtest_main -lzmq -lnl-3 -lnl-route-3 -lpthread diff --git a/tests/mock_tests/includes/check.h b/tests/mock_tests/check.h similarity index 100% rename from tests/mock_tests/includes/check.h rename to tests/mock_tests/check.h diff --git a/tests/mock_tests/common/fake_response_publisher.cpp b/tests/mock_tests/fake_response_publisher.cpp similarity index 100% rename from tests/mock_tests/common/fake_response_publisher.cpp rename to tests/mock_tests/fake_response_publisher.cpp diff --git a/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp b/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp index ccdb832d5e..7bf22373c1 100644 --- a/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp +++ b/tests/mock_tests/fdborch/flush_syncd_notif_ut.cpp @@ -1,6 +1,6 @@ -#include "ut_helper.h" -#include "mock_orchagent_main.h" -#include "mock_table.h" +#include "../ut_helper.h" +#include "../mock_orchagent_main.h" +#include "../mock_table.h" #include "port.h" #define private public // Need to modify internal cache #include "portsorch.h" diff --git a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp index 83b68800e8..2ed1a1b6af 100644 --- a/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp +++ b/tests/mock_tests/intfmgrd/add_ipv6_prefix_ut.cpp @@ -3,7 +3,7 @@ #include #include #include -#include "mock_table.h" +#include "../mock_table.h" #include "warm_restart.h" #define private public #include "intfmgr.h" diff --git a/tests/mock_tests/common/mock_consumerstatetable.cpp b/tests/mock_tests/mock_consumerstatetable.cpp similarity index 100% rename from tests/mock_tests/common/mock_consumerstatetable.cpp rename to tests/mock_tests/mock_consumerstatetable.cpp diff --git a/tests/mock_tests/common/mock_dbconnector.cpp b/tests/mock_tests/mock_dbconnector.cpp similarity index 100% rename from tests/mock_tests/common/mock_dbconnector.cpp rename to tests/mock_tests/mock_dbconnector.cpp diff --git a/tests/mock_tests/common/mock_hiredis.cpp b/tests/mock_tests/mock_hiredis.cpp similarity index 100% rename from tests/mock_tests/common/mock_hiredis.cpp rename to tests/mock_tests/mock_hiredis.cpp diff --git a/tests/mock_tests/common/mock_orchagent_main.cpp b/tests/mock_tests/mock_orchagent_main.cpp similarity index 100% rename from tests/mock_tests/common/mock_orchagent_main.cpp rename to tests/mock_tests/mock_orchagent_main.cpp diff --git a/tests/mock_tests/includes/mock_orchagent_main.h b/tests/mock_tests/mock_orchagent_main.h similarity index 100% rename from tests/mock_tests/includes/mock_orchagent_main.h rename to tests/mock_tests/mock_orchagent_main.h diff --git a/tests/mock_tests/common/mock_redisreply.cpp b/tests/mock_tests/mock_redisreply.cpp similarity index 100% rename from tests/mock_tests/common/mock_redisreply.cpp rename to tests/mock_tests/mock_redisreply.cpp diff --git a/tests/mock_tests/mock_shell_command.cpp b/tests/mock_tests/mock_shell_command.cpp new file mode 100644 index 0000000000..f3ccfbfe5e --- /dev/null +++ b/tests/mock_tests/mock_shell_command.cpp @@ -0,0 +1,15 @@ +#include +#include + +int mockCmdReturn = 0; +std::string mockCmdStdcout = ""; +std::vector mockCallArgs; + +namespace swss { + int exec(const std::string &cmd, std::string &stdout) + { + mockCallArgs.push_back(cmd); + stdout = mockCmdStdcout; + return mockCmdReturn; + } +} diff --git a/tests/mock_tests/common/mock_table.cpp b/tests/mock_tests/mock_table.cpp similarity index 100% rename from tests/mock_tests/common/mock_table.cpp rename to tests/mock_tests/mock_table.cpp diff --git a/tests/mock_tests/includes/mock_table.h b/tests/mock_tests/mock_table.h similarity index 100% rename from tests/mock_tests/includes/mock_table.h rename to tests/mock_tests/mock_table.h diff --git a/tests/mock_tests/includes/portal.h b/tests/mock_tests/portal.h similarity index 100% rename from tests/mock_tests/includes/portal.h rename to tests/mock_tests/portal.h diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index aeb1915ec1..46726c10d2 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -1,7 +1,7 @@ #include "gtest/gtest.h" #include #include -#include "mock_table.h" +#include "../mock_table.h" #define private public #include "linksync.h" #undef private diff --git a/tests/mock_tests/includes/saispy.h b/tests/mock_tests/saispy.h similarity index 100% rename from tests/mock_tests/includes/saispy.h rename to tests/mock_tests/saispy.h diff --git a/tests/mock_tests/includes/ut_helper.h b/tests/mock_tests/ut_helper.h similarity index 100% rename from tests/mock_tests/includes/ut_helper.h rename to tests/mock_tests/ut_helper.h diff --git a/tests/mock_tests/common/ut_saihelper.cpp b/tests/mock_tests/ut_saihelper.cpp similarity index 100% rename from tests/mock_tests/common/ut_saihelper.cpp rename to tests/mock_tests/ut_saihelper.cpp From 41d31e8f68f2f8e57314ab89b6840fcdeb9d7a10 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Sat, 27 Aug 2022 00:35:24 +0000 Subject: [PATCH 20/22] Moved the fun/global vars to orig location and removed tests Signed-off-by: Vivek Reddy Karri --- portsyncd/Makefile.am | 2 +- portsyncd/linksync.h | 10 +-- portsyncd/portsyncd.cpp | 69 ++++++++++++++++++++- portsyncd/portsyncd_helper.cpp | 64 ------------------- tests/mock_tests/Makefile.am | 1 - tests/mock_tests/portsyncd/portsyncd_ut.cpp | 65 ++++++++----------- 6 files changed, 95 insertions(+), 116 deletions(-) delete mode 100644 portsyncd/portsyncd_helper.cpp diff --git a/portsyncd/Makefile.am b/portsyncd/Makefile.am index cc4987f579..73f5707198 100644 --- a/portsyncd/Makefile.am +++ b/portsyncd/Makefile.am @@ -8,7 +8,7 @@ else DBGFLAGS = -g endif -portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp portsyncd_helper.cpp $(top_srcdir)/cfgmgr/shellcmd.h +portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN) portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN) diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index ecbc690102..5f8ddb41a5 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -4,16 +4,8 @@ #include "dbconnector.h" #include "producerstatetable.h" #include "netmsg.h" -#include "exec.h" -#include "warm_restart.h" -#include "shellcmd.h" #include -#include -#include -#include -#include -#include namespace swss { @@ -36,6 +28,6 @@ class LinkSync : public NetMsg } -void handlePortConfigFromConfigDB(swss::ProducerStateTable &, swss::DBConnector &, bool ); + #endif diff --git a/portsyncd/portsyncd.cpp b/portsyncd/portsyncd.cpp index 1873d14acb..c12e44e4f6 100644 --- a/portsyncd/portsyncd.cpp +++ b/portsyncd/portsyncd.cpp @@ -2,13 +2,19 @@ #include #include #include +#include +#include +#include +#include #include #include "dbconnector.h" #include "select.h" #include "netdispatcher.h" #include "netlink.h" +#include "producerstatetable.h" #include "portsyncd/linksync.h" #include "subscriberstatetable.h" +#include "exec.h" #include "warm_restart.h" using namespace std; @@ -16,8 +22,18 @@ using namespace swss; #define DEFAULT_SELECT_TIMEOUT 1000 /* ms */ -extern set g_portSet; -extern bool g_init; +/* + * This g_portSet contains all the front panel ports that the corresponding + * host interfaces needed to be created. When this LinkSync class is + * initialized, we check the database to see if some of the ports' host + * interfaces are already created and remove them from this set. We will + * remove the rest of the ports in the set when receiving the first netlink + * message indicating that the host interfaces are created. After the set + * is empty, we send out the signal PortInitDone. g_init is used to limit the + * command to be run only once. + */ +set g_portSet; +bool g_init = false; void usage() { @@ -26,6 +42,8 @@ void usage() cout << " this program will exit if configDB does not contain that info" << endl; } +void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm); + int main(int argc, char **argv) { Logger::linkToDbNative("portsyncd"); @@ -131,3 +149,50 @@ int main(int argc, char **argv) return 1; } + +static void notifyPortConfigDone(ProducerStateTable &p) +{ + /* Notify that all ports added */ + FieldValueTuple finish_notice("count", to_string(g_portSet.size())); + vector attrs = { finish_notice }; + p.set("PortConfigDone", attrs); +} + +void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) +{ + SWSS_LOG_ENTER(); + + SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); + + Table table(&cfgDb, CFG_PORT_TABLE_NAME); + std::vector ovalues; + std::vector keys; + table.getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_NOTICE("ConfigDB does not have port information, " + "however ports can be added later on, continuing..."); + } + + for ( auto &k : keys ) + { + table.get(k, ovalues); + vector attrs; + for ( auto &v : ovalues ) + { + FieldValueTuple attr(v.first, v.second); + attrs.push_back(attr); + } + if (!warm) + { + p.set(k, attrs); + } + g_portSet.insert(k); + } + if (!warm) + { + notifyPortConfigDone(p); + } + +} diff --git a/portsyncd/portsyncd_helper.cpp b/portsyncd/portsyncd_helper.cpp deleted file mode 100644 index 80a0798e2b..0000000000 --- a/portsyncd/portsyncd_helper.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "portsyncd/linksync.h" - -using namespace std; -using namespace swss; - -/* - * This g_portSet contains all the front panel ports that the corresponding - * host interfaces needed to be created. When this LinkSync class is - * initialized, we check the database to see if some of the ports' host - * interfaces are already created and remove them from this set. We will - * remove the rest of the ports in the set when receiving the first netlink - * message indicating that the host interfaces are created. After the set - * is empty, we send out the signal PortInitDone. g_init is used to limit the - * command to be run only once. - */ -set g_portSet; -bool g_init; - -static void notifyPortConfigDone(ProducerStateTable &p) -{ - /* Notify that all ports added */ - FieldValueTuple finish_notice("count", to_string(g_portSet.size())); - vector attrs = { finish_notice }; - p.set("PortConfigDone", attrs); -} - -void handlePortConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) -{ - SWSS_LOG_ENTER(); - - SWSS_LOG_NOTICE("Getting port configuration from ConfigDB..."); - - Table table(&cfgDb, CFG_PORT_TABLE_NAME); - std::vector ovalues; - std::vector keys; - table.getKeys(keys); - - if (keys.empty()) - { - SWSS_LOG_NOTICE("ConfigDB does not have port information, " - "however ports can be added later on, continuing..."); - } - - for ( auto &k : keys ) - { - table.get(k, ovalues); - vector attrs; - for ( auto &v : ovalues ) - { - FieldValueTuple attr(v.first, v.second); - attrs.push_back(attr); - } - if (!warm) - { - p.set(k, attrs); - } - g_portSet.insert(k); - } - if (!warm) - { - notifyPortConfigDone(p); - } - -} diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index bc13e2f3ad..18e4db9c0e 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -129,7 +129,6 @@ tests_LDADD = $(LDADD_GTEST) $(LDADD_SAI) -lnl-genl-3 -lhiredis -lhiredis -lpthr tests_portsyncd_SOURCES = portsyncd/portsyncd_ut.cpp \ $(top_srcdir)/portsyncd/linksync.cpp \ - $(top_srcdir)/portsyncd/portsyncd_helper.cpp \ mock_dbconnector.cpp \ common/mock_shell_command.cpp \ mock_table.cpp \ diff --git a/tests/mock_tests/portsyncd/portsyncd_ut.cpp b/tests/mock_tests/portsyncd/portsyncd_ut.cpp index 46726c10d2..a7aaf0f9f8 100644 --- a/tests/mock_tests/portsyncd/portsyncd_ut.cpp +++ b/tests/mock_tests/portsyncd/portsyncd_ut.cpp @@ -1,7 +1,7 @@ #include "gtest/gtest.h" #include #include -#include "../mock_table.h" +#include "mock_table.h" #define private public #include "linksync.h" #undef private @@ -26,7 +26,29 @@ extern "C" { extern std::string mockCmdStdcout; extern std::vector mockCallArgs; -extern std::set g_portSet; +std::set g_portSet; +bool g_init = false; + +void writeToApplDB(swss::ProducerStateTable &p, swss::DBConnector &cfgDb) +{ + swss::Table table(&cfgDb, CFG_PORT_TABLE_NAME); + std::vector ovalues; + std::vector keys; + table.getKeys(keys); + + for ( auto &k : keys ) + { + table.get(k, ovalues); + std::vector attrs; + for ( auto &v : ovalues ) + { + swss::FieldValueTuple attr(v.first, v.second); + attrs.push_back(attr); + } + p.set(k, attrs); + g_portSet.insert(k); + } +} /* Test Fixture @@ -175,41 +197,6 @@ namespace portsyncd_ut 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 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 keys_to_app_db; - m_portAppTable->getKeys(keys_to_app_db); - ASSERT_EQ(keys_to_app_db.size(), 0); - } TEST_F(PortSyncdTest, test_cacheOldIfaces) { @@ -229,7 +216,7 @@ namespace portsyncd_ut /* Handle CFG DB notifs and Write them to APPL_DB */ swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME); - handlePortConfigFromConfigDB(p, cfg_db_conn, false); + writeToApplDB(p, cfg_db_conn); /* Generate a netlink notification about the netdev iface */ std::vector flags = {IFF_UP, IFF_RUNNING}; @@ -271,7 +258,7 @@ namespace portsyncd_ut /* Handle CFG DB notifs and Write them to APPL_DB */ swss::ProducerStateTable p(m_app_db.get(), APP_PORT_TABLE_NAME); - handlePortConfigFromConfigDB(p, cfg_db_conn, false); + writeToApplDB(p, cfg_db_conn);; /* Generate a netlink notification about the netdev iface */ std::vector flags = {IFF_UP, IFF_RUNNING}; From 94b9e30b43998946a79c726127a6a77ce3fb9f8b Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Sat, 27 Aug 2022 00:38:43 +0000 Subject: [PATCH 21/22] Minor deviations Signed-off-by: Vivek Reddy Karri --- portsyncd/Makefile.am | 2 +- portsyncd/linksync.cpp | 4 +++- portsyncd/linksync.h | 2 -- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/portsyncd/Makefile.am b/portsyncd/Makefile.am index 73f5707198..3db6187059 100644 --- a/portsyncd/Makefile.am +++ b/portsyncd/Makefile.am @@ -8,7 +8,7 @@ else DBGFLAGS = -g endif -portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h +portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN) portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(CFLAGS_ASAN) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index 2e7a71d99e..639d4f2c8f 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -6,6 +6,8 @@ #include #include "logger.h" #include "netmsg.h" +#include "dbconnector.h" +#include "producerstatetable.h" #include "tokenize.h" #include "exec.h" @@ -88,7 +90,7 @@ LinkSync::LinkSync(DBConnector *appl_db, DBConnector *state_db) : if (!WarmStart::isWarmStart()) { - /* See the comments for g_portSet */ + /* See the comments for g_portSet in portsyncd.cpp */ for (auto port_iter = g_portSet.begin(); port_iter != g_portSet.end();) { string port = *port_iter; diff --git a/portsyncd/linksync.h b/portsyncd/linksync.h index 5f8ddb41a5..d72e1ba124 100644 --- a/portsyncd/linksync.h +++ b/portsyncd/linksync.h @@ -28,6 +28,4 @@ class LinkSync : public NetMsg } - - #endif From aa8e7e7ceec2f5425172fb99625a95d40e6bc8e9 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Karri Date: Sat, 27 Aug 2022 00:39:58 +0000 Subject: [PATCH 22/22] Minor deviations Signed-off-by: Vivek Reddy Karri --- portsyncd/linksync.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/portsyncd/linksync.cpp b/portsyncd/linksync.cpp index 639d4f2c8f..fc28411613 100644 --- a/portsyncd/linksync.cpp +++ b/portsyncd/linksync.cpp @@ -260,4 +260,3 @@ void LinkSync::onMsg(int nlmsg_type, struct nl_object *obj) SWSS_LOG_NOTICE("Cannot find %s in port table", key.c_str()); } } -