From 353a0580fd6861fea4d737cbe5312e6351cdbc91 Mon Sep 17 00:00:00 2001 From: kcudnik Date: Thu, 19 Aug 2021 20:07:49 +0200 Subject: [PATCH] Add SaiUnittests tests --- unittest/vslib/TestSaiUnittests.cpp | 237 ++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 unittest/vslib/TestSaiUnittests.cpp diff --git a/unittest/vslib/TestSaiUnittests.cpp b/unittest/vslib/TestSaiUnittests.cpp new file mode 100644 index 000000000000..90587c7fb267 --- /dev/null +++ b/unittest/vslib/TestSaiUnittests.cpp @@ -0,0 +1,237 @@ +#include "Sai.h" + +//#include "meta/sai_serialize.h" +#include "saivs.h" + +#include "swss/notificationproducer.h" + +#include + +using namespace saivs; + +class TestSai: + public saivs::Sai +{ + public: + // + // void call_startUnittestThread() + // { + // SWSS_LOG_ENTER(); + // startUnittestThread(); + // } + // + // void call_stopUnittestThread() + // { + // SWSS_LOG_ENTER(); + // stopUnittestThread(); + // } + // + // void call_channelOpEnableUnittest( + // _In_ const std::string &key, + // _In_ const std::vector &values) + // { + // SWSS_LOG_ENTER(); + // channelOpEnableUnittest(key, values); + // } + // + // void call_channelOpSetReadOnlyAttribute( + // _In_ const std::string &key, + // _In_ const std::vector &values) + // { + // SWSS_LOG_ENTER(); + // channelOpSetReadOnlyAttribute(key, values); + // } + // + // void call_channelOpSetStats( + // _In_ const std::string &key, + // _In_ const std::vector &values) + // { + // SWSS_LOG_ENTER(); + // channelOpSetStats(key,values); + // } + + void call_handleUnittestChannelOp( + _In_ const std::string &op, + _In_ const std::string &key, + _In_ const std::vector &values) + { + SWSS_LOG_ENTER(); + + handleUnittestChannelOp(op, key, values); + } +}; + +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; + + if (strcmp(variable, SAI_KEY_VS_SWITCH_TYPE) == 0) + return SAI_VALUE_VS_SWITCH_TYPE_BCM56850; + + 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; +} + +sai_service_method_table_t test_services = { + profile_get_value, + profile_get_next_value +}; + +TEST(SaiUnittests, ctr) +{ + TestSai sai; + + sai.initialize(0, &test_services); + + std::vector values; + + sai_attribute_t attr; + + sai_object_id_t switch_id; + + attr.id = SAI_SWITCH_ATTR_INIT_SWITCH; + attr.value.booldata = true; + + EXPECT_EQ(sai.create(SAI_OBJECT_TYPE_SWITCH, &switch_id, SAI_NULL_OBJECT_ID, 1, &attr), SAI_STATUS_SUCCESS); + + attr.id = SAI_SWITCH_ATTR_PORT_MAX_MTU; + attr.value.u32 = 41; + + EXPECT_NE(sai.set(SAI_OBJECT_TYPE_SWITCH, switch_id, &attr), SAI_STATUS_SUCCESS); + + //sai.call_handleUnittestChannelOp("op", "key", values); + + swss::DBConnector db("ASIC_DB", 0, true); + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); + + std::vector entry; + + // needs to be done only once + vsntf.send(SAI_VS_UNITTEST_ENABLE_UNITTESTS, "true", entry); + + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); + + std::string data = "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000"; + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, data, entry); + + usleep(100*1000); + + // just scramble value to make sure that GET will succeed + attr.value.u32 = 1; + + EXPECT_EQ(sai.get(SAI_OBJECT_TYPE_SWITCH, switch_id, 1, &attr), SAI_STATUS_SUCCESS); + + EXPECT_EQ(attr.value.u32, 42); +} + +TEST(SaiUnittests, handleUnittestChannelOp) +{ + TestSai sai; + + std::vector values; + + swss::DBConnector db("ASIC_DB", 0, true); + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); + + std::vector entry; + + // api not initialized + vsntf.send("unknown op", "true", entry); + + sai.initialize(0, &test_services); + + vsntf.send("unknown op", "true", entry); + + usleep(100*1000); +} + +TEST(SaiUnittests, channelOpSetReadOnlyAttribute) +{ + TestSai sai; + + sai.initialize(0, &test_services); + + std::vector values; + + swss::DBConnector db("ASIC_DB", 0, true); + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); + + std::vector entry; + + // empty fields + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "invalid", entry); + + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_NULL:oid:0x2100000000", entry); + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_PORT:oid:0x2100000000", entry); + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_ROUTE_ENTRY:oid:0x2100000000", entry); + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x0", entry); + + entry.clear(); + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU_unvalid", "42"); + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); + + entry.clear(); + entry.emplace_back("SAI_PORT_ATTR_QOS_NUMBER_OF_QUEUES", "42"); + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); + + entry.clear(); + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); + + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); + + usleep(100*1000); +} + +TEST(SaiUnittests, channelOpSetStats) +{ + TestSai sai; + + sai.initialize(0, &test_services); + + std::vector values; + + swss::DBConnector db("ASIC_DB", 0, true); + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); + + std::vector entry; + + // null oid + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x0", entry); + + // invalid oid + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x1111111111", entry); + + // valid oid + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); + + entry.clear(); + entry.emplace_back("SAI_SWITCH_STAT_IN_CONFIGURED_DROP_REASONS_0_DROPPED_PKTS", "foo"); + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); + + entry.clear(); + entry.emplace_back("bar", "54"); + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); + + usleep(100*1000); +} +