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