|
| 1 | +#define private public // make Directory::m_values available to clean it. |
| 2 | +#include "directory.h" |
| 3 | +#undef private |
| 4 | +#define protected public |
| 5 | +#include "orch.h" |
| 6 | +#undef protected |
| 7 | +#include "ut_helper.h" |
| 8 | +#include "mock_orchagent_main.h" |
| 9 | +#include "mock_table.h" |
| 10 | +#include "mock_response_publisher.h" |
| 11 | +#include "saihelper.h" |
| 12 | + |
| 13 | +namespace saihelper_test |
| 14 | +{ |
| 15 | + using namespace std; |
| 16 | + |
| 17 | + sai_switch_api_t ut_sai_switch_api; |
| 18 | + sai_switch_api_t *old_sai_switch_api; |
| 19 | + |
| 20 | + shared_ptr<swss::DBConnector> m_app_db; |
| 21 | + shared_ptr<swss::DBConnector> m_config_db; |
| 22 | + shared_ptr<swss::DBConnector> m_state_db; |
| 23 | + |
| 24 | + bool set_comm_mode_not_supported; |
| 25 | + bool use_pipeline_not_supported; |
| 26 | + |
| 27 | + sai_status_t _ut_stub_sai_set_switch_attribute( |
| 28 | + _In_ sai_object_id_t switch_id, |
| 29 | + _In_ const sai_attribute_t *attr) |
| 30 | + { |
| 31 | + switch (attr[0].id) |
| 32 | + { |
| 33 | + case SAI_REDIS_SWITCH_ATTR_REDIS_COMMUNICATION_MODE: |
| 34 | + if (set_comm_mode_not_supported) |
| 35 | + { |
| 36 | + return SAI_STATUS_NOT_SUPPORTED; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + return SAI_STATUS_SUCCESS; |
| 41 | + } |
| 42 | + break; |
| 43 | + case SAI_REDIS_SWITCH_ATTR_USE_PIPELINE: |
| 44 | + if (use_pipeline_not_supported) |
| 45 | + { |
| 46 | + return SAI_STATUS_NOT_SUPPORTED; |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + return SAI_STATUS_SUCCESS; |
| 51 | + } |
| 52 | + break; |
| 53 | + default: |
| 54 | + break; |
| 55 | + } |
| 56 | + return SAI_STATUS_SUCCESS; |
| 57 | + } |
| 58 | + |
| 59 | + void _hook_sai_apis() |
| 60 | + { |
| 61 | + ut_sai_switch_api = *sai_switch_api; |
| 62 | + old_sai_switch_api = sai_switch_api; |
| 63 | + ut_sai_switch_api.set_switch_attribute = _ut_stub_sai_set_switch_attribute; |
| 64 | + sai_switch_api = &ut_sai_switch_api; |
| 65 | + } |
| 66 | + |
| 67 | + void _unhook_sai_apis() |
| 68 | + { |
| 69 | + sai_switch_api = old_sai_switch_api; |
| 70 | + } |
| 71 | + |
| 72 | + class SaihelperTest : public ::testing::Test |
| 73 | + { |
| 74 | + public: |
| 75 | + |
| 76 | + SaihelperTest() |
| 77 | + { |
| 78 | + }; |
| 79 | + |
| 80 | + ~SaihelperTest() |
| 81 | + { |
| 82 | + }; |
| 83 | + |
| 84 | + void SetUp() override |
| 85 | + { |
| 86 | + // Init switch and create dependencies |
| 87 | + m_app_db = make_shared<swss::DBConnector>("APPL_DB", 0); |
| 88 | + m_config_db = make_shared<swss::DBConnector>("CONFIG_DB", 0); |
| 89 | + m_state_db = make_shared<swss::DBConnector>("STATE_DB", 0); |
| 90 | + |
| 91 | + set_comm_mode_not_supported = false; |
| 92 | + use_pipeline_not_supported = false; |
| 93 | + |
| 94 | + map<string, string> profile = { |
| 95 | + { "SAI_VS_SWITCH_TYPE", "SAI_VS_SWITCH_TYPE_BCM56850" }, |
| 96 | + { "KV_DEVICE_MAC_ADDRESS", "20:03:04:05:06:00" } |
| 97 | + }; |
| 98 | + |
| 99 | + ut_helper::initSaiApi(profile); |
| 100 | + |
| 101 | + sai_attribute_t attr; |
| 102 | + |
| 103 | + attr.id = SAI_SWITCH_ATTR_INIT_SWITCH; |
| 104 | + attr.value.booldata = true; |
| 105 | + |
| 106 | + auto status = sai_switch_api->create_switch(&gSwitchId, 1, &attr); |
| 107 | + ASSERT_EQ(status, SAI_STATUS_SUCCESS); |
| 108 | + } |
| 109 | + |
| 110 | + void initSwitchOrch() |
| 111 | + { |
| 112 | + TableConnector stateDbSwitchTable(m_state_db.get(), "SWITCH_CAPABILITY"); |
| 113 | + TableConnector conf_asic_sensors(m_config_db.get(), CFG_ASIC_SENSORS_TABLE_NAME); |
| 114 | + TableConnector app_switch_table(m_app_db.get(), APP_SWITCH_TABLE_NAME); |
| 115 | + TableConnector conf_suppress_asic_sdk_health_categories(m_config_db.get(), CFG_SUPPRESS_ASIC_SDK_HEALTH_EVENT_NAME); |
| 116 | + |
| 117 | + vector<TableConnector> switch_tables = { |
| 118 | + conf_asic_sensors, |
| 119 | + conf_suppress_asic_sdk_health_categories, |
| 120 | + app_switch_table |
| 121 | + }; |
| 122 | + |
| 123 | + ASSERT_EQ(gSwitchOrch, nullptr); |
| 124 | + gSwitchOrch = new SwitchOrch(m_app_db.get(), switch_tables, stateDbSwitchTable); |
| 125 | + } |
| 126 | + |
| 127 | + void TearDown() override |
| 128 | + { |
| 129 | + ::testing_db::reset(); |
| 130 | + |
| 131 | + gDirectory.m_values.clear(); |
| 132 | + |
| 133 | + delete gSwitchOrch; |
| 134 | + gSwitchOrch = nullptr; |
| 135 | + |
| 136 | + ut_helper::uninitSaiApi(); |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + TEST_F(SaihelperTest, TestSetCommunicationModeFailure) { |
| 141 | + set_comm_mode_not_supported = true; |
| 142 | + _hook_sai_apis(); |
| 143 | + initSwitchOrch(); |
| 144 | + |
| 145 | + // Assert that the program terminates after initSaiRedis() call |
| 146 | + ASSERT_DEATH({initSaiRedis();}, ""); |
| 147 | + set_comm_mode_not_supported = false; |
| 148 | + _unhook_sai_apis(); |
| 149 | + } |
| 150 | + |
| 151 | + TEST_F(SaihelperTest, TestSetRedisPipelineFailure) { |
| 152 | + use_pipeline_not_supported = true; |
| 153 | + _hook_sai_apis(); |
| 154 | + initSwitchOrch(); |
| 155 | + |
| 156 | + // Assert that the program terminates after initSaiRedis() call |
| 157 | + ASSERT_DEATH({initSaiRedis();}, ""); |
| 158 | + use_pipeline_not_supported = false; |
| 159 | + _unhook_sai_apis(); |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments