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