Merge "Destroy orig VM during resize if triggered by user"

This commit is contained in:
Jenkins 2014-10-03 01:01:23 +00:00 committed by Gerrit Code Review
commit 945ab76812
2 changed files with 46 additions and 24 deletions

View File

@ -1471,28 +1471,46 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
None, self.destroy_disks)
self.assertFalse(mock_destroy.called)
def test_destroy_instance_without_vm_ref(self):
def _destroy_instance_without_vm_ref(self, resize_exists=False,
task_state=None):
def fake_vm_ref_from_name(session, vm_name):
if resize_exists:
return 'fake-ref'
self._create_instance()
with contextlib.nested(
mock.patch.object(vm_util, 'get_vm_ref_from_name',
return_value=None),
fake_vm_ref_from_name),
mock.patch.object(self.conn._session,
'_call_method')
) as (mock_get, mock_call):
'_call_method'),
mock.patch.object(self.conn._vmops,
'_destroy_instance')
) as (mock_get, mock_call, mock_destroy):
self.instance.task_state = task_state
self.conn.destroy(self.context, self.instance,
self.network_info,
None, True)
mock_get.assert_called_with(self.conn._vmops._session,
self.instance['uuid'])
expected_args = [((self.conn._vmops._session,
self.instance['uuid'] + '-orig'),),
((self.conn._vmops._session,
self.instance['uuid']),)]
# one for VM named uuid-orig, one for VM named uuid
self.assertEqual(expected_args, mock_get.call_args_list)
self.assertEqual(2, mock_get.call_count)
if resize_exists:
if task_state == task_states.RESIZE_REVERTING:
expected = 1
else:
expected = 2
else:
expected = 1
self.assertEqual(expected, mock_destroy.call_count)
self.assertFalse(mock_call.called)
def test_destroy_instance_without_vm_ref(self):
self._destroy_instance_without_vm_ref()
def test_destroy_instance_without_vm_ref_with_resize(self):
self._destroy_instance_without_vm_ref(resize_exists=True)
def test_destroy_instance_without_vm_ref_with_resize_revert(self):
self._destroy_instance_without_vm_ref(resize_exists=True,
task_state=task_states.RESIZE_REVERTING)
def _rescue(self, config_drive=False):
# validate that the power on is only called once
self._power_on = vm_util.power_on_instance

View File

@ -838,17 +838,21 @@ class VMwareVMOps(object):
self._destroy_instance(instance,
destroy_disks=destroy_disks,
instance_name=rescue_name)
# When VM deletion is triggered in middle of VM resize before VM
# arrive RESIZED state, uuid-orig VM need to deleted to avoid
# VM leak. Within method _destroy_instance it will check vmref
# exist or not before attempt deletion.
resize_orig_vmname = instance['uuid'] + self._migrate_suffix
vm_orig_ref = vm_util.get_vm_ref_from_name(self._session,
resize_orig_vmname)
if vm_orig_ref:
self._destroy_instance(instance,
destroy_disks=destroy_disks,
instance_name=resize_orig_vmname)
# NOTE(arnaud): Destroy uuid-orig and uuid VMs iff it is not
# triggered by the revert resize api call. This prevents
# the uuid-orig VM to be deleted to be able to associate it later.
if instance.task_state != task_states.RESIZE_REVERTING:
# When VM deletion is triggered in middle of VM resize before VM
# arrive RESIZED state, uuid-orig VM need to deleted to avoid
# VM leak. Within method _destroy_instance it will check vmref
# exist or not before attempt deletion.
resize_orig_vmname = instance['uuid'] + self._migrate_suffix
vm_orig_ref = vm_util.get_vm_ref_from_name(self._session,
resize_orig_vmname)
if vm_orig_ref:
self._destroy_instance(instance,
destroy_disks=destroy_disks,
instance_name=resize_orig_vmname)
self._destroy_instance(instance, destroy_disks=destroy_disks)
LOG.debug("Instance destroyed", instance=instance)