blob: 51bb5a5934dd60820651926c9f850cb92237f454 [file] [log] [blame]
Matteo Scandolod44ca992018-05-17 15:02:10 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Baker22b46c52018-11-15 15:15:29 -080015import functools
Matteo Scandolod44ca992018-05-17 15:02:10 -070016import unittest
17from mock import patch, call, Mock, PropertyMock
18import requests_mock
19
20import os, sys
21
Matteo Scandolod44ca992018-05-17 15:02:10 -070022test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolod44ca992018-05-17 15:02:10 -070023
24class TestPullONUDevice(unittest.TestCase):
25
26 def setUp(self):
27 global DeferredException
28
29 self.sys_path_save = sys.path
Matteo Scandolod44ca992018-05-17 15:02:10 -070030
31 # Setting up the config module
32 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070033 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolod44ca992018-05-17 15:02:10 -070034 Config.clear()
35 Config.init(config, "synchronizer-config-schema.yaml")
36 # END Setting up the config module
37
Scott Baker47b47302019-01-30 16:55:07 -080038 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
39 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
Matteo Scandolo35207b72019-05-10 08:46:48 -070040 ("rcord", "rcord.xproto")])
Matteo Scandolod44ca992018-05-17 15:02:10 -070041
Scott Baker47b47302019-01-30 16:55:07 -080042 import xossynchronizer.modelaccessor
43 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
44
45 from xossynchronizer.modelaccessor import model_accessor
46 self.model_accessor = model_accessor
47
48 from pull_onus import ONUDevicePullStep
Matteo Scandolod44ca992018-05-17 15:02:10 -070049
50 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
54 self.sync_step = ONUDevicePullStep
55
56 # mock volt service
57 self.volt_service = Mock()
58 self.volt_service.id = "volt_service_id"
59 self.volt_service.voltha_url = "voltha_url"
60 self.volt_service.voltha_user = "voltha_user"
61 self.volt_service.voltha_pass = "voltha_pass"
62 self.volt_service.voltha_port = 1234
63
64 # mock OLTDevice
65 self.olt = Mock()
66 self.olt.id = 1
67
Scott Baker22b46c52018-11-15 15:15:29 -080068 # second mock OLTDevice
69 self.olt2 = Mock()
70 self.olt2.id = 2
71
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070072 # mock pon port
73 self.pon_port = Mock()
74 self.pon_port.id = 1
75
Scott Baker22b46c52018-11-15 15:15:29 -080076 # mock pon port
77 self.pon_port2 = Mock()
78 self.pon_port2.id = 2
79
Matteo Scandolod44ca992018-05-17 15:02:10 -070080 # mock voltha responses
81 self.devices = {
82 "items": [
83 {
84 "id": "0001130158f01b2d",
85 "type": "broadcom_onu",
86 "vendor": "Broadcom",
87 "serial_number": "BRCM22222222",
88 "vendor_id": "BRCM",
89 "adapter": "broadcom_onu",
90 "vlan": 0,
91 "admin_state": "ENABLED",
92 "oper_status": "ACTIVE",
93 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -080094 "reason": "starting-omci",
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070095 "parent_id": "00010fc93996afea",
96 "parent_port_no": 1
Matteo Scandolod44ca992018-05-17 15:02:10 -070097 }
98 ]
99 }
100
Scott Baker22b46c52018-11-15 15:15:29 -0800101 self.two_devices = {
102 "items": [
103 {
104 "id": "0001130158f01b2d",
105 "type": "broadcom_onu",
106 "vendor": "Broadcom",
107 "serial_number": "BRCM22222222",
108 "vendor_id": "BRCM",
109 "adapter": "broadcom_onu",
110 "vlan": 0,
111 "admin_state": "ENABLED",
112 "oper_status": "ACTIVE",
113 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -0800114 "reason": "starting-omci",
Scott Baker22b46c52018-11-15 15:15:29 -0800115 "parent_id": "00010fc93996afea",
116 "parent_port_no": 1
117 },
118 {
119 "id": "0001130158f01b2e",
120 "type": "broadcom_onu",
121 "vendor": "Broadcom",
122 "serial_number": "BRCM22222223",
123 "vendor_id": "BRCM",
124 "adapter": "broadcom_onu",
125 "vlan": 0,
126 "admin_state": "ENABLED",
127 "oper_status": "ACTIVE",
128 "connect_status": "REACHABLE",
Scott Baker3db0eef2019-01-15 11:56:41 -0800129 "reason": "omci-admin-lock",
Scott Baker22b46c52018-11-15 15:15:29 -0800130 "parent_id": "00010fc93996afeb",
131 "parent_port_no": 1
132 }
133 ],
134 }
135
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700136 # TODO add ports
137 self.ports = {
138 "items": []
139 }
140
Matteo Scandolod44ca992018-05-17 15:02:10 -0700141 def tearDown(self):
142 sys.path = self.sys_path_save
143
144 @requests_mock.Mocker()
145 def test_missing_volt_service(self, m):
146 self.assertFalse(m.called)
147
148 @requests_mock.Mocker()
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700149 def test_pull(self, m):
Matteo Scandolod44ca992018-05-17 15:02:10 -0700150
151 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
152 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700153 patch.object(PONPort.objects, "get") as mock_pon_port, \
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700154 patch.object(ONUDevice, "save", autospec=True) as mock_save:
Matteo Scandolod44ca992018-05-17 15:02:10 -0700155 olt_service_mock.return_value = [self.volt_service]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700156 mock_pon_port.return_value = self.pon_port
Matteo Scandolod44ca992018-05-17 15:02:10 -0700157 mock_olt_device.return_value = self.olt
158
159 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700160 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700161
Scott Baker47b47302019-01-30 16:55:07 -0800162 self.sync_step(model_accessor=self.model_accessor).pull_records()
Matteo Scandolod44ca992018-05-17 15:02:10 -0700163
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700164 saved_onu = mock_save.call_args[0][0]
Matteo Scandolod44ca992018-05-17 15:02:10 -0700165
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700166 self.assertEqual(saved_onu.admin_state, "ENABLED")
167 self.assertEqual(saved_onu.oper_status, "ACTIVE")
168 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800169 self.assertEqual(saved_onu.reason, "starting-omci")
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700170 self.assertEqual(saved_onu.device_type, "broadcom_onu")
171 self.assertEqual(saved_onu.vendor, "Broadcom")
172 self.assertEqual(saved_onu.device_id, "0001130158f01b2d")
Matteo Scandolod44ca992018-05-17 15:02:10 -0700173
Matteo Scandolod8ed60e2018-06-18 17:00:57 -0700174 self.assertEqual(mock_save.call_count, 1)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700175
Scott Baker22b46c52018-11-15 15:15:29 -0800176 @requests_mock.Mocker()
177 def test_pull_bad_pon(self, m):
178
179 def olt_side_effect(device_id):
180 # fail the first onu device
181 if device_id=="00010fc93996afea":
182 return self.olt
183 else:
184 return self.olt2
185
186 def pon_port_side_effect(mock_pon_port, port_no, olt_device_id):
187 # fail the first onu device
188 if olt_device_id==1:
189 raise IndexError()
190 return self.pon_port2
191
192 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
193 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
194 patch.object(PONPort.objects, "get") as mock_pon_port, \
195 patch.object(ONUDevice, "save", autospec=True) as mock_save:
196 olt_service_mock.return_value = [self.volt_service]
197 mock_pon_port.side_effect = functools.partial(pon_port_side_effect, self.pon_port)
198 mock_olt_device.side_effect = olt_side_effect
199
200 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
201 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
202 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
203
Scott Baker47b47302019-01-30 16:55:07 -0800204 self.sync_step(model_accessor=self.model_accessor).pull_records()
Scott Baker22b46c52018-11-15 15:15:29 -0800205
206 self.assertEqual(mock_save.call_count, 1)
207 saved_onu = mock_save.call_args[0][0]
208
209 # we should get the second onu in self.two_onus
210
211 self.assertEqual(saved_onu.admin_state, "ENABLED")
212 self.assertEqual(saved_onu.oper_status, "ACTIVE")
213 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800214 self.assertEqual(saved_onu.reason, "omci-admin-lock")
Scott Baker22b46c52018-11-15 15:15:29 -0800215 self.assertEqual(saved_onu.device_type, "broadcom_onu")
216 self.assertEqual(saved_onu.vendor, "Broadcom")
217 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
218
219 self.assertEqual(mock_save.call_count, 1)
220
221 @requests_mock.Mocker()
222 def test_pull_bad_olt(self, m):
223
224 def olt_side_effect(device_id):
225 # fail the first onu device
226 if device_id=="00010fc93996afea":
227 raise IndexError()
228 else:
229 return self.olt2
230
231 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
232 patch.object(OLTDevice.objects, "get") as mock_olt_device, \
233 patch.object(PONPort.objects, "get") as mock_pon_port, \
234 patch.object(ONUDevice, "save", autospec=True) as mock_save:
235 olt_service_mock.return_value = [self.volt_service]
236 mock_pon_port.return_value = self.pon_port2
237 mock_olt_device.side_effect = olt_side_effect
238
239 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.two_devices)
240 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports)
241 m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2e/ports", status_code=200, json=self.ports)
242
Scott Baker47b47302019-01-30 16:55:07 -0800243 self.sync_step(model_accessor=self.model_accessor).pull_records()
Scott Baker22b46c52018-11-15 15:15:29 -0800244
245 self.assertEqual(mock_save.call_count, 1)
246 saved_onu = mock_save.call_args[0][0]
247
248 # we should get the second onu in self.two_onus
249
250 self.assertEqual(saved_onu.admin_state, "ENABLED")
251 self.assertEqual(saved_onu.oper_status, "ACTIVE")
252 self.assertEqual(saved_onu.connect_status, "REACHABLE")
Scott Baker3db0eef2019-01-15 11:56:41 -0800253 self.assertEqual(saved_onu.reason, "omci-admin-lock")
Scott Baker22b46c52018-11-15 15:15:29 -0800254 self.assertEqual(saved_onu.device_type, "broadcom_onu")
255 self.assertEqual(saved_onu.vendor, "Broadcom")
256 self.assertEqual(saved_onu.device_id, "0001130158f01b2e")
257
258 self.assertEqual(mock_save.call_count, 1)
259
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +0530260#[SEBA-367] Unit test for blank response recieved from Voltha
261
262 @requests_mock.Mocker()
263 def test_blank_response_received(self, m):
264
265 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="")
266 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
267 patch.object(PONPort.objects, "get") as mock_pon_port, \
268 patch.object(OLTDevice.objects, "get") as mock_get, \
269 patch.object(ONUDevice, "save", autospec=True) as mock_save:
270
271 olt_service_mock.return_value = [self.volt_service]
272
273 self.sync_step(model_accessor=self.model_accessor).pull_records()
274
275 olt_service_mock.assert_called()
276 mock_pon_port.assert_not_called()
277 mock_get.assert_not_called()
278 self.assertEqual(mock_save.call_count, 0)
279
280#[SEBA-367] Unit test for invalid json received from Voltha
281
282 @requests_mock.Mocker()
283 def test_invalid_json(self, m):
284
285 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, text="{\"items\" : [host_and_port}")
286 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
287 patch.object(PONPort.objects, "get") as mock_pon_port, \
288 patch.object(OLTDevice.objects, "get") as mock_get, \
289 patch.object(ONUDevice, "save", autospec=True) as mock_save:
290
291 olt_service_mock.return_value = [self.volt_service]
292
293 self.sync_step(model_accessor=self.model_accessor).pull_records()
294
295 olt_service_mock.assert_called()
296 mock_pon_port.assert_not_called()
297 mock_get.assert_not_called()
298 self.assertEqual(mock_save.call_count, 0)
Matteo Scandolod44ca992018-05-17 15:02:10 -0700299
300if __name__ == "__main__":
Vijaykumar Kushwaha377d1da2019-03-20 10:29:42 +0530301 unittest.main()