Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -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 |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 16 | from mock import patch, Mock |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 17 | import json |
| 18 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 19 | import os |
| 20 | import sys |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 21 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 22 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 24 | |
| 25 | class TestSyncOLTDevice(unittest.TestCase): |
| 26 | |
| 27 | def setUp(self): |
| 28 | |
| 29 | self.sys_path_save = sys.path |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 30 | |
| 31 | # Setting up the config module |
| 32 | from xosconfig import Config |
| 33 | config = os.path.join(test_path, "../test_config.yaml") |
| 34 | Config.clear() |
| 35 | Config.init(config, "synchronizer-config-schema.yaml") |
| 36 | # END Setting up the config module |
| 37 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 38 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 39 | mock_modelaccessor_config(test_path, [("att-workflow-driver", "att-workflow-driver.xproto"), |
| 40 | ("olt-service", "volt.xproto"), |
Matteo Scandolo | ad6c894 | 2019-02-14 13:02:08 -0800 | [diff] [blame] | 41 | ("rcord", "rcord.xproto")]) |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 42 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 43 | import xossynchronizer.modelaccessor |
| 44 | import mock_modelaccessor |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 45 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 46 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 47 | |
| 48 | from xossynchronizer.modelaccessor import model_accessor |
| 49 | from onu_event import ONUEventStep |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 50 | |
| 51 | # import all class names to globals |
| 52 | for (k, v) in model_accessor.all_model_classes.items(): |
| 53 | globals()[k] = v |
| 54 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 55 | self.model_accessor = model_accessor |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 56 | self.log = Mock() |
| 57 | |
Scott Baker | 71d2047 | 2019-02-01 12:05:35 -0800 | [diff] [blame] | 58 | self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log) |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 59 | |
| 60 | self.event = Mock() |
| 61 | self.event_dict = { |
| 62 | 'status': 'activated', |
Hardik Windlass | 7959e1f | 2019-03-30 20:38:02 +0530 | [diff] [blame] | 63 | 'serialNumber': 'BRCM1234', |
| 64 | 'deviceId': 'of:109299321', |
| 65 | 'portNumber': '16' |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 66 | } |
| 67 | self.event.value = json.dumps(self.event_dict) |
| 68 | |
Matteo Scandolo | 242c296 | 2018-08-28 16:50:47 -0700 | [diff] [blame] | 69 | self.att = AttWorkflowDriverService(name="att-workflow-driver") |
| 70 | |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 71 | def tearDown(self): |
| 72 | sys.path = self.sys_path_save |
| 73 | |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 74 | def test_create_instance(self): |
| 75 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 76 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \ |
| 77 | patch.object(AttWorkflowDriverService.objects, "get_items") as service_mock, \ |
| 78 | patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 79 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 80 | att_si_mock.return_value = [] |
Matteo Scandolo | 242c296 | 2018-08-28 16:50:47 -0700 | [diff] [blame] | 81 | service_mock.return_value = [self.att] |
Matteo Scandolo | ccef578 | 2018-08-13 13:25:17 -0700 | [diff] [blame] | 82 | |
| 83 | self.event_step.process_event(self.event) |
| 84 | |
| 85 | att_si = mock_save.call_args[0][0] |
| 86 | |
| 87 | self.assertEqual(mock_save.call_count, 1) |
| 88 | |
Hardik Windlass | 7959e1f | 2019-03-30 20:38:02 +0530 | [diff] [blame] | 89 | self.assertEqual(att_si.serial_number, self.event_dict['serialNumber']) |
| 90 | self.assertEqual(att_si.of_dpid, self.event_dict['deviceId']) |
| 91 | self.assertEqual(att_si.uni_port_id, long(self.event_dict['portNumber'])) |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 92 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 93 | self.assertEqual(att_si.admin_onu_state, "AWAITING") |
| 94 | self.assertEqual(att_si.oper_onu_status, "ENABLED") |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 95 | |
| 96 | def test_reuse_instance(self): |
| 97 | |
| 98 | si = AttWorkflowDriverServiceInstance( |
Hardik Windlass | 7959e1f | 2019-03-30 20:38:02 +0530 | [diff] [blame] | 99 | serial_number=self.event_dict["serialNumber"], |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 100 | of_dpid="foo", |
| 101 | uni_port_id="foo" |
| 102 | ) |
| 103 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 104 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \ |
| 105 | patch.object(AttWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 106 | |
| 107 | att_si_mock.return_value = [si] |
| 108 | |
| 109 | self.event_step.process_event(self.event) |
| 110 | |
| 111 | att_si = mock_save.call_args[0][0] |
| 112 | |
| 113 | self.assertEqual(mock_save.call_count, 1) |
| 114 | |
Hardik Windlass | 7959e1f | 2019-03-30 20:38:02 +0530 | [diff] [blame] | 115 | self.assertEqual(att_si.serial_number, self.event_dict['serialNumber']) |
| 116 | self.assertEqual(att_si.of_dpid, self.event_dict['deviceId']) |
| 117 | self.assertEqual(att_si.uni_port_id, long(self.event_dict['portNumber'])) |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 118 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 119 | self.assertEqual(att_si.admin_onu_state, "AWAITING") |
| 120 | self.assertEqual(att_si.oper_onu_status, "ENABLED") |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 121 | |
| 122 | def test_disable_onu(self): |
| 123 | self.event_dict = { |
| 124 | 'status': 'disabled', |
Hardik Windlass | 7959e1f | 2019-03-30 20:38:02 +0530 | [diff] [blame] | 125 | 'serialNumber': 'BRCM1234', |
| 126 | 'deviceId': 'of:109299321', |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 127 | 'portNumber': '16', |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 128 | } |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 129 | |
| 130 | si = AttWorkflowDriverServiceInstance( |
| 131 | serial_number=self.event_dict["serialNumber"], |
| 132 | of_dpid="foo", |
| 133 | uni_port_id="foo", |
| 134 | admin_onu_state="ENABLED", |
| 135 | oper_onu_status="ENABLED", |
| 136 | ) |
| 137 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 138 | self.event.value = json.dumps(self.event_dict) |
| 139 | |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 140 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \ |
| 141 | patch.object(AttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save: |
| 142 | att_si_mock.return_value = [si] |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 143 | |
| 144 | self.event_step.process_event(self.event) |
| 145 | |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 146 | att_si = mock_save.call_args[0][0] |
| 147 | |
| 148 | self.assertEqual(mock_save.call_count, 1) |
| 149 | |
| 150 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 151 | self.assertEqual(att_si.admin_onu_state, 'ENABLED') |
| 152 | self.assertEqual(att_si.oper_onu_status, 'DISABLED') |
| 153 | |
| 154 | def test_enable_onu(self): |
| 155 | self.event_dict = { |
| 156 | 'status': 'activated', |
| 157 | 'serialNumber': 'BRCM1234', |
| 158 | 'deviceId': 'of:109299321', |
| 159 | 'portNumber': '16', |
| 160 | } |
| 161 | |
| 162 | si = AttWorkflowDriverServiceInstance( |
| 163 | serial_number=self.event_dict["serialNumber"], |
| 164 | of_dpid="foo", |
| 165 | uni_port_id="foo", |
| 166 | admin_onu_state="DISABLED", |
| 167 | oper_onu_status="DISABLED", |
| 168 | ) |
| 169 | |
| 170 | self.event.value = json.dumps(self.event_dict) |
| 171 | |
| 172 | with patch.object(AttWorkflowDriverServiceInstance.objects, "get_items") as att_si_mock, \ |
| 173 | patch.object(AttWorkflowDriverServiceInstance, "save_changed_fields", autospec=True) as mock_save: |
| 174 | att_si_mock.return_value = [si] |
| 175 | |
| 176 | self.event_step.process_event(self.event) |
| 177 | |
| 178 | att_si = mock_save.call_args[0][0] |
| 179 | |
| 180 | self.assertEqual(mock_save.call_count, 1) |
| 181 | |
| 182 | # Receiving an ONU event doesn't change the admin_onu_state until the model policy runs |
| 183 | self.assertEqual(att_si.admin_onu_state, 'DISABLED') |
| 184 | self.assertEqual(att_si.oper_onu_status, 'ENABLED') |
| 185 | |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 186 | |
Matteo Scandolo | e8c33d6 | 2018-08-16 14:37:24 -0700 | [diff] [blame] | 187 | |
| 188 | if __name__ == '__main__': |
Matteo Scandolo | 2d9f40d | 2019-04-19 08:38:10 -0700 | [diff] [blame] | 189 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) # for import of helpers.py |
Scott Baker | c2a633d | 2019-04-01 19:27:41 -0700 | [diff] [blame] | 190 | unittest.main() |