'Test fetching images.'

def test_fetch_image(self):

    dc_name = 'fake-dc'
    file_path = 'fake_file'
    ds_name = 'ds1'
    host = mock.MagicMock()
    port = 7443
    context = mock.MagicMock()
    image_data = {'id': nova.tests.unit.image.fake.get_valid_image_id(), 'disk_format': 'vmdk', 'size': 512}
    read_file_handle = mock.MagicMock()
    write_file_handle = mock.MagicMock()
    read_iter = mock.MagicMock()
    instance = objects.Instance(id=1, uuid=uuidsentinel.foo, image_ref=image_data['id'])
    def fake_read_handle(read_iter):
        return read_file_handle
    def fake_write_handle(host, port, dc_name, ds_name, cookies, file_path, file_size):
        return write_file_handle
    with test.nested(mock.patch.object(rw_handles, 'ImageReadHandle', side_effect=fake_read_handle), mock.patch.object(rw_handles, 'FileWriteHandle', side_effect=fake_write_handle), mock.patch.object(images, 'image_transfer'), mock.patch.object(images.IMAGE_API, 'get', return_value=image_data), mock.patch.object(images.IMAGE_API, 'download', return_value=read_iter)) as (glance_read, http_write, image_transfer, image_show, image_download):
        images.fetch_image(context, instance, host, port, dc_name, ds_name, file_path)
    glance_read.assert_called_once_with(read_iter)
    http_write.assert_called_once_with(host, port, dc_name, ds_name, None, file_path, image_data['size'])
    image_transfer.assert_called_once_with(read_file_handle, write_file_handle)
    image_download.assert_called_once_with(context, instance['image_ref'])
    image_show.assert_called_once_with(context, instance['image_ref'])
