Skip to content

Commit

Permalink
Add sairedis lib unit tests (sonic-net#919)
Browse files Browse the repository at this point in the history
* [tests] Remove new operator from create objects

* [sairedis] Move config examples from src dir to lib dir

* [sairedis] Correct endpoint naming in client and server

* Add ClientConfig tests

* Add ClientServerSai tests

* Fix ClientConfig tests

* Add Context tests

* Add ContextConfig tests

* Add ContextConfigContainer tests

* Add Utils tests

* Fix Switch tests
  • Loading branch information
kcudnik authored Sep 4, 2021
1 parent 536af40 commit d8eb0ea
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/ClientConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::shared_ptr<ClientConfig> ClientConfig::loadFromFile(
auto cc = std::make_shared<ClientConfig>();

cc->m_zmqEndpoint = j["zmq_endpoint"];
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];
cc->m_zmqNtfEndpoint = j["zmq_ntf_endpoint"];

SWSS_LOG_NOTICE("client config: %s, %s",
cc->m_zmqEndpoint.c_str(),
Expand Down
2 changes: 1 addition & 1 deletion lib/ServerConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ std::shared_ptr<ServerConfig> ServerConfig::loadFromFile(
auto cc = std::make_shared<ServerConfig>();

cc->m_zmqEndpoint = j["zmq_endpoint"];
cc->m_zmqNtfEndpoint = j["zmq_endpoint_ntf"];
cc->m_zmqNtfEndpoint = j["zmq_ntf_endpoint"];

SWSS_LOG_NOTICE("server config: %s, %s",
cc->m_zmqEndpoint.c_str(),
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 8 additions & 2 deletions unittest/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ LDADD_GTEST = -L/usr/src/gtest -lgtest -lgtest_main

tests_SOURCES = \
main.cpp \
TestSwitch.cpp
TestSwitch.cpp \
TestClientConfig.cpp \
TestClientServerSai.cppa \
TestContext.cpp \
TestContextConfig.cpp \
TestContextConfigContainer.cpp \
TestUtils.cpp

tests_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
tests_LDADD = $(LDADD_GTEST) $(top_srcdir)/lib/libSaiRedis.a -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)
Expand Down Expand Up @@ -62,4 +68,4 @@ testslibsairedis_SOURCES = main_libsairedis.cpp \
testslibsairedis_CXXFLAGS = $(DBGFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS_COMMON)
testslibsairedis_LDADD = $(LDADD_GTEST) -L$(top_srcdir)/lib/.libs -lsairedis -lhiredis -lswsscommon -lpthread -L$(top_srcdir)/meta/.libs -lsaimetadata -lsaimeta -lzmq $(CODE_COVERAGE_LIBS)

TESTS = tests testslibsairedis
TESTS = testslibsairedis tests
14 changes: 14 additions & 0 deletions unittest/lib/TestClientConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "ClientConfig.h"

#include <gtest/gtest.h>

using namespace sairedis;

TEST(ClientConfig, loadFromFile)
{
EXPECT_NE(ClientConfig::loadFromFile("non_existing"), nullptr);

EXPECT_NE(ClientConfig::loadFromFile("files/client_config_bad.txt"), nullptr);

EXPECT_NE(ClientConfig::loadFromFile("files/client_config_ok.txt"), nullptr);
}
83 changes: 83 additions & 0 deletions unittest/lib/TestClientServerSai.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "ClientServerSai.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace sairedis;

static const char* profile_get_value(
_In_ sai_switch_profile_id_t profile_id,
_In_ const char* variable)
{
SWSS_LOG_ENTER();

if (variable == NULL)
return NULL;

return nullptr;
}

static int profile_get_next_value(
_In_ sai_switch_profile_id_t profile_id,
_Out_ const char** variable,
_Out_ const char** value)
{
SWSS_LOG_ENTER();

return 0;
}

static sai_service_method_table_t test_services = {
profile_get_value,
profile_get_next_value
};

TEST(ClientServerSai, ctr)
{
auto css = std::make_shared<ClientServerSai>();
}

TEST(ClientServerSai, initialize)
{
auto css = std::make_shared<ClientServerSai>();

EXPECT_NE(SAI_STATUS_SUCCESS, css->initialize(1, nullptr));

EXPECT_NE(SAI_STATUS_SUCCESS, css->initialize(0, nullptr));

EXPECT_EQ(SAI_STATUS_SUCCESS, css->initialize(0, &test_services));

css = nullptr; // invoke uninitialize in destructor

css = std::make_shared<ClientServerSai>();

EXPECT_EQ(SAI_STATUS_SUCCESS, css->initialize(0, &test_services));

EXPECT_NE(SAI_STATUS_SUCCESS, css->initialize(0, &test_services));
}

TEST(ClientServerSai, objectTypeQuery)
{
auto css = std::make_shared<ClientServerSai>();

EXPECT_EQ(SAI_OBJECT_TYPE_NULL, css->objectTypeQuery(0x1111111111111111L));
}

TEST(ClientServerSai, switchIdQuery)
{
auto css = std::make_shared<ClientServerSai>();

EXPECT_EQ(SAI_NULL_OBJECT_ID, css->switchIdQuery(0x1111111111111111L));
}

TEST(ClientServerSai, logSet)
{
auto css = std::make_shared<ClientServerSai>();

EXPECT_NE(SAI_STATUS_SUCCESS, css->logSet(SAI_API_PORT, SAI_LOG_LEVEL_NOTICE));

EXPECT_EQ(SAI_STATUS_SUCCESS, css->initialize(0, &test_services));

EXPECT_EQ(SAI_STATUS_SUCCESS, css->logSet(SAI_API_PORT, SAI_LOG_LEVEL_NOTICE));
}
31 changes: 31 additions & 0 deletions unittest/lib/TestContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "Context.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace sairedis;

static sai_switch_notifications_t handle_notification(
_In_ std::shared_ptr<Notification> notification,
_In_ Context* context)
{
SWSS_LOG_ENTER();

sai_switch_notifications_t ntf;

memset(&ntf, 0, sizeof(ntf));

return ntf;
}

TEST(Context, populateMetadata)
{
auto recorder = std::make_shared<Recorder>();

auto cc = std::make_shared<ContextConfig>(0, "syncd", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB");

auto ctx = std::make_shared<Context>(cc, recorder,handle_notification);

ctx->populateMetadata(0x212121212212121L);
}
31 changes: 31 additions & 0 deletions unittest/lib/TestContextConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "ContextConfig.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace sairedis;

TEST(ContextConfig, hasConflict)
{
auto cc = std::make_shared<ContextConfig>(0, "syncd", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB");

EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(0, "syncd", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncd", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncb", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncb", "ASIC_DD", "COUNTERS_DB","FLEX_DB", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncb", "ASIC_DD", "COUNTERS_DD","FLEX_DB", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncb", "ASIC_DD", "COUNTERS_DD","FLEX_DD", "STATE_DB")));
EXPECT_TRUE(cc->hasConflict(std::make_shared<ContextConfig>(1, "syncb", "ASIC_DD", "COUNTERS_DD","FLEX_DD", "STATE_DD")));

auto aa = std::make_shared<ContextConfig>(1, "syncb", "ASIC_DD", "COUNTERS_DD","FLEX_DD", "STATE_DD");

aa->m_zmqEndpoint = "AA";

EXPECT_TRUE(cc->hasConflict(aa));

aa->m_zmqNtfEndpoint = "AA";

EXPECT_FALSE(cc->hasConflict(aa));
}

32 changes: 32 additions & 0 deletions unittest/lib/TestContextConfigContainer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "ContextConfigContainer.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace sairedis;

TEST(ContextConfigContainer, get)
{
ContextConfigContainer ccc;

EXPECT_EQ(ccc.get(0), nullptr);
}

TEST(ContextConfigContainer, loadFromFile)
{
ContextConfigContainer ccc;

EXPECT_NE(ccc.loadFromFile("files/ccc_bad.txt"), nullptr);
}

TEST(ContextConfigContainer, insert)
{
ContextConfigContainer ccc;

auto cc = std::make_shared<ContextConfig>(0, "syncd", "ASIC_DB", "COUNTERS_DB","FLEX_DB", "STATE_DB");

ccc.insert(cc);

EXPECT_THROW(ccc.insert(cc), std::runtime_error);
}
4 changes: 3 additions & 1 deletion unittest/lib/TestSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include <gtest/gtest.h>

#include <memory>

using namespace sairedis;

TEST(Switch, ctr)
{
EXPECT_THROW(new Switch(SAI_NULL_OBJECT_ID), std::runtime_error);
EXPECT_THROW(std::make_shared<Switch>(SAI_NULL_OBJECT_ID), std::runtime_error);
}
62 changes: 62 additions & 0 deletions unittest/lib/TestUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "Utils.h"

#include "swss/logger.h"

#include <gtest/gtest.h>

using namespace sairedis;

TEST(Utils, clearOidValues)
{
sai_attribute_t attr;

sai_object_id_t oids[1];

attr.id = 1000;

EXPECT_THROW(Utils::clearOidValues(SAI_OBJECT_TYPE_NULL, 1, &attr), std::runtime_error);

attr.id = SAI_ACL_ENTRY_ATTR_FIELD_IN_PORT;

attr.value.aclfield.data.oid = 1;

Utils::clearOidValues(SAI_OBJECT_TYPE_ACL_ENTRY, 1, &attr);

EXPECT_EQ(attr.value.aclfield.data.oid, 0);

attr.id = SAI_ACL_ENTRY_ATTR_FIELD_IN_PORTS;

attr.value.aclfield.enable = true;

attr.value.aclfield.data.objlist.count = 1;
attr.value.aclfield.data.objlist.list = oids;

oids[0] = 1;

Utils::clearOidValues(SAI_OBJECT_TYPE_ACL_ENTRY, 1, &attr);

EXPECT_EQ(oids[0], 0);

attr.id = SAI_ACL_ENTRY_ATTR_ACTION_COUNTER;

attr.value.aclaction.parameter.oid = 1;

Utils::clearOidValues(SAI_OBJECT_TYPE_ACL_ENTRY, 1, &attr);

EXPECT_EQ(attr.value.aclaction.parameter.oid, 0);

attr.id = SAI_ACL_ENTRY_ATTR_ACTION_REDIRECT_LIST;

attr.value.aclaction.enable = true;

attr.value.aclaction.parameter.objlist.count = 1;
attr.value.aclaction.parameter.objlist.list = oids;

oids[0] = 1;

Utils::clearOidValues(SAI_OBJECT_TYPE_ACL_ENTRY, 1, &attr);

EXPECT_EQ(oids[0], 0);

}

1 change: 1 addition & 0 deletions unittest/lib/files/ccc_bad.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo bar
1 change: 1 addition & 0 deletions unittest/lib/files/client_config_bad.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo bar
4 changes: 4 additions & 0 deletions unittest/lib/files/client_config_ok.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"zmq_endpoint": "ipc:///tmp/saiServer",
"zmq_ntf_endpoint": "ipc:///tmp/saiServerNtf"
}
8 changes: 4 additions & 4 deletions unittest/vslib/TestBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace saivs;

TEST(Buffer, ctr)
{
EXPECT_THROW(new Buffer(0,0), std::runtime_error);
EXPECT_THROW(std::make_shared<Buffer>(nullptr, 0), std::runtime_error);

Buffer b((const uint8_t*)"foo", 3);
}
Expand All @@ -22,7 +22,7 @@ TEST(Buffer, dtr)

TEST(Buffer, getData)
{
EXPECT_THROW(new Buffer(0,0), std::runtime_error);
EXPECT_THROW(std::make_shared<Buffer>(nullptr,0), std::runtime_error);

Buffer b((const uint8_t*)"foo", 3);

Expand All @@ -31,7 +31,7 @@ TEST(Buffer, getData)

TEST(Buffer, getSize)
{
EXPECT_THROW(new Buffer(0,0), std::runtime_error);
EXPECT_THROW(std::make_shared<Buffer>(nullptr,0), std::runtime_error);

Buffer b((const uint8_t*)"foo", 3);

Expand All @@ -40,7 +40,7 @@ TEST(Buffer, getSize)

TEST(Buffer, flow)
{
EXPECT_THROW(new Buffer(0,0), std::runtime_error);
EXPECT_THROW(std::make_shared<Buffer>(nullptr,0), std::runtime_error);

Buffer b((const uint8_t*)"foo", 3);

Expand Down

0 comments on commit d8eb0ea

Please sign in to comment.