Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 1 | # 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 Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 15 | import functools |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 16 | import unittest |
| 17 | from mock import patch, call, Mock, PropertyMock |
| 18 | import requests_mock |
| 19 | |
| 20 | import os, sys |
| 21 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 23 | |
| 24 | class TestPullONUDevice(unittest.TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | global DeferredException |
| 28 | |
| 29 | self.sys_path_save = sys.path |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 30 | |
| 31 | # Setting up the config module |
| 32 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 33 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 34 | Config.clear() |
| 35 | Config.init(config, "synchronizer-config-schema.yaml") |
| 36 | # END Setting up the config module |
| 37 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 38 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 39 | mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), |
Matteo Scandolo | 35207b7 | 2019-05-10 08:46:48 -0700 | [diff] [blame^] | 40 | ("rcord", "rcord.xproto")]) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 41 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 42 | 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 Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 49 | |
| 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 Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 68 | # second mock OLTDevice |
| 69 | self.olt2 = Mock() |
| 70 | self.olt2.id = 2 |
| 71 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 72 | # mock pon port |
| 73 | self.pon_port = Mock() |
| 74 | self.pon_port.id = 1 |
| 75 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 76 | # mock pon port |
| 77 | self.pon_port2 = Mock() |
| 78 | self.pon_port2.id = 2 |
| 79 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 80 | # 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 Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 94 | "reason": "starting-omci", |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 95 | "parent_id": "00010fc93996afea", |
| 96 | "parent_port_no": 1 |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 97 | } |
| 98 | ] |
| 99 | } |
| 100 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 101 | 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 Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 114 | "reason": "starting-omci", |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 115 | "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 Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 129 | "reason": "omci-admin-lock", |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 130 | "parent_id": "00010fc93996afeb", |
| 131 | "parent_port_no": 1 |
| 132 | } |
| 133 | ], |
| 134 | } |
| 135 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 136 | # TODO add ports |
| 137 | self.ports = { |
| 138 | "items": [] |
| 139 | } |
| 140 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 141 | 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 Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 149 | def test_pull(self, m): |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 150 | |
| 151 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
| 152 | patch.object(OLTDevice.objects, "get") as mock_olt_device, \ |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 153 | patch.object(PONPort.objects, "get") as mock_pon_port, \ |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 154 | patch.object(ONUDevice, "save", autospec=True) as mock_save: |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 155 | olt_service_mock.return_value = [self.volt_service] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 156 | mock_pon_port.return_value = self.pon_port |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 157 | 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 Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 160 | m.get("http://voltha_url:1234/api/v1/devices/0001130158f01b2d/ports", status_code=200, json=self.ports) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 161 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 162 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 163 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 164 | saved_onu = mock_save.call_args[0][0] |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 165 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 166 | self.assertEqual(saved_onu.admin_state, "ENABLED") |
| 167 | self.assertEqual(saved_onu.oper_status, "ACTIVE") |
| 168 | self.assertEqual(saved_onu.connect_status, "REACHABLE") |
Scott Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 169 | self.assertEqual(saved_onu.reason, "starting-omci") |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 170 | self.assertEqual(saved_onu.device_type, "broadcom_onu") |
| 171 | self.assertEqual(saved_onu.vendor, "Broadcom") |
| 172 | self.assertEqual(saved_onu.device_id, "0001130158f01b2d") |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 173 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 174 | self.assertEqual(mock_save.call_count, 1) |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 175 | |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 176 | @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 Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 204 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 205 | |
| 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 Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 214 | self.assertEqual(saved_onu.reason, "omci-admin-lock") |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 215 | 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 Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 243 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 244 | |
| 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 Baker | 3db0eef | 2019-01-15 11:56:41 -0800 | [diff] [blame] | 253 | self.assertEqual(saved_onu.reason, "omci-admin-lock") |
Scott Baker | 22b46c5 | 2018-11-15 15:15:29 -0800 | [diff] [blame] | 254 | 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 Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 260 | #[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 Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 299 | |
| 300 | if __name__ == "__main__": |
Vijaykumar Kushwaha | 377d1da | 2019-03-20 10:29:42 +0530 | [diff] [blame] | 301 | unittest.main() |