Remove direct access to glance client

All internal clients of Glance should be using GlanceImageService. This
refactors the code in the xen and vmware virt drivers that were
directly instantiating a glance.client object

Related to bp integrate-python-glanceclient

Change-Id: I354254060e400d9c1dfd6d041b071e3960a9ece6
This commit is contained in:
Brian Waldon
2012-06-23 12:32:50 -07:00
parent dcaafb892d
commit 8172a3d706
3 changed files with 97 additions and 33 deletions

View File

@@ -24,8 +24,9 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
import nova.image.fake
from nova import test
from nova.tests.glance import stubs as glance_stubs
import nova.tests.api.openstack.fakes as api_fakes
from nova.tests.vmwareapi import db_fakes
from nova.tests.vmwareapi import stubs
from nova.virt.vmwareapi import fake as vmwareapi_fake
@@ -50,7 +51,6 @@ class VMWareAPIVMTestCase(test.TestCase):
vmwareapi_fake.reset()
db_fakes.stub_out_db_instance_api(self.stubs)
stubs.set_stubs(self.stubs)
glance_stubs.stubout_glance_client(self.stubs)
self.conn = vmwareapi_conn.VMWareESXDriver(False)
# NOTE(vish): none of the network plugging code is actually
# being tested
@@ -78,10 +78,12 @@ class VMWareAPIVMTestCase(test.TestCase):
'disk_format': 'vhd',
'size': 512,
}
api_fakes.stub_out_image_service(self.stubs)
def tearDown(self):
super(VMWareAPIVMTestCase, self).tearDown()
vmwareapi_fake.cleanup()
nova.image.fake.FakeImageService_reset()
def _create_instance_in_the_db(self):
values = {'name': 1,

View File

@@ -29,6 +29,7 @@ from nova import context
from nova import db
from nova import exception
from nova import flags
import nova.image.fake
from nova import log as logging
from nova.openstack.common import importutils
from nova.openstack.common import timeutils
@@ -36,18 +37,68 @@ from nova import test
from nova.tests.db import fakes as db_fakes
from nova.tests import fake_network
from nova.tests import fake_utils
from nova.tests.glance import stubs as glance_stubs
from nova.tests.xenapi import stubs
from nova.virt.xenapi import connection as xenapi_conn
from nova.virt.xenapi import fake as xenapi_fake
from nova.virt.xenapi import vm_utils
from nova.virt.xenapi import vmops
from nova.virt.xenapi import volume_utils
import nova.tests.api.openstack.fakes as api_fakes
LOG = logging.getLogger(__name__)
FLAGS = flags.FLAGS
IMAGE_MACHINE = '1'
IMAGE_KERNEL = '2'
IMAGE_RAMDISK = '3'
IMAGE_RAW = '4'
IMAGE_VHD = '5'
IMAGE_ISO = '6'
IMAGE_FIXTURES = {
IMAGE_MACHINE: {
'image_meta': {'name': 'fakemachine', 'size': 0,
'disk_format': 'ami',
'container_format': 'ami'},
},
IMAGE_KERNEL: {
'image_meta': {'name': 'fakekernel', 'size': 0,
'disk_format': 'aki',
'container_format': 'aki'},
},
IMAGE_RAMDISK: {
'image_meta': {'name': 'fakeramdisk', 'size': 0,
'disk_format': 'ari',
'container_format': 'ari'},
},
IMAGE_RAW: {
'image_meta': {'name': 'fakeraw', 'size': 0,
'disk_format': 'raw',
'container_format': 'bare'},
},
IMAGE_VHD: {
'image_meta': {'name': 'fakevhd', 'size': 0,
'disk_format': 'vhd',
'container_format': 'ovf'},
},
IMAGE_ISO: {
'image_meta': {'name': 'fakeiso', 'size': 0,
'disk_format': 'iso',
'container_format': 'bare'},
},
}
def set_image_fixtures():
image_service = nova.image.fake.FakeImageService()
image_service.delete_all()
for image_id, image_meta in IMAGE_FIXTURES.items():
image_meta = image_meta['image_meta']
image_meta['id'] = image_id
image_service.create(None, image_meta)
def stub_vm_utils_with_vdi_attached_here(function, should_return=True):
"""
@@ -62,23 +113,23 @@ def stub_vm_utils_with_vdi_attached_here(function, should_return=True):
fake_dev = 'fakedev'
yield fake_dev
def fake_stream_disk(*args, **kwargs):
def fake_image_service_get(*args, **kwargs):
pass
def fake_is_vdi_pv(*args, **kwargs):
return should_return
orig_vdi_attached_here = vm_utils.vdi_attached_here
orig_stream_disk = vm_utils._stream_disk
orig_image_service_get = nova.image.fake._FakeImageService.get
orig_is_vdi_pv = vm_utils._is_vdi_pv
try:
vm_utils.vdi_attached_here = fake_vdi_attached_here
vm_utils._stream_disk = fake_stream_disk
nova.image.fake._FakeImageService.get = fake_image_service_get
vm_utils._is_vdi_pv = fake_is_vdi_pv
return function(self, *args, **kwargs)
finally:
vm_utils._is_vdi_pv = orig_is_vdi_pv
vm_utils._stream_disk = orig_stream_disk
nova.image.fake._FakeImageService.get = orig_image_service_get
vm_utils.vdi_attached_here = orig_vdi_attached_here
return decorated_function
@@ -219,16 +270,23 @@ class XenAPIVMTestCase(test.TestCase):
xenapi_fake.create_network('fake', FLAGS.flat_network_bridge)
stubs.stubout_session(self.stubs, stubs.FakeSessionForVMTests)
stubs.stubout_get_this_vm_uuid(self.stubs)
stubs.stubout_stream_disk(self.stubs)
stubs.stubout_is_vdi_pv(self.stubs)
stubs.stub_out_vm_methods(self.stubs)
glance_stubs.stubout_glance_client(self.stubs)
fake_utils.stub_out_utils_execute(self.stubs)
self.user_id = 'fake'
self.project_id = 'fake'
self.context = context.RequestContext(self.user_id, self.project_id)
self.conn = xenapi_conn.XenAPIDriver(False)
api_fakes.stub_out_image_service(self.stubs)
set_image_fixtures()
stubs.stubout_image_service_get(self.stubs)
stubs.stubout_stream_disk(self.stubs)
def tearDown(self):
super(XenAPIVMTestCase, self).tearDown()
nova.image.fake.FakeImageService_reset()
def test_init_host(self):
session = xenapi_conn.XenAPISession('test_url', 'root', 'test_pass')
vm = vm_utils.get_this_vm_ref(session)
@@ -477,7 +535,7 @@ class XenAPIVMTestCase(test.TestCase):
# admin_pass isn't part of the DB model, but it does get set as
# an attribute for spawn to use
instance.admin_pass = 'herp'
image_meta = {'id': glance_stubs.FakeGlance.IMAGE_VHD,
image_meta = {'id': IMAGE_VHD,
'disk_format': 'vhd'}
self.conn.spawn(self.context, instance, image_meta, network_info)
self.create_vm_record(self.conn, os_type, instance['name'])
@@ -487,7 +545,7 @@ class XenAPIVMTestCase(test.TestCase):
def test_spawn_empty_dns(self):
"""Test spawning with an empty dns list"""
self._test_spawn(glance_stubs.FakeGlance.IMAGE_VHD, None, None,
self._test_spawn(IMAGE_VHD, None, None,
os_type="linux", architecture="x86-64",
empty_dns=True)
self.check_vm_params_for_linux()
@@ -527,11 +585,11 @@ class XenAPIVMTestCase(test.TestCase):
@stub_vm_utils_with_vdi_attached_here
def test_spawn_raw_glance(self):
self._test_spawn(glance_stubs.FakeGlance.IMAGE_RAW, None, None)
self._test_spawn(IMAGE_RAW, None, None)
self.check_vm_params_for_linux()
def test_spawn_vhd_glance_linux(self):
self._test_spawn(glance_stubs.FakeGlance.IMAGE_VHD, None, None,
self._test_spawn(IMAGE_VHD, None, None,
os_type="linux", architecture="x86-64")
self.check_vm_params_for_linux()
@@ -560,20 +618,20 @@ class XenAPIVMTestCase(test.TestCase):
self.assertEqual(len(self.vm['VBDs']), 1)
def test_spawn_vhd_glance_windows(self):
self._test_spawn(glance_stubs.FakeGlance.IMAGE_VHD, None, None,
self._test_spawn(IMAGE_VHD, None, None,
os_type="windows", architecture="i386")
self.check_vm_params_for_windows()
def test_spawn_iso_glance(self):
self._test_spawn(glance_stubs.FakeGlance.IMAGE_ISO, None, None,
self._test_spawn(IMAGE_ISO, None, None,
os_type="windows", architecture="i386")
self.check_vm_params_for_windows()
def test_spawn_glance(self):
stubs.stubout_fetch_image_glance_disk(self.stubs)
self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE,
glance_stubs.FakeGlance.IMAGE_KERNEL,
glance_stubs.FakeGlance.IMAGE_RAMDISK)
self._test_spawn(IMAGE_MACHINE,
IMAGE_KERNEL,
IMAGE_RAMDISK)
self.check_vm_params_for_linux_with_external_kernel()
def test_spawn_netinject_file(self):
@@ -603,9 +661,9 @@ class XenAPIVMTestCase(test.TestCase):
# Capture the tee .../etc/network/interfaces command
(r'tee.*interfaces', _tee_handler),
])
self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE,
glance_stubs.FakeGlance.IMAGE_KERNEL,
glance_stubs.FakeGlance.IMAGE_RAMDISK,
self._test_spawn(IMAGE_MACHINE,
IMAGE_KERNEL,
IMAGE_RAMDISK,
check_injection=True)
self.assertTrue(self._tee_executed)
@@ -654,8 +712,7 @@ class XenAPIVMTestCase(test.TestCase):
self.assertFalse(self._tee_executed)
def test_spawn_vlanmanager(self):
self.flags(image_service='nova.image.glance.GlanceImageService',
network_manager='nova.network.manager.VlanManager',
self.flags(network_manager='nova.network.manager.VlanManager',
vlan_interface='fake0')
def dummy(*args, **kwargs):
@@ -678,9 +735,9 @@ class XenAPIVMTestCase(test.TestCase):
vpn=None,
rxtx_factor=3,
project_id=self.project_id)
self._test_spawn(glance_stubs.FakeGlance.IMAGE_MACHINE,
glance_stubs.FakeGlance.IMAGE_KERNEL,
glance_stubs.FakeGlance.IMAGE_RAMDISK,
self._test_spawn(IMAGE_MACHINE,
IMAGE_KERNEL,
IMAGE_RAMDISK,
instance_id=2,
create_record=False)
# TODO(salvatore-orlando): a complete test here would require
@@ -704,7 +761,7 @@ class XenAPIVMTestCase(test.TestCase):
xenapi_fake.create_vbd(vm_ref, "rootfs", userdevice=0)
conn = xenapi_conn.XenAPIDriver(False)
image_meta = {'id': glance_stubs.FakeGlance.IMAGE_VHD,
image_meta = {'id': IMAGE_VHD,
'disk_format': 'vhd'}
conn.rescue(self.context, instance, [], image_meta)
@@ -765,7 +822,7 @@ class XenAPIVMTestCase(test.TestCase):
instance = db.instance_create(self.context, instance_values)
network_info = fake_network.fake_get_instance_nw_info(self.stubs,
spectacular=True)
image_meta = {'id': glance_stubs.FakeGlance.IMAGE_VHD,
image_meta = {'id': IMAGE_VHD,
'disk_format': 'vhd'}
if spawn:
instance.admin_pass = 'herp'
@@ -855,7 +912,6 @@ class XenAPIMigrateInstance(test.TestCase):
fake_utils.stub_out_utils_execute(self.stubs)
stubs.stub_out_migration_methods(self.stubs)
stubs.stubout_get_this_vm_uuid(self.stubs)
glance_stubs.stubout_glance_client(self.stubs)
def test_resize_xenserver_6(self):
instance = db.instance_create(self.context, self.instance_values)
@@ -1019,7 +1075,6 @@ class XenAPIDetermineDiskImageTestCase(test.TestCase):
"""Unit tests for code that detects the ImageType."""
def setUp(self):
super(XenAPIDetermineDiskImageTestCase, self).setUp()
glance_stubs.stubout_glance_client(self.stubs)
class FakeInstance(object):
pass

View File

@@ -19,6 +19,7 @@ import random
from eventlet import tpool
import nova.image.fake
from nova.openstack.common import jsonutils
from nova.virt.xenapi import connection as xenapi_conn
from nova.virt.xenapi import fake
@@ -81,10 +82,16 @@ def stubout_get_this_vm_uuid(stubs):
stubs.Set(vm_utils, 'get_this_vm_uuid', f)
def stubout_stream_disk(stubs):
def f(_1, _2, _3, _4):
def stubout_image_service_get(stubs):
def fake_get(*args, **kwargs):
pass
stubs.Set(vm_utils, '_stream_disk', f)
stubs.Set(nova.image.fake._FakeImageService, 'get', fake_get)
def stubout_stream_disk(stubs):
def fake_stream_disk(*args, **kwargs):
pass
stubs.Set(vm_utils, '_stream_disk', fake_stream_disk)
def stubout_is_vdi_pv(stubs):