Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2018 Open Networking Foundation |
| 3 | |
| 4 | This program is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation, either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "bal_mocker.h" |
| 19 | #include "core.h" |
| 20 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 21 | using namespace testing; |
| 22 | |
| 23 | class TestOltEnable : public Test { |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 24 | protected: |
| 25 | virtual void SetUp() { |
| 26 | } |
| 27 | |
| 28 | virtual void TearDown() { |
| 29 | // Code here will be called immediately after each test |
| 30 | // (right before the destructor). |
| 31 | } |
| 32 | }; |
| 33 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 34 | // This is used to set custom bcmolt_cfg value to bcmolt_cfg pointer coming in |
| 35 | // bcmolt_cfg_get_stub. |
| 36 | ACTION_P(SetArg1ToBcmOltCfg, value) { *static_cast<bcmolt_olt_cfg*>(arg1) = value; }; |
| 37 | |
| 38 | |
| 39 | // Create a mock function for bcmolt_cfg_get_stub C++ function |
| 40 | MOCK_GLOBAL_FUNC2(bcmolt_cfg_get_stub, bcmos_errno(bcmolt_oltid, void*)); |
| 41 | |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 42 | |
| 43 | // Test Fixture for OltEnable |
| 44 | |
| 45 | // Test 1: OltEnableSuccess case |
| 46 | TEST_F(TestOltEnable, OltEnableSuccess){ |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 47 | // NiceMock is used to suppress 'WillByDefault' return errors. |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 48 | // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 49 | NiceMock<BalMocker> balMock; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 50 | bcmos_errno host_init_res = BCM_ERR_OK; |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 51 | bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 52 | bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED; |
| 53 | bcmos_errno olt_oper_res = BCM_ERR_OK; |
| 54 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 55 | bcmolt_olt_cfg olt_cfg = { }; |
| 56 | bcmolt_olt_key olt_key = { }; |
| 57 | BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key); |
| 58 | olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY; |
| 59 | |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 60 | Status olt_enable_res; |
| 61 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 62 | ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res)); |
| 63 | EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _)) |
| 64 | .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res))); |
| 65 | EXPECT_CALL(balMock, bcmolt_cfg_get(_, _)) |
| 66 | .Times(BCM_MAX_DEVS_PER_LINE_CARD) |
| 67 | .WillRepeatedly(Return(bal_cfg_get_res)); |
| 68 | ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res)); |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 69 | |
| 70 | olt_enable_res = Enable_(1, NULL); |
| 71 | ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() ); |
| 72 | } |
| 73 | |
| 74 | // Test 2: OltEnableFail_host_init_fail |
| 75 | TEST_F(TestOltEnable, OltEnableFail_host_init_fail) { |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 76 | // NiceMock is used to suppress 'WillByDefault' return errors. |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 77 | // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 78 | NiceMock<BalMocker> balMock; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 79 | bcmos_errno host_init_res = BCM_ERR_INTERNAL; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 80 | |
| 81 | Status olt_enable_res; |
| 82 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 83 | // Ensure that the state of the OLT is in deactivated to start with.. |
| 84 | state.deactivate(); |
| 85 | |
| 86 | ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res)); |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 87 | |
| 88 | olt_enable_res = Enable_(1, NULL); |
| 89 | ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() ); |
| 90 | } |
| 91 | |
| 92 | // Test 3: OltEnableSuccess_PON_Device_Connected |
| 93 | TEST_F(TestOltEnable, OltEnableSuccess_PON_Device_Connected) { |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 94 | |
| 95 | // NiceMock is used to suppress 'WillByDefault' return errors. |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 96 | // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 97 | NiceMock<BalMocker> balMock; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 98 | bcmos_errno host_init_res = BCM_ERR_OK; |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 99 | bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 100 | bcmos_errno bal_cfg_get_res = BCM_ERR_OK; |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 101 | bcmos_errno olt_oper_res = BCM_ERR_OK; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 102 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 103 | bcmolt_olt_cfg olt_cfg = { }; |
| 104 | bcmolt_olt_key olt_key = { }; |
| 105 | BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key); |
| 106 | olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 107 | |
| 108 | Status olt_enable_res; |
| 109 | |
| 110 | // Ensure that the state of the OLT is in deactivated to start with.. |
| 111 | state.deactivate(); |
| 112 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 113 | ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res)); |
| 114 | EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _)) |
| 115 | .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res))); |
| 116 | EXPECT_CALL(balMock, bcmolt_cfg_get(_, _)) |
| 117 | .Times(BCM_MAX_DEVS_PER_LINE_CARD) |
| 118 | .WillRepeatedly(Return(bal_cfg_get_res)); |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 119 | |
| 120 | olt_enable_res = Enable_(1, NULL); |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 121 | ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() ); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | // Test 4: OltEnableFail_All_PON_Enable_Fail |
| 126 | TEST_F(TestOltEnable, OltEnableFail_All_PON_Enable_Fail) { |
| 127 | |
| 128 | // NiceMock is used to suppress 'WillByDefault' return errors. |
| 129 | // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md |
| 130 | NiceMock<BalMocker> balMock; |
| 131 | bcmos_errno host_init_res = BCM_ERR_OK; |
| 132 | bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK; |
| 133 | bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED; |
| 134 | bcmos_errno olt_oper_res = BCM_ERR_INTERNAL; |
| 135 | |
| 136 | bcmolt_olt_cfg olt_cfg = { }; |
| 137 | bcmolt_olt_key olt_key = { }; |
| 138 | BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key); |
| 139 | olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY; |
| 140 | |
| 141 | Status olt_enable_res; |
| 142 | |
| 143 | // Ensure that the state of the OLT is in deactivated to start with.. |
| 144 | state.deactivate(); |
| 145 | |
| 146 | ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res)); |
| 147 | EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _)) |
| 148 | .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res))); |
| 149 | EXPECT_CALL(balMock, bcmolt_cfg_get(_, _)) |
| 150 | .Times(BCM_MAX_DEVS_PER_LINE_CARD) |
| 151 | .WillRepeatedly(Return(bal_cfg_get_res)); |
| 152 | ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res)); |
| 153 | |
| 154 | olt_enable_res = Enable_(1, NULL); |
| 155 | |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 156 | ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() ); |
| 157 | } |
| 158 | |
| 159 | // Test 5 OltEnableSuccess_One_PON_Enable_Fail : One PON device enable fails, but all others succeed. |
| 160 | TEST_F(TestOltEnable, OltEnableSuccess_One_PON_Enable_Fail) { |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 161 | |
| 162 | // NiceMock is used to suppress 'WillByDefault' return errors. |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 163 | // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 164 | NiceMock<BalMocker> balMock; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 165 | bcmos_errno host_init_res = BCM_ERR_OK; |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 166 | bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK; |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 167 | bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED; |
| 168 | bcmos_errno olt_oper_res_fail = BCM_ERR_INTERNAL; |
| 169 | bcmos_errno olt_oper_res_success = BCM_ERR_OK; |
| 170 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 171 | bcmolt_olt_cfg olt_cfg = { }; |
| 172 | bcmolt_olt_key olt_key = { }; |
| 173 | BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key); |
| 174 | olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY; |
| 175 | |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 176 | Status olt_enable_res; |
| 177 | |
| 178 | // Ensure that the state of the OLT is in deactivated to start with.. |
| 179 | state.deactivate(); |
| 180 | |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 181 | ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res)); |
| 182 | EXPECT_GLOBAL_CALL(bcmolt_cfg_get_stub, bcmolt_cfg_get_stub(_, _)) |
| 183 | .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res))); |
| 184 | EXPECT_CALL(balMock, bcmolt_cfg_get(_, _)) |
| 185 | .Times(BCM_MAX_DEVS_PER_LINE_CARD) |
| 186 | .WillRepeatedly(Return(bal_cfg_get_res)); |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 187 | // For the the first PON mac device, the activation result will fail, and will succeed for all other PON mac devices. |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 188 | EXPECT_CALL(balMock, bcmolt_oper_submit(_, _)) |
| 189 | .WillOnce(Return(olt_oper_res_fail)) |
| 190 | .WillRepeatedly(Return(olt_oper_res_success)); |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 191 | olt_enable_res = Enable_(1, NULL); |
Jason Huang | bf45ffb | 2019-10-30 17:29:02 +0800 | [diff] [blame] | 192 | |
Thiyagarajan Subramani | 89fffc0 | 2019-05-13 21:33:20 +0000 | [diff] [blame] | 193 | ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() ); |
| 194 | } |