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 | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 18 | from mock import patch, call |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 19 | import mock |
| 20 | |
| 21 | import os, sys |
| 22 | sys.path.append("../../..") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 23 | config = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/test_config.yaml") |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 24 | from xosconfig import Config |
| 25 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 26 | |
| 27 | import synchronizers.new_base.modelaccessor |
| 28 | |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 29 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 30 | import model_policy_vsgtenant |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 31 | from model_policy_vsgtenant import VSGTenantPolicy |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 32 | from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 33 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 34 | # --------------------------------------------------------------------------------------------------------------------- |
| 35 | # Begin Model Policy Testing Framework |
| 36 | # TODO: Autogenerate this using xproto |
| 37 | # --------------------------------------------------------------------------------------------------------------------- |
| 38 | |
| 39 | # A list of all mock object stores that have been created |
| 40 | AllMockObjectStores = [] |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 41 | |
| 42 | class MockObjectList: |
| 43 | item_list = None |
| 44 | |
| 45 | def __init__(self, initial=None): |
| 46 | self.id_counter = 0 |
| 47 | if initial: |
| 48 | self.item_list=initial |
| 49 | elif self.item_list is None: |
| 50 | self.item_list=[] |
| 51 | |
| 52 | def get_items(self): |
| 53 | return self.item_list |
| 54 | |
| 55 | def count(self): |
| 56 | return len(self.get_items()) |
| 57 | |
| 58 | def first(self): |
| 59 | return self.get_items()[0] |
| 60 | |
| 61 | def all(self): |
| 62 | return self.get_items() |
| 63 | |
| 64 | def filter(self, **kwargs): |
| 65 | items = self.get_items() |
| 66 | for (k,v) in kwargs.items(): |
| 67 | items = [x for x in items if getattr(x,k) == v] |
| 68 | return items |
| 69 | |
| 70 | def get(self, **kwargs): |
| 71 | objs = self.filter(**kwargs) |
| 72 | if not objs: |
| 73 | raise Exception("No objects matching %s" % str(kwargs)) |
| 74 | return objs[0] |
| 75 | |
| 76 | class MockObjectStore(MockObjectList): |
| 77 | def save(self, o): |
| 78 | if (not hasattr(o,"id")) or (not o.id): |
| 79 | for item in self.get_items(): |
| 80 | if item.id >= self.id_counter: |
| 81 | self.id_counter = item.id + 1 |
| 82 | |
| 83 | o.id = self.id_counter |
| 84 | self.id_counter = self.id_counter + 1 |
| 85 | |
| 86 | for item in self.get_items(): |
| 87 | if item.id == o.id: |
| 88 | item = o |
| 89 | break |
| 90 | else: |
| 91 | self.get_items().append(o) |
| 92 | |
| 93 | class MockObject(object): |
| 94 | objects = None |
| 95 | id = None |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 96 | deleted = False |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 97 | def __init__(self, **kwargs): |
| 98 | for (k,v) in kwargs.items(): |
| 99 | setattr(self,k,v) |
| 100 | @property |
| 101 | def self_content_type_id(self): |
| 102 | return self.__class__.__name__ |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 103 | @property |
| 104 | def leaf_model(self): |
| 105 | return self |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 106 | def save(self): |
| 107 | if self.objects: |
| 108 | self.objects.save(self) |
| 109 | def delete(self): |
| 110 | pass |
| 111 | |
| 112 | def get_MockObjectStore(x): |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 113 | store = globals()["Mock%sObjects" % x]() |
| 114 | if not store in AllMockObjectStores: |
| 115 | AllMockObjectStores.append(store) |
| 116 | return store |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 117 | |
| 118 | class MockFlavorObjects(MockObjectStore): pass |
| 119 | class MockFlavor(MockObject): |
| 120 | objects = get_MockObjectStore("Flavor") |
| 121 | name = None |
| 122 | |
| 123 | class MockInstanceObjects(MockObjectStore): pass |
| 124 | class MockInstance(MockObject): |
| 125 | objects = get_MockObjectStore("Instance") |
| 126 | name = None |
| 127 | |
| 128 | class MockDeploymentObjects(MockObjectStore): pass |
| 129 | class MockDeployment(MockObject): |
| 130 | objects = get_MockObjectStore("Deployment") |
| 131 | name = None |
| 132 | |
| 133 | class MockUserObjects(MockObjectStore): pass |
| 134 | class MockUser(MockObject): |
| 135 | objects = get_MockObjectStore("User") |
| 136 | email = None |
| 137 | |
| 138 | class MockSliceObjects(MockObjectStore): pass |
| 139 | class MockSlice(MockObject): |
| 140 | objects = get_MockObjectStore("Slice") |
| 141 | name = None |
| 142 | default_node = None |
| 143 | networks = None |
| 144 | |
| 145 | class MockNodeObjects(MockObjectStore): pass |
| 146 | class MockNode(MockObject): |
| 147 | objects = get_MockObjectStore("Node") |
| 148 | hostname = None |
| 149 | site_deployment = None |
| 150 | |
| 151 | class MockImageObjects(MockObjectStore): pass |
| 152 | class MockImage(MockObject): |
| 153 | objects = get_MockObjectStore("Image") |
| 154 | name = None |
| 155 | |
| 156 | class MockTagObjects(MockObjectStore): pass |
| 157 | class MockTag(MockObject): |
| 158 | objects = get_MockObjectStore("Tag") |
| 159 | name = None |
| 160 | value = None |
| 161 | |
| 162 | class MockNetworkTemplateObjects(MockObjectStore): pass |
| 163 | class MockNetworkTemplate(MockObject): |
| 164 | objects = get_MockObjectStore("NetworkTemplate") |
| 165 | name = None |
| 166 | visibility = None |
| 167 | |
| 168 | class MockNetworkParameterTypeObjects(MockObjectStore): pass |
| 169 | class MockNetworkParameterType(MockObject): |
| 170 | objects = get_MockObjectStore("NetworkParameterType") |
| 171 | name = None |
| 172 | |
| 173 | class MockNetworkParameterObjects(MockObjectStore): pass |
| 174 | class MockNetworkParameter(MockObject): |
| 175 | objects = get_MockObjectStore("NetworkParameter") |
| 176 | value = None |
| 177 | parameter_id = None |
| 178 | |
| 179 | class MockNetworkObjects(MockObjectStore): pass |
| 180 | class MockNetwork(MockObject): |
| 181 | objects = get_MockObjectStore("Network") |
| 182 | name = None |
| 183 | template = None |
| 184 | |
| 185 | class MockPortObjects(MockObjectStore): pass |
| 186 | class MockPort(MockObject): |
| 187 | objects = get_MockObjectStore("Port") |
| 188 | name = None |
| 189 | def set_parameter(self, name, value): |
| 190 | pass |
| 191 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 192 | class MockServiceInstanceObjects(MockObjectStore): pass |
| 193 | class MockServiceInstance(MockObject): |
| 194 | objects = get_MockObjectStore("ServiceInstance") |
| 195 | def __init__(self, **kwargs): |
| 196 | super(MockServiceInstance, self).__init__(**kwargs) |
| 197 | |
| 198 | class MockServiceInstanceLinkObjects(MockObjectStore): pass |
| 199 | class MockServiceInstanceLink(MockObject): |
| 200 | objects = get_MockObjectStore("ServiceInstanceLink") |
| 201 | provider_service_instance = None |
| 202 | subscriber_service_instance = None |
| 203 | def __init__(self, **kwargs): |
| 204 | super(MockServiceInstanceLink, self).__init__(**kwargs) |
| 205 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 206 | class MockAddressManagerServiceInstanceObjects(MockObjectStore): pass |
| 207 | class MockAddressManagerServiceInstance(MockObject): |
| 208 | objects = get_MockObjectStore("AddressManagerServiceInstance") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 209 | public_ip = None |
| 210 | public_mac = None |
| 211 | address_pool_id = None |
| 212 | def set_attribute(self, name, value): |
| 213 | pass |
| 214 | |
| 215 | class MockVoltTenantObjects(MockObjectStore): pass |
| 216 | class MockVoltTenant(MockObject): |
| 217 | objects = get_MockObjectStore("VoltTenant") |
| 218 | c_tag = None |
| 219 | s_tag = None |
| 220 | |
| 221 | class MockVSGServiceObjects(MockObjectStore): pass |
| 222 | class MockVSGService(MockObject): |
| 223 | objects = get_MockObjectStore("VSGService") |
| 224 | name = None |
| 225 | node_label = None |
| 226 | slices = None |
| 227 | def __init__(self, **kwargs): |
| 228 | super(MockVSGService, self).__init__(**kwargs) |
| 229 | |
| 230 | class MockVSGTenantObjects(MockObjectStore): pass |
| 231 | class MockVSGTenant(MockObject): |
| 232 | objects = get_MockObjectStore("VSGTenant") |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 233 | owner = None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 234 | deleted = False |
| 235 | instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 236 | creator = None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 237 | volt = None |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 238 | link_deleted_count = 0 |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 239 | service_specific_attribute = {} |
| 240 | |
| 241 | def get_image(self): |
| 242 | return None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 243 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 244 | # --------------------------------------------------------------------------------------------------------------------- |
| 245 | # End Model Policy Testing Framework |
| 246 | # --------------------------------------------------------------------------------------------------------------------- |
| 247 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 248 | class TestModelPolicyVsgTenant(unittest.TestCase): |
| 249 | def setUp(self): |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 250 | # Some of the functions we call have side-effects. For example, creating a VSGTenant may lead to creation of |
| 251 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 252 | for store in AllMockObjectStores: |
| 253 | store.items = [] |
| 254 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 255 | self.policy = VSGTenantPolicy() |
| 256 | self.tenant = MockVSGTenant() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 257 | self.user = MockUser(email="testadmin@test.org") |
| 258 | self.tenant = MockVSGTenant(creator=self.user, id=1) |
| 259 | self.flavor = MockFlavor(name="m1.small") |
| 260 | self.npt_ctag = MockNetworkParameterType(name="c_tag", id=1) |
| 261 | self.npt_stag = MockNetworkParameterType(name="s_tag", id=2) |
| 262 | self.npt_neutron_port_name = MockNetworkParameterType(name="neutron_port_name", id=3) |
| 263 | self.node = MockNode(hostname="my.node.com") |
| 264 | self.slice = MockSlice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm") |
| 265 | self.priv_template = MockNetworkTemplate(name="access_network", visibility="private") |
| 266 | self.priv_network = MockNetwork(name="mysite_test1_private", template=self.priv_template) |
| 267 | self.image = MockImage(name="trusty-server-multi-nic") |
| 268 | self.deployment = MockDeployment(name="testdeployment") |
| 269 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Instance = MockInstance |
| 270 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Flavor = MockFlavor |
| 271 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Tag = MockTag |
| 272 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Node = MockNode |
| 273 | model_policy_vsgtenant.Instance = MockInstance |
| 274 | model_policy_vsgtenant.Flavor = MockFlavor |
| 275 | model_policy_vsgtenant.Tag = MockTag |
| 276 | model_policy_vsgtenant.VSGService = MockVSGService |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 277 | model_policy_vsgtenant.VSGTenant = MockVSGTenant |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 278 | model_policy_vsgtenant.Node = MockNode |
| 279 | model_policy_vsgtenant.Port = MockPort |
| 280 | model_policy_vsgtenant.NetworkParameterType = MockNetworkParameterType |
| 281 | model_policy_vsgtenant.NetworkParameter = MockNetworkParameter |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 282 | model_policy_vsgtenant.ServiceInstance = MockServiceInstance |
| 283 | model_policy_vsgtenant.AddressManagerServiceInstance = MockAddressManagerServiceInstance |
| 284 | |
| 285 | MockTag.objects.item_list = [] |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 286 | |
| 287 | @patch.object(VSGTenantPolicy, "manage_container") |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 288 | @patch.object(VSGTenantPolicy, "manage_address_service_instance") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 289 | @patch.object(VSGTenantPolicy, "cleanup_orphans") |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 290 | def test_handle_create(self, cleanup_orphans, manage_address_service_instance, manage_container): |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 291 | self.policy.handle_create(self.tenant) |
| 292 | manage_container.assert_called_with(self.tenant) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 293 | manage_address_service_instance.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 294 | cleanup_orphans.assert_called_with(self.tenant) |
| 295 | |
| 296 | @patch.object(VSGTenantPolicy, "manage_container") |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 297 | @patch.object(VSGTenantPolicy, "manage_address_service_instance") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 298 | @patch.object(VSGTenantPolicy, "cleanup_orphans") |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 299 | def test_handle_update(self, cleanup_orphans, manage_address_service_instance, manage_container): |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 300 | self.policy.handle_create(self.tenant) |
| 301 | manage_container.assert_called_with(self.tenant) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 302 | manage_address_service_instance.assert_called_with(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 303 | cleanup_orphans.assert_called_with(self.tenant) |
| 304 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 305 | @patch.object(MockAddressManagerServiceInstance, "delete") |
| 306 | def test_handle_delete_asi_exist(self, amsi_delete): |
| 307 | vrtenant = MockAddressManagerServiceInstance() |
| 308 | self.tenant.address_service_instance = vrtenant |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 309 | self.policy.handle_delete(self.tenant) |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 310 | # The delete model_policy no longer deletes the AddressManagerServiceInstance. It's now handled by logic in |
| 311 | # ServiceInstanceLink, together with model_policies in the target object. |
| 312 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 313 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 314 | @patch.object(MockAddressManagerServiceInstance, "delete") |
| 315 | def test_handle_delete_asi_noexist(self, amsi_delete): |
| 316 | self.tenant.address_service_instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 317 | self.policy.handle_delete(self.tenant) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 318 | amsi_delete.assert_not_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 319 | |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 320 | @patch.object(MockVSGTenantObjects, "get_items") |
| 321 | @patch.object(MockInstanceObjects, "get_items") |
| 322 | @patch.object(MockInstance, "delete") |
| 323 | def test_handle_delete_cleanup_instance(self, instance_delete, instance_objects, vsgtenant_objects): |
| 324 | vsg_service = MockVSGService() |
| 325 | instance = MockInstance(id=1) |
| 326 | instance_objects.return_value = [instance] |
| 327 | self.tenant.address_service_instance = None |
| 328 | self.tenant.instance = instance |
| 329 | self.tenant.instance_id = instance.id |
| 330 | self.tenant.owner = vsg_service |
| 331 | vsgtenant_objects.return_value = [self.tenant] |
| 332 | self.policy.handle_delete(self.tenant) |
| 333 | instance_delete.assert_called() |
| 334 | |
| 335 | @patch.object(MockVSGTenantObjects, "get_items") |
| 336 | @patch.object(MockInstanceObjects, "get_items") |
| 337 | @patch.object(MockInstance, "delete") |
| 338 | def test_handle_delete_cleanup_instance_live(self, instance_delete, instance_objects, vsgtenant_objects): |
| 339 | # Make sure if an Instance still has active VSG Tenants, that we don't clean it up |
| 340 | vsg_service = MockVSGService() |
| 341 | instance = MockInstance(id=1) |
| 342 | instance_objects.return_value = [instance] |
| 343 | self.tenant.address_service_instance = None |
| 344 | self.tenant.instance = instance |
| 345 | self.tenant.instance_id = instance.id |
| 346 | self.tenant.owner = vsg_service |
| 347 | |
| 348 | other_tenant = MockVSGTenant() |
| 349 | other_tenant.address_service_instance = None |
| 350 | other_tenant.instance = instance |
| 351 | other_tenant.instance_id = instance.id |
| 352 | other_tenant.owner = vsg_service |
| 353 | |
| 354 | vsgtenant_objects.return_value = [self.tenant, other_tenant] |
| 355 | |
| 356 | self.policy.handle_delete(self.tenant) |
| 357 | instance_delete.assert_not_called() |
| 358 | |
| 359 | @patch.object(MockServiceInstanceObjects, "get_items") |
| 360 | @patch.object(MockAddressManagerServiceInstanceObjects, "get_items") |
| 361 | @patch.object(MockTagObjects, "get_items") |
| 362 | @patch.object(MockVSGTenantObjects, "get_items") |
| 363 | @patch.object(MockInstanceObjects, "get_items") |
| 364 | @patch.object(MockAddressManagerServiceInstance, "delete") |
| 365 | @patch.object(MockTag, "delete") |
| 366 | @patch.object(MockInstance, "delete") |
| 367 | def test_handle_delete_cleanup_instance_and_tags_and_stuff(self, instance_delete, tag_delete, amsi_delete, |
| 368 | instance_objects, vsgtenant_objects, tag_objects, |
| 369 | amsi_objects, si_objects): |
| 370 | vsg_service = MockVSGService() |
| 371 | am_instance = MockAddressManagerServiceInstance() |
| 372 | amsi_objects.return_value = [am_instance] |
| 373 | si_objects.return_value = [am_instance] # AddressManagerServiceInstance is a subclass of ServiceInstance |
| 374 | instance = MockInstance(id=1) |
| 375 | instance_objects.return_value = [instance] |
| 376 | self.tenant.address_service_instance = None |
| 377 | self.tenant.instance = instance |
| 378 | self.tenant.instance_id = instance.id |
| 379 | self.tenant.owner = vsg_service |
| 380 | vsgtenant_objects.return_value = [self.tenant] |
| 381 | stag_tag = MockTag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 382 | object_id=instance.id, name="s_tag") |
| 383 | vrouter_tag = MockTag(service_id=self.tenant.owner.id, content_type=instance.self_content_type_id, |
| 384 | object_id=instance.id, name="vm_vrouter_tenant", value=am_instance.id) |
| 385 | tag_objects.return_value = [stag_tag, vrouter_tag] |
| 386 | self.policy.handle_delete(self.tenant) |
| 387 | instance_delete.assert_called() |
| 388 | assert stag_tag.delete.called |
| 389 | assert vrouter_tag.delete.called |
| 390 | assert am_instance.delete.called |
| 391 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 392 | @patch.object(MockAddressManagerServiceInstanceObjects, "get_items") |
| 393 | @patch.object(MockAddressManagerServiceInstance, "delete") |
| 394 | def test_cleanup_orphans(self, amsi_delete, amsi_objects): |
| 395 | vrtenant = MockAddressManagerServiceInstance(id=1) |
| 396 | self.tenant.address_service_instance = vrtenant |
Scott Baker | ad310b9 | 2017-09-12 11:23:07 -0700 | [diff] [blame^] | 397 | some_other_vrtenant = MockAddressManagerServiceInstance(id=2) |
| 398 | link = MockServiceInstanceLink(subscriber_service_instance=self.tenant, provider_service_instance=some_other_vrtenant) |
| 399 | self.tenant.subscribed_links = MockObjectList(initial=[link]) |
| 400 | amsi_objects.return_value = [some_other_vrtenant] |
| 401 | self.policy.cleanup_orphans(self.tenant) |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 402 | amsi_delete.assert_called() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 403 | |
| 404 | @patch.object(MockTag, "objects") |
| 405 | def test_find_instance_for_s_tag_noexist(self, tag_objects): |
| 406 | tag_objects.filter.return_value = [] |
| 407 | instance = self.policy.find_instance_for_s_tag(3) |
| 408 | self.assertEqual(instance, None) |
| 409 | |
| 410 | @patch.object(MockTag, "objects") |
| 411 | def test_find_instance_for_s_tag(self, tag_objects): |
| 412 | tagged_instance = MockInstance() |
| 413 | tag = MockTag(content_object = tagged_instance) |
| 414 | tag_objects.filter.return_value = [tag] |
| 415 | instance = self.policy.find_instance_for_s_tag(3) |
| 416 | self.assertEqual(instance, tagged_instance) |
| 417 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 418 | def test_manage_container_no_volt(self): |
| 419 | with self.assertRaises(Exception) as e: |
| 420 | self.policy.manage_container(self.tenant) |
| 421 | self.assertEqual(e.exception.message, "This VSG container has no volt") |
| 422 | |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 423 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 424 | @patch.object(MockVSGTenant, "save") |
| 425 | @patch.object(MockVSGTenant, "volt") |
| 426 | def test_manage_container_noinstance(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 427 | instance = MockInstance() |
| 428 | volt.s_tag=222 |
| 429 | volt.c_tag=111 |
| 430 | find_or_make_instance_for_s_tag.return_value = instance |
| 431 | self.policy.manage_container(self.tenant) |
| 432 | self.assertEqual(self.tenant.instance, instance) |
| 433 | tenant_save.assert_called() |
| 434 | |
| 435 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 436 | @patch.object(MockVSGTenant, "save") |
| 437 | @patch.object(MockVSGTenant, "volt") |
| 438 | def test_manage_container_hasinstance(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 439 | instance = MockInstance() |
| 440 | volt.s_tag=222 |
| 441 | volt.c_tag=111 |
| 442 | self.tenant.instance = instance |
| 443 | self.policy.manage_container(self.tenant) |
| 444 | find_or_make_instance_for_s_tag.assert_not_called() |
| 445 | self.assertEqual(self.tenant.instance, instance) |
| 446 | tenant_save.assert_not_called() |
| 447 | |
| 448 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 449 | @patch.object(MockVSGTenant, "save") |
| 450 | @patch.object(MockVSGTenant, "volt") |
| 451 | def test_manage_container_deleted(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 452 | self.tenant.deleted = True |
| 453 | self.policy.manage_container(self.tenant) |
| 454 | find_or_make_instance_for_s_tag.assert_not_called() |
| 455 | tenant_save.assert_not_called() |
| 456 | |
| 457 | @patch.object(MockPort, "save") |
| 458 | @patch.object(MockPort, "objects") |
| 459 | def test_find_or_make_port_noexist(self, port_objects, port_save): |
| 460 | instance = MockInstance(id=123) |
| 461 | network = MockInstance(id=456) |
| 462 | port_objects.filter.return_value = [] |
| 463 | port=self.policy.find_or_make_port(instance, network) |
| 464 | self.assertNotEqual(port, None) |
| 465 | port_save.assert_called() |
| 466 | |
| 467 | @patch.object(MockPort, "save") |
| 468 | @patch.object(MockPort, "objects") |
| 469 | def test_find_or_make_port_exists(self, port_objects, port_save): |
| 470 | someport = MockPort() |
| 471 | def mock_port_filter(network_id, instance_id): |
| 472 | if (network_id==456) and (instance_id==123): |
| 473 | return [someport] |
| 474 | return None |
| 475 | instance = MockInstance(id=123) |
| 476 | network = MockInstance(id=456) |
| 477 | port_objects.filter.side_effect = mock_port_filter |
| 478 | port=self.policy.find_or_make_port(instance, network) |
| 479 | self.assertEqual(port, someport) |
| 480 | port_save.assert_not_called() |
| 481 | |
| 482 | @patch.object(MockVSGServiceObjects, "get_items") |
| 483 | def test_get_lan_network_noexist(self, vsgservice_objects): |
| 484 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 485 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 486 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 487 | self.slice.networks = MockObjectList() |
| 488 | with self.assertRaises(Exception) as e: |
| 489 | self.policy.get_lan_network(self.tenant, None) |
| 490 | self.assertEqual(e.exception.message, "No lan_network") |
| 491 | |
| 492 | @patch.object(MockVSGServiceObjects, "get_items") |
| 493 | def test_get_lan_network(self, vsgservice_objects): |
| 494 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 495 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 496 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 497 | self.slice.networks = MockObjectList([self.priv_network]) |
| 498 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 499 | self.assertEqual(lan_network, self.priv_network) |
| 500 | |
| 501 | @patch.object(MockVSGServiceObjects, "get_items") |
| 502 | def test_get_lan_network_toomany(self, vsgservice_objects): |
| 503 | some_other_network = MockNetwork(name="mysite_test1_private", template=self.priv_template) |
| 504 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 505 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 506 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 507 | self.slice.networks = MockObjectList([self.priv_network, some_other_network]) |
| 508 | with self.assertRaises(Exception) as e: |
| 509 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 510 | self.assertEqual(e.exception.message, "The vSG slice should only have one non-management private network") |
| 511 | |
| 512 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 513 | def test_port_set_parameter_noparamexist(self, npt_objects): |
| 514 | npt_objects.return_value = [self.npt_stag] |
| 515 | port = MockPort() |
| 516 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 517 | self.assertNotEqual(MockNetworkParameter.objects.all(), []) |
| 518 | param = MockNetworkParameter.objects.first() |
| 519 | self.assertEqual(param.value, "123") |
| 520 | self.assertEqual(param.parameter, self.npt_stag) |
| 521 | |
| 522 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 523 | @patch.object(MockNetworkParameterObjects, "get_items") |
| 524 | def test_port_set_parameter_paramexist(self, np_objects, npt_objects): |
| 525 | port = MockPort(id=1) |
| 526 | np_orig = MockNetworkParameter(parameter_id=self.npt_stag.id, value="456", object_id=port.id, content_type=port.self_content_type_id) |
| 527 | np_objects.return_value = [np_orig] |
| 528 | npt_objects.return_value = [self.npt_stag] |
| 529 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 530 | self.assertEqual(MockNetworkParameter.objects.count(), 1) |
| 531 | param = MockNetworkParameter.objects.first() |
| 532 | self.assertEqual(param.value, "123") |
| 533 | |
| 534 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 535 | @patch.object(MockNodeObjects, "get_items") |
| 536 | @patch.object(MockFlavorObjects, "get_items") |
| 537 | @patch.object(MockVSGServiceObjects, "get_items") |
| 538 | @patch.object(MockVSGTenant, "volt") |
| 539 | @patch.object(MockVSGTenant, "save") |
| 540 | @patch.object(VSGTenantPolicy, "get_image") |
| 541 | @patch.object(VSGTenantPolicy, "allocate_public_service_instance") |
| 542 | @patch.object(LeastLoadedNodeScheduler, "pick") |
| 543 | @patch.object(MockNode, "site_deployment") |
| 544 | @patch.object(MockInstance, "save") |
| 545 | @patch.object(MockInstance, "delete") |
| 546 | @patch.object(VSGTenantPolicy, "port_set_parameter") |
| 547 | def test_find_or_make_instance_for_s_tag(self, port_set_parameter, instance_delete, instance_save, site_deployment, |
| 548 | pick, get_psi, get_image, tenant_save, volt, |
| 549 | vsgservice_objects, flavor_objects, node_objects, npt_objects): |
| 550 | # setup mocks |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 551 | vrtenant = MockAddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 552 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 553 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 554 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 555 | volt.s_tag=222 |
| 556 | volt.c_tag=111 |
| 557 | get_image.return_value = self.image |
| 558 | get_psi.return_value = vrtenant |
| 559 | pick.return_value = (self.node, None) |
| 560 | site_deployment.deployment = self.deployment |
| 561 | flavor_objects.return_value=[self.flavor] |
| 562 | node_objects.return_value=[self.node] |
| 563 | npt_objects.return_value=[self.npt_stag, self.npt_ctag, self.npt_neutron_port_name] |
| 564 | self.slice.networks = MockObjectList([self.priv_network]) |
| 565 | # done setup mocks |
| 566 | |
| 567 | # call the function under test |
| 568 | instance = self.policy.find_or_make_instance_for_s_tag(self.tenant, self.tenant.volt.s_tag) |
| 569 | |
| 570 | # make sure Instance was created |
| 571 | self.assertNotEqual(instance, None) |
| 572 | self.assertEqual(instance.creator.email, "testadmin@test.org") |
| 573 | self.assertEqual(instance.image.name, "trusty-server-multi-nic") |
| 574 | self.assertEqual(instance.flavor.name, "m1.small") |
| 575 | self.assertEqual(instance.isolation, "vm") |
| 576 | self.assertEqual(instance.node.hostname, "my.node.com") |
| 577 | self.assertEqual(instance.slice.name, "mysite_test1") |
| 578 | self.assertEqual(instance.parent, None) |
| 579 | instance_save.assert_called() |
| 580 | instance_delete.assert_not_called() |
| 581 | |
| 582 | # Access Network Port should have tags to c-tag and s-tag |
| 583 | port = MockPort.objects.first() |
| 584 | self.assertEqual(port.instance, instance) |
| 585 | self.assertEqual(port.network, self.priv_network) |
| 586 | port_set_parameter.assert_has_calls([mock.call(port, "c_tag", 111), |
| 587 | mock.call(port, "s_tag", 222), |
| 588 | mock.call(port, "neutron_port_name", "stag-222")]) |
| 589 | |
| 590 | # The instance should be tagged with the s-tag |
| 591 | tag = MockTag.objects.get(name="s_tag") |
| 592 | self.assertEqual(tag.value, "222") |
| 593 | self.assertEqual(tag.object_id, instance.id) |
| 594 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 595 | # The instance should have a tag pointing to its address_service_instance |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 596 | tag = MockTag.objects.get(name="vm_vrouter_tenant") |
| 597 | self.assertNotEqual(tag.value, vrtenant.id) |
| 598 | self.assertEqual(tag.object_id, instance.id) |
| 599 | |
| 600 | # Allocate_public_service_instance should have been called |
| 601 | get_psi.assert_called() |
| 602 | |
| 603 | @patch.object(VSGTenantPolicy, "allocate_public_service_instance") |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 604 | def test_manage_address_service_instance(self, get_psi): |
| 605 | vrtenant = MockAddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 606 | get_psi.return_value = vrtenant |
| 607 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 608 | self.tenant.address_service_instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 609 | |
Scott Baker | add58dd | 2017-08-23 15:56:58 -0700 | [diff] [blame] | 610 | self.policy.manage_address_service_instance(self.tenant) |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 611 | |
| 612 | get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant) |
| 613 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 614 | if __name__ == '__main__': |
| 615 | unittest.main() |
| 616 | |