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 | |
| 15 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import requests_mock |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | xos_dir=os.path.join(test_path, "../../..") |
| 24 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 25 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 26 | services_dir = os.path.join(xos_dir, "../../xos_services") |
| 27 | sys.path.append(xos_dir) |
| 28 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 29 | # END Hack to load synchronizer framework |
| 30 | |
| 31 | # generate model from xproto |
| 32 | def get_models_fn(service_name, xproto_name): |
| 33 | name = os.path.join(service_name, "xos", xproto_name) |
| 34 | if os.path.exists(os.path.join(services_dir, name)): |
| 35 | return name |
| 36 | else: |
| 37 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 41 | # END generate model from xproto |
| 42 | |
| 43 | class TestPullONUDevice(unittest.TestCase): |
| 44 | |
| 45 | def setUp(self): |
| 46 | global DeferredException |
| 47 | |
| 48 | self.sys_path_save = sys.path |
| 49 | sys.path.append(xos_dir) |
| 50 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 51 | |
| 52 | # Setting up the config module |
| 53 | from xosconfig import Config |
| 54 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 55 | Config.clear() |
| 56 | Config.init(config, "synchronizer-config-schema.yaml") |
| 57 | # END Setting up the config module |
| 58 | |
| 59 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 60 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 61 | |
| 62 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 63 | # and apparently it is not overriding the generated model accessor |
| 64 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 65 | get_models_fn("vsg", "vsg.xproto"), |
| 66 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
| 67 | import synchronizers.new_base.modelaccessor |
| 68 | from pull_onus import ONUDevicePullStep, model_accessor |
| 69 | |
| 70 | # import all class names to globals |
| 71 | for (k, v) in model_accessor.all_model_classes.items(): |
| 72 | globals()[k] = v |
| 73 | |
| 74 | self.sync_step = ONUDevicePullStep |
| 75 | |
| 76 | # mock volt service |
| 77 | self.volt_service = Mock() |
| 78 | self.volt_service.id = "volt_service_id" |
| 79 | self.volt_service.voltha_url = "voltha_url" |
| 80 | self.volt_service.voltha_user = "voltha_user" |
| 81 | self.volt_service.voltha_pass = "voltha_pass" |
| 82 | self.volt_service.voltha_port = 1234 |
| 83 | |
| 84 | # mock OLTDevice |
| 85 | self.olt = Mock() |
| 86 | self.olt.id = 1 |
| 87 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 88 | # mock pon port |
| 89 | self.pon_port = Mock() |
| 90 | self.pon_port.id = 1 |
| 91 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 92 | # mock voltha responses |
| 93 | self.devices = { |
| 94 | "items": [ |
| 95 | { |
| 96 | "id": "0001130158f01b2d", |
| 97 | "type": "broadcom_onu", |
| 98 | "vendor": "Broadcom", |
| 99 | "serial_number": "BRCM22222222", |
| 100 | "vendor_id": "BRCM", |
| 101 | "adapter": "broadcom_onu", |
| 102 | "vlan": 0, |
| 103 | "admin_state": "ENABLED", |
| 104 | "oper_status": "ACTIVE", |
| 105 | "connect_status": "REACHABLE", |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 106 | "parent_id": "00010fc93996afea", |
| 107 | "parent_port_no": 1 |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 108 | } |
| 109 | ] |
| 110 | } |
| 111 | |
| 112 | def tearDown(self): |
| 113 | sys.path = self.sys_path_save |
| 114 | |
| 115 | @requests_mock.Mocker() |
| 116 | def test_missing_volt_service(self, m): |
| 117 | self.assertFalse(m.called) |
| 118 | |
| 119 | @requests_mock.Mocker() |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 120 | def test_pull(self, m): |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 121 | |
| 122 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
| 123 | patch.object(OLTDevice.objects, "get") as mock_olt_device, \ |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 124 | patch.object(PONPort.objects, "get") as mock_pon_port, \ |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 125 | patch.object(ONUDevice, "save") as mock_save: |
| 126 | olt_service_mock.return_value = [self.volt_service] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 127 | mock_pon_port.return_value = self.pon_port |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 128 | mock_olt_device.return_value = self.olt |
| 129 | |
| 130 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
| 131 | |
| 132 | self.sync_step().pull_records() |
| 133 | |
| 134 | # TODO how to asster this? |
| 135 | # self.assertEqual(existing_olt.admin_state, "ENABLED") |
| 136 | # self.assertEqual(existing_olt.oper_status, "ACTIVE") |
| 137 | # self.assertEqual(existing_olt.volt_service_id, "volt_service_id") |
| 138 | # self.assertEqual(existing_olt.device_id, "test_id") |
| 139 | # self.assertEqual(existing_olt.of_id, "of_id") |
| 140 | # self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000") |
| 141 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 142 | mock_save.assert_called_with() |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 143 | |
| 144 | @requests_mock.Mocker() |
| 145 | def _test_pull_existing(self, m): |
| 146 | |
| 147 | existing_olt = Mock() |
| 148 | existing_olt.enacted = 2 |
| 149 | existing_olt.updated = 1 |
| 150 | |
| 151 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
| 152 | patch.object(OLTDevice.objects, "filter") as mock_get, \ |
| 153 | patch.object(existing_olt, "save") as mock_save: |
| 154 | olt_service_mock.return_value = [self.volt_service] |
| 155 | mock_get.return_value = [existing_olt] |
| 156 | |
| 157 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
| 158 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices) |
| 159 | |
| 160 | self.sync_step().pull_records() |
| 161 | |
| 162 | self.assertEqual(existing_olt.admin_state, "ENABLED") |
| 163 | self.assertEqual(existing_olt.oper_status, "ACTIVE") |
| 164 | self.assertEqual(existing_olt.volt_service_id, "volt_service_id") |
| 165 | self.assertEqual(existing_olt.device_id, "test_id") |
| 166 | self.assertEqual(existing_olt.of_id, "of_id") |
| 167 | self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000") |
| 168 | |
| 169 | mock_save.assert_called() |
| 170 | |
| 171 | @requests_mock.Mocker() |
| 172 | def _test_pull_existing_do_not_sync(self, m): |
| 173 | existing_olt = Mock() |
| 174 | existing_olt.enacted = 1 |
| 175 | existing_olt.updated = 2 |
| 176 | |
| 177 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
| 178 | patch.object(OLTDevice.objects, "get") as mock_get, \ |
| 179 | patch.object(existing_olt, "save") as mock_save: |
| 180 | olt_service_mock.return_value = [self.volt_service] |
| 181 | mock_get.return_value = existing_olt |
| 182 | |
| 183 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
| 184 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices) |
| 185 | |
| 186 | self.sync_step().pull_records() |
| 187 | |
| 188 | mock_save.assert_not_called() |
| 189 | |
| 190 | if __name__ == "__main__": |
| 191 | unittest.main() |