def test_create_snapshot_force(self): """Test snapshot in use can be created forcibly.""" def fake_cast(ctxt, topic, msg): pass self.stubs.Set(rpc, 'cast', fake_cast) instance_uuid = '12345678-1234-5678-1234-567812345678' volume = self._create_volume() self.volume.create_volume(self.context, volume['id']) db.volume_attached(self.context, volume['id'], instance_uuid, '/dev/sda1') volume_api = cinder.volume.api.API() volume = volume_api.get(self.context, volume['id']) self.assertRaises(exception.InvalidVolume, volume_api.create_snapshot, self.context, volume, 'fake_name', 'fake_description') snapshot_ref = volume_api.create_snapshot_force(self.context, volume, 'fake_name', 'fake_description') db.snapshot_destroy(self.context, snapshot_ref['id']) db.volume_destroy(self.context, volume['id'])
def test_create_consistencygroup_from_src_create_volume_failed(self, mock_create): ctxt = context.RequestContext("fake", "fake", auth_token=True) consistencygroup_id = utils.create_consistencygroup(ctxt)["id"] volume_id = utils.create_volume(ctxt, consistencygroup_id=consistencygroup_id)["id"] cgsnapshot_id = utils.create_cgsnapshot(ctxt, consistencygroup_id=consistencygroup_id)["id"] snapshot_id = utils.create_snapshot(ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status="available")["id"] test_cg_name = "test cg" body = { "consistencygroup-from-src": { "name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id, } } req = webob.Request.blank("/v2/fake/consistencygroups/create_from_src") req.method = "POST" req.headers["Content-Type"] = "application/json" req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict["badRequest"]["code"]) 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)
def test_unmanage_volume_with_snapshots(self): """Return 400 if the volume exists but has snapshots.""" vol = utils.create_volume(self.ctxt) snap = utils.create_snapshot(self.ctxt, vol.id) res = self._get_resp(vol.id) self.assertEqual(400, res.status_int, res) db.volume_destroy(self.ctxt, vol.id) db.snapshot_destroy(self.ctxt, snap.id)
def create_snapshot(self, context, volume_id, snapshot_id): snapshot = db.snapshot_get(context, snapshot_id) context = context.elevated() client = LunrClient(snapshot, logger=LOG) params = {'volume': snapshot['volume_id']} try: client.backups.create(snapshot['id'], **params) except LunrError, e: LOG.debug(_('error creating snapshot %s'), snapshot_id) # Don't leave an error'd snapshot around, the raise here # will notify the caller of the error (See Github Issue #322) db.snapshot_destroy(context, snapshot['id']) raise
def create_snapshot(self, context, volume_id, snapshot_id): snapshot = db.snapshot_get(context, snapshot_id) context = context.elevated() client = LunrClient(snapshot, logger=LOG) params = { 'volume': snapshot['volume_id'] } try: client.backups.create(snapshot['id'], **params) except LunrError, e: LOG.debug(_('error creating snapshot %s'), snapshot_id) # Don't leave an error'd snapshot around, the raise here # will notify the caller of the error (See Github Issue #322) db.snapshot_destroy(context, snapshot['id']) raise
def test_create_volume_from_snapshot(self): self.driver.do_setup(None) vol_type = test_utils.create_volume_type(self.ctxt, self, name='my_vol_type') volume = test_utils.create_volume(self.ctxt, size=4, volume_type_id=vol_type.id) snapshot = test_utils.create_snapshot(self.ctxt, volume_id=volume.id) self.driver.create_volume_from_snapshot(volume, snapshot) proj = self.db.data["projects"][lightos.LIGHTOS_DEFAULT_PROJECT_NAME] actual_volumes = proj["volumes"] self.assertEqual(1, len(actual_volumes)) self.driver.delete_snapshot(snapshot) self.driver.delete_volume(volume) db.volume_destroy(self.ctxt, volume.id) db.snapshot_destroy(self.ctxt, snapshot.id)
def delete_snapshot(self, context, snapshot_id): snapshot = db.snapshot_get(context, snapshot_id) context = context.elevated() LOG.debug(_("snapshot %s: deleting"), snapshot['name']) reserve_opts = {'snapshots': -1} volume = db.volume_get(context, snapshot['volume_id']) try: QUOTAS.add_volume_type_opts(context, reserve_opts, volume.get('volume_type_id')) reservations = QUOTAS.reserve(context, **reserve_opts) except Exception: reservations = None LOG.exception(_("Failed to update usages deleting snapshot")) client = LunrClient(snapshot, logger=LOG) try: client.backups.delete(snapshot['id']) except LunrError, e: # ignore Not Found on delete_snapshot. Don't wait on status. if e.code == 404: db.snapshot_destroy(context, snapshot['id']) LOG.debug(_("snapshot %s: deleted successfully"), snapshot['name']) elif e.code == 409: db.snapshot_update(context, snapshot['id'], {'status': 'available'}) LOG.debug(_("snapshot %s: snapshot is busy"), snapshot['name']) if reservations: QUOTAS.rollback(context, reservations) raise else: LOG.debug(_('error deleting snapshot %s'), snapshot['id']) db.snapshot_update(context, snapshot['id'], {'status': 'error_deleting'}) if reservations: QUOTAS.rollback(context, reservations) raise
def test_create_consistencygroup_from_src(self, mock_validate): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) consistencygroup = 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'] snapshot_id = utils.create_snapshot(self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = { "consistencygroup-from-src": { "name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id } } req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) self.assertEqual(test_cg_name, res_dict['consistencygroup']['name']) self.assertTrue(mock_validate.called) 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(self, mock_validate): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) consistencygroup = 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'] snapshot_id = utils.create_snapshot( self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(202, res.status_int) self.assertIn('id', res_dict['consistencygroup']) self.assertEqual(test_cg_name, res_dict['consistencygroup']['name']) self.assertTrue(mock_validate.called) 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_show_cgsnapshot(self): vol_type = utils.create_volume_type(context.get_admin_context(), self, name='my_vol_type') consistencygroup = utils.create_group( self.context, group_type_id=fake.GROUP_TYPE_ID, volume_type_ids=[vol_type['id']]) volume_id = utils.create_volume(self.context, volume_type_id=vol_type['id'], group_id= consistencygroup.id)['id'] cgsnapshot = utils.create_group_snapshot( self.context, group_id=consistencygroup.id, group_type_id=fake.GROUP_TYPE_ID,) snapshot_id = utils.create_snapshot( self.context, volume_type_id=vol_type['id'], volume_id=volume_id, group_snapshot_id=cgsnapshot.id)['id'] req = webob.Request.blank('/v2/%s/cgsnapshots/%s' % ( fake.PROJECT_ID, cgsnapshot.id)) req.method = 'GET' req.headers['Content-Type'] = 'application/json' res = req.get_response(fakes.wsgi_app( fake_auth_context=self.user_ctxt)) res_dict = jsonutils.loads(res.body) self.assertEqual(http_client.OK, res.status_int) self.assertEqual('this is a test group snapshot', res_dict['cgsnapshot']['description']) self.assertEqual('test_group_snapshot', res_dict['cgsnapshot']['name']) self.assertEqual('creating', res_dict['cgsnapshot']['status']) db.snapshot_destroy(context.get_admin_context(), snapshot_id) cgsnapshot.destroy() db.volume_destroy(context.get_admin_context(), volume_id) consistencygroup.destroy()
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'] volume_id = utils.create_volume( ctxt, consistencygroup_id=consistencygroup_id)['id'] cgsnapshot_id = utils.create_cgsnapshot( ctxt, consistencygroup_id=consistencygroup_id)['id'] snapshot_id = utils.create_snapshot( ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) msg = _('Invalid ConsistencyGroup: No host to create consistency ' '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)
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'] volume_id = utils.create_volume( ctxt, consistencygroup_id=consistencygroup_id)['id'] cgsnapshot_id = utils.create_cgsnapshot( ctxt, consistencygroup_id=consistencygroup_id)['id'] snapshot_id = utils.create_snapshot(ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = { "consistencygroup-from-src": { "name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id } } req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) msg = _('Invalid ConsistencyGroup: No host to create consistency ' '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)
def test_create_consistencygroup_from_src_both_snap_cg(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) consistencygroup = 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'] snapshot_id = utils.create_snapshot( self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = {"consistencygroup-from-src": {"name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id, "source_cgid": consistencygroup.id}} req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) 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_both_snap_cg(self): self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) consistencygroup = 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'] snapshot_id = utils.create_snapshot(self.ctxt, volume_id, cgsnapshot_id=cgsnapshot_id, status='available')['id'] test_cg_name = 'test cg' body = { "consistencygroup-from-src": { "name": test_cg_name, "description": "Consistency Group 1", "cgsnapshot_id": cgsnapshot_id, "source_cgid": consistencygroup.id } } req = webob.Request.blank('/v2/fake/consistencygroups/create_from_src') req.method = 'POST' req.headers['Content-Type'] = 'application/json' req.body = json.dumps(body) res = req.get_response(fakes.wsgi_app()) res_dict = json.loads(res.body) self.assertEqual(400, res.status_int) self.assertEqual(400, res_dict['badRequest']['code']) self.assertIsNotNone(res_dict['badRequest']['message']) 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 destroy(self): db.snapshot_destroy(self._context, self.id)
def destroy(self): updated_values = db.snapshot_destroy(self._context, self.id) self.update(updated_values) self.obj_reset_changes(updated_values.keys())
def destroy(self, context): db.snapshot_destroy(context, self.id)