Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -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 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 21 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 22 | |
| 23 | class TestSyncOLTDevice(unittest.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | global DeferredException |
| 27 | |
| 28 | self.sys_path_save = sys.path |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 29 | |
| 30 | # Setting up the config module |
| 31 | from xosconfig import Config |
Matteo Scandolo | f7ebb11 | 2018-09-18 16:17:22 -0700 | [diff] [blame] | 32 | config = os.path.join(test_path, "../test_config.yaml") |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 33 | Config.clear() |
| 34 | Config.init(config, "synchronizer-config-schema.yaml") |
| 35 | # END Setting up the config module |
| 36 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 37 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 38 | mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"), |
| 39 | ("vsg", "vsg.xproto"), |
Matteo Scandolo | e04a488 | 2019-02-14 10:22:24 -0800 | [diff] [blame^] | 40 | ("rcord", "rcord.xproto"),]) |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -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_olts import OLTDevicePullStep |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -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 = OLTDevicePullStep |
| 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" |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 62 | self.volt_service.voltha_port = 1234 |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 63 | |
| 64 | # mock voltha responses |
| 65 | self.devices = { |
| 66 | "items": [ |
| 67 | { |
| 68 | "id": "test_id", |
| 69 | "type": "simulated_olt", |
| 70 | "host_and_port": "172.17.0.1:50060", |
| 71 | "admin_state": "ENABLED", |
Matteo Scandolo | d6fce51 | 2018-10-16 10:35:29 -0700 | [diff] [blame] | 72 | "oper_status": "ACTIVE", |
| 73 | "serial_number": "serial_number", |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 74 | } |
| 75 | ] |
| 76 | } |
| 77 | |
| 78 | self.logical_devices = { |
| 79 | "items": [ |
| 80 | { |
| 81 | "root_device_id": "test_id", |
| 82 | "id": "of_id", |
| 83 | "datapath_id": "55334486016" |
| 84 | } |
| 85 | ] |
| 86 | } |
| 87 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 88 | self.ports = { |
| 89 | "items": [ |
| 90 | { |
| 91 | "label": "PON port", |
| 92 | "port_no": 1, |
| 93 | "type": "PON_OLT", |
| 94 | "admin_state": "ENABLED", |
| 95 | "oper_status": "ACTIVE" |
| 96 | }, |
| 97 | { |
| 98 | "label": "NNI facing Ethernet port", |
| 99 | "port_no": 2, |
| 100 | "type": "ETHERNET_NNI", |
| 101 | "admin_state": "ENABLED", |
| 102 | "oper_status": "ACTIVE" |
| 103 | } |
| 104 | ] |
| 105 | } |
| 106 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 107 | def tearDown(self): |
| 108 | sys.path = self.sys_path_save |
| 109 | |
| 110 | @requests_mock.Mocker() |
| 111 | def test_missing_volt_service(self, m): |
| 112 | self.assertFalse(m.called) |
| 113 | |
| 114 | @requests_mock.Mocker() |
| 115 | def test_pull(self, m): |
| 116 | |
| 117 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 118 | patch.object(OLTDevice, "save") as mock_olt_save, \ |
| 119 | patch.object(PONPort, "save") as mock_pon_save, \ |
| 120 | patch.object(NNIPort, "save") as mock_nni_save: |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 121 | olt_service_mock.return_value = [self.volt_service] |
| 122 | |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 123 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 124 | m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports) |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 125 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 126 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 127 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 128 | |
| 129 | # TODO how to asster this? |
| 130 | # self.assertEqual(existing_olt.admin_state, "ENABLED") |
| 131 | # self.assertEqual(existing_olt.oper_status, "ACTIVE") |
| 132 | # self.assertEqual(existing_olt.volt_service_id, "volt_service_id") |
| 133 | # self.assertEqual(existing_olt.device_id, "test_id") |
| 134 | # self.assertEqual(existing_olt.of_id, "of_id") |
| 135 | # self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000") |
| 136 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 137 | mock_olt_save.assert_called() |
| 138 | mock_pon_save.assert_called() |
| 139 | mock_nni_save.assert_called() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 140 | |
| 141 | @requests_mock.Mocker() |
| 142 | def test_pull_existing(self, m): |
| 143 | |
| 144 | existing_olt = Mock() |
Scott Baker | 09798d8 | 2019-01-17 08:34:59 -0800 | [diff] [blame] | 145 | existing_olt.admin_state = "ENABLED" |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 146 | existing_olt.enacted = 2 |
| 147 | existing_olt.updated = 1 |
| 148 | |
| 149 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 150 | patch.object(OLTDevice.objects, "filter") as mock_get, \ |
| 151 | patch.object(PONPort, "save") as mock_pon_save, \ |
| 152 | patch.object(NNIPort, "save") as mock_nni_save, \ |
| 153 | patch.object(existing_olt, "save") as mock_olt_save: |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 154 | olt_service_mock.return_value = [self.volt_service] |
| 155 | mock_get.return_value = [existing_olt] |
| 156 | |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 157 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 158 | m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports) |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 159 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 160 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 161 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 162 | |
| 163 | self.assertEqual(existing_olt.admin_state, "ENABLED") |
| 164 | self.assertEqual(existing_olt.oper_status, "ACTIVE") |
| 165 | self.assertEqual(existing_olt.volt_service_id, "volt_service_id") |
| 166 | self.assertEqual(existing_olt.device_id, "test_id") |
| 167 | self.assertEqual(existing_olt.of_id, "of_id") |
| 168 | self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000") |
| 169 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 170 | mock_olt_save.assert_called() |
| 171 | mock_pon_save.assert_called() |
| 172 | mock_nni_save.assert_called() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 173 | |
| 174 | @requests_mock.Mocker() |
| 175 | def test_pull_existing_do_not_sync(self, m): |
| 176 | existing_olt = Mock() |
| 177 | existing_olt.enacted = 1 |
| 178 | existing_olt.updated = 2 |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 179 | existing_olt.device_id = "test_id" |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 180 | |
| 181 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 182 | patch.object(OLTDevice.objects, "filter") as mock_get, \ |
| 183 | patch.object(PONPort, "save") as mock_pon_save, \ |
| 184 | patch.object(NNIPort, "save") as mock_nni_save, \ |
| 185 | patch.object(existing_olt, "save") as mock_olt_save: |
| 186 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 187 | olt_service_mock.return_value = [self.volt_service] |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 188 | mock_get.return_value = [existing_olt] |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 189 | |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 190 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices) |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 191 | m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports) |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 192 | m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices) |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 193 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 194 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 195 | |
Matteo Scandolo | 6be6ee9 | 2018-05-24 15:07:51 -0700 | [diff] [blame] | 196 | mock_olt_save.assert_not_called() |
| 197 | mock_pon_save.assert_called() |
| 198 | mock_nni_save.assert_called() |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 199 | |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 200 | @requests_mock.Mocker() |
| 201 | def test_pull_deleted_object(self, m): |
| 202 | existing_olt = Mock() |
| 203 | existing_olt.enacted = 2 |
| 204 | existing_olt.updated = 1 |
| 205 | existing_olt.device_id = "test_id" |
| 206 | |
| 207 | m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json={"items": []}) |
| 208 | |
| 209 | with patch.object(VOLTService.objects, "all") as olt_service_mock, \ |
| 210 | patch.object(OLTDevice.objects, "get_items") as mock_get, \ |
| 211 | patch.object(existing_olt, "delete") as mock_olt_delete: |
| 212 | |
| 213 | olt_service_mock.return_value = [self.volt_service] |
| 214 | mock_get.return_value = [existing_olt] |
| 215 | |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 216 | self.sync_step(model_accessor=self.model_accessor).pull_records() |
Matteo Scandolo | a4fb032 | 2018-08-20 17:35:29 -0700 | [diff] [blame] | 217 | |
| 218 | mock_olt_delete.assert_called() |
| 219 | |
| 220 | |
Matteo Scandolo | 3352341 | 2018-04-12 15:21:13 -0700 | [diff] [blame] | 221 | if __name__ == "__main__": |
| 222 | unittest.main() |