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 |
| 18 | from mock import patch |
| 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 | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 34 | MockObjectStores = {} |
| 35 | |
| 36 | class MockObjectList: |
| 37 | item_list = None |
| 38 | |
| 39 | def __init__(self, initial=None): |
| 40 | self.id_counter = 0 |
| 41 | if initial: |
| 42 | self.item_list=initial |
| 43 | elif self.item_list is None: |
| 44 | self.item_list=[] |
| 45 | |
| 46 | def get_items(self): |
| 47 | return self.item_list |
| 48 | |
| 49 | def count(self): |
| 50 | return len(self.get_items()) |
| 51 | |
| 52 | def first(self): |
| 53 | return self.get_items()[0] |
| 54 | |
| 55 | def all(self): |
| 56 | return self.get_items() |
| 57 | |
| 58 | def filter(self, **kwargs): |
| 59 | items = self.get_items() |
| 60 | for (k,v) in kwargs.items(): |
| 61 | items = [x for x in items if getattr(x,k) == v] |
| 62 | return items |
| 63 | |
| 64 | def get(self, **kwargs): |
| 65 | objs = self.filter(**kwargs) |
| 66 | if not objs: |
| 67 | raise Exception("No objects matching %s" % str(kwargs)) |
| 68 | return objs[0] |
| 69 | |
| 70 | class MockObjectStore(MockObjectList): |
| 71 | def save(self, o): |
| 72 | if (not hasattr(o,"id")) or (not o.id): |
| 73 | for item in self.get_items(): |
| 74 | if item.id >= self.id_counter: |
| 75 | self.id_counter = item.id + 1 |
| 76 | |
| 77 | o.id = self.id_counter |
| 78 | self.id_counter = self.id_counter + 1 |
| 79 | |
| 80 | for item in self.get_items(): |
| 81 | if item.id == o.id: |
| 82 | item = o |
| 83 | break |
| 84 | else: |
| 85 | self.get_items().append(o) |
| 86 | |
| 87 | class MockObject(object): |
| 88 | objects = None |
| 89 | id = None |
| 90 | def __init__(self, **kwargs): |
| 91 | for (k,v) in kwargs.items(): |
| 92 | setattr(self,k,v) |
| 93 | @property |
| 94 | def self_content_type_id(self): |
| 95 | return self.__class__.__name__ |
| 96 | def save(self): |
| 97 | if self.objects: |
| 98 | self.objects.save(self) |
| 99 | def delete(self): |
| 100 | pass |
| 101 | |
| 102 | def get_MockObjectStore(x): |
| 103 | return globals()["Mock%sObjects" % x]() |
| 104 | |
| 105 | class MockFlavorObjects(MockObjectStore): pass |
| 106 | class MockFlavor(MockObject): |
| 107 | objects = get_MockObjectStore("Flavor") |
| 108 | name = None |
| 109 | |
| 110 | class MockInstanceObjects(MockObjectStore): pass |
| 111 | class MockInstance(MockObject): |
| 112 | objects = get_MockObjectStore("Instance") |
| 113 | name = None |
| 114 | |
| 115 | class MockDeploymentObjects(MockObjectStore): pass |
| 116 | class MockDeployment(MockObject): |
| 117 | objects = get_MockObjectStore("Deployment") |
| 118 | name = None |
| 119 | |
| 120 | class MockUserObjects(MockObjectStore): pass |
| 121 | class MockUser(MockObject): |
| 122 | objects = get_MockObjectStore("User") |
| 123 | email = None |
| 124 | |
| 125 | class MockSliceObjects(MockObjectStore): pass |
| 126 | class MockSlice(MockObject): |
| 127 | objects = get_MockObjectStore("Slice") |
| 128 | name = None |
| 129 | default_node = None |
| 130 | networks = None |
| 131 | |
| 132 | class MockNodeObjects(MockObjectStore): pass |
| 133 | class MockNode(MockObject): |
| 134 | objects = get_MockObjectStore("Node") |
| 135 | hostname = None |
| 136 | site_deployment = None |
| 137 | |
| 138 | class MockImageObjects(MockObjectStore): pass |
| 139 | class MockImage(MockObject): |
| 140 | objects = get_MockObjectStore("Image") |
| 141 | name = None |
| 142 | |
| 143 | class MockTagObjects(MockObjectStore): pass |
| 144 | class MockTag(MockObject): |
| 145 | objects = get_MockObjectStore("Tag") |
| 146 | name = None |
| 147 | value = None |
| 148 | |
| 149 | class MockNetworkTemplateObjects(MockObjectStore): pass |
| 150 | class MockNetworkTemplate(MockObject): |
| 151 | objects = get_MockObjectStore("NetworkTemplate") |
| 152 | name = None |
| 153 | visibility = None |
| 154 | |
| 155 | class MockNetworkParameterTypeObjects(MockObjectStore): pass |
| 156 | class MockNetworkParameterType(MockObject): |
| 157 | objects = get_MockObjectStore("NetworkParameterType") |
| 158 | name = None |
| 159 | |
| 160 | class MockNetworkParameterObjects(MockObjectStore): pass |
| 161 | class MockNetworkParameter(MockObject): |
| 162 | objects = get_MockObjectStore("NetworkParameter") |
| 163 | value = None |
| 164 | parameter_id = None |
| 165 | |
| 166 | class MockNetworkObjects(MockObjectStore): pass |
| 167 | class MockNetwork(MockObject): |
| 168 | objects = get_MockObjectStore("Network") |
| 169 | name = None |
| 170 | template = None |
| 171 | |
| 172 | class MockPortObjects(MockObjectStore): pass |
| 173 | class MockPort(MockObject): |
| 174 | objects = get_MockObjectStore("Port") |
| 175 | name = None |
| 176 | def set_parameter(self, name, value): |
| 177 | pass |
| 178 | |
| 179 | class MockVRouterTenantObjects(MockObjectStore): pass |
| 180 | class MockVRouterTenant(MockObject): |
| 181 | objects = get_MockObjectStore("VRouterTenant") |
| 182 | public_ip = None |
| 183 | public_mac = None |
| 184 | address_pool_id = None |
| 185 | def set_attribute(self, name, value): |
| 186 | pass |
| 187 | |
| 188 | class MockVoltTenantObjects(MockObjectStore): pass |
| 189 | class MockVoltTenant(MockObject): |
| 190 | objects = get_MockObjectStore("VoltTenant") |
| 191 | c_tag = None |
| 192 | s_tag = None |
| 193 | |
| 194 | class MockVSGServiceObjects(MockObjectStore): pass |
| 195 | class MockVSGService(MockObject): |
| 196 | objects = get_MockObjectStore("VSGService") |
| 197 | name = None |
| 198 | node_label = None |
| 199 | slices = None |
| 200 | def __init__(self, **kwargs): |
| 201 | super(MockVSGService, self).__init__(**kwargs) |
| 202 | |
| 203 | class MockVSGTenantObjects(MockObjectStore): pass |
| 204 | class MockVSGTenant(MockObject): |
| 205 | objects = get_MockObjectStore("VSGTenant") |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 206 | owner = None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 207 | deleted = False |
| 208 | instance = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 209 | creator = None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 210 | volt = None |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 211 | service_specific_attribute = {} |
| 212 | |
| 213 | def get_image(self): |
| 214 | return None |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 215 | |
| 216 | class TestModelPolicyVsgTenant(unittest.TestCase): |
| 217 | def setUp(self): |
| 218 | self.policy = VSGTenantPolicy() |
| 219 | self.tenant = MockVSGTenant() |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 220 | self.user = MockUser(email="testadmin@test.org") |
| 221 | self.tenant = MockVSGTenant(creator=self.user, id=1) |
| 222 | self.flavor = MockFlavor(name="m1.small") |
| 223 | self.npt_ctag = MockNetworkParameterType(name="c_tag", id=1) |
| 224 | self.npt_stag = MockNetworkParameterType(name="s_tag", id=2) |
| 225 | self.npt_neutron_port_name = MockNetworkParameterType(name="neutron_port_name", id=3) |
| 226 | self.node = MockNode(hostname="my.node.com") |
| 227 | self.slice = MockSlice(name="mysite_test1", default_flavor=self.flavor, default_isolation="vm") |
| 228 | self.priv_template = MockNetworkTemplate(name="access_network", visibility="private") |
| 229 | self.priv_network = MockNetwork(name="mysite_test1_private", template=self.priv_template) |
| 230 | self.image = MockImage(name="trusty-server-multi-nic") |
| 231 | self.deployment = MockDeployment(name="testdeployment") |
| 232 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Instance = MockInstance |
| 233 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Flavor = MockFlavor |
| 234 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Tag = MockTag |
| 235 | synchronizers.new_base.model_policies.model_policy_tenantwithcontainer.Node = MockNode |
| 236 | model_policy_vsgtenant.Instance = MockInstance |
| 237 | model_policy_vsgtenant.Flavor = MockFlavor |
| 238 | model_policy_vsgtenant.Tag = MockTag |
| 239 | model_policy_vsgtenant.VSGService = MockVSGService |
| 240 | model_policy_vsgtenant.Node = MockNode |
| 241 | model_policy_vsgtenant.Port = MockPort |
| 242 | model_policy_vsgtenant.NetworkParameterType = MockNetworkParameterType |
| 243 | model_policy_vsgtenant.NetworkParameter = MockNetworkParameter |
| 244 | |
| 245 | @patch.object(VSGTenantPolicy, "manage_container") |
| 246 | @patch.object(VSGTenantPolicy, "manage_vrouter") |
| 247 | @patch.object(VSGTenantPolicy, "cleanup_orphans") |
| 248 | def test_handle_create(self, cleanup_orphans, manage_vrouter, manage_container): |
| 249 | self.policy.handle_create(self.tenant) |
| 250 | manage_container.assert_called_with(self.tenant) |
| 251 | manage_vrouter.assert_called_with(self.tenant) |
| 252 | cleanup_orphans.assert_called_with(self.tenant) |
| 253 | |
| 254 | @patch.object(VSGTenantPolicy, "manage_container") |
| 255 | @patch.object(VSGTenantPolicy, "manage_vrouter") |
| 256 | @patch.object(VSGTenantPolicy, "cleanup_orphans") |
| 257 | def test_handle_update(self, cleanup_orphans, manage_vrouter, manage_container): |
| 258 | self.policy.handle_create(self.tenant) |
| 259 | manage_container.assert_called_with(self.tenant) |
| 260 | manage_vrouter.assert_called_with(self.tenant) |
| 261 | cleanup_orphans.assert_called_with(self.tenant) |
| 262 | |
| 263 | @patch.object(MockVRouterTenant, "delete") |
| 264 | def test_handle_delete_vrouter_exist(self, vroutertenant_delete): |
| 265 | vrtenant = MockVRouterTenant() |
| 266 | self.tenant.vrouter = vrtenant |
| 267 | self.policy.handle_delete(self.tenant) |
| 268 | vroutertenant_delete.assert_called() |
| 269 | |
| 270 | @patch.object(MockVRouterTenant, "delete") |
| 271 | def test_handle_delete_vrouter_noexist(self, vroutertenant_delete): |
| 272 | self.tenant.vrouter = None |
| 273 | self.policy.handle_delete(self.tenant) |
| 274 | vroutertenant_delete.assert_not_called() |
| 275 | |
| 276 | @patch.object(MockVRouterTenantObjects, "get_items") |
| 277 | @patch.object(MockVRouterTenant, "delete") |
| 278 | def test_cleanup_orphans(self, vroutertenant_delete, vroutertenant_objects): |
| 279 | vrtenant = MockVRouterTenant(id=1) |
| 280 | self.tenant.vrouter = vrtenant |
| 281 | some_other_vrtenant = MockVRouterTenant(id=2, subscriber_tenant_id = self.tenant.id) |
| 282 | vroutertenant_objects.get_items = [some_other_vrtenant] |
| 283 | self.policy.handle_delete(self.tenant) |
| 284 | vroutertenant_delete.assert_called() |
| 285 | |
| 286 | @patch.object(MockTag, "objects") |
| 287 | def test_find_instance_for_s_tag_noexist(self, tag_objects): |
| 288 | tag_objects.filter.return_value = [] |
| 289 | instance = self.policy.find_instance_for_s_tag(3) |
| 290 | self.assertEqual(instance, None) |
| 291 | |
| 292 | @patch.object(MockTag, "objects") |
| 293 | def test_find_instance_for_s_tag(self, tag_objects): |
| 294 | tagged_instance = MockInstance() |
| 295 | tag = MockTag(content_object = tagged_instance) |
| 296 | tag_objects.filter.return_value = [tag] |
| 297 | instance = self.policy.find_instance_for_s_tag(3) |
| 298 | self.assertEqual(instance, tagged_instance) |
| 299 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 300 | def test_manage_container_no_volt(self): |
| 301 | with self.assertRaises(Exception) as e: |
| 302 | self.policy.manage_container(self.tenant) |
| 303 | self.assertEqual(e.exception.message, "This VSG container has no volt") |
| 304 | |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 305 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 306 | @patch.object(MockVSGTenant, "save") |
| 307 | @patch.object(MockVSGTenant, "volt") |
| 308 | def test_manage_container_noinstance(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 309 | instance = MockInstance() |
| 310 | volt.s_tag=222 |
| 311 | volt.c_tag=111 |
| 312 | find_or_make_instance_for_s_tag.return_value = instance |
| 313 | self.policy.manage_container(self.tenant) |
| 314 | self.assertEqual(self.tenant.instance, instance) |
| 315 | tenant_save.assert_called() |
| 316 | |
| 317 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 318 | @patch.object(MockVSGTenant, "save") |
| 319 | @patch.object(MockVSGTenant, "volt") |
| 320 | def test_manage_container_hasinstance(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 321 | instance = MockInstance() |
| 322 | volt.s_tag=222 |
| 323 | volt.c_tag=111 |
| 324 | self.tenant.instance = instance |
| 325 | self.policy.manage_container(self.tenant) |
| 326 | find_or_make_instance_for_s_tag.assert_not_called() |
| 327 | self.assertEqual(self.tenant.instance, instance) |
| 328 | tenant_save.assert_not_called() |
| 329 | |
| 330 | @patch.object(VSGTenantPolicy, "find_or_make_instance_for_s_tag") |
| 331 | @patch.object(MockVSGTenant, "save") |
| 332 | @patch.object(MockVSGTenant, "volt") |
| 333 | def test_manage_container_deleted(self, volt, tenant_save, find_or_make_instance_for_s_tag): |
| 334 | self.tenant.deleted = True |
| 335 | self.policy.manage_container(self.tenant) |
| 336 | find_or_make_instance_for_s_tag.assert_not_called() |
| 337 | tenant_save.assert_not_called() |
| 338 | |
| 339 | @patch.object(MockPort, "save") |
| 340 | @patch.object(MockPort, "objects") |
| 341 | def test_find_or_make_port_noexist(self, port_objects, port_save): |
| 342 | instance = MockInstance(id=123) |
| 343 | network = MockInstance(id=456) |
| 344 | port_objects.filter.return_value = [] |
| 345 | port=self.policy.find_or_make_port(instance, network) |
| 346 | self.assertNotEqual(port, None) |
| 347 | port_save.assert_called() |
| 348 | |
| 349 | @patch.object(MockPort, "save") |
| 350 | @patch.object(MockPort, "objects") |
| 351 | def test_find_or_make_port_exists(self, port_objects, port_save): |
| 352 | someport = MockPort() |
| 353 | def mock_port_filter(network_id, instance_id): |
| 354 | if (network_id==456) and (instance_id==123): |
| 355 | return [someport] |
| 356 | return None |
| 357 | instance = MockInstance(id=123) |
| 358 | network = MockInstance(id=456) |
| 359 | port_objects.filter.side_effect = mock_port_filter |
| 360 | port=self.policy.find_or_make_port(instance, network) |
| 361 | self.assertEqual(port, someport) |
| 362 | port_save.assert_not_called() |
| 363 | |
| 364 | @patch.object(MockVSGServiceObjects, "get_items") |
| 365 | def test_get_lan_network_noexist(self, vsgservice_objects): |
| 366 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 367 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 368 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 369 | self.slice.networks = MockObjectList() |
| 370 | with self.assertRaises(Exception) as e: |
| 371 | self.policy.get_lan_network(self.tenant, None) |
| 372 | self.assertEqual(e.exception.message, "No lan_network") |
| 373 | |
| 374 | @patch.object(MockVSGServiceObjects, "get_items") |
| 375 | def test_get_lan_network(self, vsgservice_objects): |
| 376 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 377 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 378 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 379 | self.slice.networks = MockObjectList([self.priv_network]) |
| 380 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 381 | self.assertEqual(lan_network, self.priv_network) |
| 382 | |
| 383 | @patch.object(MockVSGServiceObjects, "get_items") |
| 384 | def test_get_lan_network_toomany(self, vsgservice_objects): |
| 385 | some_other_network = MockNetwork(name="mysite_test1_private", template=self.priv_template) |
| 386 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 387 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 388 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 389 | self.slice.networks = MockObjectList([self.priv_network, some_other_network]) |
| 390 | with self.assertRaises(Exception) as e: |
| 391 | lan_network = self.policy.get_lan_network(self.tenant, None) |
| 392 | self.assertEqual(e.exception.message, "The vSG slice should only have one non-management private network") |
| 393 | |
| 394 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 395 | def test_port_set_parameter_noparamexist(self, npt_objects): |
| 396 | npt_objects.return_value = [self.npt_stag] |
| 397 | port = MockPort() |
| 398 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 399 | self.assertNotEqual(MockNetworkParameter.objects.all(), []) |
| 400 | param = MockNetworkParameter.objects.first() |
| 401 | self.assertEqual(param.value, "123") |
| 402 | self.assertEqual(param.parameter, self.npt_stag) |
| 403 | |
| 404 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 405 | @patch.object(MockNetworkParameterObjects, "get_items") |
| 406 | def test_port_set_parameter_paramexist(self, np_objects, npt_objects): |
| 407 | port = MockPort(id=1) |
| 408 | np_orig = MockNetworkParameter(parameter_id=self.npt_stag.id, value="456", object_id=port.id, content_type=port.self_content_type_id) |
| 409 | np_objects.return_value = [np_orig] |
| 410 | npt_objects.return_value = [self.npt_stag] |
| 411 | self.policy.port_set_parameter(port, "s_tag", "123") |
| 412 | self.assertEqual(MockNetworkParameter.objects.count(), 1) |
| 413 | param = MockNetworkParameter.objects.first() |
| 414 | self.assertEqual(param.value, "123") |
| 415 | |
| 416 | @patch.object(MockNetworkParameterTypeObjects, "get_items") |
| 417 | @patch.object(MockNodeObjects, "get_items") |
| 418 | @patch.object(MockFlavorObjects, "get_items") |
| 419 | @patch.object(MockVSGServiceObjects, "get_items") |
| 420 | @patch.object(MockVSGTenant, "volt") |
| 421 | @patch.object(MockVSGTenant, "save") |
| 422 | @patch.object(VSGTenantPolicy, "get_image") |
| 423 | @patch.object(VSGTenantPolicy, "allocate_public_service_instance") |
| 424 | @patch.object(LeastLoadedNodeScheduler, "pick") |
| 425 | @patch.object(MockNode, "site_deployment") |
| 426 | @patch.object(MockInstance, "save") |
| 427 | @patch.object(MockInstance, "delete") |
| 428 | @patch.object(VSGTenantPolicy, "port_set_parameter") |
| 429 | def test_find_or_make_instance_for_s_tag(self, port_set_parameter, instance_delete, instance_save, site_deployment, |
| 430 | pick, get_psi, get_image, tenant_save, volt, |
| 431 | vsgservice_objects, flavor_objects, node_objects, npt_objects): |
| 432 | # setup mocks |
| 433 | vrtenant = MockVRouterTenant(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 434 | vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice])) |
| 435 | vsgservice_objects.return_value = [vsgservice] |
Scott Baker | 8a0841b | 2017-07-27 13:28:37 -0700 | [diff] [blame] | 436 | self.tenant.owner = vsgservice |
Scott Baker | d482116 | 2017-06-22 14:21:00 -0700 | [diff] [blame] | 437 | volt.s_tag=222 |
| 438 | volt.c_tag=111 |
| 439 | get_image.return_value = self.image |
| 440 | get_psi.return_value = vrtenant |
| 441 | pick.return_value = (self.node, None) |
| 442 | site_deployment.deployment = self.deployment |
| 443 | flavor_objects.return_value=[self.flavor] |
| 444 | node_objects.return_value=[self.node] |
| 445 | npt_objects.return_value=[self.npt_stag, self.npt_ctag, self.npt_neutron_port_name] |
| 446 | self.slice.networks = MockObjectList([self.priv_network]) |
| 447 | # done setup mocks |
| 448 | |
| 449 | # call the function under test |
| 450 | instance = self.policy.find_or_make_instance_for_s_tag(self.tenant, self.tenant.volt.s_tag) |
| 451 | |
| 452 | # make sure Instance was created |
| 453 | self.assertNotEqual(instance, None) |
| 454 | self.assertEqual(instance.creator.email, "testadmin@test.org") |
| 455 | self.assertEqual(instance.image.name, "trusty-server-multi-nic") |
| 456 | self.assertEqual(instance.flavor.name, "m1.small") |
| 457 | self.assertEqual(instance.isolation, "vm") |
| 458 | self.assertEqual(instance.node.hostname, "my.node.com") |
| 459 | self.assertEqual(instance.slice.name, "mysite_test1") |
| 460 | self.assertEqual(instance.parent, None) |
| 461 | instance_save.assert_called() |
| 462 | instance_delete.assert_not_called() |
| 463 | |
| 464 | # Access Network Port should have tags to c-tag and s-tag |
| 465 | port = MockPort.objects.first() |
| 466 | self.assertEqual(port.instance, instance) |
| 467 | self.assertEqual(port.network, self.priv_network) |
| 468 | port_set_parameter.assert_has_calls([mock.call(port, "c_tag", 111), |
| 469 | mock.call(port, "s_tag", 222), |
| 470 | mock.call(port, "neutron_port_name", "stag-222")]) |
| 471 | |
| 472 | # The instance should be tagged with the s-tag |
| 473 | tag = MockTag.objects.get(name="s_tag") |
| 474 | self.assertEqual(tag.value, "222") |
| 475 | self.assertEqual(tag.object_id, instance.id) |
| 476 | |
| 477 | # The instance should have a tag pointing to its vrouter |
| 478 | tag = MockTag.objects.get(name="vm_vrouter_tenant") |
| 479 | self.assertNotEqual(tag.value, vrtenant.id) |
| 480 | self.assertEqual(tag.object_id, instance.id) |
| 481 | |
| 482 | # Allocate_public_service_instance should have been called |
| 483 | get_psi.assert_called() |
| 484 | |
| 485 | @patch.object(VSGTenantPolicy, "allocate_public_service_instance") |
| 486 | def test_manage_vrouter(self, get_psi): |
| 487 | vrtenant = MockVRouterTenant(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06") |
| 488 | get_psi.return_value = vrtenant |
| 489 | |
| 490 | self.tenant.vrouter = None |
| 491 | |
| 492 | self.policy.manage_vrouter(self.tenant) |
| 493 | |
| 494 | get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant) |
| 495 | |
Scott Baker | 9674688 | 2017-06-09 14:12:15 -0700 | [diff] [blame] | 496 | if __name__ == '__main__': |
| 497 | unittest.main() |
| 498 | |