cinder/cinder/tests/unit/objects/test_consistencygroup.py
Gorka Eguileor 938cb0c5ab Move get_by_id to CinderObject
Currently each Versioned Object needs to implement its own get_by_id,
with this patch they don't need anymore, since it will be included in
the base class CinderObject and it will work for all the objects.

This will help for other things like having a refresh method or
conditional updates in the objects.

Related-Bug: #1490944
Related-Bug: #1238093
Related-Bug: #1490946
Related-Bug: #1469659
Change-Id: I355dc8eaefed93003533ee083f74acd1315f057e
2015-11-20 14:34:21 +01:00

104 lines
4.5 KiB
Python

# Copyright 2015 Yahoo Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from cinder import exception
from cinder import objects
from cinder.tests.unit import objects as test_objects
fake_consistencygroup = {
'id': '1',
'user_id': 'fake_user_id',
'project_id': 'fake_project_id',
'host': 'fake_host',
'availability_zone': 'fake_az',
'name': 'fake_name',
'description': 'fake_description',
'volume_type_id': 'fake_volume_type_id',
'status': 'creating',
'cgsnapshot_id': 'fake_id',
'source_cgid': None,
}
class TestConsistencyGroup(test_objects.BaseObjectsTestCase):
@mock.patch('cinder.db.sqlalchemy.api.consistencygroup_get',
return_value=fake_consistencygroup)
def test_get_by_id(self, consistencygroup_get):
consistencygroup = objects.ConsistencyGroup.get_by_id(self.context, 1)
self._compare(self, fake_consistencygroup, consistencygroup)
consistencygroup_get.assert_called_once_with(self.context, 1)
@mock.patch('cinder.db.sqlalchemy.api.model_query')
def test_get_by_id_no_existing_id(self, model_query):
model_query().filter_by().first.return_value = None
self.assertRaises(exception.ConsistencyGroupNotFound,
objects.ConsistencyGroup.get_by_id, self.context,
123)
@mock.patch('cinder.db.consistencygroup_create',
return_value=fake_consistencygroup)
def test_create(self, consistencygroup_create):
fake_cg = fake_consistencygroup.copy()
del fake_cg['id']
consistencygroup = objects.ConsistencyGroup(context=self.context,
**fake_cg)
consistencygroup.create()
self._compare(self, fake_consistencygroup, consistencygroup)
def test_create_with_id_except_exception(self, ):
consistencygroup = objects.ConsistencyGroup(context=self.context,
**{'id': 1})
self.assertRaises(exception.ObjectActionError, consistencygroup.create)
@mock.patch('cinder.db.consistencygroup_update')
def test_save(self, consistencygroup_update):
consistencygroup = objects.ConsistencyGroup._from_db_object(
self.context, objects.ConsistencyGroup(), fake_consistencygroup)
consistencygroup.status = 'active'
consistencygroup.save()
consistencygroup_update.assert_called_once_with(self.context,
consistencygroup.id,
{'status': 'active'})
@mock.patch('cinder.db.consistencygroup_destroy')
def test_destroy(self, consistencygroup_destroy):
consistencygroup = objects.ConsistencyGroup(context=self.context,
id='1')
consistencygroup.destroy()
self.assertTrue(consistencygroup_destroy.called)
admin_context = consistencygroup_destroy.call_args[0][0]
self.assertTrue(admin_context.is_admin)
class TestConsistencyGroupList(test_objects.BaseObjectsTestCase):
@mock.patch('cinder.db.consistencygroup_get_all',
return_value=[fake_consistencygroup])
def test_get_all(self, consistencygroup_get_all):
consistencygroups = objects.ConsistencyGroupList.get_all(self.context)
self.assertEqual(1, len(consistencygroups))
TestConsistencyGroup._compare(self, fake_consistencygroup,
consistencygroups[0])
@mock.patch('cinder.db.consistencygroup_get_all_by_project',
return_value=[fake_consistencygroup])
def test_get_all_by_project(self, consistencygroup_get_all_by_project):
consistencygroups = objects.ConsistencyGroupList.get_all_by_project(
self.context, self.project_id)
self.assertEqual(1, len(consistencygroups))
TestConsistencyGroup._compare(self, fake_consistencygroup,
consistencygroups[0])