diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 79fc3058eb9c..6584edb3abd9 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -5032,7 +5032,12 @@ class ComputeManager(manager.Manager): def _resize_instance(self, context, instance, image, migration, instance_type, clean_shutdown, request_spec): - with self._error_out_instance_on_exception(context, instance), \ + # Pass instance_state=instance.vm_state because we can resize + # a STOPPED server and we don't want to set it back to ACTIVE + # in case migrate_disk_and_power_off raises InstanceFaultRollback. + instance_state = instance.vm_state + with self._error_out_instance_on_exception( + context, instance, instance_state=instance_state), \ errors_out_migration_ctxt(migration): network_info = self.network_api.get_instance_nw_info(context, instance) diff --git a/nova/tests/unit/compute/test_compute_mgr.py b/nova/tests/unit/compute/test_compute_mgr.py index 0712767f85cb..94a9bb0d9894 100644 --- a/nova/tests/unit/compute/test_compute_mgr.py +++ b/nova/tests/unit/compute/test_compute_mgr.py @@ -7829,6 +7829,28 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase, # Assert that we set the migration to an error state self.assertEqual("error", self.migration.status) + def test_resize_instance_fail_rollback_stays_stopped(self): + """Tests that when the driver's migrate_disk_and_power_off method + raises InstanceFaultRollback that the instance vm_state is preserved + rather than reset to ACTIVE which would be wrong if resizing a STOPPED + server. + """ + with self._mock_resize_instance() as ( + migrate_disk_and_power_off, notify): + migrate_disk_and_power_off.side_effect = \ + exception.InstanceFaultRollback( + exception.ResizeError(reason='unable to resize disk down')) + self.instance.vm_state = vm_states.STOPPED + self.assertRaises( + exception.ResizeError, self.compute.resize_instance, + context=self.context, instance=self.instance, image=self.image, + migration=self.migration, + instance_type='type', clean_shutdown=True, + request_spec=objects.RequestSpec()) + + # Assert the instance vm_state was unchanged. + self.assertEqual(vm_states.STOPPED, self.instance.vm_state) + def test_resize_instance_notify_failure(self): # Raise an exception sending the end notification, which is after we # cast the migration to the destination host