Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -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 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 17 | import unittest |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 18 | from mock import patch, call, Mock, PropertyMock |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 19 | import mock |
| 20 | |
| 21 | import os, sys |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 22 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 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") |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 29 | |
Scott Baker | e96314e | 2018-01-07 08:28:46 -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)) |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 41 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 42 | class TestModelPolicyVsgTenant(unittest.TestCase): |
| 43 | def setUp(self): |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 44 | global VSGServiceInstancePolicy, LeastLoadedNodeScheduler, MockObjectList |
| 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 | e96314e | 2018-01-07 08:28:46 -0800 | [diff] [blame^] | 56 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("vsg", "vsg.xproto"), |
| 57 | get_models_fn("addressmanager", "addressmanager.xproto")]) |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 58 | |
| 59 | import synchronizers.new_base.modelaccessor |
| 60 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 61 | import model_policy_vsgserviceinstance |
| 62 | from model_policy_vsgserviceinstance import VSGServiceInstancePolicy, model_accessor |
| 63 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler |
| 64 | |
| 65 | from mock_modelaccessor import MockObjectList |
| 66 | |
| 67 | # import all class names to globals |
| 68 | for (k, v) in model_accessor.all_model_classes.items(): |
| 69 | globals()[k] = v |
| 70 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 71 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 72 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 73 | model_accessor.reset_all_object_stores() |
| 74 | |
| 75 | # attic functions that are not present in the mock model accessor |
| 76 | VSGServiceInstance.volt = PropertyMock(return_value = None) |
| 77 | AddressManagerServiceInstance.set_attribute = Mock() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 78 | |
Scott Baker | 645c0c5 | 2017-09-15 10:38:32 -0700 | [diff] [blame] | 79 | self.policy = VSGServiceInstancePolicy() |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 80 | self.tenant = VSGServiceInstance() |
| 81 | self.user = User(email="testadmin@test.org") |
| 82 | self.tenant = VSGServiceInstance(creator=self.user, id=1) |
| 83 | self.flavor = Flavor(name="m1.small") |
| 84 | self.npt_ctag = NetworkParameterType(name="c_tag", id=1) |
| 85 | self.npt_stag = NetworkParameterType(name="s_tag", id=2) |
| 86 | self.npt_neutron_port_name = NetworkParameterType(name="neutron_port_name", id=3) |
| 87 | self.node = Node(hostname="my.node.com") |
| 88 | self.slice = Slice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm") |
| 89 | self.priv_template = NetworkTemplate(name="access_network", visibility="private") |
| 90 | self.priv_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 91 | self.image = Image(name="trusty-server-multi-nic") |
| 92 | self.deployment = Deployment(name="testdeployment") |
| 93 | Tag.objects.item_list = [] |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 94 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 95 | def tearDownb(self): |
| 96 | sys.path = self.sys_path_save |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 97 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 98 | def test_handle_create(self): |
| 99 | with patch.object(VSGServiceInstancePolicy, "manage_container") as manage_container, \ |
| 100 | patch.object(VSGServiceInstancePolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 101 | patch.object(VSGServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans: |
| 102 | self.policy.handle_create(self.tenant) |
| 103 | manage_container.assert_called_with(self.tenant) |
| 104 | manage_address_service_instance.assert_called_with(self.tenant) |
| 105 | cleanup_orphans.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 106 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 107 | def test_handle_update(self): |
| 108 | with patch.object(VSGServiceInstancePolicy, "manage_container") as manage_container, \ |
| 109 | patch.object(VSGServiceInstancePolicy, "manage_address_service_instance") as manage_address_service_instance, \ |
| 110 | patch.object(VSGServiceInstancePolicy, "cleanup_orphans") as cleanup_orphans: |
| 111 | self.policy.handle_create(self.tenant) |
| 112 | manage_container.assert_called_with(self.tenant) |
| 113 | manage_address_service_instance.assert_called_with(self.tenant) |
| 114 | cleanup_orphans.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 115 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 116 | def test_handle_delete_asi_exist(self): |
| 117 | with patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 118 | vrtenant = AddressManagerServiceInstance() |
| 119 | self.tenant.address_service_instance = vrtenant |
| 120 | self.policy.handle_delete(self.tenant) |
| 121 | # The delete model_policy no longer deletes the AddressManagerServiceInstance. It's now handled by logic in |
| 122 | # ServiceInstanceLink, together with model_policies in the target object. |
| 123 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 124 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 125 | def test_handle_delete_asi_noexist(self): |
| 126 | with patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 127 | self.tenant.address_service_instance = None |
| 128 | self.policy.handle_delete(self.tenant) |
| 129 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 130 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 131 | def test_handle_delete_cleanup_instance(self): |
| 132 | with patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 133 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 134 | patch.object(Instance, "delete") as instance_delete: |
| 135 | vsg_service = VSGService() |
| 136 | instance = Instance(id=1) |
| 137 | instance_objects.return_value = [instance] |
| 138 | self.tenant.address_service_instance = None |
| 139 | self.tenant.instance = instance |
| 140 | self.tenant.instance_id = instance.id |
| 141 | self.tenant.owner = vsg_service |
| 142 | vsgserviceinstance_objects.return_value = [self.tenant] |
| 143 | self.policy.handle_delete(self.tenant) |
| 144 | instance_delete.assert_called() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 145 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 146 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 147 | def test_handle_delete_cleanup_instance_live(self): |
| 148 | with patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 149 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 150 | patch.object(Instance, "delete") as instance_delete: |
| 151 | # Make sure if an Instance still has active VSG Tenants, that we don't clean it up |
| 152 | vsg_service = VSGService() |
| 153 | instance = Instance(id=1) |
| 154 | instance_objects.return_value = [instance] |
| 155 | self.tenant.address_service_instance = None |
| 156 | self.tenant.instance = instance |
| 157 | self.tenant.instance_id = instance.id |
| 158 | self.tenant.owner = vsg_service |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 159 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 160 | other_tenant = VSGServiceInstance() |
| 161 | other_tenant.address_service_instance = None |
| 162 | other_tenant.instance = instance |
| 163 | other_tenant.instance_id = instance.id |
| 164 | other_tenant.owner = vsg_service |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 165 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 166 | vsgserviceinstance_objects.return_value = [self.tenant, other_tenant] |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 167 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 168 | self.policy.handle_delete(self.tenant) |
| 169 | instance_delete.assert_not_called() |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame] | 170 | |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 171 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 172 | def test_handle_delete_cleanup_instance_and_tags_and_stuff(self): |
| 173 | with patch.object(ServiceInstance.objects, "get_items") as si_objects, \ |
| 174 | patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 175 | patch.object(Tag.objects, "get_items") as tag_objects, \ |
| 176 | patch.object(VSGServiceInstance.objects, "get_items") as vsgserviceinstance_objects, \ |
| 177 | patch.object(Instance.objects, "get_items") as instance_objects, \ |
| 178 | patch.object(AddressManagerServiceInstance, "delete") as amsi_delete, \ |
| 179 | patch.object(Tag, "delete") as tag_delete, \ |
| 180 | patch.object(Instance, "delete") as instance_delete: |
| 181 | vsg_service = VSGService() |
| 182 | am_instance = AddressManagerServiceInstance() |
| 183 | amsi_objects.return_value = [am_instance] |
| 184 | si_objects.return_value = [am_instance] # AddressManagerServiceInstance is a subclass of ServiceInstance |
| 185 | instance = Instance(id=1) |
| 186 | instance_objects.return_value = [instance] |
| 187 | self.tenant.address_service_instance = None |
| 188 | self.tenant.instance = instance |
| 189 | self.tenant.instance_id = instance.id |
| 190 | self.tenant.owner = vsg_service |
| 191 | vsgserviceinstance_objects.return_value = [self.tenant] |
| 192 | stag_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 193 | object_id=instance.id, name="s_tag") |
| 194 | vrouter_tag = Tag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 195 | object_id=instance.id, name="vm_vrouter_tenant", value=am_instance.id) |
| 196 | tag_objects.return_value = [stag_tag, vrouter_tag] |
| 197 | self.policy.handle_delete(self.tenant) |
| 198 | instance_delete.assert_called() |
| 199 | assert stag_tag.delete.called |
| 200 | assert vrouter_tag.delete.called |
| 201 | assert am_instance.delete.called |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 202 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 203 | def test_cleanup_orphans(self): |
| 204 | with patch.object(AddressManagerServiceInstance.objects, "get_items") as amsi_objects, \ |
| 205 | patch.object(AddressManagerServiceInstance, "delete") as amsi_delete: |
| 206 | vrtenant = AddressManagerServiceInstance(id=1) |
| 207 | self.tenant.address_service_instance = vrtenant |
| 208 | some_other_vrtenant = AddressManagerServiceInstance(id=2) |
| 209 | link = ServiceInstanceLink(subscriber_service_instance=self.tenant, provider_service_instance=some_other_vrtenant) |
| 210 | self.tenant.subscribed_links = MockObjectList(initial=[link]) |
| 211 | amsi_objects.return_value = [some_other_vrtenant] |
| 212 | self.policy.cleanup_orphans(self.tenant) |
| 213 | amsi_delete.assert_called() |
| 214 | |
| 215 | def test_find_instance_for_s_tag_noexist(self): |
| 216 | with patch.object(Tag.objects, "get_items") as tag_objects: |
| 217 | tag_objects.filter.return_value = [] |
| 218 | instance = self.policy.find_instance_for_s_tag(3) |
| 219 | self.assertEqual(instance, None) |
| 220 | |
| 221 | def test_find_instance_for_s_tag(self): |
| 222 | with patch.object(Tag, "objects") as tag_objects: |
| 223 | tagged_instance = Instance() |
| 224 | tag = Tag(content_object = tagged_instance) |
| 225 | tag_objects.filter.return_value = [tag] |
| 226 | instance = self.policy.find_instance_for_s_tag(3) |
| 227 | self.assertEqual(instance, tagged_instance) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 228 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 229 | def test_manage_container_no_volt(self): |
| 230 | with self.assertRaises(Exception) as e: |
| 231 | self.policy.manage_container(self.tenant) |
| 232 | self.assertEqual(e.exception.message, "This VSG container has no volt") |
| 233 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 234 | def test_manage_container_noinstance(self): |
| 235 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 236 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 237 | patch.object(VSGServiceInstance, "volt") as volt: |
| 238 | instance = Instance() |
| 239 | volt.s_tag=222 |
| 240 | volt.c_tag=111 |
| 241 | find_or_make_instance_for_s_tag.return_value = instance |
| 242 | self.policy.manage_container(self.tenant) |
| 243 | self.assertEqual(self.tenant.instance, instance) |
| 244 | tenant_save.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 245 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 246 | def test_manage_container_hasinstance(self): |
| 247 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 248 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 249 | patch.object(VSGServiceInstance, "volt") as volt: |
| 250 | instance = Instance() |
| 251 | volt.s_tag=222 |
| 252 | volt.c_tag=111 |
| 253 | self.tenant.instance = instance |
| 254 | self.policy.manage_container(self.tenant) |
| 255 | find_or_make_instance_for_s_tag.assert_not_called() |
| 256 | self.assertEqual(self.tenant.instance, instance) |
| 257 | tenant_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 258 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 259 | def test_manage_container_deleted(self): |
| 260 | with patch.object(VSGServiceInstancePolicy, "find_or_make_instance_for_s_tag") as find_or_make_instance_for_s_tag, \ |
| 261 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 262 | patch.object(VSGServiceInstance, "volt") as volt: |
| 263 | self.tenant.deleted = True |
| 264 | self.policy.manage_container(self.tenant) |
| 265 | find_or_make_instance_for_s_tag.assert_not_called() |
| 266 | tenant_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 267 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 268 | def test_find_or_make_port_noexist(self): |
| 269 | with patch.object(Port, "save") as port_save, \ |
| 270 | patch.object(Port, "objects") as port_objects: |
| 271 | instance = Instance(id=123) |
| 272 | network = Instance(id=456) |
| 273 | port_objects.filter.return_value = [] |
| 274 | port=self.policy.find_or_make_port(instance, network) |
| 275 | self.assertNotEqual(port, None) |
| 276 | port_save.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 277 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 278 | def test_find_or_make_port_exists(self): |
| 279 | with patch.object(Port, "save") as port_save, \ |
| 280 | patch.object(Port, "objects") as port_objects: |
| 281 | someport = Port() |
| 282 | def mock_port_filter(network_id, instance_id): |
| 283 | if (network_id==456) and (instance_id==123): |
| 284 | return [someport] |
| 285 | return None |
| 286 | instance = Instance(id=123) |
| 287 | network = Instance(id=456) |
| 288 | port_objects.filter.side_effect = mock_port_filter |
| 289 | port=self.policy.find_or_make_port(instance, network) |
| 290 | self.assertEqual(port, someport) |
| 291 | port_save.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 292 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 293 | def test_get_lan_network_noexist(self): |
| 294 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 295 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 296 | vsgservice_objects.return_value = [vsgservice] |
| 297 | self.tenant.owner = vsgservice |
| 298 | self.slice.networks = MockObjectList() |
| 299 | with self.assertRaises(Exception) as e: |
| 300 | self.policy.get_lan_network(self.tenant, None) |
| 301 | self.assertEqual(e.exception.message, "No lan_network") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 302 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 303 | def test_get_lan_network(self): |
| 304 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 305 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 306 | vsgservice_objects.return_value = [vsgservice] |
| 307 | self.tenant.owner = vsgservice |
| 308 | self.slice.networks = MockObjectList([self.priv_network]) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 309 | lan_network = self.policy.get_lan_network(self.tenant, None) |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 310 | self.assertEqual(lan_network, self.priv_network) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 311 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 312 | def test_get_lan_network_toomany(self): |
| 313 | with patch.object(VSGService.objects, "get_items") as vsgservice_objects: |
| 314 | some_other_network = Network(name="mysite_test1_private", template=self.priv_template) |
| 315 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 316 | vsgservice_objects.return_value = [vsgservice] |
| 317 | self.tenant.owner = vsgservice |
| 318 | self.slice.networks = MockObjectList([self.priv_network, some_other_network]) |
| 319 | with self.assertRaises(Exception) as e: |
| 320 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 321 | self.assertEqual(e.exception.message, "The vSG slice should only have one non-management private network") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 322 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 323 | def test_port_set_parameter_noparamexist(self): |
| 324 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects: |
| 325 | npt_objects.return_value = [self.npt_stag] |
| 326 | port = Port() |
| 327 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 328 | self.assertNotEqual(NetworkParameter.objects.all(), []) |
| 329 | param = NetworkParameter.objects.first() |
| 330 | self.assertEqual(param.value, "123") |
| 331 | self.assertEqual(param.parameter, self.npt_stag) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 332 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 333 | def test_port_set_parameter_paramexist(self): |
| 334 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 335 | patch.object(NetworkParameter.objects, "get_items") as np_objects: |
| 336 | port = Port(id=1) |
| 337 | np_orig = NetworkParameter(parameter_id=self.npt_stag.id, value="456", object_id=port.id, content_type=port.self_content_type_id) |
| 338 | np_objects.return_value = [np_orig] |
| 339 | npt_objects.return_value = [self.npt_stag] |
| 340 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 341 | self.assertEqual(NetworkParameter.objects.count(), 1) |
| 342 | param = NetworkParameter.objects.first() |
| 343 | self.assertEqual(param.value, "123") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 344 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 345 | def test_find_or_make_instance_for_s_tag(self): |
| 346 | with patch.object(NetworkParameterType.objects, "get_items") as npt_objects, \ |
| 347 | patch.object(Node.objects, "get_items") as node_objects, \ |
| 348 | patch.object(Flavor.objects, "get_items") as flavor_objects, \ |
| 349 | patch.object(VSGService.objects, "get_items") as vsgservice_objects, \ |
| 350 | patch.object(VSGServiceInstance, "volt") as volt, \ |
| 351 | patch.object(VSGServiceInstance, "save") as tenant_save, \ |
| 352 | patch.object(VSGServiceInstancePolicy, "get_image") as get_image, \ |
| 353 | patch.object(VSGServiceInstancePolicy, "allocate_public_service_instance") as get_psi, \ |
| 354 | patch.object(LeastLoadedNodeScheduler, "pick") as pick, \ |
| 355 | patch.object(Node, "site_deployment") as site_deployment, \ |
| 356 | patch.object(Instance, "save") as instance_save, \ |
| 357 | patch.object(Instance, "delete") as instance_delete, \ |
| 358 | patch.object(VSGServiceInstancePolicy, "port_set_parameter") as port_set_parameter: |
| 359 | # setup mocks |
| 360 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 361 | vsgservice=VSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 362 | vsgservice_objects.return_value = [vsgservice] |
| 363 | self.tenant.owner = vsgservice |
| 364 | volt.s_tag=222 |
| 365 | volt.c_tag=111 |
| 366 | get_image.return_value = self.image |
| 367 | get_psi.return_value = vrtenant |
| 368 | pick.return_value = (self.node, None) |
| 369 | site_deployment.deployment = self.deployment |
| 370 | flavor_objects.return_value=[self.flavor] |
| 371 | node_objects.return_value=[self.node] |
| 372 | npt_objects.return_value=[self.npt_stag, self.npt_ctag, self.npt_neutron_port_name] |
| 373 | self.slice.networks = MockObjectList([self.priv_network]) |
| 374 | # done setup mocks |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 375 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 376 | # call the function under test |
| 377 | instance = self.policy.find_or_make_instance_for_s_tag(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 378 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 379 | # make sure Instance was created |
| 380 | self.assertNotEqual(instance, None) |
| 381 | self.assertEqual(instance.creator.email, "testadmin@test.org") |
| 382 | self.assertEqual(instance.image.name, "trusty-server-multi-nic") |
| 383 | self.assertEqual(instance.flavor.name, "m1.small") |
| 384 | self.assertEqual(instance.isolation, "vm") |
| 385 | self.assertEqual(instance.node.hostname, "my.node.com") |
| 386 | self.assertEqual(instance.slice.name, "mysite_test1") |
| 387 | self.assertEqual(instance.parent, None) |
| 388 | instance_save.assert_called() |
| 389 | instance_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 390 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 391 | # Access Network Port should have tags to c-tag and s-tag |
| 392 | port = Port.objects.first() |
| 393 | self.assertEqual(port.instance, instance) |
| 394 | self.assertEqual(port.network, self.priv_network) |
| 395 | port_set_parameter.assert_has_calls([mock.call(port, "c_tag", 111), |
| 396 | mock.call(port, "s_tag", 222), |
| 397 | mock.call(port, "neutron_port_name", "stag-222")]) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 398 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 399 | # The instance should be tagged with the s-tag |
| 400 | tag = Tag.objects.get(name="s_tag") |
| 401 | self.assertEqual(tag.value, "222") |
| 402 | self.assertEqual(tag.object_id, instance.id) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 403 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 404 | # The instance should have a tag pointing to its address_service_instance |
| 405 | tag = Tag.objects.get(name="vm_vrouter_tenant") |
| 406 | self.assertNotEqual(tag.value, vrtenant.id) |
| 407 | self.assertEqual(tag.object_id, instance.id) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 408 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 409 | # Allocate_public_service_instance should have been called |
| 410 | get_psi.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 411 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 412 | def test_manage_address_service_instance(self): |
| 413 | with patch.object(VSGServiceInstancePolicy, "allocate_public_service_instance") as get_psi: |
| 414 | vrtenant = AddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 415 | get_psi.return_value = vrtenant |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 416 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 417 | self.tenant.address_service_instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 418 | |
Scott Baker | 1a5ba26 | 2017-11-15 16:09:50 -0800 | [diff] [blame] | 419 | self.policy.manage_address_service_instance(self.tenant) |
| 420 | |
| 421 | get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 422 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 423 | if __name__ == '__main__': |
| 424 | unittest.main() |
| 425 | |