'Test for nova.virt.libvirt.libvirt_driver.LivirtConnection
.finish_migration.'

@mock.patch.object(libvirt_driver.LibvirtDriver, '_inject_data')
@mock.patch.object(libvirt_driver.LibvirtDriver, 'get_info')
@mock.patch.object(libvirt_driver.LibvirtDriver, '_create_domain_and_network')
@mock.patch.object(libvirt_driver.LibvirtDriver, '_disk_raw_to_qcow2')
@mock.patch.object(libvirt_utils, 'write_to_file')
@mock.patch.object(libvirt_driver.LibvirtDriver, '_set_host_enabled')
@mock.patch.object(libvirt_driver.LibvirtDriver, '_build_device_metadata')
@mock.patch.object(libvirt_driver.LibvirtDriver, '_supports_direct_io')
@mock.patch('nova.api.metadata.base.InstanceMetadata')
def _test_finish_migration(self, mock_instance_metadata, mock_supports_direct_io, mock_build_device_metadata, mock_set_host_enabled, mock_write_to_file, mock_raw_to_qcow2, mock_create_domain_and_network, mock_get_info, mock_inject_data, power_on=True, resize_instance=False):

    self.flags(use_cow_images=True)
    if power_on:
        state = power_state.RUNNING
    else:
        state = power_state.SHUTDOWN
    mock_get_info.return_value = hardware.InstanceInfo(state=state)
    instance = self._create_instance({'config_drive': str(True), 'task_state': task_states.RESIZE_FINISH, 'flavor': {'swap': 500}})
    bdi = {'block_device_mapping': []}
    migration = objects.Migration()
    migration.source_compute = 'fake-source-compute'
    migration.dest_compute = 'fake-dest-compute'
    migration.source_node = 'fake-source-node'
    migration.dest_node = 'fake-dest-node'
    image_meta = objects.ImageMeta.from_dict(self.test_image_meta)
    disk_info = list(fake_disk_info_byname(instance, type='raw').values())
    disk_info_text = jsonutils.dumps(disk_info)
    backend = self.useFixture(fake_imagebackend.ImageBackendFixture())
    mock_create_domain_and_network.return_value = libvirt_guest.Guest('fake_dom')
    self.drvr.finish_migration(context.get_admin_context(), migration, instance, disk_info_text, [], image_meta, resize_instance, bdi, power_on)
    instance_path = libvirt_utils.get_instance_path(instance)
    convert_calls = [mock.call(os.path.join(instance_path, name)) for name in ('disk', 'disk.local', 'disk.swap')]
    mock_raw_to_qcow2.assert_has_calls(convert_calls, any_order=True)
    self.assertEqual(len(convert_calls), mock_raw_to_qcow2.call_count)
    disks = backend.disks
    for name in ('kernel', 'ramdisk', 'disk', 'disk.local', 'disk.swap'):
        self.assertTrue(disks[name].cache.called, ('cache()    not    called    for    %s' % name))
    root_disk = disks['disk']
    self.assertTrue(root_disk.create_snap.called)
    if ('disk.config' in disks):
        self.assertFalse(disks['disk.config'].import_file.called)
    self.assertFalse(mock_inject_data.called)
    mock_create_domain_and_network.assert_called_once_with(mock.ANY, mock.ANY, instance, [], block_device_info=bdi, power_on=power_on, vifs_already_plugged=True, post_xml_callback=mock.ANY)
