Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
| 17 | import unittest |
| 18 | from mock import patch, call, Mock, PropertyMock |
| 19 | |
| 20 | import os, sys |
| 21 | |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | |
| 24 | class TestModelPolicyTtWorkflowDriverWhiteListEntry(unittest.TestCase): |
| 25 | def setUp(self): |
| 26 | self.sys_path_save = sys.path |
| 27 | |
| 28 | config = os.path.join(test_path, "../test_config.yaml") |
| 29 | from xosconfig import Config |
| 30 | Config.clear() |
| 31 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 32 | |
| 33 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 34 | mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"), |
| 35 | ("olt-service", "volt.xproto"), |
| 36 | ("rcord", "rcord.xproto")]) |
| 37 | |
| 38 | import xossynchronizer.modelaccessor |
| 39 | import mock_modelaccessor |
| 40 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 41 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 42 | |
| 43 | from xossynchronizer.modelaccessor import model_accessor |
| 44 | from model_policy_tt_workflow_driver_whitelistentry import TtWorkflowDriverWhiteListEntryPolicy, TtHelpers |
| 45 | self.TtHelpers = TtHelpers |
| 46 | |
| 47 | from mock_modelaccessor import MockObjectList |
| 48 | self.MockObjectList = MockObjectList |
| 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 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 55 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 56 | model_accessor.reset_all_object_stores() |
| 57 | |
| 58 | self.policy = TtWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor) |
| 59 | |
| 60 | self.service = TtWorkflowDriverService() |
| 61 | |
| 62 | |
| 63 | def tearDown(self): |
| 64 | sys.path = self.sys_path_save |
| 65 | self.service = None |
| 66 | |
| 67 | def test_enable_onu(self): |
| 68 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid") |
| 69 | with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \ |
| 70 | patch.object(si, "save") as save_si: |
| 71 | validate_onu.return_value = [True, "valid onu"] |
| 72 | |
| 73 | self.policy.validate_onu_state(si) |
| 74 | |
| 75 | save_si.assert_called_once() |
| 76 | save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated']) |
| 77 | |
| 78 | def test_disable_onu(self): |
| 79 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid") |
| 80 | with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \ |
| 81 | patch.object(si, "save") as save_si: |
| 82 | validate_onu.return_value = [False, "invalid onu"] |
| 83 | |
| 84 | self.policy.validate_onu_state(si) |
| 85 | |
| 86 | save_si.assert_called_once() |
| 87 | save_si.assert_called_with(always_update_timestamp=True, update_fields=['onu_state', 'serial_number', 'updated']) |
| 88 | |
| 89 | def test_whitelist_update(self): |
| 90 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id) |
| 91 | wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service) |
| 92 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \ |
| 93 | patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 94 | patch.object(wle, "save") as wle_save: |
| 95 | oss_si_items.return_value = [si] |
| 96 | |
| 97 | |
| 98 | self.policy.handle_update(wle) |
| 99 | |
| 100 | validate_onu_state.assert_called_with(si) |
| 101 | self.assertTrue(wle.backend_need_delete_policy) |
| 102 | wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_delete_policy', 'owner', 'serial_number']) |
| 103 | |
| 104 | def test_whitelist_delete(self): |
| 105 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id) |
| 106 | wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service) |
| 107 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \ |
| 108 | patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 109 | patch.object(wle, "save") as wle_save: |
| 110 | oss_si_items.return_value = [si] |
| 111 | |
| 112 | self.policy.handle_delete(wle) |
| 113 | |
| 114 | validate_onu_state.assert_called_with(si) |
| 115 | self.assertTrue(wle.backend_need_reap) |
| 116 | wle_save.assert_called_with(always_update_timestamp=False, update_fields=['backend_need_reap', 'owner', 'serial_number']) |
| 117 | if __name__ == '__main__': |
| 118 | unittest.main() |
| 119 | |