Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [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 | import mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | service_dir=os.path.join(test_path, "../../../..") |
| 25 | xos_dir=os.path.join(test_path, "../../..") |
| 26 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 27 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 28 | services_dir=os.path.join(xos_dir, "../../xos_services") |
| 29 | |
| 30 | class TestModelPolicyVOLTTenant(unittest.TestCase): |
| 31 | def setUp(self): |
| 32 | global VOLTTenantPolicy, MockObjectList |
| 33 | |
| 34 | self.sys_path_save = sys.path |
| 35 | sys.path.append(xos_dir) |
| 36 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 37 | |
| 38 | config = os.path.join(test_path, "test_config.yaml") |
| 39 | from xosconfig import Config |
| 40 | Config.clear() |
| 41 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 42 | |
| 43 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 44 | build_mock_modelaccessor(xos_dir, services_dir, ["olt-service/xos/volt.xproto", "vsg/xos/vsg.xproto", "../profiles/rcord/xos/rcord.xproto"]) |
| 45 | |
| 46 | import synchronizers.new_base.modelaccessor |
| 47 | import model_policy_volttenant |
| 48 | from model_policy_volttenant import VOLTTenantPolicy, model_accessor |
| 49 | |
| 50 | from mock_modelaccessor import 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 creation of |
| 57 | # 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 = VOLTTenantPolicy() |
| 61 | self.tenant = VOLTTenant(s_tag=111, c_tag=222, service_specific_id=1234) |
| 62 | |
| 63 | self.vsg_service = VSGService(name="the vsg service") |
| 64 | |
| 65 | def tearDown(self): |
| 66 | sys.path = self.sys_path_save |
| 67 | |
| 68 | def test_handle_create(self): |
| 69 | with patch.object(VOLTTenantPolicy, "manage_vsg") as manage_vsg, \ |
| 70 | patch.object(VOLTTenantPolicy, "manage_subscriber") as manage_subscriber, \ |
| 71 | patch.object(VOLTTenantPolicy, "cleanup_orphans") as cleanup_orphans: |
| 72 | self.policy.handle_create(self.tenant) |
| 73 | manage_subscriber.assert_called_with(self.tenant) |
| 74 | manage_vsg.assert_called_with(self.tenant) |
| 75 | cleanup_orphans.assert_called_with(self.tenant) |
| 76 | |
| 77 | def test_manage_vsg(self): |
| 78 | with patch.object(VOLTTenantPolicy, "get_current_vsg") as get_current_vsg, \ |
| 79 | patch.object(VOLTTenantPolicy, "create_vsg") as create_vsg, \ |
| 80 | patch.object(VSGService.objects, "get_items") as vsg_items: |
| 81 | |
| 82 | vsg_items.return_value = [self.vsg_service] |
| 83 | get_current_vsg.return_value = None |
| 84 | self.policy.manage_vsg(self.tenant) |
| 85 | |
| 86 | create_vsg.assert_called() |
| 87 | |
| 88 | def test_get_current_vsg(self): |
| 89 | with patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 90 | vsg = VSGServiceInstance() |
| 91 | link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id) |
| 92 | |
| 93 | link_items.return_value = [link] |
| 94 | |
| 95 | vsg = self.policy.get_current_vsg(self.tenant) |
| 96 | |
| 97 | self.assertNotEqual(vsg, None) |
| 98 | |
| 99 | def test_get_current_vsg_noexist(self): |
| 100 | vsg = self.policy.get_current_vsg(self.tenant) |
| 101 | |
| 102 | self.assertEqual(vsg, None) |
| 103 | |
| 104 | def test_create_vsg(self): |
| 105 | with patch.object(VSGService.objects, "get_items") as vsg_items, \ |
| 106 | patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 107 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
| 108 | |
| 109 | vsg_items.return_value = [self.vsg_service] |
| 110 | self.policy.create_vsg(self.tenant) |
| 111 | |
| 112 | # Should have created a vsg |
| 113 | |
| 114 | self.assertEqual(save_vsg.call_count, 1) |
| 115 | vsg = save_vsg.call_args[0][0] |
| 116 | self.assertEqual(vsg.creator, self.tenant.creator) |
| 117 | |
| 118 | # Should have created a link from OLT to vsg |
| 119 | |
| 120 | self.assertEqual(save_link.call_count, 1) |
| 121 | link = save_link.call_args[0][0] |
| 122 | self.assertEqual(link.provider_service_instance, vsg) |
| 123 | self.assertEqual(link.subscriber_service_instance, self.tenant) |
| 124 | |
| 125 | def test_manage_subscriber(self): |
| 126 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 127 | patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr: |
| 128 | |
| 129 | self.tenant.provided_links = MockObjectList() |
| 130 | |
| 131 | self.policy.manage_subscriber(self.tenant) |
| 132 | |
| 133 | self.assertEqual(save_csr.call_count, 1) |
| 134 | csr = save_csr.call_args[0][0] |
| 135 | |
| 136 | self.assertEqual(save_link.call_count, 1) |
| 137 | link = save_link.call_args[0][0] |
| 138 | self.assertEqual(link.provider_service_instance, self.tenant) |
| 139 | self.assertEqual(link.subscriber_service_instance, csr) |
| 140 | |
| 141 | def test_manage_subscriber_exists(self): |
| 142 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 143 | patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr, \ |
| 144 | patch.object(CordSubscriberRoot.objects, "get_items") as csr_items, \ |
| 145 | patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 146 | self.tenant.provided_links = MockObjectList() |
| 147 | |
| 148 | subscriber = CordSubscriberRoot(service_specific_id=1234) |
| 149 | csr_items.return_value = [subscriber] |
| 150 | |
| 151 | link = ServiceInstanceLink(provider_service_instance= self.tenant, subscriber_service_instance = subscriber) |
| 152 | link_items.return_value = [link] |
| 153 | |
| 154 | self.tenant.provided_links = MockObjectList(initial=[link]) |
| 155 | |
| 156 | self.policy.manage_subscriber(self.tenant) |
| 157 | |
| 158 | self.assertEqual(save_csr.call_count, 0) |
| 159 | self.assertEqual(save_link.call_count, 0) |
| 160 | |
| 161 | def test_manage_subscriber_exists_nolink(self): |
| 162 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
| 163 | patch.object(CordSubscriberRoot, "save", autospec=True) as save_csr, \ |
| 164 | patch.object(CordSubscriberRoot.objects, "get_items") as csr_items, \ |
| 165 | patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 166 | self.tenant.provided_links = MockObjectList() |
| 167 | |
| 168 | subscriber = CordSubscriberRoot(service_specific_id=1234) |
| 169 | csr_items.return_value = [subscriber] |
| 170 | |
| 171 | self.tenant.provided_links = MockObjectList() |
| 172 | |
| 173 | self.policy.manage_subscriber(self.tenant) |
| 174 | |
| 175 | self.assertEqual(save_csr.call_count, 0) |
| 176 | |
| 177 | self.assertEqual(save_link.call_count, 1) |
| 178 | link = save_link.call_args[0][0] |
| 179 | self.assertEqual(link.provider_service_instance, self.tenant) |
| 180 | self.assertEqual(link.subscriber_service_instance, subscriber) |
| 181 | |
| 182 | def test_handle_delete(self): |
| 183 | self.policy.handle_delete(self.tenant) |
| 184 | # handle delete does nothing, and should trivially succeed |
| 185 | |
| 186 | def test_cleanup_orphans(self): |
| 187 | with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \ |
| 188 | patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \ |
| 189 | patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 190 | |
| 191 | vsg1 = VSGServiceInstance(id=123) |
| 192 | vsg2 = VSGServiceInstance(id=456) |
| 193 | link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id, |
| 194 | subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id) |
| 195 | link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id, |
| 196 | subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id) |
| 197 | |
| 198 | self.tenant.subscribed_links=MockObjectList(initial=[link1,link2]) |
| 199 | |
| 200 | vsg_si_items.return_value = [vsg1, vsg2] |
| 201 | link_items.return_value = [link1, link2] |
| 202 | |
| 203 | self.policy.cleanup_orphans(self.tenant) |
| 204 | |
| 205 | # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be |
| 206 | # deleted. |
| 207 | |
| 208 | self.assertEqual(delete_link.call_count, 1) |
| 209 | |
| 210 | if __name__ == '__main__': |
| 211 | unittest.main() |
| 212 | |