From 9f68b7d493435948b0e1e0305300f18a6a6f5c86 Mon Sep 17 00:00:00 2001 From: Daniel Tadrzak Date: Thu, 30 Jul 2015 18:48:34 +0200 Subject: [PATCH] Cleanup for cinder tests with ConsistencyGroups Switch cinder tests to use ConsistencyGroup objects instead of directly calls to db. Additionally switch over from dict to object notation. Drivers tests need to be fixed in other patch. Partial-Implements: blueprint cinder-objects Change-Id: Ib3d4c791e095d89e7bc5e5e9a6b26e8433517ffb --- cinder/consistencygroup/api.py | 6 +- .../unit/api/contrib/test_cgsnapshots.py | 114 ++--- .../api/contrib/test_consistencygroups.py | 460 ++++++++---------- cinder/tests/unit/test_storwize_svc.py | 3 +- cinder/tests/unit/test_volume.py | 44 +- cinder/tests/unit/utils.py | 30 +- 6 files changed, 303 insertions(+), 354 deletions(-) diff --git a/cinder/consistencygroup/api.py b/cinder/consistencygroup/api.py index bed1cd9a8..52cc19162 100644 --- a/cinder/consistencygroup/api.py +++ b/cinder/consistencygroup/api.py @@ -205,9 +205,9 @@ class API(base.Base): kwargs['host'] = orig_cg.host if source_cg: - kwargs['volume_type_id'] = source_cg.get('volume_type_id') - kwargs['availability_zone'] = source_cg.get('availability_zone') - kwargs['host'] = source_cg.get('host') + kwargs['volume_type_id'] = source_cg.volume_type_id + kwargs['availability_zone'] = source_cg.availability_zone + kwargs['host'] = source_cg.host group = None try: diff --git a/cinder/tests/unit/api/contrib/test_cgsnapshots.py b/cinder/tests/unit/api/contrib/test_cgsnapshots.py index 49903cfa0..61266b6a4 100644 --- a/cinder/tests/unit/api/contrib/test_cgsnapshots.py +++ b/cinder/tests/unit/api/contrib/test_cgsnapshots.py @@ -66,12 +66,12 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id)[attrib_name] def test_show_cgsnapshot(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id) + consistencygroup_id=consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots/%s' % cgsnapshot_id) req.method = 'GET' @@ -91,16 +91,15 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_show_cgsnapshot_xml_content_type(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id) + consistencygroup_id=consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots/%s' % cgsnapshot_id) req.method = 'GET' @@ -116,8 +115,7 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_show_cgsnapshot_with_cgsnapshot_NotFound(self): req = webob.Request.blank('/v2/fake/cgsnapshots/9999') @@ -132,16 +130,16 @@ class CgsnapshotsAPITestCase(test.TestCase): res_dict['itemNotFound']['message']) def test_list_cgsnapshots_json(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id1 = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id) + consistencygroup_id=consistencygroup.id) cgsnapshot_id2 = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id) + consistencygroup_id=consistencygroup.id) cgsnapshot_id3 = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id) + consistencygroup_id=consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots') req.method = 'GET' @@ -171,20 +169,19 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id1) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_list_cgsnapshots_xml(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots') req.method = 'GET' @@ -211,20 +208,19 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id1) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_list_cgsnapshots_detail_json(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots/detail') req.method = 'GET' @@ -268,20 +264,19 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id1) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_list_cgsnapshots_detail_xml(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume(self.context, consistencygroup_id= - consistencygroup_id)['id'] + consistencygroup.id)['id'] cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id= - consistencygroup_id) + consistencygroup.id) req = webob.Request.blank('/v2/fake/cgsnapshots/detail') req.method = 'GET' @@ -328,23 +323,21 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id1) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() @mock.patch( 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') def test_create_cgsnapshot_json(self, mock_validate): cgsnapshot_id = "1" - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) utils.create_volume( - self.context, - consistencygroup_id=consistencygroup_id) + self.context, consistencygroup_id=consistencygroup.id) body = {"cgsnapshot": {"name": "cg1", "description": "CG Snapshot 1", - "consistencygroup_id": consistencygroup_id}} + "consistencygroup_id": consistencygroup.id}} req = webob.Request.blank('/v2/fake/cgsnapshots') req.method = 'POST' req.headers['Content-Type'] = 'application/json' @@ -358,6 +351,7 @@ class CgsnapshotsAPITestCase(test.TestCase): self.assertTrue(mock_validate.called) db.cgsnapshot_destroy(context.get_admin_context(), cgsnapshot_id) + consistencygroup.destroy() def test_create_cgsnapshot_with_no_body(self): # omit body from the request @@ -379,15 +373,14 @@ class CgsnapshotsAPITestCase(test.TestCase): side_effect=exception.InvalidCgSnapshot( reason='invalid cgsnapshot')) def test_create_with_invalid_cgsnapshot(self, mock_create_cgsnapshot): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) utils.create_volume( - self.context, - consistencygroup_id=consistencygroup_id) + self.context, consistencygroup_id=consistencygroup.id) body = {"cgsnapshot": {"name": "cg1", "description": "CG Snapshot 1", - "consistencygroup_id": consistencygroup_id}} + "consistencygroup_id": consistencygroup.id}} req = webob.Request.blank('/v2/fake/cgsnapshots') req.body = json.dumps(body) req.method = 'POST' @@ -399,20 +392,20 @@ class CgsnapshotsAPITestCase(test.TestCase): self.assertEqual(400, res_dict['badRequest']['code']) self.assertEqual('Invalid CgSnapshot: invalid cgsnapshot', res_dict['badRequest']['message']) + consistencygroup.destroy() @mock.patch.object(consistencygroupAPI.API, 'create_cgsnapshot', side_effect=exception.CgSnapshotNotFound( cgsnapshot_id='invalid_id')) def test_create_with_cgsnapshot_not_found(self, mock_create_cgsnapshot): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) utils.create_volume( - self.context, - consistencygroup_id=consistencygroup_id) + self.context, consistencygroup_id=consistencygroup.id) body = {"cgsnapshot": {"name": "cg1", "description": "CG Snapshot 1", - "consistencygroup_id": consistencygroup_id}} + "consistencygroup_id": consistencygroup.id}} req = webob.Request.blank('/v2/fake/cgsnapshots') req.method = 'POST' @@ -425,14 +418,15 @@ class CgsnapshotsAPITestCase(test.TestCase): self.assertEqual(404, res_dict['itemNotFound']['code']) self.assertEqual('CgSnapshot invalid_id could not be found.', res_dict['itemNotFound']['message']) + consistencygroup.destroy() def test_delete_cgsnapshot_available(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume( self.context, - consistencygroup_id=consistencygroup_id)['id'] + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id, + consistencygroup_id=consistencygroup.id, status='available') req = webob.Request.blank('/v2/fake/cgsnapshots/%s' % cgsnapshot_id) @@ -448,8 +442,7 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_delete_cgsnapshot_with_cgsnapshot_NotFound(self): req = webob.Request.blank('/v2/fake/cgsnapshots/9999') @@ -464,13 +457,11 @@ class CgsnapshotsAPITestCase(test.TestCase): res_dict['itemNotFound']['message']) def test_delete_cgsnapshot_with_Invalidcgsnapshot(self): - consistencygroup_id = utils.create_consistencygroup(self.context)['id'] + consistencygroup = utils.create_consistencygroup(self.context) volume_id = utils.create_volume( - self.context, - consistencygroup_id=consistencygroup_id)['id'] + self.context, consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = self._create_cgsnapshot( - consistencygroup_id=consistencygroup_id, - status='invalid') + consistencygroup_id=consistencygroup.id, status='invalid') req = webob.Request.blank('/v2/fake/cgsnapshots/%s' % cgsnapshot_id) req.method = 'DELETE' @@ -487,5 +478,4 @@ class CgsnapshotsAPITestCase(test.TestCase): cgsnapshot_id) db.volume_destroy(context.get_admin_context(), volume_id) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() diff --git a/cinder/tests/unit/api/contrib/test_consistencygroups.py b/cinder/tests/unit/api/contrib/test_consistencygroups.py index 4fd83a22f..680ae17cc 100644 --- a/cinder/tests/unit/api/contrib/test_consistencygroups.py +++ b/cinder/tests/unit/api/contrib/test_consistencygroups.py @@ -42,9 +42,11 @@ class ConsistencyGroupsAPITestCase(test.TestCase): def setUp(self): super(ConsistencyGroupsAPITestCase, self).setUp() self.cg_api = cinder.consistencygroup.API() + self.ctxt = context.RequestContext('fake', 'fake', auth_token=True) - @staticmethod def _create_consistencygroup( + self, + ctxt=None, name='test_consistencygroup', description='this is a test consistency group', volume_type_id='123456', @@ -52,28 +54,23 @@ class ConsistencyGroupsAPITestCase(test.TestCase): host='fakehost', status='creating'): """Create a consistency group object.""" - consistencygroup = {} - consistencygroup['user_id'] = 'fake' - consistencygroup['project_id'] = 'fake' - consistencygroup['availability_zone'] = availability_zone - consistencygroup['name'] = name - consistencygroup['description'] = description - consistencygroup['volume_type_id'] = volume_type_id - consistencygroup['host'] = host - consistencygroup['status'] = status - return db.consistencygroup_create( - context.get_admin_context(), - consistencygroup)['id'] - - @staticmethod - def _get_consistencygroup_attrib(consistencygroup_id, attrib_name): - return db.consistencygroup_get(context.get_admin_context(), - consistencygroup_id)[attrib_name] + ctxt = ctxt or self.ctxt + consistencygroup = objects.ConsistencyGroup(ctxt) + consistencygroup.user_id = 'fake' + consistencygroup.project_id = 'fake' + consistencygroup.availability_zone = availability_zone + consistencygroup.name = name + consistencygroup.description = description + consistencygroup.volume_type_id = volume_type_id + consistencygroup.host = host + consistencygroup.status = status + consistencygroup.create() + return consistencygroup def test_show_consistencygroup(self): - consistencygroup_id = self._create_consistencygroup() + consistencygroup = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups/%s' % - consistencygroup_id) + consistencygroup.id) req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app()) @@ -89,25 +86,22 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual('creating', res_dict['consistencygroup']['status']) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_show_consistencygroup_xml_content_type(self): - consistencygroup_id = self._create_consistencygroup() + consistencygroup = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups/%s' % - consistencygroup_id) + consistencygroup.id) req.method = 'GET' req.headers['Content-Type'] = 'application/xml' req.headers['Accept'] = 'application/xml' res = req.get_response(fakes.wsgi_app()) self.assertEqual(200, res.status_int) dom = minidom.parseString(res.body) - consistencygroup = dom.getElementsByTagName('consistencygroup') - name = consistencygroup.item(0).getAttribute('name') + consistencygroups = dom.getElementsByTagName('consistencygroup') + name = consistencygroups.item(0).getAttribute('name') self.assertEqual("test_consistencygroup", name.strip()) - db.consistencygroup_destroy( - context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_show_consistencygroup_with_consistencygroup_NotFound(self): req = webob.Request.blank('/v2/fake/consistencygroups/9999') @@ -122,9 +116,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['itemNotFound']['message']) def test_list_consistencygroups_json(self): - consistencygroup_id1 = self._create_consistencygroup() - consistencygroup_id2 = self._create_consistencygroup() - consistencygroup_id3 = self._create_consistencygroup() + consistencygroup1 = self._create_consistencygroup() + consistencygroup2 = self._create_consistencygroup() + consistencygroup3 = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'GET' @@ -133,30 +127,27 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict = json.loads(res.body) self.assertEqual(200, res.status_int) - self.assertEqual(consistencygroup_id1, + self.assertEqual(consistencygroup1.id, res_dict['consistencygroups'][0]['id']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][0]['name']) - self.assertEqual(consistencygroup_id2, + self.assertEqual(consistencygroup2.id, res_dict['consistencygroups'][1]['id']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][1]['name']) - self.assertEqual(consistencygroup_id3, + self.assertEqual(consistencygroup3.id, res_dict['consistencygroups'][2]['id']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][2]['name']) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id3) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id2) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id1) + consistencygroup1.destroy() + consistencygroup2.destroy() + consistencygroup3.destroy() def test_list_consistencygroups_xml(self): - consistencygroup_id1 = self._create_consistencygroup() - consistencygroup_id2 = self._create_consistencygroup() - consistencygroup_id3 = self._create_consistencygroup() + consistencygroup1 = self._create_consistencygroup() + consistencygroup2 = self._create_consistencygroup() + consistencygroup3 = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups') req.method = 'GET' @@ -168,24 +159,21 @@ class ConsistencyGroupsAPITestCase(test.TestCase): dom = minidom.parseString(res.body) consistencygroup_list = dom.getElementsByTagName('consistencygroup') - self.assertEqual(consistencygroup_id1, + self.assertEqual(consistencygroup1.id, consistencygroup_list.item(0).getAttribute('id')) - self.assertEqual(consistencygroup_id2, + self.assertEqual(consistencygroup2.id, consistencygroup_list.item(1).getAttribute('id')) - self.assertEqual(consistencygroup_id3, + self.assertEqual(consistencygroup3.id, consistencygroup_list.item(2).getAttribute('id')) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id3) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id2) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id1) + consistencygroup3.destroy() + consistencygroup2.destroy() + consistencygroup1.destroy() def test_list_consistencygroups_detail_json(self): - consistencygroup_id1 = self._create_consistencygroup() - consistencygroup_id2 = self._create_consistencygroup() - consistencygroup_id3 = self._create_consistencygroup() + consistencygroup1 = self._create_consistencygroup() + consistencygroup2 = self._create_consistencygroup() + consistencygroup3 = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups/detail') req.method = 'GET' @@ -201,7 +189,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['consistencygroups'][0]['description']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][0]['name']) - self.assertEqual(consistencygroup_id1, + self.assertEqual(consistencygroup1.id, res_dict['consistencygroups'][0]['id']) self.assertEqual('creating', res_dict['consistencygroups'][0]['status']) @@ -212,7 +200,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['consistencygroups'][1]['description']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][1]['name']) - self.assertEqual(consistencygroup_id2, + self.assertEqual(consistencygroup2.id, res_dict['consistencygroups'][1]['id']) self.assertEqual('creating', res_dict['consistencygroups'][1]['status']) @@ -223,22 +211,19 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['consistencygroups'][2]['description']) self.assertEqual('test_consistencygroup', res_dict['consistencygroups'][2]['name']) - self.assertEqual(consistencygroup_id3, + self.assertEqual(consistencygroup3.id, res_dict['consistencygroups'][2]['id']) self.assertEqual('creating', res_dict['consistencygroups'][2]['status']) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id3) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id2) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id1) + consistencygroup1.destroy() + consistencygroup2.destroy() + consistencygroup3.destroy() def test_list_consistencygroups_detail_xml(self): - consistencygroup_id1 = self._create_consistencygroup() - consistencygroup_id2 = self._create_consistencygroup() - consistencygroup_id3 = self._create_consistencygroup() + consistencygroup1 = self._create_consistencygroup() + consistencygroup2 = self._create_consistencygroup() + consistencygroup3 = self._create_consistencygroup() req = webob.Request.blank('/v2/fake/consistencygroups/detail') req.method = 'GET' @@ -260,7 +245,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'test_consistencygroup', consistencygroup_detail.item(0).getAttribute('name')) self.assertEqual( - consistencygroup_id1, + consistencygroup1.id, consistencygroup_detail.item(0).getAttribute('id')) self.assertEqual( 'creating', @@ -276,7 +261,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'test_consistencygroup', consistencygroup_detail.item(1).getAttribute('name')) self.assertEqual( - consistencygroup_id2, + consistencygroup2.id, consistencygroup_detail.item(1).getAttribute('id')) self.assertEqual( 'creating', @@ -292,18 +277,15 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'test_consistencygroup', consistencygroup_detail.item(2).getAttribute('name')) self.assertEqual( - consistencygroup_id3, + consistencygroup3.id, consistencygroup_detail.item(2).getAttribute('id')) self.assertEqual( 'creating', consistencygroup_detail.item(2).getAttribute('status')) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id3) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id2) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id1) + consistencygroup3.destroy() + consistencygroup2.destroy() + consistencygroup1.destroy() @mock.patch( 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') @@ -330,7 +312,10 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertIn('id', res_dict['consistencygroup']) self.assertTrue(mock_validate.called) - db.consistencygroup_destroy(context.get_admin_context(), group_id) + group_id = res_dict['consistencygroup']['id'] + cg = objects.ConsistencyGroup.get_by_id(context.get_admin_context(), + group_id) + cg.destroy() def test_create_consistencygroup_with_no_body(self): # omit body from the request @@ -349,22 +334,21 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['badRequest']['message']) def test_delete_consistencygroup_available(self): - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": True}} req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) + consistencygroup = objects.ConsistencyGroup.get_by_id( + self.ctxt, consistencygroup.id) self.assertEqual(202, res.status_int) - self.assertEqual('deleting', - self._get_consistencygroup_attrib(consistencygroup_id, - 'status')) + self.assertEqual('deleting', consistencygroup.status) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_delete_consistencygroup_with_consistencygroup_NotFound(self): req = webob.Request.blank('/v2/fake/consistencygroups/9999/delete') @@ -380,9 +364,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res_dict['itemNotFound']['message']) def test_delete_consistencygroup_with_Invalidconsistencygroup(self): - consistencygroup_id = self._create_consistencygroup(status='invalid') + consistencygroup = self._create_consistencygroup(status='invalid') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": False}} @@ -396,15 +380,14 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'available or error, but current status is: invalid')) self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(context.get_admin_context(), - consistencygroup_id) + consistencygroup.destroy() def test_delete_consistencygroup_no_host(self): - consistencygroup_id = self._create_consistencygroup( + consistencygroup = self._create_consistencygroup( host=None, status='error') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": True}} @@ -412,14 +395,13 @@ class ConsistencyGroupsAPITestCase(test.TestCase): res = req.get_response(fakes.wsgi_app()) self.assertEqual(202, res.status_int) - cg = db.consistencygroup_get( + cg = objects.ConsistencyGroup.get_by_id( context.get_admin_context(read_deleted='yes'), - consistencygroup_id) - self.assertEqual('deleted', cg['status']) - self.assertIsNone(cg['host']) + consistencygroup.id) + self.assertEqual('deleted', cg.status) + self.assertIsNone(cg.host) def test_create_delete_consistencygroup_update_quota(self): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) name = 'mycg' description = 'consistency group 1' fake_type = {'id': '1', 'name': 'fake_type'} @@ -431,25 +413,26 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.stubs.Set(self.cg_api, 'update_quota', mock.Mock()) - cg = self.cg_api.create(ctxt, name, description, fake_type['name']) + cg = self.cg_api.create(self.ctxt, name, description, + fake_type['name']) self.cg_api.update_quota.assert_called_once_with( - ctxt, cg, 1) - self.assertEqual('creating', cg['status']) - self.assertIsNone(cg['host']) + self.ctxt, cg, 1) + self.assertEqual('creating', cg.status) + self.assertIsNone(cg.host) self.cg_api.update_quota.reset_mock() cg.status = 'error' - self.cg_api.delete(ctxt, cg) + self.cg_api.delete(self.ctxt, cg) self.cg_api.update_quota.assert_called_once_with( - ctxt, cg, -1, ctxt.project_id) + self.ctxt, cg, -1, self.ctxt.project_id) cg = objects.ConsistencyGroup.get_by_id( context.get_admin_context(read_deleted='yes'), cg.id) - self.assertEqual('deleted', cg['status']) + self.assertEqual('deleted', cg.status) def test_delete_consistencygroup_with_invalid_body(self): - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"invalid_request_element": {"force": False}} @@ -459,9 +442,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res.status_int) def test_delete_consistencygroup_with_invalid_force_value_in_body(self): - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": "abcd"}} @@ -471,9 +454,9 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res.status_int) def test_delete_consistencygroup_with_empty_force_value_in_body(self): - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/delete' % - consistencygroup_id) + consistencygroup.id) req.method = 'POST' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"force": ""}} @@ -504,36 +487,33 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') def test_update_consistencygroup_success(self, mock_validate): volume_type_id = '123456' - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available', - host='test_host') + consistencygroup = self._create_consistencygroup(status='available', + host='test_host') remove_volume_id = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=volume_type_id, - consistencygroup_id=consistencygroup_id)['id'] + consistencygroup_id=consistencygroup.id)['id'] remove_volume_id2 = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=volume_type_id, - consistencygroup_id=consistencygroup_id)['id'] + consistencygroup_id=consistencygroup.id)['id'] - self.assertEqual('available', - self._get_consistencygroup_attrib(consistencygroup_id, - 'status')) + self.assertEqual('available', consistencygroup.status) - cg_volumes = db.volume_get_all_by_group(ctxt.elevated(), - consistencygroup_id) + cg_volumes = db.volume_get_all_by_group(self.ctxt.elevated(), + consistencygroup.id) cg_vol_ids = [cg_vol['id'] for cg_vol in cg_volumes] self.assertIn(remove_volume_id, cg_vol_ids) self.assertIn(remove_volume_id2, cg_vol_ids) add_volume_id = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=volume_type_id)['id'] add_volume_id2 = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=volume_type_id)['id'] req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' name = 'newcg' @@ -547,19 +527,19 @@ class ConsistencyGroupsAPITestCase(test.TestCase): req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) + consistencygroup = objects.ConsistencyGroup.get_by_id( + self.ctxt, consistencygroup.id) self.assertEqual(202, res.status_int) - self.assertEqual('updating', - self._get_consistencygroup_attrib(consistencygroup_id, - 'status')) self.assertTrue(mock_validate.called) + self.assertEqual('updating', consistencygroup.status) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_add_volume_not_found(self): - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(ctxt=self.ctxt, + status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"name": None, @@ -575,16 +555,16 @@ class ConsistencyGroupsAPITestCase(test.TestCase): msg = (_("Invalid volume: Cannot add volume fake-volume-uuid " "to consistency group %(group_id)s because volume cannot " "be found.") % - {'group_id': consistencygroup_id}) + {'group_id': consistencygroup.id}) self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_remove_volume_not_found(self): - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(ctxt=self.ctxt, + status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"name": None, @@ -600,16 +580,16 @@ class ConsistencyGroupsAPITestCase(test.TestCase): msg = (_("Invalid volume: Cannot remove volume fake-volume-uuid " "from consistency group %(group_id)s because it is not " "in the group.") % - {'group_id': consistencygroup_id}) + {'group_id': consistencygroup.id}) self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_empty_parameters(self): - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(ctxt=self.ctxt, + status='available') req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"name": "", @@ -625,18 +605,18 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual('Name has a minimum character requirement of 1.', res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_add_volume_invalid_state(self): volume_type_id = '123456' - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(ctxt=self.ctxt, + status='available') add_volume_id = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=volume_type_id, status='wrong_status')['id'] req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' add_volumes = add_volume_id @@ -655,21 +635,21 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "invalid state: %(status)s. Valid states are: ('available', " "'in-use').") % {'volume_id': add_volume_id, - 'group_id': consistencygroup_id, + 'group_id': consistencygroup.id, 'status': 'wrong_status'}) self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_add_volume_invalid_volume_type(self): - ctxt = context.RequestContext('fake', 'fake') - consistencygroup_id = self._create_consistencygroup(status='available') + consistencygroup = self._create_consistencygroup(ctxt=self.ctxt, + status='available') wrong_type = 'wrong-volume-type-id' add_volume_id = utils.create_volume( - ctxt, + self.ctxt, volume_type_id=wrong_type)['id'] req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' add_volumes = add_volume_id @@ -687,19 +667,18 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "to consistency group %(group_id)s because volume type " "%(volume_type)s is not supported by the group.") % {'volume_id': add_volume_id, - 'group_id': consistencygroup_id, + 'group_id': consistencygroup.id, 'volume_type': wrong_type}) self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() def test_update_consistencygroup_invalid_state(self): - ctxt = context.RequestContext('fake', 'fake') wrong_status = 'wrong_status' - consistencygroup_id = self._create_consistencygroup( - status=wrong_status) + consistencygroup = self._create_consistencygroup(status=wrong_status, + ctxt=self.ctxt) req = webob.Request.blank('/v2/fake/consistencygroups/%s/update' % - consistencygroup_id) + consistencygroup.id) req.method = 'PUT' req.headers['Content-Type'] = 'application/json' body = {"consistencygroup": {"name": "new name", @@ -716,23 +695,22 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "available, but current status is: %s.") % wrong_status self.assertEqual(msg, res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + consistencygroup.destroy() @mock.patch( 'cinder.api.openstack.wsgi.Controller.validate_name_and_description') def test_create_consistencygroup_from_src(self, mock_validate): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup(ctxt)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = utils.create_cgsnapshot( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] snapshot_id = utils.create_snapshot( - ctxt, + self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] @@ -754,27 +732,28 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(test_cg_name, res_dict['consistencygroup']['name']) self.assertTrue(mock_validate.called) - db.consistencygroup_destroy(ctxt.elevated(), - res_dict['consistencygroup']['id']) - db.snapshot_destroy(ctxt.elevated(), snapshot_id) - db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + cg_ref = objects.ConsistencyGroup.get_by_id( + self.ctxt.elevated(), res_dict['consistencygroup']['id']) + + cg_ref.destroy() + db.snapshot_destroy(self.ctxt.elevated(), snapshot_id) + db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() def test_create_consistencygroup_from_src_cg(self): self.mock_object(volume_api.API, "create", stubs.stub_volume_create) - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - source_cgid = utils.create_consistencygroup(ctxt)['id'] + source_cg = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=source_cgid)['id'] + self.ctxt, + consistencygroup_id=source_cg.id)['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", - "source_cgid": source_cgid}} + "source_cgid": source_cg.id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' @@ -786,24 +765,24 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertIn('id', res_dict['consistencygroup']) self.assertEqual(test_cg_name, res_dict['consistencygroup']['name']) - db.consistencygroup_destroy(ctxt.elevated(), - res_dict['consistencygroup']['id']) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), source_cgid) + cg = objects.ConsistencyGroup.get_by_id( + self.ctxt, res_dict['consistencygroup']['id']) + cg.destroy + db.volume_destroy(self.ctxt.elevated(), volume_id) + source_cg.destroy() def test_create_consistencygroup_from_src_both_snap_cg(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup(ctxt)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = utils.create_cgsnapshot( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] snapshot_id = utils.create_snapshot( - ctxt, + self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] @@ -814,7 +793,7 @@ class ConsistencyGroupsAPITestCase(test.TestCase): "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id, "source_cgid": - consistencygroup_id}} + consistencygroup.id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' @@ -826,10 +805,10 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) - db.snapshot_destroy(ctxt.elevated(), snapshot_id) - db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + db.snapshot_destroy(self.ctxt.elevated(), snapshot_id) + db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() def test_create_consistencygroup_from_src_invalid_body(self): name = 'cg1' @@ -865,18 +844,15 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertIsNotNone(res_dict['badRequest']['message']) def test_create_consistencygroup_from_src_no_host(self): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup( - ctxt, - host=None)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt, host=None) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = utils.create_cgsnapshot( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] snapshot_id = utils.create_snapshot( - ctxt, + self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] @@ -899,21 +875,19 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'group') self.assertIn(msg, res_dict['badRequest']['message']) - db.snapshot_destroy(ctxt.elevated(), snapshot_id) - db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + db.snapshot_destroy(self.ctxt.elevated(), snapshot_id) + db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() def test_create_consistencygroup_from_src_cgsnapshot_empty(self): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup( - ctxt)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = utils.create_cgsnapshot( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, @@ -931,20 +905,18 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) - db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() def test_create_consistencygroup_from_src_source_cg_empty(self): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - source_cgid = utils.create_consistencygroup( - ctxt)['id'] + source_cg = utils.create_consistencygroup(self.ctxt) test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", - "source_cgid": source_cgid}} + "source_cgid": source_cg.id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' @@ -956,15 +928,13 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) - db.consistencygroup_destroy(ctxt.elevated(), source_cgid) + source_cg.destroy() def test_create_consistencygroup_from_src_cgsnapshot_notfound(self): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup( - ctxt)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, @@ -982,8 +952,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(404, res_dict['itemNotFound']['code']) self.assertIsNotNone(res_dict['itemNotFound']['message']) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() def test_create_consistencygroup_from_src_source_cg_notfound(self): test_cg_name = 'test cg' @@ -1007,16 +977,15 @@ class ConsistencyGroupsAPITestCase(test.TestCase): 'Create volume failed.')) def test_create_consistencygroup_from_src_cgsnapshot_create_volume_failed( self, mock_create): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - consistencygroup_id = utils.create_consistencygroup(ctxt)['id'] + consistencygroup = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] cgsnapshot_id = utils.create_cgsnapshot( - ctxt, - consistencygroup_id=consistencygroup_id)['id'] + self.ctxt, + consistencygroup_id=consistencygroup.id)['id'] snapshot_id = utils.create_snapshot( - ctxt, + self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] @@ -1038,27 +1007,26 @@ class ConsistencyGroupsAPITestCase(test.TestCase): msg = _("Create volume failed.") self.assertEqual(msg, res_dict['badRequest']['message']) - db.snapshot_destroy(ctxt.elevated(), snapshot_id) - db.cgsnapshot_destroy(ctxt.elevated(), cgsnapshot_id) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), consistencygroup_id) + db.snapshot_destroy(self.ctxt.elevated(), snapshot_id) + db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id) + db.volume_destroy(self.ctxt.elevated(), volume_id) + consistencygroup.destroy() @mock.patch.object(volume_api.API, 'create', side_effect=exception.CinderException( 'Create volume failed.')) def test_create_consistencygroup_from_src_cg_create_volume_failed( self, mock_create): - ctxt = context.RequestContext('fake', 'fake', auth_token=True) - source_cgid = utils.create_consistencygroup(ctxt)['id'] + source_cg = utils.create_consistencygroup(self.ctxt) volume_id = utils.create_volume( - ctxt, - consistencygroup_id=source_cgid)['id'] + self.ctxt, + consistencygroup_id=source_cg.id)['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", - "source_cgid": source_cgid}} + "source_cgid": source_cg.id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' @@ -1070,5 +1038,5 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) - db.volume_destroy(ctxt.elevated(), volume_id) - db.consistencygroup_destroy(ctxt.elevated(), source_cgid) + db.volume_destroy(self.ctxt.elevated(), volume_id) + source_cg.destroy() diff --git a/cinder/tests/unit/test_storwize_svc.py b/cinder/tests/unit/test_storwize_svc.py index b812bcd28..828a1d109 100644 --- a/cinder/tests/unit/test_storwize_svc.py +++ b/cinder/tests/unit/test_storwize_svc.py @@ -3502,7 +3502,8 @@ class StorwizeSVCDriverTestCase(test.TestCase): def test_storwize_consistency_group_snapshot(self): cg_type = self._create_consistency_group_volume_type() - + self.ctxt.user_id = 'fake_user_id' + self.ctxt.project_id = 'fake_project_id' cg = self._create_consistencygroup_in_db(volume_type_id=cg_type['id']) model_update = self.driver.create_consistencygroup(self.ctxt, cg) diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index ff10e08ae..fb538d240 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -4675,14 +4675,13 @@ class VolumeTestCase(BaseVolumeTestCase): self.assertDictMatch(expected, msg['payload']) self.assertEqual( group.id, - db.consistencygroup_get(context.get_admin_context(), - group.id).id) + objects.ConsistencyGroup.get_by_id(context.get_admin_context(), + group.id).id) self.volume.delete_consistencygroup(self.context, group) - cg = db.consistencygroup_get( - context.get_admin_context(read_deleted='yes'), - group.id) - self.assertEqual('deleted', cg['status']) + cg = objects.ConsistencyGroup.get_by_id( + context.get_admin_context(read_deleted='yes'), group.id) + self.assertEqual('deleted', cg.status) self.assertEqual(4, len(self.notifier.notifications), self.notifier.notifications) msg = self.notifier.notifications[2] @@ -4693,7 +4692,7 @@ class VolumeTestCase(BaseVolumeTestCase): expected['status'] = 'deleted' self.assertDictMatch(expected, msg['payload']) self.assertRaises(exception.NotFound, - db.consistencygroup_get, + objects.ConsistencyGroup.get_by_id, self.context, group.id) @@ -4714,7 +4713,6 @@ class VolumeTestCase(BaseVolumeTestCase): self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2') - group = objects.ConsistencyGroup.get_by_id(self.context, group['id']) self.volume.create_consistencygroup(self.context, group) volume = tests_utils.create_volume( @@ -4819,14 +4817,12 @@ class VolumeTestCase(BaseVolumeTestCase): availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', status='available') - group_id = group['id'] volume = tests_utils.create_volume( self.context, - consistencygroup_id=group_id, + consistencygroup_id=group.id, status='available', host=CONF.host, size=1) - group = objects.ConsistencyGroup.get_by_id(self.context, group['id']) volume_id = volume['id'] cgsnapshot_returns = self._create_cgsnapshot(group.id, volume_id) cgsnapshot_id = cgsnapshot_returns[0]['id'] @@ -4838,7 +4834,6 @@ class VolumeTestCase(BaseVolumeTestCase): availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', cgsnapshot_id=cgsnapshot_id) - group2 = objects.ConsistencyGroup.get_by_id(self.context, group2.id) volume2 = tests_utils.create_volume( self.context, consistencygroup_id=group2.id, @@ -4895,7 +4890,7 @@ class VolumeTestCase(BaseVolumeTestCase): context.get_admin_context(read_deleted='yes'), group2.id) self.assertEqual('deleted', cg2.status) self.assertRaises(exception.NotFound, - db.consistencygroup_get, + objects.ConsistencyGroup.get_by_id, self.context, group2.id) @@ -4904,8 +4899,7 @@ class VolumeTestCase(BaseVolumeTestCase): self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2', - source_cgid=group_id) - group3 = objects.ConsistencyGroup.get_by_id(self.context, group3.id) + source_cgid=group.id) volume3 = tests_utils.create_volume( self.context, consistencygroup_id=group3.id, @@ -4920,7 +4914,7 @@ class VolumeTestCase(BaseVolumeTestCase): self.assertEqual('available', cg3.status) self.assertEqual(group3.id, cg3.id) - self.assertEqual(group_id, cg3.source_cgid) + self.assertEqual(group.id, cg3.source_cgid) self.assertIsNone(cg3.cgsnapshot_id) self.volume.delete_cgsnapshot(self.context, cgsnapshot_id) @@ -5060,17 +5054,12 @@ class VolumeTestCase(BaseVolumeTestCase): self.context, availability_zone=CONF.storage_availability_zone, volume_type='type1,type2') - group = objects.ConsistencyGroup.get_by_id(self.context, group['id']) volume = tests_utils.create_volume( self.context, consistencygroup_id=group.id, **self.volume_params) volume_id = volume['id'] self.volume.create_volume(self.context, volume_id) - cgsnapshot = tests_utils.create_cgsnapshot( - self.context, - consistencygroup_id=group.id) - cgsnapshot_id = cgsnapshot['id'] if len(self.notifier.notifications) > 2: self.assertFalse(self.notifier.notifications[2]) @@ -5159,7 +5148,6 @@ class VolumeTestCase(BaseVolumeTestCase): availability_zone=CONF.storage_availability_zone, volume_type='type1,type2') - group = objects.ConsistencyGroup.get_by_id(self.context, group['id']) volume = tests_utils.create_volume( self.context, consistencygroup_id=group.id, @@ -5171,12 +5159,12 @@ class VolumeTestCase(BaseVolumeTestCase): self.volume.create_volume(self.context, volume_id) self.volume.delete_consistencygroup(self.context, group) - cg = db.consistencygroup_get( + cg = objects.ConsistencyGroup.get_by_id( context.get_admin_context(read_deleted='yes'), group.id) - self.assertEqual('deleted', cg['status']) + self.assertEqual('deleted', cg.status) self.assertRaises(exception.NotFound, - db.consistencygroup_get, + objects.ConsistencyGroup.get_by_id, self.context, group.id) @@ -5196,7 +5184,6 @@ class VolumeTestCase(BaseVolumeTestCase): availability_zone=CONF.storage_availability_zone, volume_type='type1,type2') - group = objects.ConsistencyGroup.get_by_id(self.context, group['id']) volume = tests_utils.create_volume( self.context, consistencygroup_id=group.id, @@ -5211,10 +5198,9 @@ class VolumeTestCase(BaseVolumeTestCase): self.volume.delete_consistencygroup, self.context, group) - cg = db.consistencygroup_get(self.context, - group.id) + cg = objects.ConsistencyGroup.get_by_id(self.context, group.id) # Group is not deleted - self.assertEqual('available', cg['status']) + self.assertEqual('available', cg.status) def test_secure_file_operations_enabled(self): """Test secure file operations setting for base driver. diff --git a/cinder/tests/unit/utils.py b/cinder/tests/unit/utils.py index c3e161238..4890486ec 100644 --- a/cinder/tests/unit/utils.py +++ b/cinder/tests/unit/utils.py @@ -20,6 +20,7 @@ from oslo_utils import timeutils from cinder import context from cinder import db +from cinder import objects def get_test_admin_context(): @@ -112,21 +113,24 @@ def create_consistencygroup(ctxt, source_cgid=None, **kwargs): """Create a consistencygroup object in the DB.""" - cg = {} - cg['host'] = host - cg['user_id'] = ctxt.user_id or 'fake_user_id' - cg['project_id'] = ctxt.project_id or 'fake_project_id' - cg['status'] = status - cg['name'] = name - cg['description'] = description - cg['availability_zone'] = availability_zone + + cg = objects.ConsistencyGroup(ctxt) + cg.host = host + cg.user_id = ctxt.user_id or 'fake_user_id' + cg.project_id = ctxt.project_id or 'fake_project_id' + cg.status = status + cg.name = name + cg.description = description + cg.availability_zone = availability_zone + if volume_type_id: - cg['volume_type_id'] = volume_type_id - cg['cgsnapshot_id'] = cgsnapshot_id - cg['source_cgid'] = source_cgid + cg.volume_type_id = volume_type_id + cg.cgsnapshot_id = cgsnapshot_id + cg.source_cgid = source_cgid for key in kwargs: - cg[key] = kwargs[key] - return db.consistencygroup_create(ctxt, cg) + setattr(cg, key, kwargs[key]) + cg.create() + return cg def create_cgsnapshot(ctxt, -- 2.45.2