diff --git a/cinder/tests/test_volume.py b/cinder/tests/test_volume.py index eafd09743..b4f317441 100644 --- a/cinder/tests/test_volume.py +++ b/cinder/tests/test_volume.py @@ -1678,6 +1678,42 @@ class VolumeTestCase(BaseVolumeTestCase): self.context, snapshot_id) + @mock.patch.object(db, 'snapshot_create', + side_effect=exception.InvalidSnapshot( + 'Create snapshot in db failed!')) + def test_create_snapshot_failed_db_snapshot(self, mock_snapshot): + """Test exception handling when create snapshot in db failed.""" + test_volume = tests_utils.create_volume( + self.context, + **self.volume_params) + self.volume.create_volume(self.context, test_volume['id']) + test_volume['status'] = 'available' + volume_api = cinder.volume.api.API() + self.assertRaises(exception.InvalidSnapshot, + volume_api.create_snapshot, + self.context, + test_volume, + 'fake_name', + 'fake_description') + + @mock.patch.object(QUOTAS, 'commit', + side_effect=exception.QuotaError( + 'Snapshot quota commit failed!')) + def test_create_snapshot_failed_quota_commit(self, mock_snapshot): + """Test exception handling when snapshot quota commit failed.""" + test_volume = tests_utils.create_volume( + self.context, + **self.volume_params) + self.volume.create_volume(self.context, test_volume['id']) + test_volume['status'] = 'available' + volume_api = cinder.volume.api.API() + self.assertRaises(exception.QuotaError, + volume_api.create_snapshot, + self.context, + test_volume, + 'fake_name', + 'fake_description') + def test_cannot_delete_volume_in_use(self): """Test volume can't be deleted in invalid stats.""" # create a volume and assign to host diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 89355772e..42437d084 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -524,13 +524,15 @@ class API(base.Base): 'encryption_key_id': volume['encryption_key_id'], 'metadata': metadata} + snapshot = None try: snapshot = self.db.snapshot_create(context, options) QUOTAS.commit(context, reservations) except Exception: with excutils.save_and_reraise_exception(): try: - self.db.snapshot_destroy(context, volume['id']) + if snapshot: + self.db.snapshot_destroy(context, snapshot['id']) finally: QUOTAS.rollback(context, reservations)