'Tests the finish_revert_migration method on vmops.'

@mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs')
@mock.patch.object(vmops.VMwareVMOps, '_resize_create_ephemerals_and_swap')
@mock.patch.object(vmops.VMwareVMOps, '_remove_ephemerals_and_swap')
@mock.patch.object(ds_util, 'disk_delete')
@mock.patch.object(ds_util, 'disk_move')
@mock.patch.object(ds_util, 'file_exists', return_value=True)
@mock.patch.object(vmops.VMwareVMOps, '_get_ds_browser', return_value='fake-browser')
@mock.patch.object(vm_util, 'reconfigure_vm')
@mock.patch.object(vm_util, 'get_vm_resize_spec', return_value='fake-spec')
@mock.patch.object(vm_util, 'power_off_instance')
@mock.patch.object(vm_util, 'get_vm_ref', return_value='fake-ref')
@mock.patch.object(vm_util, 'power_on_instance')
def _test_finish_revert_migration(self, fake_power_on, fake_get_vm_ref, fake_power_off, fake_resize_spec, fake_reconfigure_vm, fake_get_browser, fake_original_exists, fake_disk_move, fake_disk_delete, fake_remove_ephemerals_and_swap, fake_resize_create_ephemerals_and_swap, fake_get_extra_specs, power_on):

    datastore = ds_obj.Datastore(ref='fake-ref', name='fake')
    device = vmwareapi_fake.DataObject()
    backing = vmwareapi_fake.DataObject()
    backing.datastore = datastore.ref
    device.backing = backing
    vmdk = vm_util.VmdkInfo('[fake]    uuid/root.vmdk', 'fake-adapter', 'fake-disk', 'fake-capacity', device)
    dc_info = ds_util.DcInfo(ref='fake_ref', name='fake', vmFolder='fake_folder')
    extra_specs = vm_util.ExtraSpecs()
    fake_get_extra_specs.return_value = extra_specs
    with test.nested(mock.patch.object(self._vmops, 'get_datacenter_ref_and_name', return_value=dc_info), mock.patch.object(vm_util, 'get_vmdk_info', return_value=vmdk)) as (fake_get_dc_ref_and_name, fake_get_vmdk_info):
        self._vmops._volumeops = mock.Mock()
        mock_attach_disk = self._vmops._volumeops.attach_disk_to_vm
        mock_detach_disk = self._vmops._volumeops.detach_disk_from_vm
        self._vmops.finish_revert_migration(self._context, instance=self._instance, network_info=None, block_device_info=None, power_on=power_on)
        fake_get_vm_ref.assert_called_once_with(self._session, self._instance)
        fake_power_off.assert_called_once_with(self._session, self._instance, 'fake-ref')
        metadata = ('name:fake_display_name\nuserid:fake_user\nusername:None\nprojectid:fake_project\nprojectname:None\nflavor:name:m1.small\nflavor:memory_mb:512\nflavor:vcpus:1\nflavor:ephemeral_gb:0\nflavor:root_gb:10\nflavor:swap:0\nimageid:70a599e0-31e7-49b7-b260-868f441e862b\npackage:%s\n' % version.version_string_with_package())
        fake_resize_spec.assert_called_once_with(self._session.vim.client.factory, int(self._instance.vcpus), int(self._instance.memory_mb), extra_specs, metadata=metadata)
        fake_reconfigure_vm.assert_called_once_with(self._session, 'fake-ref', 'fake-spec')
        fake_get_vmdk_info.assert_called_once_with(self._session, 'fake-ref', uuid=self._instance.uuid)
        fake_get_browser.assert_called_once_with('fake-ref')
        fake_original_exists.assert_called_once_with(self._session, 'fake-browser', ds_obj.DatastorePath(datastore.name, 'uuid'), 'original.vmdk')
        mock_detach_disk.assert_called_once_with('fake-ref', self._instance, device)
        fake_disk_delete.assert_called_once_with(self._session, dc_info.ref, '[fake]    uuid/root.vmdk')
        fake_disk_move.assert_called_once_with(self._session, dc_info.ref, '[fake]    uuid/original.vmdk', '[fake]    uuid/root.vmdk')
        mock_attach_disk.assert_called_once_with('fake-ref', self._instance, 'fake-adapter', 'fake-disk', '[fake]    uuid/root.vmdk')
        fake_remove_ephemerals_and_swap.assert_called_once_with('fake-ref')
        fake_resize_create_ephemerals_and_swap.assert_called_once_with('fake-ref', self._instance, None)
    if power_on:
        fake_power_on.assert_called_once_with(self._session, self._instance)
    else:
        self.assertFalse(fake_power_on.called)
