blob: 4fb4542ab2b56722770d193eeeb00ae061caa587 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -07001
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 Baker96746882017-06-09 14:12:15 -070017import unittest
Scott Bakerad310b92017-09-12 11:23:07 -070018from mock import patch, call
Scott Baker96746882017-06-09 14:12:15 -070019import mock
20
21import os, sys
22sys.path.append("../../..")
Scott Bakerd4821162017-06-22 14:21:00 -070023config = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/test_config.yaml")
Scott Baker96746882017-06-09 14:12:15 -070024from xosconfig import Config
25Config.init(config, 'synchronizer-config-schema.yaml')
26
27import synchronizers.new_base.modelaccessor
28
Scott Bakerd4821162017-06-22 14:21:00 -070029import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer
30import model_policy_vsgtenant
Scott Baker96746882017-06-09 14:12:15 -070031from model_policy_vsgtenant import VSGTenantPolicy
Scott Bakerd4821162017-06-22 14:21:00 -070032from synchronizers.new_base.model_policies.model_policy_tenantwithcontainer import LeastLoadedNodeScheduler
Scott Baker96746882017-06-09 14:12:15 -070033
Scott Bakerad310b92017-09-12 11:23:07 -070034# ---------------------------------------------------------------------------------------------------------------------
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
40AllMockObjectStores = []
Scott Bakerd4821162017-06-22 14:21:00 -070041
42class 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
76class 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
93class MockObject(object):
94 objects = None
95 id = None
Scott Bakerad310b92017-09-12 11:23:07 -070096 deleted = False
Scott Bakerd4821162017-06-22 14:21:00 -070097 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 Bakerad310b92017-09-12 11:23:07 -0700103 @property
104 def leaf_model(self):
105 return self
Scott Bakerd4821162017-06-22 14:21:00 -0700106 def save(self):
107 if self.objects:
108 self.objects.save(self)
109 def delete(self):
110 pass
111
112def get_MockObjectStore(x):
Scott Bakerad310b92017-09-12 11:23:07 -0700113 store = globals()["Mock%sObjects" % x]()
114 if not store in AllMockObjectStores:
115 AllMockObjectStores.append(store)
116 return store
Scott Bakerd4821162017-06-22 14:21:00 -0700117
118class MockFlavorObjects(MockObjectStore): pass
119class MockFlavor(MockObject):
120 objects = get_MockObjectStore("Flavor")
121 name = None
122
123class MockInstanceObjects(MockObjectStore): pass
124class MockInstance(MockObject):
125 objects = get_MockObjectStore("Instance")
126 name = None
127
128class MockDeploymentObjects(MockObjectStore): pass
129class MockDeployment(MockObject):
130 objects = get_MockObjectStore("Deployment")
131 name = None
132
133class MockUserObjects(MockObjectStore): pass
134class MockUser(MockObject):
135 objects = get_MockObjectStore("User")
136 email = None
137
138class MockSliceObjects(MockObjectStore): pass
139class MockSlice(MockObject):
140 objects = get_MockObjectStore("Slice")
141 name = None
142 default_node = None
143 networks = None
144
145class MockNodeObjects(MockObjectStore): pass
146class MockNode(MockObject):
147 objects = get_MockObjectStore("Node")
148 hostname = None
149 site_deployment = None
150
151class MockImageObjects(MockObjectStore): pass
152class MockImage(MockObject):
153 objects = get_MockObjectStore("Image")
154 name = None
155
156class MockTagObjects(MockObjectStore): pass
157class MockTag(MockObject):
158 objects = get_MockObjectStore("Tag")
159 name = None
160 value = None
161
162class MockNetworkTemplateObjects(MockObjectStore): pass
163class MockNetworkTemplate(MockObject):
164 objects = get_MockObjectStore("NetworkTemplate")
165 name = None
166 visibility = None
167
168class MockNetworkParameterTypeObjects(MockObjectStore): pass
169class MockNetworkParameterType(MockObject):
170 objects = get_MockObjectStore("NetworkParameterType")
171 name = None
172
173class MockNetworkParameterObjects(MockObjectStore): pass
174class MockNetworkParameter(MockObject):
175 objects = get_MockObjectStore("NetworkParameter")
176 value = None
177 parameter_id = None
178
179class MockNetworkObjects(MockObjectStore): pass
180class MockNetwork(MockObject):
181 objects = get_MockObjectStore("Network")
182 name = None
183 template = None
184
185class MockPortObjects(MockObjectStore): pass
186class MockPort(MockObject):
187 objects = get_MockObjectStore("Port")
188 name = None
189 def set_parameter(self, name, value):
190 pass
191
Scott Bakerad310b92017-09-12 11:23:07 -0700192class MockServiceInstanceObjects(MockObjectStore): pass
193class MockServiceInstance(MockObject):
194 objects = get_MockObjectStore("ServiceInstance")
195 def __init__(self, **kwargs):
196 super(MockServiceInstance, self).__init__(**kwargs)
197
198class MockServiceInstanceLinkObjects(MockObjectStore): pass
199class 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 Bakeradd58dd2017-08-23 15:56:58 -0700206class MockAddressManagerServiceInstanceObjects(MockObjectStore): pass
207class MockAddressManagerServiceInstance(MockObject):
208 objects = get_MockObjectStore("AddressManagerServiceInstance")
Scott Bakerd4821162017-06-22 14:21:00 -0700209 public_ip = None
210 public_mac = None
211 address_pool_id = None
212 def set_attribute(self, name, value):
213 pass
214
215class MockVoltTenantObjects(MockObjectStore): pass
216class MockVoltTenant(MockObject):
217 objects = get_MockObjectStore("VoltTenant")
218 c_tag = None
219 s_tag = None
220
221class MockVSGServiceObjects(MockObjectStore): pass
222class 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
230class MockVSGTenantObjects(MockObjectStore): pass
231class MockVSGTenant(MockObject):
232 objects = get_MockObjectStore("VSGTenant")
Scott Baker8a0841b2017-07-27 13:28:37 -0700233 owner = None
Scott Baker96746882017-06-09 14:12:15 -0700234 deleted = False
235 instance = None
Scott Bakerd4821162017-06-22 14:21:00 -0700236 creator = None
Scott Baker96746882017-06-09 14:12:15 -0700237 volt = None
Scott Bakerad310b92017-09-12 11:23:07 -0700238 link_deleted_count = 0
Scott Bakerd4821162017-06-22 14:21:00 -0700239 service_specific_attribute = {}
240
241 def get_image(self):
242 return None
Scott Baker96746882017-06-09 14:12:15 -0700243
Scott Bakerad310b92017-09-12 11:23:07 -0700244# ---------------------------------------------------------------------------------------------------------------------
245# End Model Policy Testing Framework
246# ---------------------------------------------------------------------------------------------------------------------
247
Scott Baker96746882017-06-09 14:12:15 -0700248class TestModelPolicyVsgTenant(unittest.TestCase):
249 def setUp(self):
Scott Bakerad310b92017-09-12 11:23:07 -0700250 # 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 Baker96746882017-06-09 14:12:15 -0700255 self.policy = VSGTenantPolicy()
256 self.tenant = MockVSGTenant()
Scott Bakerd4821162017-06-22 14:21:00 -0700257 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 Bakerad310b92017-09-12 11:23:07 -0700277 model_policy_vsgtenant.VSGTenant = MockVSGTenant
Scott Bakerd4821162017-06-22 14:21:00 -0700278 model_policy_vsgtenant.Node = MockNode
279 model_policy_vsgtenant.Port = MockPort
280 model_policy_vsgtenant.NetworkParameterType = MockNetworkParameterType
281 model_policy_vsgtenant.NetworkParameter = MockNetworkParameter
Scott Bakerad310b92017-09-12 11:23:07 -0700282 model_policy_vsgtenant.ServiceInstance = MockServiceInstance
283 model_policy_vsgtenant.AddressManagerServiceInstance = MockAddressManagerServiceInstance
284
285 MockTag.objects.item_list = []
Scott Bakerd4821162017-06-22 14:21:00 -0700286
287 @patch.object(VSGTenantPolicy, "manage_container")
Scott Bakeradd58dd2017-08-23 15:56:58 -0700288 @patch.object(VSGTenantPolicy, "manage_address_service_instance")
Scott Bakerd4821162017-06-22 14:21:00 -0700289 @patch.object(VSGTenantPolicy, "cleanup_orphans")
Scott Bakeradd58dd2017-08-23 15:56:58 -0700290 def test_handle_create(self, cleanup_orphans, manage_address_service_instance, manage_container):
Scott Bakerd4821162017-06-22 14:21:00 -0700291 self.policy.handle_create(self.tenant)
292 manage_container.assert_called_with(self.tenant)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700293 manage_address_service_instance.assert_called_with(self.tenant)
Scott Bakerd4821162017-06-22 14:21:00 -0700294 cleanup_orphans.assert_called_with(self.tenant)
295
296 @patch.object(VSGTenantPolicy, "manage_container")
Scott Bakeradd58dd2017-08-23 15:56:58 -0700297 @patch.object(VSGTenantPolicy, "manage_address_service_instance")
Scott Bakerd4821162017-06-22 14:21:00 -0700298 @patch.object(VSGTenantPolicy, "cleanup_orphans")
Scott Bakeradd58dd2017-08-23 15:56:58 -0700299 def test_handle_update(self, cleanup_orphans, manage_address_service_instance, manage_container):
Scott Bakerd4821162017-06-22 14:21:00 -0700300 self.policy.handle_create(self.tenant)
301 manage_container.assert_called_with(self.tenant)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700302 manage_address_service_instance.assert_called_with(self.tenant)
Scott Bakerd4821162017-06-22 14:21:00 -0700303 cleanup_orphans.assert_called_with(self.tenant)
304
Scott Bakeradd58dd2017-08-23 15:56:58 -0700305 @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 Bakerd4821162017-06-22 14:21:00 -0700309 self.policy.handle_delete(self.tenant)
Scott Bakerad310b92017-09-12 11:23:07 -0700310 # 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 Bakerd4821162017-06-22 14:21:00 -0700313
Scott Bakeradd58dd2017-08-23 15:56:58 -0700314 @patch.object(MockAddressManagerServiceInstance, "delete")
315 def test_handle_delete_asi_noexist(self, amsi_delete):
316 self.tenant.address_service_instance = None
Scott Bakerd4821162017-06-22 14:21:00 -0700317 self.policy.handle_delete(self.tenant)
Scott Bakeradd58dd2017-08-23 15:56:58 -0700318 amsi_delete.assert_not_called()
Scott Bakerd4821162017-06-22 14:21:00 -0700319
Scott Bakerad310b92017-09-12 11:23:07 -0700320 @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 Bakeradd58dd2017-08-23 15:56:58 -0700392 @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 Bakerad310b92017-09-12 11:23:07 -0700397 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 Bakeradd58dd2017-08-23 15:56:58 -0700402 amsi_delete.assert_called()
Scott Bakerd4821162017-06-22 14:21:00 -0700403
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 Baker96746882017-06-09 14:12:15 -0700418 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 Bakerd4821162017-06-22 14:21:00 -0700423 @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 Baker8a0841b2017-07-27 13:28:37 -0700486 self.tenant.owner = vsgservice
Scott Bakerd4821162017-06-22 14:21:00 -0700487 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 Baker8a0841b2017-07-27 13:28:37 -0700496 self.tenant.owner = vsgservice
Scott Bakerd4821162017-06-22 14:21:00 -0700497 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 Baker8a0841b2017-07-27 13:28:37 -0700506 self.tenant.owner = vsgservice
Scott Bakerd4821162017-06-22 14:21:00 -0700507 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 Bakeradd58dd2017-08-23 15:56:58 -0700551 vrtenant = MockAddressManagerServiceInstance(public_ip="1.2.3.4", public_mac="01:02:03:04:05:06")
Scott Bakerd4821162017-06-22 14:21:00 -0700552 vsgservice=MockVSGService(name="myvsgservice", id=1, slices=MockObjectList(initial=[self.slice]))
553 vsgservice_objects.return_value = [vsgservice]
Scott Baker8a0841b2017-07-27 13:28:37 -0700554 self.tenant.owner = vsgservice
Scott Bakerd4821162017-06-22 14:21:00 -0700555 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 Bakeradd58dd2017-08-23 15:56:58 -0700595 # The instance should have a tag pointing to its address_service_instance
Scott Bakerd4821162017-06-22 14:21:00 -0700596 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 Bakeradd58dd2017-08-23 15:56:58 -0700604 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 Bakerd4821162017-06-22 14:21:00 -0700606 get_psi.return_value = vrtenant
607
Scott Bakeradd58dd2017-08-23 15:56:58 -0700608 self.tenant.address_service_instance = None
Scott Bakerd4821162017-06-22 14:21:00 -0700609
Scott Bakeradd58dd2017-08-23 15:56:58 -0700610 self.policy.manage_address_service_instance(self.tenant)
Scott Bakerd4821162017-06-22 14:21:00 -0700611
612 get_psi.assert_called_with(address_pool_name="addresses_vsg", subscriber_tenant=self.tenant)
613
Scott Baker96746882017-06-09 14:12:15 -0700614if __name__ == '__main__':
615 unittest.main()
616