Skip to content

Commit

Permalink
Add RealObjectIdManager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Aug 23, 2021
1 parent 5f6b004 commit ab2ef9d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 27 deletions.
3 changes: 2 additions & 1 deletion unittest/vslib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ tests_SOURCES = main.cpp \
TestMACsecEgressFilter.cpp \
TestMACsecForwarder.cpp \
TestMACsecIngressFilter.cpp \
TestNetMsgRegistrar.cpp
TestNetMsgRegistrar.cpp \
TestRealObjectIdManager.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/vslib/libSaiVS.a -lhiredis -lswsscommon -lnl-genl-3 -lnl-nf-3 -lnl-route-3 -lnl-3 \
Expand Down
4 changes: 4 additions & 0 deletions unittest/vslib/TestNetMsgRegistrar.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "NetMsgRegistrar.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace saivs;

static void callback(int, struct nl_object*)
{
SWSS_LOG_ENTER();

// empty
}

Expand Down
88 changes: 88 additions & 0 deletions unittest/vslib/TestRealObjectIdManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "RealObjectIdManager.h"

#include <gtest/gtest.h>

using namespace saivs;

TEST(RealObjectIdManager, ctr)
{
EXPECT_THROW(std::make_shared<RealObjectIdManager>(10000, nullptr), std::runtime_error);

EXPECT_THROW(std::make_shared<RealObjectIdManager>(0, nullptr), std::runtime_error);

auto scc = std::make_shared<SwitchConfigContainer>();

std::make_shared<RealObjectIdManager>(0, scc);
}

TEST(RealObjectIdManager, saiSwitchIdQuery)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

EXPECT_EQ(mgr.saiSwitchIdQuery(0), 0);

EXPECT_THROW(mgr.saiSwitchIdQuery(1), std::runtime_error);
}

TEST(RealObjectIdManager, saiObjectTypeQuery)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

EXPECT_EQ(mgr.saiObjectTypeQuery(0), 0);

EXPECT_EQ(mgr.saiObjectTypeQuery(-1), 0);
}

TEST(RealObjectIdManager, clear)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

mgr.clear();
}

TEST(RealObjectIdManager, updateWarmBootObjectIndex)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

EXPECT_THROW(mgr.updateWarmBootObjectIndex(0), std::runtime_error);
}

TEST(RealObjectIdManager, switchIdQuery)
{
EXPECT_EQ(RealObjectIdManager::switchIdQuery(1), 0);
}

TEST(RealObjectIdManager, allocateNewSwitchObjectId)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

EXPECT_EQ(mgr.allocateNewSwitchObjectId("foo"), 0);
}

TEST(RealObjectIdManager, allocateNewObjectId)
{
auto scc = std::make_shared<SwitchConfigContainer>();

RealObjectIdManager mgr(0, scc);

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
EXPECT_THROW(mgr.allocateNewObjectId((sai_object_type_t)1000,(sai_object_id_t)-1), std::runtime_error);
#pragma GCC diagnostic pop

EXPECT_THROW(mgr.allocateNewObjectId(SAI_OBJECT_TYPE_SWITCH, (sai_object_id_t)-1), std::runtime_error);

}



24 changes: 5 additions & 19 deletions vslib/RealObjectIdManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ RealObjectIdManager::RealObjectIdManager(
globalContext,
SAI_VS_GLOBAL_CONTEXT_MAX);
}

if (container == nullptr)
{
SWSS_LOG_THROW("switch config container cannot be nullptr");
}
}

sai_object_id_t RealObjectIdManager::saiSwitchIdQuery(
Expand Down Expand Up @@ -172,25 +177,6 @@ void RealObjectIdManager::clear()
m_indexer.clear();
}

uint32_t RealObjectIdManager::allocateNewSwitchIndex()
{
SWSS_LOG_ENTER();

for (uint32_t index = 0; index < SAI_VS_SWITCH_INDEX_MAX; ++index)
{
if (m_switchIndexes.find(index) != m_switchIndexes.end())
continue;

m_switchIndexes.insert(index);

SWSS_LOG_NOTICE("allocated new switch index 0x%x", index);

return index;
}

SWSS_LOG_THROW("no more available switch indexes (used count is: %zu)", m_switchIndexes.size());
}

void RealObjectIdManager::releaseSwitchIndex(
_In_ uint32_t index)
{
Expand Down
7 changes: 0 additions & 7 deletions vslib/RealObjectIdManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ namespace saivs
void releaseSwitchIndex(
_In_ uint32_t index);

/**
* @brief Allocate new switch index.
*
* Will throw if there are no more available switch indexes.
*/
uint32_t allocateNewSwitchIndex();

/**
* @brief Construct object id.
*
Expand Down

0 comments on commit ab2ef9d

Please sign in to comment.