Merge "Fix scope of errors_out_migration in finish_resize"

This commit is contained in:
Jenkins 2017-09-05 06:46:42 +00:00 committed by Gerrit Code Review
commit 5bcfaf3e8a
2 changed files with 49 additions and 25 deletions

View File

@ -4029,18 +4029,11 @@ class ComputeManager(manager.Manager):
instance.launched_at = timeutils.utcnow() instance.launched_at = timeutils.utcnow()
instance.save(expected_task_state=task_states.RESIZE_FINISH) instance.save(expected_task_state=task_states.RESIZE_FINISH)
self._update_scheduler_instance_info(context, instance) return network_info
self._notify_about_instance_usage(
context, instance, "finish_resize.end",
network_info=network_info)
compute_utils.notify_about_instance_action(context, instance,
self.host, action=fields.NotificationAction.RESIZE_FINISH,
phase=fields.NotificationPhase.END)
@wrap_exception() @wrap_exception()
@reverts_task_state @reverts_task_state
@wrap_instance_event(prefix='compute') @wrap_instance_event(prefix='compute')
@errors_out_migration
@wrap_instance_fault @wrap_instance_fault
def finish_resize(self, context, disk_info, image, instance, def finish_resize(self, context, disk_info, image, instance,
reservations, migration): reservations, migration):
@ -4050,10 +4043,19 @@ class ComputeManager(manager.Manager):
new host machine. new host machine.
""" """
with self._error_out_instance_on_exception(context, instance): with self._error_out_instance_on_exception(context, instance), \
errors_out_migration_ctxt(migration):
image_meta = objects.ImageMeta.from_dict(image) image_meta = objects.ImageMeta.from_dict(image)
self._finish_resize(context, instance, migration, network_info = self._finish_resize(context, instance, migration,
disk_info, image_meta) disk_info, image_meta)
self._update_scheduler_instance_info(context, instance)
self._notify_about_instance_usage(
context, instance, "finish_resize.end",
network_info=network_info)
compute_utils.notify_about_instance_action(context, instance,
self.host, action=fields.NotificationAction.RESIZE_FINISH,
phase=fields.NotificationPhase.END)
@wrap_exception() @wrap_exception()
@wrap_instance_fault @wrap_instance_fault

View File

@ -5470,29 +5470,51 @@ class ComputeManagerMigrationTestCase(test.NoDBTestCase):
self.useFixture(fixtures.SpawnIsSynchronousFixture()) self.useFixture(fixtures.SpawnIsSynchronousFixture())
self.useFixture(fixtures.EventReporterStub()) self.useFixture(fixtures.EventReporterStub())
def test_finish_resize_failure(self): @contextlib.contextmanager
def _mock_finish_resize(self):
with test.nested( with test.nested(
mock.patch.object(self.compute, '_finish_resize', mock.patch.object(self.compute, '_finish_resize'),
side_effect=exception.ResizeError(reason='')),
mock.patch.object(db, 'instance_fault_create'), mock.patch.object(db, 'instance_fault_create'),
mock.patch.object(self.compute, '_instance_update'), mock.patch.object(self.compute, '_update_resource_tracker'),
mock.patch.object(self.instance, 'save'), mock.patch.object(self.instance, 'save'),
mock.patch.object(self.migration, 'save'), ) as (_finish_resize, fault_create, instance_update, instance_save):
mock.patch.object(self.migration, 'obj_as_admin',
return_value=mock.MagicMock())
) as (meth, fault_create, instance_update, instance_save,
migration_save, migration_obj_as_admin):
fault_create.return_value = ( fault_create.return_value = (
test_instance_fault.fake_faults['fake-uuid'][0]) test_instance_fault.fake_faults['fake-uuid'][0])
yield _finish_resize
def test_finish_resize_failure(self):
migration = mock.NonCallableMagicMock()
migration.status = 'post-migrating'
with self._mock_finish_resize() as _finish_resize:
_finish_resize.side_effect = self.TestResizeError
self.assertRaises( self.assertRaises(
exception.ResizeError, self.compute.finish_resize, self.TestResizeError, self.compute.finish_resize,
context=self.context, disk_info=[], image=self.image, context=self.context, disk_info=[], image=self.image,
instance=self.instance, reservations=[], instance=self.instance, reservations=[],
migration=self.migration migration=migration
) )
self.assertEqual("error", self.migration.status)
migration_save.assert_called_once_with() # Assert that we set the migration to an error state
migration_obj_as_admin.assert_called_once_with() self.assertEqual("error", migration.status)
@mock.patch('nova.compute.manager.ComputeManager.'
'_notify_about_instance_usage')
def test_finish_resize_notify_failure(self, notify):
migration = mock.NonCallableMagicMock()
migration.status = 'post-migrating'
with self._mock_finish_resize():
notify.side_effect = self.TestResizeError
self.assertRaises(
self.TestResizeError, self.compute.finish_resize,
context=self.context, disk_info=[], image=self.image,
instance=self.instance, reservations=[],
migration=migration
)
# Assert that we did not set the migration to an error state
self.assertEqual('post-migrating', migration.status)
@contextlib.contextmanager @contextlib.contextmanager
def _mock_resize_instance(self): def _mock_resize_instance(self):