Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -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 json |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 22 | |
| 23 | class TestSyncOLTDevice(unittest.TestCase): |
| 24 | |
| 25 | def setUp(self): |
| 26 | |
| 27 | self.sys_path_save = sys.path |
| 28 | |
| 29 | # Setting up the config module |
| 30 | from xosconfig import Config |
| 31 | config = os.path.join(test_path, "../test_config.yaml") |
| 32 | Config.clear() |
| 33 | Config.init(config, "synchronizer-config-schema.yaml") |
| 34 | # END Setting up the config module |
| 35 | |
| 36 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 37 | mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"), |
| 38 | ("olt-service", "volt.xproto"), |
| 39 | ("rcord", "rcord.xproto")]) |
| 40 | |
| 41 | import xossynchronizer.modelaccessor |
| 42 | import mock_modelaccessor |
| 43 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 44 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 45 | |
| 46 | from xossynchronizer.modelaccessor import model_accessor |
| 47 | from onu_event import ONUEventStep |
| 48 | |
| 49 | # import all class names to globals |
| 50 | for (k, v) in model_accessor.all_model_classes.items(): |
| 51 | globals()[k] = v |
| 52 | |
| 53 | self.model_accessor = model_accessor |
| 54 | self.log = Mock() |
| 55 | |
| 56 | self.event_step = ONUEventStep(model_accessor=self.model_accessor, log=self.log) |
| 57 | |
| 58 | self.event = Mock() |
| 59 | self.event_dict = { |
| 60 | 'status': 'activated', |
| 61 | 'serial_number': 'BRCM1234', |
| 62 | 'of_dpid': 'of:109299321', |
| 63 | 'uni_port_id': 16 |
| 64 | } |
| 65 | self.event.value = json.dumps(self.event_dict) |
| 66 | |
| 67 | self.tt = TtWorkflowDriverService(name="tt-workflow-driver") |
| 68 | |
| 69 | def tearDown(self): |
| 70 | sys.path = self.sys_path_save |
| 71 | |
| 72 | |
| 73 | def test_create_instance(self): |
| 74 | |
| 75 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \ |
| 76 | patch.object(TtWorkflowDriverService.objects, "get_items") as service_mock, \ |
| 77 | patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
| 78 | |
| 79 | tt_si_mock.return_value = [] |
| 80 | service_mock.return_value = [self.tt] |
| 81 | |
| 82 | self.event_step.process_event(self.event) |
| 83 | |
| 84 | tt_si = mock_save.call_args[0][0] |
| 85 | |
| 86 | self.assertEqual(mock_save.call_count, 1) |
| 87 | |
| 88 | self.assertEqual(tt_si.serial_number, self.event_dict['serial_number']) |
| 89 | self.assertEqual(tt_si.of_dpid, self.event_dict['of_dpid']) |
| 90 | self.assertEqual(tt_si.uni_port_id, self.event_dict['uni_port_id']) |
| 91 | self.assertEqual(tt_si.onu_state, "ENABLED") |
| 92 | |
| 93 | def test_reuse_instance(self): |
| 94 | |
| 95 | si = TtWorkflowDriverServiceInstance( |
| 96 | serial_number=self.event_dict["serial_number"], |
| 97 | of_dpid="foo", |
| 98 | uni_port_id="foo" |
| 99 | ) |
| 100 | |
| 101 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as tt_si_mock , \ |
| 102 | patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
| 103 | |
| 104 | tt_si_mock.return_value = [si] |
| 105 | |
| 106 | self.event_step.process_event(self.event) |
| 107 | |
| 108 | tt_si = mock_save.call_args[0][0] |
| 109 | |
| 110 | self.assertEqual(mock_save.call_count, 1) |
| 111 | |
| 112 | self.assertEqual(tt_si.serial_number, self.event_dict['serial_number']) |
| 113 | self.assertEqual(tt_si.of_dpid, self.event_dict['of_dpid']) |
| 114 | self.assertEqual(tt_si.uni_port_id, self.event_dict['uni_port_id']) |
| 115 | self.assertEqual(tt_si.onu_state, "ENABLED") |
| 116 | |
| 117 | def test_disable_onu(self): |
| 118 | self.event_dict = { |
| 119 | 'status': 'disabled', |
| 120 | 'serial_number': 'BRCM1234', |
| 121 | 'of_dpid': 'of:109299321', |
| 122 | 'uni_port_id': 16 |
| 123 | } |
| 124 | self.event.value = json.dumps(self.event_dict) |
| 125 | |
| 126 | with patch.object(TtWorkflowDriverServiceInstance, "save", autospec=True) as mock_save: |
| 127 | |
| 128 | self.event_step.process_event(self.event) |
| 129 | |
| 130 | self.assertEqual(mock_save.call_count, 0) |
| 131 | |
| 132 | |
| 133 | if __name__ == '__main__': |
| 134 | sys.path.append("..") # for import of helpers.py |
| 135 | unittest.main() |