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 | |
Scott Baker | 27e4fcb | 2018-01-09 10:24:52 -0800 | [diff] [blame] | 30 | # While transitioning from static to dynamic load, the path to find neighboring xproto files has changed. So check |
| 31 | # both possible locations... |
| 32 | def get_models_fn(service_name, xproto_name): |
| 33 | name = os.path.join(service_name, "xos", xproto_name) |
| 34 | if os.path.exists(os.path.join(services_dir, name)): |
| 35 | return name |
| 36 | else: |
| 37 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 41 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 42 | class TestModelPolicyVOLTServiceInstance(unittest.TestCase): |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 43 | def setUp(self): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 44 | global VOLTServiceInstancePolicy, MockObjectList |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 45 | |
| 46 | self.sys_path_save = sys.path |
| 47 | sys.path.append(xos_dir) |
| 48 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 49 | |
| 50 | config = os.path.join(test_path, "test_config.yaml") |
| 51 | from xosconfig import Config |
| 52 | Config.clear() |
| 53 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 54 | |
| 55 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Scott Baker | 27e4fcb | 2018-01-09 10:24:52 -0800 | [diff] [blame] | 56 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 57 | get_models_fn("vsg", "vsg.xproto"), |
| 58 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 59 | |
| 60 | import synchronizers.new_base.modelaccessor |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 61 | import model_policy_voltserviceinstance |
| 62 | from model_policy_voltserviceinstance import VOLTServiceInstancePolicy, model_accessor |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 63 | |
| 64 | from mock_modelaccessor import MockObjectList |
| 65 | |
| 66 | # import all class names to globals |
| 67 | for (k, v) in model_accessor.all_model_classes.items(): |
| 68 | globals()[k] = v |
| 69 | |
| 70 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 71 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 72 | model_accessor.reset_all_object_stores() |
| 73 | |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 74 | self.policy = VOLTServiceInstancePolicy() |
| 75 | self.tenant = VOLTServiceInstance(s_tag=111, c_tag=222, service_specific_id=1234) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 76 | |
| 77 | self.vsg_service = VSGService(name="the vsg service") |
| 78 | |
| 79 | def tearDown(self): |
| 80 | sys.path = self.sys_path_save |
| 81 | |
| 82 | def test_handle_create(self): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 83 | with patch.object(VOLTServiceInstancePolicy, "manage_vsg") as manage_vsg, \ |
| 84 | patch.object(VOLTServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans: |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 85 | self.policy.handle_create(self.tenant) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 86 | manage_vsg.assert_called_with(self.tenant) |
| 87 | cleanup_orphans.assert_called_with(self.tenant) |
| 88 | |
| 89 | def test_manage_vsg(self): |
Matteo Scandolo | d245801 | 2018-03-01 13:40:36 -0800 | [diff] [blame] | 90 | with patch.object(VOLTServiceInstancePolicy, "get_current_vsg") as get_current_vsg, \ |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 91 | patch.object(VOLTServiceInstancePolicy, "create_eastbound_instance") as create_vsg, \ |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 92 | patch.object(VSGService.objects, "get_items") as vsg_items: |
| 93 | |
| 94 | vsg_items.return_value = [self.vsg_service] |
| 95 | get_current_vsg.return_value = None |
| 96 | self.policy.manage_vsg(self.tenant) |
| 97 | |
| 98 | create_vsg.assert_called() |
| 99 | |
| 100 | def test_get_current_vsg(self): |
| 101 | with patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 102 | vsg = VSGServiceInstance() |
| 103 | link = ServiceInstanceLink(provider_service_instance=vsg, subscriber_service_instance_id=self.tenant.id) |
| 104 | |
| 105 | link_items.return_value = [link] |
| 106 | |
| 107 | vsg = self.policy.get_current_vsg(self.tenant) |
| 108 | |
| 109 | self.assertNotEqual(vsg, None) |
| 110 | |
| 111 | def test_get_current_vsg_noexist(self): |
| 112 | vsg = self.policy.get_current_vsg(self.tenant) |
| 113 | |
| 114 | self.assertEqual(vsg, None) |
| 115 | |
| 116 | def test_create_vsg(self): |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 117 | # with patch.object(model_accessor, "get_model_class") as mock_model_accessor, \ |
| 118 | with patch.object(ServiceInstanceLink, "save", autospec=True) as save_link, \ |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 119 | patch.object(VSGServiceInstance, "save", autospec=True) as save_vsg: |
| 120 | |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 121 | # mock_model_accessor.return_value = VSGServiceInstance |
| 122 | |
| 123 | link = Mock() |
| 124 | link.provider_service.get_service_instance_class_name.return_value = "VSGServiceInstance" |
| 125 | |
| 126 | si = Mock() |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 127 | si.subscribed_links.all.return_value = [] |
| 128 | si.owner.subscribed_dependencies.all.return_value = [link] |
| 129 | |
| 130 | self.policy.create_eastbound_instance(si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 131 | |
| 132 | # Should have created a vsg |
| 133 | |
| 134 | self.assertEqual(save_vsg.call_count, 1) |
| 135 | vsg = save_vsg.call_args[0][0] |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 136 | |
| 137 | # Should have created a link from OLT to vsg |
| 138 | |
| 139 | self.assertEqual(save_link.call_count, 1) |
| 140 | link = save_link.call_args[0][0] |
| 141 | self.assertEqual(link.provider_service_instance, vsg) |
Matteo Scandolo | 89f1648 | 2018-03-12 16:12:53 -0700 | [diff] [blame] | 142 | self.assertEqual(link.subscriber_service_instance, si) |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 143 | |
Scott Baker | b404b49 | 2017-12-01 13:01:10 -0800 | [diff] [blame] | 144 | def test_handle_delete(self): |
| 145 | self.policy.handle_delete(self.tenant) |
| 146 | # handle delete does nothing, and should trivially succeed |
| 147 | |
| 148 | def test_cleanup_orphans(self): |
| 149 | with patch.object(ServiceInstanceLink, "delete", autospec=True) as delete_link, \ |
| 150 | patch.object(VSGServiceInstance.objects, "get_items") as vsg_si_items, \ |
| 151 | patch.object(ServiceInstanceLink.objects, "get_items") as link_items: |
| 152 | |
| 153 | vsg1 = VSGServiceInstance(id=123) |
| 154 | vsg2 = VSGServiceInstance(id=456) |
| 155 | link1 = ServiceInstanceLink(provider_service_instance=vsg1, provider_service_instance_id=vsg1.id, |
| 156 | subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id) |
| 157 | link2 = ServiceInstanceLink(provider_service_instance=vsg2, provider_service_instance_id=vsg2.id, |
| 158 | subscriber_service_instance=self.tenant, subscriber_service_instance_id=self.tenant.id) |
| 159 | |
| 160 | self.tenant.subscribed_links=MockObjectList(initial=[link1,link2]) |
| 161 | |
| 162 | vsg_si_items.return_value = [vsg1, vsg2] |
| 163 | link_items.return_value = [link1, link2] |
| 164 | |
| 165 | self.policy.cleanup_orphans(self.tenant) |
| 166 | |
| 167 | # Since there are two VSGs linked to this VOLT, cleanup_orphans() will have caused one of them to be |
| 168 | # deleted. |
| 169 | |
| 170 | self.assertEqual(delete_link.call_count, 1) |
| 171 | |
| 172 | if __name__ == '__main__': |
| 173 | unittest.main() |
| 174 | |