Merge "xenapi: refactor - extract image_utils"
This commit is contained in:
3 changed files with 144 additions and 18 deletions
89
nova/tests/virt/xenapi/image/test_utils.py
Normal file
89
nova/tests/virt/xenapi/image/test_utils.py
Normal file
@@ -0,0 +1,89 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.image import glance
from nova import test
from nova.virt.xenapi.image import utils
class GlanceImageTestCase(test.TestCase):
def _get_image(self):
return utils.GlanceImage('context', 'href')
def _stub_out_glance_services(self):
image_service = self.mox.CreateMock(glance.GlanceImageService)
self.mox.StubOutWithMock(utils.glance, 'get_remote_image_service')
utils.glance.get_remote_image_service('context', 'href').AndReturn(
(image_service, 'id'))
return image_service
def test__image_id(self):
self._stub_out_glance_services()
self.mox.ReplayAll()
image = self._get_image()
self.assertEquals('id', image._image_id)
def test__image_service(self):
image_service = self._stub_out_glance_services()
self.mox.ReplayAll()
image = self._get_image()
self.assertEquals(image_service, image._image_service)
def test_meta(self):
image_service = self._stub_out_glance_services()
image_service.show('context', 'id').AndReturn('metadata')
self.mox.ReplayAll()
image = self._get_image()
self.assertEquals('metadata', image.meta)
def test_meta_caching(self):
image_service = self._stub_out_glance_services()
self.mox.ReplayAll()
image = self._get_image()
image._cached_meta = 'metadata'
self.assertEquals('metadata', image.meta)
def test_download_to(self):
image_service = self._stub_out_glance_services()
image_service.download('context', 'id', 'fobj').AndReturn('result')
self.mox.ReplayAll()
image = self._get_image()
self.assertEquals('result', image.download_to('fobj'))
class RawImageTestCase(test.TestCase):
def test_get_size(self):
glance_image = self.mox.CreateMock(utils.GlanceImage)
glance_image.meta = {'size': '123'}
raw_image = utils.RawImage(glance_image)
self.mox.ReplayAll()
self.assertEquals(123, raw_image.get_size())
def test_stream_to(self):
glance_image = self.mox.CreateMock(utils.GlanceImage)
glance_image.download_to('file').AndReturn('result')
raw_image = utils.RawImage(glance_image)
self.mox.ReplayAll()
self.assertEquals('result', raw_image.stream_to('file'))
48
nova/virt/xenapi/image/utils.py
Normal file
48
nova/virt/xenapi/image/utils.py
Normal file
@@ -0,0 +1,48 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.image import glance
class GlanceImage(object):
def __init__(self, context, image_href_or_id):
self._context = context
self._image_service, self._image_id = glance.get_remote_image_service(
context, image_href_or_id)
self._cached_meta = None
@property
def meta(self):
if self._cached_meta is None:
self._cached_meta = self._image_service.show(
self._context, self._image_id)
return self._cached_meta
def download_to(self, fileobj):
return self._image_service.download(
self._context, self._image_id, fileobj)
class RawImage(object):
def __init__(self, glance_image):
self.glance_image = glance_image
def get_size(self):
return int(self.glance_image.meta['size'])
def stream_to(self, fileobj):
return self.glance_image.download_to(fileobj)
@@ -39,7 +39,6 @@ from nova.compute import flavors
from nova.compute import power_state
from nova.compute import task_states
from nova import exception
from nova.image import glance
from nova.network import model as network_model
from nova.openstack.common import excutils
from nova.openstack.common.gettextutils import _
@@ -54,6 +53,7 @@ from nova.virt import configdrive
from nova.virt.disk import api as disk
from nova.virt.disk.vfs import localfs as vfsimpl
from nova.virt.xenapi import agent
from nova.virt.xenapi.image import utils as image_utils
from nova.virt.xenapi import volume_utils
@@ -1277,17 +1277,6 @@ def _check_vdi_size(context, session, instance, vdi_uuid):
raise exception.InstanceTypeDiskTooSmall()
def get_stream_funct_for(context, image_service, image_id):
stream_func = lambda f: image_service.download(
context, image_id, f)
return stream_func
def get_virtual_size(context, image_service, image_id):
meta = image_service.show(context, image_id)
return int(meta['size'])
def _fetch_disk_image(context, session, instance, name_label, image_id,
image_type):
"""Fetch the image from Glance
@@ -1313,9 +1302,10 @@ def _fetch_disk_image(context, session, instance, name_label, image_id,
else:
sr_ref = safe_find_sr(session)
image_service, image_id = glance.get_remote_image_service(
context, image_id)
virtual_size = get_virtual_size(context, image_service, image_id)
glance_image = image_utils.GlanceImage(context, image_id)
image = image_utils.RawImage(glance_image)
virtual_size = image.get_size()
vdi_size = virtual_size
LOG.debug(_("Size for image %(image_id)s: %(virtual_size)d"),
{'image_id': image_id, 'virtual_size': virtual_size},
@@ -1340,9 +1330,8 @@ def _fetch_disk_image(context, session, instance, name_label, image_id,
vdi_uuid = session.call_xenapi("VDI.get_uuid", vdi_ref)
with vdi_attached_here(session, vdi_ref, read_only=False) as dev:
stream_func = get_stream_funct_for(context, image_service,
image_id)
_stream_disk(session, stream_func, image_type, virtual_size, dev)
_stream_disk(
session, image.stream_to, image_type, virtual_size, dev)
if image_type in (ImageType.KERNEL, ImageType.RAMDISK):
# We need to invoke a plugin for copying the
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.