from cinder import context
from cinder import db
from cinder import exception
+from cinder import objects
from cinder import test
from cinder.tests.unit.api import fakes
from cinder.tests.unit import utils
self.context.project_id = 'fake'
self.context.user_id = 'fake'
- @staticmethod
- def _create_cgsnapshot(
- name='test_cgsnapshot',
- description='this is a test cgsnapshot',
- consistencygroup_id='1',
- status='creating'):
- """Create a cgsnapshot object."""
- cgsnapshot = {}
- cgsnapshot['user_id'] = 'fake'
- cgsnapshot['project_id'] = 'fake'
- cgsnapshot['consistencygroup_id'] = consistencygroup_id
- cgsnapshot['name'] = name
- cgsnapshot['description'] = description
- cgsnapshot['status'] = status
- return db.cgsnapshot_create(context.get_admin_context(),
- cgsnapshot)['id']
-
- @staticmethod
- def _get_cgsnapshot_attrib(cgsnapshot_id, attrib_name):
- return db.cgsnapshot_get(context.get_admin_context(),
- cgsnapshot_id)[attrib_name]
-
def test_show_cgsnapshot(self):
consistencygroup = utils.create_consistencygroup(self.context)
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id)
+ cgsnapshot = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots/%s' %
- cgsnapshot_id)
+ cgsnapshot.id)
req.method = 'GET'
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
res_dict['cgsnapshot']['name'])
self.assertEqual('creating', res_dict['cgsnapshot']['status'])
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id)
+ cgsnapshot.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id)
+ cgsnapshot = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots/%s' %
- cgsnapshot_id)
+ cgsnapshot.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)
- cgsnapshot = dom.getElementsByTagName('cgsnapshot')
- name = cgsnapshot.item(0).getAttribute('name')
- self.assertEqual('test_cgsnapshot', name.strip())
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id)
+
+ cgsnapshots = dom.getElementsByTagName('cgsnapshot')
+ name = cgsnapshots.item(0).getAttribute('name')
+ self.assertEqual("test_cgsnapshot", name.strip())
+ cgsnapshot.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id1 = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id)
- cgsnapshot_id2 = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id)
- cgsnapshot_id3 = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id)
+ cgsnapshot1 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot2 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot3 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots')
req.method = 'GET'
res_dict = json.loads(res.body)
self.assertEqual(200, res.status_int)
- self.assertEqual(cgsnapshot_id1,
+ self.assertEqual(cgsnapshot1.id,
res_dict['cgsnapshots'][0]['id'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][0]['name'])
- self.assertEqual(cgsnapshot_id2,
+ self.assertEqual(cgsnapshot2.id,
res_dict['cgsnapshots'][1]['id'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][1]['name'])
- self.assertEqual(cgsnapshot_id3,
+ self.assertEqual(cgsnapshot3.id,
res_dict['cgsnapshots'][2]['id'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][2]['name'])
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id3)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id2)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id1)
+ cgsnapshot3.destroy()
+ cgsnapshot2.destroy()
+ cgsnapshot1.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
+ cgsnapshot1 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot2 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot3 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots')
req.method = 'GET'
dom = minidom.parseString(res.body)
cgsnapshot_list = dom.getElementsByTagName('cgsnapshot')
- self.assertEqual(cgsnapshot_id1,
+ self.assertEqual(cgsnapshot1.id,
cgsnapshot_list.item(0).getAttribute('id'))
- self.assertEqual(cgsnapshot_id2,
+ self.assertEqual(cgsnapshot2.id,
cgsnapshot_list.item(1).getAttribute('id'))
- self.assertEqual(cgsnapshot_id3,
+ self.assertEqual(cgsnapshot3.id,
cgsnapshot_list.item(2).getAttribute('id'))
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id3)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id2)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id1)
+ cgsnapshot3.destroy()
+ cgsnapshot2.destroy()
+ cgsnapshot1.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
+ cgsnapshot1 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot2 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot3 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots/detail')
req.method = 'GET'
res_dict['cgsnapshots'][0]['description'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][0]['name'])
- self.assertEqual(cgsnapshot_id1,
+ self.assertEqual(cgsnapshot1.id,
res_dict['cgsnapshots'][0]['id'])
self.assertEqual('creating',
res_dict['cgsnapshots'][0]['status'])
res_dict['cgsnapshots'][1]['description'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][1]['name'])
- self.assertEqual(cgsnapshot_id2,
+ self.assertEqual(cgsnapshot2.id,
res_dict['cgsnapshots'][1]['id'])
self.assertEqual('creating',
res_dict['cgsnapshots'][1]['status'])
res_dict['cgsnapshots'][2]['description'])
self.assertEqual('test_cgsnapshot',
res_dict['cgsnapshots'][2]['name'])
- self.assertEqual(cgsnapshot_id3,
+ self.assertEqual(cgsnapshot3.id,
res_dict['cgsnapshots'][2]['id'])
self.assertEqual('creating',
res_dict['cgsnapshots'][2]['status'])
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id3)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id2)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id1)
+
+ cgsnapshot3.destroy()
+ cgsnapshot2.destroy()
+ cgsnapshot1.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(self.context,
consistencygroup_id=
consistencygroup.id)['id']
- cgsnapshot_id1 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id2 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
- cgsnapshot_id3 = self._create_cgsnapshot(consistencygroup_id=
- consistencygroup.id)
+ cgsnapshot1 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot2 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
+ cgsnapshot3 = utils.create_cgsnapshot(
+ self.context, consistencygroup_id=consistencygroup.id)
req = webob.Request.blank('/v2/fake/cgsnapshots/detail')
req.method = 'GET'
dom = minidom.parseString(res.body)
cgsnapshot_detail = dom.getElementsByTagName('cgsnapshot')
- self.assertEqual('this is a test cgsnapshot',
- cgsnapshot_detail.item(0).getAttribute('description'))
- self.assertEqual('test_cgsnapshot',
- cgsnapshot_detail.item(0).getAttribute('name'))
- self.assertEqual(cgsnapshot_id1,
- cgsnapshot_detail.item(0).getAttribute('id'))
- self.assertEqual('creating',
- cgsnapshot_detail.item(0).getAttribute('status'))
-
- self.assertEqual('this is a test cgsnapshot',
- cgsnapshot_detail.item(1).getAttribute('description'))
- self.assertEqual('test_cgsnapshot',
- cgsnapshot_detail.item(1).getAttribute('name'))
- self.assertEqual(cgsnapshot_id2,
- cgsnapshot_detail.item(1).getAttribute('id'))
- self.assertEqual('creating',
- cgsnapshot_detail.item(1).getAttribute('status'))
-
- self.assertEqual('this is a test cgsnapshot',
- cgsnapshot_detail.item(2).getAttribute('description'))
- self.assertEqual('test_cgsnapshot',
- cgsnapshot_detail.item(2).getAttribute('name'))
- self.assertEqual(cgsnapshot_id3,
- cgsnapshot_detail.item(2).getAttribute('id'))
- self.assertEqual('creating',
- cgsnapshot_detail.item(2).getAttribute('status'))
-
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id3)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id2)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id1)
+ self.assertEqual(
+ 'this is a test cgsnapshot',
+ cgsnapshot_detail.item(0).getAttribute('description'))
+ self.assertEqual(
+ 'test_cgsnapshot',
+ cgsnapshot_detail.item(0).getAttribute('name'))
+ self.assertEqual(
+ cgsnapshot1.id,
+ cgsnapshot_detail.item(0).getAttribute('id'))
+ self.assertEqual(
+ 'creating', cgsnapshot_detail.item(0).getAttribute('status'))
+
+ self.assertEqual(
+ 'this is a test cgsnapshot',
+ cgsnapshot_detail.item(1).getAttribute('description'))
+ self.assertEqual(
+ 'test_cgsnapshot',
+ cgsnapshot_detail.item(1).getAttribute('name'))
+ self.assertEqual(
+ cgsnapshot2.id,
+ cgsnapshot_detail.item(1).getAttribute('id'))
+ self.assertEqual(
+ 'creating', cgsnapshot_detail.item(1).getAttribute('status'))
+
+ self.assertEqual(
+ 'this is a test cgsnapshot',
+ cgsnapshot_detail.item(2).getAttribute('description'))
+ self.assertEqual(
+ 'test_cgsnapshot',
+ cgsnapshot_detail.item(2).getAttribute('name'))
+ self.assertEqual(
+ cgsnapshot3.id,
+ cgsnapshot_detail.item(2).getAttribute('id'))
+ self.assertEqual(
+ 'creating', cgsnapshot_detail.item(2).getAttribute('status'))
+
+ cgsnapshot3.destroy()
+ cgsnapshot2.destroy()
+ cgsnapshot1.destroy()
db.volume_destroy(context.get_admin_context(),
volume_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 = utils.create_consistencygroup(self.context)
utils.create_volume(
self.context, consistencygroup_id=consistencygroup.id)
self.assertIn('id', res_dict['cgsnapshot'])
self.assertTrue(mock_validate.called)
- db.cgsnapshot_destroy(context.get_admin_context(), cgsnapshot_id)
consistencygroup.destroy()
+ cgsnapshot = objects.CGSnapshot.get_by_id(
+ context.get_admin_context(), res_dict['cgsnapshot']['id'])
+ cgsnapshot.destroy()
def test_create_cgsnapshot_with_no_body(self):
# omit body from the request
volume_id = utils.create_volume(
self.context,
consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = self._create_cgsnapshot(
+ cgsnapshot = utils.create_cgsnapshot(
+ self.context,
consistencygroup_id=consistencygroup.id,
status='available')
req = webob.Request.blank('/v2/fake/cgsnapshots/%s' %
- cgsnapshot_id)
+ cgsnapshot.id)
req.method = 'DELETE'
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
+ cgsnapshot = objects.CGSnapshot.get_by_id(self.context, cgsnapshot.id)
self.assertEqual(202, res.status_int)
- self.assertEqual('deleting', self._get_cgsnapshot_attrib(cgsnapshot_id,
- 'status'))
+ self.assertEqual('deleting', cgsnapshot.status)
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id)
+ cgsnapshot.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
def test_delete_cgsnapshot_with_Invalidcgsnapshot(self):
consistencygroup = utils.create_consistencygroup(self.context)
volume_id = utils.create_volume(
- self.context, consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = self._create_cgsnapshot(
- consistencygroup_id=consistencygroup.id, status='invalid')
+ self.context,
+ consistencygroup_id=consistencygroup.id)['id']
+ cgsnapshot = utils.create_cgsnapshot(
+ self.context,
+ consistencygroup_id=consistencygroup.id,
+ status='invalid')
req = webob.Request.blank('/v2/fake/cgsnapshots/%s' %
- cgsnapshot_id)
+ cgsnapshot.id)
req.method = 'DELETE'
req.headers['Content-Type'] = 'application/json'
res = req.get_response(fakes.wsgi_app())
self.assertEqual('Invalid cgsnapshot',
res_dict['badRequest']['message'])
- db.cgsnapshot_destroy(context.get_admin_context(),
- cgsnapshot_id)
+ cgsnapshot.destroy()
db.volume_destroy(context.get_admin_context(),
volume_id)
consistencygroup.destroy()
volume_id = utils.create_volume(
self.ctxt,
consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = utils.create_cgsnapshot(
- self.ctxt,
- consistencygroup_id=consistencygroup.id)['id']
+ cgsnapshot = utils.create_cgsnapshot(
+ self.ctxt, consistencygroup_id=consistencygroup.id)
snapshot = utils.create_snapshot(
self.ctxt,
volume_id,
- cgsnapshot_id=cgsnapshot_id,
+ cgsnapshot_id=cgsnapshot.id,
status='available')
test_cg_name = 'test cg'
body = {"consistencygroup-from-src": {"name": test_cg_name,
"description":
"Consistency Group 1",
- "cgsnapshot_id": cgsnapshot_id}}
+ "cgsnapshot_id": cgsnapshot.id}}
req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
req.method = 'POST'
req.headers['Content-Type'] = 'application/json'
cg_ref.destroy()
snapshot.destroy()
- db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id)
db.volume_destroy(self.ctxt.elevated(), volume_id)
consistencygroup.destroy()
+ cgsnapshot.destroy()
def test_create_consistencygroup_from_src_cg(self):
self.mock_object(volume_api.API, "create", stubs.stub_volume_create)
volume_id = utils.create_volume(
self.ctxt,
consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = utils.create_cgsnapshot(
- self.ctxt,
- consistencygroup_id=consistencygroup.id)['id']
+ cgsnapshot = utils.create_cgsnapshot(
+ self.ctxt, consistencygroup_id=consistencygroup.id)
snapshot = utils.create_snapshot(
self.ctxt,
volume_id,
- cgsnapshot_id=cgsnapshot_id,
+ cgsnapshot_id=cgsnapshot.id,
status='available')
test_cg_name = 'test cg'
body = {"consistencygroup-from-src": {"name": test_cg_name,
"description":
"Consistency Group 1",
- "cgsnapshot_id": cgsnapshot_id}}
+ "cgsnapshot_id": cgsnapshot.id}}
req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
req.method = 'POST'
req.headers['Content-Type'] = 'application/json'
self.assertIn(msg, res_dict['badRequest']['message'])
snapshot.destroy()
- db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id)
db.volume_destroy(self.ctxt.elevated(), volume_id)
consistencygroup.destroy()
+ cgsnapshot.destroy()
def test_create_consistencygroup_from_src_cgsnapshot_empty(self):
consistencygroup = utils.create_consistencygroup(self.ctxt)
volume_id = utils.create_volume(
self.ctxt,
consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = utils.create_cgsnapshot(
+ cgsnapshot = utils.create_cgsnapshot(
self.ctxt,
- consistencygroup_id=consistencygroup.id)['id']
+ consistencygroup_id=consistencygroup.id)
test_cg_name = 'test cg'
body = {"consistencygroup-from-src": {"name": test_cg_name,
"description":
"Consistency Group 1",
- "cgsnapshot_id": cgsnapshot_id}}
+ "cgsnapshot_id": cgsnapshot.id}}
req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
req.method = 'POST'
req.headers['Content-Type'] = 'application/json'
self.assertEqual(400, res_dict['badRequest']['code'])
self.assertIsNotNone(res_dict['badRequest']['message'])
- db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id)
db.volume_destroy(self.ctxt.elevated(), volume_id)
consistencygroup.destroy()
+ cgsnapshot.destroy()
def test_create_consistencygroup_from_src_source_cg_empty(self):
source_cg = utils.create_consistencygroup(self.ctxt)
volume_id = utils.create_volume(
self.ctxt,
consistencygroup_id=consistencygroup.id)['id']
- cgsnapshot_id = utils.create_cgsnapshot(
- self.ctxt,
- consistencygroup_id=consistencygroup.id)['id']
+ cgsnapshot = utils.create_cgsnapshot(
+ self.ctxt, consistencygroup_id=consistencygroup.id)
snapshot = utils.create_snapshot(
self.ctxt,
volume_id,
- cgsnapshot_id=cgsnapshot_id,
+ cgsnapshot_id=cgsnapshot.id,
status='available')
test_cg_name = 'test cg'
body = {"consistencygroup-from-src": {"name": test_cg_name,
"description":
"Consistency Group 1",
- "cgsnapshot_id": cgsnapshot_id}}
+ "cgsnapshot_id": cgsnapshot.id}}
req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src')
req.method = 'POST'
req.headers['Content-Type'] = 'application/json'
self.assertEqual(msg, res_dict['badRequest']['message'])
snapshot.destroy()
- db.cgsnapshot_destroy(self.ctxt.elevated(), cgsnapshot_id)
db.volume_destroy(self.ctxt.elevated(), volume_id)
consistencygroup.destroy()
+ cgsnapshot.destroy()
@mock.patch.object(volume_api.API, 'create',
side_effect=exception.CinderException(