Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame^] | 1 | |
| 2 | # Copyright 2020-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 |
| 19 | |
| 20 | import os |
| 21 | import sys |
| 22 | |
| 23 | test_path = os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | |
| 25 | |
| 26 | class TestModelPolicyNttWorkflowDriverWhiteListEntry(unittest.TestCase): |
| 27 | def setUp(self): |
| 28 | self.sys_path_save = sys.path |
| 29 | |
| 30 | config = os.path.join(test_path, "../test_config.yaml") |
| 31 | from xosconfig import Config |
| 32 | Config.clear() |
| 33 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 34 | |
| 35 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 36 | mock_modelaccessor_config(test_path, [("ntt-workflow-driver", "ntt-workflow-driver.xproto"), |
| 37 | ("olt-service", "volt.xproto"), |
| 38 | ("rcord", "rcord.xproto")]) |
| 39 | |
| 40 | import xossynchronizer.modelaccessor |
| 41 | import mock_modelaccessor |
| 42 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 43 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 44 | |
| 45 | from xossynchronizer.modelaccessor import model_accessor |
| 46 | from model_policy_ntt_workflow_driver_whitelistentry import NttWorkflowDriverWhiteListEntryPolicy, NttHelpers |
| 47 | self.NttHelpers = NttHelpers |
| 48 | |
| 49 | from mock_modelaccessor import MockObjectList |
| 50 | self.MockObjectList = MockObjectList |
| 51 | |
| 52 | # import all class names to globals |
| 53 | for (k, v) in model_accessor.all_model_classes.items(): |
| 54 | globals()[k] = v |
| 55 | |
| 56 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to |
| 57 | # creation of tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 58 | model_accessor.reset_all_object_stores() |
| 59 | |
| 60 | self.policy = NttWorkflowDriverWhiteListEntryPolicy(model_accessor=model_accessor) |
| 61 | |
| 62 | self.service = NttWorkflowDriverService() |
| 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 = NttWorkflowDriverServiceInstance(mac_address="0a0a0a", owner_id=self.service.id, valid="invalid") |
| 70 | with patch.object(self.NttHelpers, "validate_onu") as validate_onu, \ |
| 71 | patch.object(si, "save") as save_si: |
| 72 | validate_onu.return_value = [True, "valid onu"] |
| 73 | |
| 74 | self.policy.validate_onu_state(si) |
| 75 | |
| 76 | save_si.assert_called_once() |
| 77 | save_si.assert_called_with( |
| 78 | always_update_timestamp=True, update_fields=[ |
| 79 | 'admin_onu_state', 'mac_address', 'status_message', 'updated']) |
| 80 | |
| 81 | def test_disable_onu(self): |
| 82 | si = NttWorkflowDriverServiceInstance(mac_address="0a0a0a", owner_id=self.service.id, valid="invalid") |
| 83 | with patch.object(self.NttHelpers, "validate_onu") as validate_onu, \ |
| 84 | patch.object(si, "save") as save_si: |
| 85 | validate_onu.return_value = [False, "invalid onu"] |
| 86 | |
| 87 | self.policy.validate_onu_state(si) |
| 88 | |
| 89 | save_si.assert_called_once() |
| 90 | save_si.assert_called_with( |
| 91 | always_update_timestamp=True, update_fields=[ |
| 92 | 'admin_onu_state', 'mac_address', 'status_message', 'updated']) |
| 93 | |
| 94 | def test_whitelist_update(self): |
| 95 | si = NttWorkflowDriverServiceInstance(mac_address="0a0a0a", owner_id=self.service.id) |
| 96 | wle = NttWorkflowDriverWhiteListEntry(mac_address="0a0a0a", owner_id=self.service.id, owner=self.service) |
| 97 | with patch.object(NttWorkflowDriverServiceInstance.objects, "get_items") as oss_si_items, \ |
| 98 | patch.object(self.policy, "validate_onu_state") as validate_onu_state, \ |
| 99 | patch.object(wle, "save") as wle_save: |
| 100 | oss_si_items.return_value = [si] |
| 101 | |
| 102 | self.policy.handle_update(wle) |
| 103 | |
| 104 | validate_onu_state.assert_called_with(si) |
| 105 | self.assertTrue(wle.backend_need_delete_policy) |
| 106 | wle_save.assert_called_with( |
| 107 | always_update_timestamp=False, update_fields=[ |
| 108 | 'backend_need_delete_policy', 'mac_address', 'owner']) |
| 109 | |
| 110 | def test_whitelist_delete(self): |
| 111 | si = NttWorkflowDriverServiceInstance(mac_address="0a0a0a", owner_id=self.service.id) |
| 112 | wle = NttWorkflowDriverWhiteListEntry(mac_address="0a0a0a", owner_id=self.service.id, owner=self.service) |
| 113 | with patch.object(NttWorkflowDriverServiceInstance.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) |
| 122 | wle_save.assert_called_with( |
| 123 | always_update_timestamp=False, update_fields=[ |
| 124 | 'backend_need_reap', 'mac_address', 'owner']) |
| 125 | |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")) |
| 129 | unittest.main() |