blob: 3588f95e84a9c139a3baa64cc3e779f6b26491bd [file] [log] [blame]
Amit Ghoshfcad4d32019-11-13 10:24:55 +00001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "gtest/gtest.h"
17#include "bal_mocker.h"
18#include "core.h"
19using namespace testing;
20using namespace std;
21
22class TestOltEnable : public Test {
23 protected:
24 virtual void SetUp() {
25 }
26
27 virtual void TearDown() {
28 // Code here will be called immediately after each test
29 // (right before the destructor).
30 }
31};
32
33// This is used to set custom bcmolt_cfg value to bcmolt_cfg pointer coming in
34// bcmolt_cfg_get__bal_state_stub.
35ACTION_P(SetArg1ToBcmOltCfg, value) { *static_cast<bcmolt_olt_cfg*>(arg1) = value; };
36
37
38// Create a mock function for bcmolt_cfg_get__bal_state_stub C++ function
39MOCK_GLOBAL_FUNC2(bcmolt_cfg_get__bal_state_stub, bcmos_errno(bcmolt_oltid, void*));
40
41
42// Test Fixture for OltEnable
43
44// Test 1: OltEnableSuccess case
45TEST_F(TestOltEnable, OltEnableSuccess){
46 // NiceMock is used to suppress 'WillByDefault' return errors.
47 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
48 NiceMock<BalMocker> balMock;
49 bcmos_errno host_init_res = BCM_ERR_OK;
50 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
51 bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
52 bcmos_errno olt_oper_res = BCM_ERR_OK;
53
54 bcmolt_olt_cfg olt_cfg = { };
55 bcmolt_olt_key olt_key = { };
56 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
57 olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
58
59 Status olt_enable_res;
60
61 ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
62 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__bal_state_stub, bcmolt_cfg_get__bal_state_stub(_, _))
63 .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
64 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
65 .Times(BCM_MAX_DEVS_PER_LINE_CARD)
66 .WillRepeatedly(Return(bal_cfg_get_res));
67 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
68
69 olt_enable_res = Enable_(1, NULL);
70 ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
71}
72
73// Test 2: OltEnableFail_host_init_fail
74TEST_F(TestOltEnable, OltEnableFail_host_init_fail) {
75 // NiceMock is used to suppress 'WillByDefault' return errors.
76 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
77 NiceMock<BalMocker> balMock;
78 bcmos_errno host_init_res = BCM_ERR_INTERNAL;
79
80 Status olt_enable_res;
81
82 // Ensure that the state of the OLT is in deactivated to start with..
83 state.deactivate();
84
85 ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
86
87 olt_enable_res = Enable_(1, NULL);
88 ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() );
89}
90
91// Test 3: OltEnableSuccess_PON_Device_Connected
92TEST_F(TestOltEnable, OltEnableSuccess_PON_Device_Connected) {
93
94 // NiceMock is used to suppress 'WillByDefault' return errors.
95 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
96 NiceMock<BalMocker> balMock;
97 bcmos_errno host_init_res = BCM_ERR_OK;
98 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
99 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
100 bcmos_errno olt_oper_res = BCM_ERR_OK;
101
102 bcmolt_olt_cfg olt_cfg = { };
103 bcmolt_olt_key olt_key = { };
104 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
105 olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
106
107 Status olt_enable_res;
108
109 // Ensure that the state of the OLT is in deactivated to start with..
110 state.deactivate();
111
112 ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
113 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__bal_state_stub, bcmolt_cfg_get__bal_state_stub(_, _))
114 .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
115 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
116 .Times(BCM_MAX_DEVS_PER_LINE_CARD)
117 .WillRepeatedly(Return(bal_cfg_get_res));
118
119 olt_enable_res = Enable_(1, NULL);
120 ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
121
122}
123
124// Test 4: OltEnableFail_All_PON_Enable_Fail
125TEST_F(TestOltEnable, OltEnableFail_All_PON_Enable_Fail) {
126
127 // NiceMock is used to suppress 'WillByDefault' return errors.
128 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
129 NiceMock<BalMocker> balMock;
130 bcmos_errno host_init_res = BCM_ERR_OK;
131 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
132 bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
133 bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
134
135 bcmolt_olt_cfg olt_cfg = { };
136 bcmolt_olt_key olt_key = { };
137 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
138 olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
139
140 Status olt_enable_res;
141
142 // Ensure that the state of the OLT is in deactivated to start with..
143 state.deactivate();
144
145 ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
146 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__bal_state_stub, bcmolt_cfg_get__bal_state_stub(_, _))
147 .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
148 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
149 .Times(BCM_MAX_DEVS_PER_LINE_CARD)
150 .WillRepeatedly(Return(bal_cfg_get_res));
151 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
152
153 olt_enable_res = Enable_(1, NULL);
154
155 ASSERT_TRUE( olt_enable_res.error_message() != Status::OK.error_message() );
156}
157
158// Test 5 OltEnableSuccess_One_PON_Enable_Fail : One PON device enable fails, but all others succeed.
159TEST_F(TestOltEnable, OltEnableSuccess_One_PON_Enable_Fail) {
160
161 // NiceMock is used to suppress 'WillByDefault' return errors.
162 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
163 NiceMock<BalMocker> balMock;
164 bcmos_errno host_init_res = BCM_ERR_OK;
165 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
166 bcmos_errno bal_cfg_get_res = BCM_ERR_NOT_CONNECTED;
167 bcmos_errno olt_oper_res_fail = BCM_ERR_INTERNAL;
168 bcmos_errno olt_oper_res_success = BCM_ERR_OK;
169
170 bcmolt_olt_cfg olt_cfg = { };
171 bcmolt_olt_key olt_key = { };
172 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
173 olt_cfg.data.bal_state = BCMOLT_BAL_STATE_BAL_AND_SWITCH_READY;
174
175 Status olt_enable_res;
176
177 // Ensure that the state of the OLT is in deactivated to start with..
178 state.deactivate();
179
180 ON_CALL(balMock, bcmolt_host_init(_)).WillByDefault(Return(host_init_res));
181 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__bal_state_stub, bcmolt_cfg_get__bal_state_stub(_, _))
182 .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
183 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
184 .Times(BCM_MAX_DEVS_PER_LINE_CARD)
185 .WillRepeatedly(Return(bal_cfg_get_res));
186 // For the the first PON mac device, the activation result will fail, and will succeed for all other PON mac devices.
187 EXPECT_CALL(balMock, bcmolt_oper_submit(_, _))
188 .WillOnce(Return(olt_oper_res_fail))
189 .WillRepeatedly(Return(olt_oper_res_success));
190 olt_enable_res = Enable_(1, NULL);
191
192 ASSERT_TRUE( olt_enable_res.error_message() == Status::OK.error_message() );
193}
194
195////////////////////////////////////////////////////////////////////////
196// For testing Enable/Disable functionality
197////////////////////////////////////////////////////////////////////////
198
199int num_of_pon_ports = 16;
200
201// Create a mock function for bcmolt_cfg_get__olt_topology_stub C++ function
202MOCK_GLOBAL_FUNC2(bcmolt_cfg_get__olt_topology_stub, bcmos_errno(bcmolt_oltid, void*));
203
204class TestOltDisableReenable : public Test {
205 protected:
206 virtual void SetUp() {
207 NiceMock<BalMocker> balMock;
208 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
209
210 bcmolt_olt_cfg olt_cfg = { };
211 bcmolt_olt_key olt_key = { };
212
213 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
214
215 olt_cfg.data.topology.topology_maps.len = num_of_pon_ports;
216 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_, _))
217 .WillOnce(DoAll(SetArg1ToBcmOltCfg(olt_cfg), Return(bal_cfg_get_stub_res)));
218
219 ProbeDeviceCapabilities_();
220
221 }
222
223 virtual void TearDown() {
224 // Code here will be called immediately after each test
225 // (right before the destructor).
226 }
227};
228
229
230// Test Fixture for OltDisable
231
232// Test 1: OltDisableSuccess case
233TEST_F(TestOltDisableReenable, OltDisableSuccess){
234 // NiceMock is used to suppress 'WillByDefault' return errors.
235 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
236 NiceMock<BalMocker> balMock;
237 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
238
239 Status olt_disable_res;
240
241 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
242 .Times(num_of_pon_ports)
243 .WillRepeatedly(Return(bal_cfg_get_res));
244
245 olt_disable_res = Disable_();
246 ASSERT_TRUE( olt_disable_res.error_message() == Status::OK.error_message() );
247
248}
249
250// Test 2: OltDisableAllPonFailed case
251TEST_F(TestOltDisableReenable, OltDisableAllPonFailed){
252 // NiceMock is used to suppress 'WillByDefault' return errors.
253 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
254 NiceMock<BalMocker> balMock;
255 bcmos_errno bal_cfg_get_res = BCM_ERR_INTERNAL;
256
257 Status olt_disable_res;
258
259 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
260 .Times(num_of_pon_ports)
261 .WillRepeatedly(Return(bal_cfg_get_res));
262
263 olt_disable_res = Disable_();
264 ASSERT_TRUE( olt_disable_res.error_code() == grpc::StatusCode::INTERNAL);
265}
266
267
268// Test Fixture for OltReenable
269
270// Test 1: OltReenableSuccess case
271TEST_F(TestOltDisableReenable, OltReenableSuccess){
272 // NiceMock is used to suppress 'WillByDefault' return errors.
273 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
274 NiceMock<BalMocker> balMock;
275 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
276
277 Status olt_reenable_res;
278
279 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
280 .Times(num_of_pon_ports*2)
281 .WillRepeatedly(Return(bal_cfg_get_res));
282
283 olt_reenable_res = Reenable_();
284 ASSERT_TRUE( olt_reenable_res.error_message() == Status::OK.error_message() );
285
286}
287
288// Test 2: OltReenableAllPonFailed case
289TEST_F(TestOltDisableReenable, OltReenableAllPonFailed){
290 // NiceMock is used to suppress 'WillByDefault' return errors.
291 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
292 NiceMock<BalMocker> balMock;
293 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
294 bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
295
296 Status olt_reenable_res;
297
298 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
299 .Times(num_of_pon_ports)
300 .WillRepeatedly(Return(bal_cfg_get_res));
301 EXPECT_CALL(balMock,bcmolt_oper_submit(_, _))
302 .Times(num_of_pon_ports)
303 .WillRepeatedly(Return(olt_oper_res));
304 olt_reenable_res = Reenable_();
305 ASSERT_TRUE( olt_reenable_res.error_code() == grpc::StatusCode::INTERNAL);
306}
307
308////////////////////////////////////////////////////////////////////////////
309// For testing ProbeDeviceCapabilities functionality
310////////////////////////////////////////////////////////////////////////////
311class TestProbeDevCapabilities : public Test {
312 protected:
313 NiceMock<BalMocker> balMock;
314 bcmos_errno olt_res_success = BCM_ERR_OK;
315 bcmos_errno olt_res_fail = BCM_ERR_COMM_FAIL;
316 bcmos_errno dev_res_success = BCM_ERR_OK;
317 bcmos_errno dev_res_fail = BCM_ERR_COMM_FAIL;
318
319 virtual void SetUp() {
320 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
321
322 bcmolt_olt_cfg olt_cfg = { };
323 bcmolt_olt_key olt_key = { };
324
325 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
326
327 olt_cfg.data.topology.topology_maps.len = num_of_pon_ports;
328 }
329
330 virtual void TearDown() {
331 }
332};
333
334// Test 1 - If querying the OLT fails, the method must return error
335TEST_F(TestProbeDevCapabilities, ProbeDev_OltQueryFailed) {
336
337 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
338 .WillOnce(Return(olt_res_fail));
339
340 Status query_status = ProbeDeviceCapabilities_();
341 ASSERT_TRUE( query_status.error_message() != Status::OK.error_message() );
342}
343
344// Test 2 - If all devices are queried successfully, the method must return Status::OK
345TEST_F(TestProbeDevCapabilities, ProbeDev_OltQuerySucceeded_DevQueriesSucceeded) {
346
347 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
348 .WillOnce(Return(olt_res_success));
349
350 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
351 .WillRepeatedly(Return(dev_res_success));
352
353 Status query_status = ProbeDeviceCapabilities_();
354
355 ASSERT_TRUE( query_status.error_message() == Status::OK.error_message() );
356}
357
358// Test 3 - After successfully probing the OLT, even if probing all devices failed, the method must return error
359TEST_F(TestProbeDevCapabilities, ProbedDev_OltQuerySucceeded_AllDevQueriesFailed) {
360
361 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
362 .WillOnce(Return(olt_res_success));
363
364 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
365 .WillRepeatedly(Return(dev_res_fail));
366
367 Status query_status = ProbeDeviceCapabilities_();
368
369 ASSERT_TRUE( query_status.error_message() != Status::OK.error_message() );
370}
371
372// Test 4 - After successfully probing the OLT, if probing some devices fail, the method returns success
373TEST_F(TestProbeDevCapabilities, ProbedDev_OltQuerySucceeded_SomeDevQueriesFailed) {
374
375 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
376 .WillOnce(Return(olt_res_success));
377
378 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
379 .WillOnce(Return(olt_res_success))
380 .WillRepeatedly(Return(dev_res_fail));
381
382 Status query_status = ProbeDeviceCapabilities_();
383
384 ASSERT_TRUE( query_status.error_message() == Status::OK.error_message() );
385}
386