|
| 1 | +#include "Sai.h" |
| 2 | + |
| 3 | +//#include "meta/sai_serialize.h" |
| 4 | +#include "saivs.h" |
| 5 | + |
| 6 | +#include "swss/notificationproducer.h" |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | + |
| 10 | +using namespace saivs; |
| 11 | + |
| 12 | +class TestSai: |
| 13 | + public saivs::Sai |
| 14 | +{ |
| 15 | + public: |
| 16 | + // |
| 17 | + // void call_startUnittestThread() |
| 18 | + // { |
| 19 | + // SWSS_LOG_ENTER(); |
| 20 | + // startUnittestThread(); |
| 21 | + // } |
| 22 | + // |
| 23 | + // void call_stopUnittestThread() |
| 24 | + // { |
| 25 | + // SWSS_LOG_ENTER(); |
| 26 | + // stopUnittestThread(); |
| 27 | + // } |
| 28 | + // |
| 29 | + // void call_channelOpEnableUnittest( |
| 30 | + // _In_ const std::string &key, |
| 31 | + // _In_ const std::vector<swss::FieldValueTuple> &values) |
| 32 | + // { |
| 33 | + // SWSS_LOG_ENTER(); |
| 34 | + // channelOpEnableUnittest(key, values); |
| 35 | + // } |
| 36 | + // |
| 37 | + // void call_channelOpSetReadOnlyAttribute( |
| 38 | + // _In_ const std::string &key, |
| 39 | + // _In_ const std::vector<swss::FieldValueTuple> &values) |
| 40 | + // { |
| 41 | + // SWSS_LOG_ENTER(); |
| 42 | + // channelOpSetReadOnlyAttribute(key, values); |
| 43 | + // } |
| 44 | + // |
| 45 | + // void call_channelOpSetStats( |
| 46 | + // _In_ const std::string &key, |
| 47 | + // _In_ const std::vector<swss::FieldValueTuple> &values) |
| 48 | + // { |
| 49 | + // SWSS_LOG_ENTER(); |
| 50 | + // channelOpSetStats(key,values); |
| 51 | + // } |
| 52 | + |
| 53 | + void call_handleUnittestChannelOp( |
| 54 | + _In_ const std::string &op, |
| 55 | + _In_ const std::string &key, |
| 56 | + _In_ const std::vector<swss::FieldValueTuple> &values) |
| 57 | + { |
| 58 | + SWSS_LOG_ENTER(); |
| 59 | + |
| 60 | + handleUnittestChannelOp(op, key, values); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +static const char* profile_get_value( |
| 65 | + _In_ sai_switch_profile_id_t profile_id, |
| 66 | + _In_ const char* variable) |
| 67 | +{ |
| 68 | + SWSS_LOG_ENTER(); |
| 69 | + |
| 70 | + if (variable == NULL) |
| 71 | + return NULL; |
| 72 | + |
| 73 | + if (strcmp(variable, SAI_KEY_VS_SWITCH_TYPE) == 0) |
| 74 | + return SAI_VALUE_VS_SWITCH_TYPE_BCM56850; |
| 75 | + |
| 76 | + return nullptr; |
| 77 | +} |
| 78 | + |
| 79 | +static int profile_get_next_value( |
| 80 | + _In_ sai_switch_profile_id_t profile_id, |
| 81 | + _Out_ const char** variable, |
| 82 | + _Out_ const char** value) |
| 83 | +{ |
| 84 | + SWSS_LOG_ENTER(); |
| 85 | + |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +sai_service_method_table_t test_services = { |
| 90 | + profile_get_value, |
| 91 | + profile_get_next_value |
| 92 | +}; |
| 93 | + |
| 94 | +TEST(SaiUnittests, ctr) |
| 95 | +{ |
| 96 | + TestSai sai; |
| 97 | + |
| 98 | + sai.initialize(0, &test_services); |
| 99 | + |
| 100 | + std::vector<swss::FieldValueTuple> values; |
| 101 | + |
| 102 | + sai_attribute_t attr; |
| 103 | + |
| 104 | + sai_object_id_t switch_id; |
| 105 | + |
| 106 | + attr.id = SAI_SWITCH_ATTR_INIT_SWITCH; |
| 107 | + attr.value.booldata = true; |
| 108 | + |
| 109 | + EXPECT_EQ(sai.create(SAI_OBJECT_TYPE_SWITCH, &switch_id, SAI_NULL_OBJECT_ID, 1, &attr), SAI_STATUS_SUCCESS); |
| 110 | + |
| 111 | + attr.id = SAI_SWITCH_ATTR_PORT_MAX_MTU; |
| 112 | + attr.value.u32 = 41; |
| 113 | + |
| 114 | + EXPECT_NE(sai.set(SAI_OBJECT_TYPE_SWITCH, switch_id, &attr), SAI_STATUS_SUCCESS); |
| 115 | + |
| 116 | + //sai.call_handleUnittestChannelOp("op", "key", values); |
| 117 | + |
| 118 | + swss::DBConnector db("ASIC_DB", 0, true); |
| 119 | + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); |
| 120 | + |
| 121 | + std::vector<swss::FieldValueTuple> entry; |
| 122 | + |
| 123 | + // needs to be done only once |
| 124 | + vsntf.send(SAI_VS_UNITTEST_ENABLE_UNITTESTS, "true", entry); |
| 125 | + |
| 126 | + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); |
| 127 | + |
| 128 | + std::string data = "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000"; |
| 129 | + |
| 130 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, data, entry); |
| 131 | + |
| 132 | + usleep(100*1000); |
| 133 | + |
| 134 | + // just scramble value to make sure that GET will succeed |
| 135 | + attr.value.u32 = 1; |
| 136 | + |
| 137 | + EXPECT_EQ(sai.get(SAI_OBJECT_TYPE_SWITCH, switch_id, 1, &attr), SAI_STATUS_SUCCESS); |
| 138 | + |
| 139 | + EXPECT_EQ(attr.value.u32, 42); |
| 140 | +} |
| 141 | + |
| 142 | +TEST(SaiUnittests, handleUnittestChannelOp) |
| 143 | +{ |
| 144 | + TestSai sai; |
| 145 | + |
| 146 | + std::vector<swss::FieldValueTuple> values; |
| 147 | + |
| 148 | + swss::DBConnector db("ASIC_DB", 0, true); |
| 149 | + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); |
| 150 | + |
| 151 | + std::vector<swss::FieldValueTuple> entry; |
| 152 | + |
| 153 | + // api not initialized |
| 154 | + vsntf.send("unknown op", "true", entry); |
| 155 | + |
| 156 | + sai.initialize(0, &test_services); |
| 157 | + |
| 158 | + vsntf.send("unknown op", "true", entry); |
| 159 | + |
| 160 | + usleep(100*1000); |
| 161 | +} |
| 162 | + |
| 163 | +TEST(SaiUnittests, channelOpSetReadOnlyAttribute) |
| 164 | +{ |
| 165 | + TestSai sai; |
| 166 | + |
| 167 | + sai.initialize(0, &test_services); |
| 168 | + |
| 169 | + std::vector<swss::FieldValueTuple> values; |
| 170 | + |
| 171 | + swss::DBConnector db("ASIC_DB", 0, true); |
| 172 | + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); |
| 173 | + |
| 174 | + std::vector<swss::FieldValueTuple> entry; |
| 175 | + |
| 176 | + // empty fields |
| 177 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "invalid", entry); |
| 178 | + |
| 179 | + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); |
| 180 | + |
| 181 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_NULL:oid:0x2100000000", entry); |
| 182 | + |
| 183 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_PORT:oid:0x2100000000", entry); |
| 184 | + |
| 185 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_ROUTE_ENTRY:oid:0x2100000000", entry); |
| 186 | + |
| 187 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x0", entry); |
| 188 | + |
| 189 | + entry.clear(); |
| 190 | + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU_unvalid", "42"); |
| 191 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); |
| 192 | + |
| 193 | + entry.clear(); |
| 194 | + entry.emplace_back("SAI_PORT_ATTR_QOS_NUMBER_OF_QUEUES", "42"); |
| 195 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); |
| 196 | + |
| 197 | + entry.clear(); |
| 198 | + entry.emplace_back("SAI_SWITCH_ATTR_PORT_MAX_MTU", "42"); |
| 199 | + |
| 200 | + vsntf.send(SAI_VS_UNITTEST_SET_RO_OP, "SAI_OBJECT_TYPE_SWITCH:oid:0x2100000000", entry); |
| 201 | + |
| 202 | + usleep(100*1000); |
| 203 | +} |
| 204 | + |
| 205 | +TEST(SaiUnittests, channelOpSetStats) |
| 206 | +{ |
| 207 | + TestSai sai; |
| 208 | + |
| 209 | + sai.initialize(0, &test_services); |
| 210 | + |
| 211 | + std::vector<swss::FieldValueTuple> values; |
| 212 | + |
| 213 | + swss::DBConnector db("ASIC_DB", 0, true); |
| 214 | + swss::NotificationProducer vsntf(&db, SAI_VS_UNITTEST_CHANNEL); |
| 215 | + |
| 216 | + std::vector<swss::FieldValueTuple> entry; |
| 217 | + |
| 218 | + // null oid |
| 219 | + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x0", entry); |
| 220 | + |
| 221 | + // invalid oid |
| 222 | + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x1111111111", entry); |
| 223 | + |
| 224 | + // valid oid |
| 225 | + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); |
| 226 | + |
| 227 | + entry.clear(); |
| 228 | + entry.emplace_back("SAI_SWITCH_STAT_IN_CONFIGURED_DROP_REASONS_0_DROPPED_PKTS", "foo"); |
| 229 | + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); |
| 230 | + |
| 231 | + entry.clear(); |
| 232 | + entry.emplace_back("bar", "54"); |
| 233 | + vsntf.send(SAI_VS_UNITTEST_SET_STATS_OP, "oid:0x2100000000", entry); |
| 234 | + |
| 235 | + usleep(100*1000); |
| 236 | +} |
| 237 | + |
0 commit comments