blob: 12fd0eeaa4bbc4efc82c519183cc15298d41b500 [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;
Chaitrashree G S73e084d2019-11-20 16:18:59 -0500237 bcmos_errno olt_oper_res = BCM_ERR_OK;
Amit Ghoshfcad4d32019-11-13 10:24:55 +0000238
239 Status olt_disable_res;
Chaitrashree G S73e084d2019-11-20 16:18:59 -0500240 state.deactivate();
241 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
Amit Ghoshfcad4d32019-11-13 10:24:55 +0000242 olt_disable_res = Disable_();
243 ASSERT_TRUE( olt_disable_res.error_message() == Status::OK.error_message() );
244
245}
246
247// Test 2: OltDisableAllPonFailed case
248TEST_F(TestOltDisableReenable, OltDisableAllPonFailed){
249 // NiceMock is used to suppress 'WillByDefault' return errors.
250 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
251 NiceMock<BalMocker> balMock;
Chaitrashree G S73e084d2019-11-20 16:18:59 -0500252 bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
Amit Ghoshfcad4d32019-11-13 10:24:55 +0000253
254 Status olt_disable_res;
Chaitrashree G S73e084d2019-11-20 16:18:59 -0500255 state.deactivate();
256 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
Amit Ghoshfcad4d32019-11-13 10:24:55 +0000257 olt_disable_res = Disable_();
258 ASSERT_TRUE( olt_disable_res.error_code() == grpc::StatusCode::INTERNAL);
259}
260
261
262// Test Fixture for OltReenable
263
264// Test 1: OltReenableSuccess case
265TEST_F(TestOltDisableReenable, OltReenableSuccess){
266 // NiceMock is used to suppress 'WillByDefault' return errors.
267 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
268 NiceMock<BalMocker> balMock;
269 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
270
271 Status olt_reenable_res;
272
273 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
274 .Times(num_of_pon_ports*2)
275 .WillRepeatedly(Return(bal_cfg_get_res));
276
277 olt_reenable_res = Reenable_();
278 ASSERT_TRUE( olt_reenable_res.error_message() == Status::OK.error_message() );
279
280}
281
282// Test 2: OltReenableAllPonFailed case
283TEST_F(TestOltDisableReenable, OltReenableAllPonFailed){
284 // NiceMock is used to suppress 'WillByDefault' return errors.
285 // This is described in https://github.com/arangodb-helper/gtest/blob/master/googlemock/docs/CookBook.md
286 NiceMock<BalMocker> balMock;
287 bcmos_errno bal_cfg_get_res = BCM_ERR_OK;
288 bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
289
290 Status olt_reenable_res;
291
292 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
293 .Times(num_of_pon_ports)
294 .WillRepeatedly(Return(bal_cfg_get_res));
295 EXPECT_CALL(balMock,bcmolt_oper_submit(_, _))
296 .Times(num_of_pon_ports)
297 .WillRepeatedly(Return(olt_oper_res));
298 olt_reenable_res = Reenable_();
299 ASSERT_TRUE( olt_reenable_res.error_code() == grpc::StatusCode::INTERNAL);
300}
301
302////////////////////////////////////////////////////////////////////////////
303// For testing ProbeDeviceCapabilities functionality
304////////////////////////////////////////////////////////////////////////////
305class TestProbeDevCapabilities : public Test {
306 protected:
307 NiceMock<BalMocker> balMock;
308 bcmos_errno olt_res_success = BCM_ERR_OK;
309 bcmos_errno olt_res_fail = BCM_ERR_COMM_FAIL;
310 bcmos_errno dev_res_success = BCM_ERR_OK;
311 bcmos_errno dev_res_fail = BCM_ERR_COMM_FAIL;
312
313 virtual void SetUp() {
314 bcmos_errno bal_cfg_get_stub_res = BCM_ERR_OK;
315
316 bcmolt_olt_cfg olt_cfg = { };
317 bcmolt_olt_key olt_key = { };
318
319 BCMOLT_CFG_INIT(&olt_cfg, olt, olt_key);
320
321 olt_cfg.data.topology.topology_maps.len = num_of_pon_ports;
322 }
323
324 virtual void TearDown() {
325 }
326};
327
328// Test 1 - If querying the OLT fails, the method must return error
329TEST_F(TestProbeDevCapabilities, ProbeDev_OltQueryFailed) {
330
331 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
332 .WillOnce(Return(olt_res_fail));
333
334 Status query_status = ProbeDeviceCapabilities_();
335 ASSERT_TRUE( query_status.error_message() != Status::OK.error_message() );
336}
337
338// Test 2 - If all devices are queried successfully, the method must return Status::OK
339TEST_F(TestProbeDevCapabilities, ProbeDev_OltQuerySucceeded_DevQueriesSucceeded) {
340
341 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
342 .WillOnce(Return(olt_res_success));
343
344 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
345 .WillRepeatedly(Return(dev_res_success));
346
347 Status query_status = ProbeDeviceCapabilities_();
348
349 ASSERT_TRUE( query_status.error_message() == Status::OK.error_message() );
350}
351
352// Test 3 - After successfully probing the OLT, even if probing all devices failed, the method must return error
353TEST_F(TestProbeDevCapabilities, ProbedDev_OltQuerySucceeded_AllDevQueriesFailed) {
354
355 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
356 .WillOnce(Return(olt_res_success));
357
358 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
359 .WillRepeatedly(Return(dev_res_fail));
360
361 Status query_status = ProbeDeviceCapabilities_();
362
363 ASSERT_TRUE( query_status.error_message() != Status::OK.error_message() );
364}
365
366// Test 4 - After successfully probing the OLT, if probing some devices fail, the method returns success
367TEST_F(TestProbeDevCapabilities, ProbedDev_OltQuerySucceeded_SomeDevQueriesFailed) {
368
369 EXPECT_GLOBAL_CALL(bcmolt_cfg_get__olt_topology_stub, bcmolt_cfg_get__olt_topology_stub(_,_))
370 .WillOnce(Return(olt_res_success));
371
372 EXPECT_CALL(balMock, bcmolt_cfg_get(_, _))
373 .WillOnce(Return(olt_res_success))
374 .WillRepeatedly(Return(dev_res_fail));
375
376 Status query_status = ProbeDeviceCapabilities_();
377
378 ASSERT_TRUE( query_status.error_message() == Status::OK.error_message() );
379}
380
Chaitrashree G S73e084d2019-11-20 16:18:59 -0500381////////////////////////////////////////////////////////////////////////////
382// For testing DisablePonIf functionality
383////////////////////////////////////////////////////////////////////////////
384
385class TestDisablePonIf : public Test {
386 protected:
387 virtual void SetUp() {
388 }
389
390 virtual void TearDown() {
391 }
392};
393
394// Test 1 - DisablePonIf success case
395TEST_F(TestDisablePonIf, DisablePonIfSuccess) {
396 bcmos_errno olt_oper_res = BCM_ERR_OK;
397 bcmos_errno bal_cfg_set_res = BCM_ERR_OK;
398 NiceMock<BalMocker> balMock;
399 uint32_t pon_id=1;
400
401 //ON_CALL(balMock, bcmolt_cfg_set(_, _)).WillByDefault(Return(bal_cfg_set_res));
402 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
403 state.deactivate();
404 Status status = DisablePonIf_(pon_id);
405
406 ASSERT_TRUE( status.error_message() == Status::OK.error_message() );
407}
408
409// Test 2 - DisablePonIf Failure case
410TEST_F(TestDisablePonIf, DisablePonIfFailed) {
411 bcmos_errno olt_oper_res = BCM_ERR_INTERNAL;
412 NiceMock<BalMocker> balMock;
413 uint32_t pon_id=1;
414
415 ON_CALL(balMock, bcmolt_oper_submit(_, _)).WillByDefault(Return(olt_oper_res));
416 state.deactivate();
417 Status status = DisablePonIf_(pon_id);
418
419 ASSERT_TRUE( status.error_message() != Status::OK.error_message() );
420}
421
422// Test 3 - DisablePonIf ONU discovery failure case
423TEST_F(TestDisablePonIf, DisablePonIfOnuDiscoveryFail) {
424 NiceMock<BalMocker> balMock;
425 uint32_t pon_id=1;
426 bcmos_errno bal_cfg_set_res= BCM_ERR_INTERNAL;
427 ON_CALL(balMock, bcmolt_cfg_set(_, _)).WillByDefault(Return(bal_cfg_set_res));
428 state.deactivate();
429 Status status = DisablePonIf_(pon_id);
430
431 ASSERT_TRUE( status.error_message() != Status::OK.error_message() );
432}
433