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 |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 18 | from mock import patch |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 19 | |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 20 | import os |
| 21 | import sys |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 22 | |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 23 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 24 | |
| 25 | class TestModelPolicyTtWorkflowDriverWhiteListEntry(unittest.TestCase): |
| 26 | def setUp(self): |
| 27 | self.sys_path_save = sys.path |
| 28 | |
| 29 | config = os.path.join(test_path, "../test_config.yaml") |
| 30 | from xosconfig import Config |
| 31 | Config.clear() |
| 32 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 33 | |
| 34 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 35 | mock_modelaccessor_config(test_path, [("tt-workflow-driver", "tt-workflow-driver.xproto"), |
| 36 | ("olt-service", "volt.xproto"), |
| 37 | ("rcord", "rcord.xproto")]) |
| 38 | |
| 39 | import xossynchronizer.modelaccessor |
| 40 | import mock_modelaccessor |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 41 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 42 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 43 | |
| 44 | from xossynchronizer.modelaccessor import model_accessor |
| 45 | from model_policy_tt_workflow_driver_whitelistentry import TtWorkflowDriverWhiteListEntryPolicy, TtHelpers |
| 46 | self.TtHelpers = TtHelpers |
| 47 | |
| 48 | from mock_modelaccessor import MockObjectList |
| 49 | self.MockObjectList = MockObjectList |
| 50 | |
| 51 | # import all class names to globals |
| 52 | for (k, v) in model_accessor.all_model_classes.items(): |
| 53 | globals()[k] = v |
| 54 | |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 55 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to |
| 56 | # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 57 | model_accessor.reset_all_object_stores() |
| 58 | |
| 59 | self.policy = TtWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor) |
| 60 | |
| 61 | self.service = TtWorkflowDriverService() |
| 62 | |
| 63 | |
| 64 | def tearDown(self): |
| 65 | sys.path = self.sys_path_save |
| 66 | self.service = None |
| 67 | |
| 68 | def test_enable_onu(self): |
| 69 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid") |
| 70 | with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \ |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 71 | patch.object(si, "save") as save_si: |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 72 | validate_onu.return_value = [True, "valid onu"] |
| 73 | |
| 74 | self.policy.validate_onu_state(si) |
| 75 | |
| 76 | save_si.assert_called_once() |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 77 | save_si.assert_called_with( |
| 78 | always_update_timestamp=True, update_fields=[ |
| 79 | 'admin_onu_state', 'serial_number', 'updated']) |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 80 | |
| 81 | def test_disable_onu(self): |
| 82 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id, valid="invalid") |
| 83 | with patch.object(self.TtHelpers, "validate_onu") as validate_onu, \ |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 84 | patch.object(si, "save") as save_si: |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 85 | validate_onu.return_value = [False, "invalid onu"] |
| 86 | |
| 87 | self.policy.validate_onu_state(si) |
| 88 | |
| 89 | save_si.assert_called_once() |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 90 | save_si.assert_called_with( |
| 91 | always_update_timestamp=True, update_fields=[ |
| 92 | 'admin_onu_state', 'serial_number', 'updated']) |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 93 | |
| 94 | def test_whitelist_update(self): |
| 95 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id) |
| 96 | wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service) |
| 97 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \ |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 98 | patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 99 | patch.object(wle, "save") as wle_save: |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 100 | oss_si_items.return_value = [si] |
| 101 | |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 102 | self.policy.handle_update(wle) |
| 103 | |
| 104 | validate_onu_state.assert_called_with(si) |
| 105 | self.assertTrue(wle.backend_need_delete_policy) |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 106 | wle_save.assert_called_with( |
| 107 | always_update_timestamp=False, update_fields=[ |
| 108 | 'backend_need_delete_policy', 'owner', 'serial_number']) |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 109 | |
| 110 | def test_whitelist_delete(self): |
| 111 | si = TtWorkflowDriverServiceInstance(serial_number="BRCM333", owner_id=self.service.id) |
| 112 | wle = TtWorkflowDriverWhiteListEntry(serial_number="brcm333", owner_id=self.service.id, owner=self.service) |
| 113 | with patch.object(TtWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \ |
| 114 | patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 115 | patch.object(wle, "save") as wle_save: |
| 116 | oss_si_items.return_value = [si] |
| 117 | |
| 118 | self.policy.handle_delete(wle) |
| 119 | |
| 120 | validate_onu_state.assert_called_with(si) |
| 121 | self.assertTrue(wle.backend_need_reap) |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 122 | wle_save.assert_called_with( |
| 123 | always_update_timestamp=False, update_fields=[ |
| 124 | 'backend_need_reap', 'owner', 'serial_number']) |
Andy Bavier | 561f3e5 | 2019-03-15 10:46:05 -0700 | [diff] [blame] | 125 | |
Andy Bavier | 0ce0dae | 2019-05-10 17:46:02 -0700 | [diff] [blame] | 126 | |
| 127 | if __name__ == '__main__': |
| 128 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 129 | unittest.main() |