From 5f357504ff95ea4731ba309f285b937066d8908a Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Wed, 9 Nov 2022 11:14:03 +0100 Subject: [PATCH] Set backup status to error on VolumeNotFound When creating a backup and it fails we will try to update the status on the volume but if it does not exist we will instead fail on that and the backup will be stuck in creating status which means cloud admin need to help resetting the status. This catches the VolumeNotFound exception and ignores updating the volume status and thus backup will be set to error and can be deleted by user instead. Closes-Bug: #1996049 Change-Id: Iafa92ece7f83323af257c1702df5029469b11739 (cherry picked from commit 813df9b99f23bdc54787a0c918e47f9d61df3232) --- cinder/backup/manager.py | 13 ++++++++---- cinder/tests/unit/backup/test_backup.py | 20 +++++++++++++++++++ ...otfound-set-to-error-fa47b3631093a702.yaml | 5 +++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/backup-volumenotfound-set-to-error-fa47b3631093a702.yaml diff --git a/cinder/backup/manager.py b/cinder/backup/manager.py index 92d482434f3..ec17e4bdcc7 100644 --- a/cinder/backup/manager.py +++ b/cinder/backup/manager.py @@ -416,10 +416,15 @@ def create_backup(self, context, backup): snapshot.status = fields.SnapshotStatus.AVAILABLE snapshot.save() else: - self.db.volume_update( - context, volume_id, - {'status': previous_status, - 'previous_status': 'error_backing-up'}) + try: + self.db.volume_update( + context, volume_id, + {'status': previous_status, + 'previous_status': 'error_backing-up'}) + except exception.VolumeNotFound: + # If the volume was deleted we cannot update its + # status but we still want to set the backup to error. + pass volume_utils.update_backup_error(backup, str(err)) def _start_backup(self, context, backup, volume): diff --git a/cinder/tests/unit/backup/test_backup.py b/cinder/tests/unit/backup/test_backup.py index eca450637fb..d63271e73f0 100644 --- a/cinder/tests/unit/backup/test_backup.py +++ b/cinder/tests/unit/backup/test_backup.py @@ -673,6 +673,26 @@ def my_start_backup(*args, **kwargs): backup.refresh() self.assertEqual(fields.BackupStatus.DELETED, backup.status) + @mock.patch('cinder.backup.manager.BackupManager._start_backup', + side_effect=FakeBackupException(str(uuid.uuid4()))) + @mock.patch.object(db, 'volume_update') + def test_create_backup_aborted_volume_not_found(self, vol_up_mock, + start_backup_mock): + """Test error handling when backup fails and volume does not exist.""" + vol_id = self._create_volume_db_entry(size=1) + backup = self._create_backup_db_entry(volume_id=vol_id) + + vol_up_mock.side_effect = exception.VolumeNotFound(volume_id=vol_id) + + self.assertRaises(FakeBackupException, + self.backup_mgr.create_backup, + self.ctxt, + backup) + backup.refresh() + self.assertEqual(fields.BackupStatus.ERROR, backup.status) + self.assertTrue(start_backup_mock.called) + self.assertTrue(vol_up_mock.called) + @mock.patch('cinder.backup.manager.BackupManager._start_backup', side_effect=FakeBackupException(str(uuid.uuid4()))) def test_create_backup_with_snapshot_error(self, mock_start_backup): diff --git a/releasenotes/notes/backup-volumenotfound-set-to-error-fa47b3631093a702.yaml b/releasenotes/notes/backup-volumenotfound-set-to-error-fa47b3631093a702.yaml new file mode 100644 index 00000000000..6a84d372930 --- /dev/null +++ b/releasenotes/notes/backup-volumenotfound-set-to-error-fa47b3631093a702.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + `Bug #1996049 `_: Fixed bug + where backup was not set to error on failure when volume did not exist.