Merge _spawn and _configure_instance in xenapi driver

They were separated only to make it easier to stubout, but we want
more test coverage, not less. So merge them together and fix the
tests while we're at it.

Change-Id: I18333017868b91fb71b8d3dce831463a1cd5bf81
This commit is contained in:
Johannes Erdfelt 2012-04-13 22:26:13 +00:00
parent f6a691b7af
commit 548cba0637
5 changed files with 55 additions and 51 deletions

View File

@ -348,6 +348,7 @@ def stub_out_db_instance_api(stubs, injected=True):
'm1.tiny': dict(id=2,
memory_mb=512,
vcpus=1,
vcpu_weight=None,
root_gb=0,
ephemeral_gb=10,
flavorid=1,
@ -356,6 +357,7 @@ def stub_out_db_instance_api(stubs, injected=True):
'm1.small': dict(id=5,
memory_mb=2048,
vcpus=1,
vcpu_weight=None,
root_gb=20,
ephemeral_gb=0,
flavorid=2,
@ -365,6 +367,7 @@ def stub_out_db_instance_api(stubs, injected=True):
dict(id=1,
memory_mb=4096,
vcpus=2,
vcpu_weight=None,
root_gb=40,
ephemeral_gb=40,
flavorid=3,
@ -373,6 +376,7 @@ def stub_out_db_instance_api(stubs, injected=True):
'm1.large': dict(id=3,
memory_mb=8192,
vcpus=4,
vcpu_weight=None,
root_gb=80,
ephemeral_gb=80,
flavorid=4,
@ -382,6 +386,7 @@ def stub_out_db_instance_api(stubs, injected=True):
dict(id=4,
memory_mb=16384,
vcpus=8,
vcpu_weight=None,
root_gb=160,
ephemeral_gb=160,
flavorid=5,

View File

@ -211,10 +211,6 @@ class XenAPIVolumeTestCase(test.TestCase):
'/dev/sdc')
def configure_instance(*args):
pass
class XenAPIVMTestCase(test.TestCase):
"""Unit tests for VM operations."""
def setUp(self):
@ -234,8 +230,6 @@ class XenAPIVMTestCase(test.TestCase):
stubs.stubout_get_this_vm_uuid(self.stubs)
stubs.stubout_stream_disk(self.stubs)
stubs.stubout_is_vdi_pv(self.stubs)
self.stubs.Set(vmops.VMOps, '_configure_instance',
configure_instance)
stubs.stub_out_vm_methods(self.stubs)
glance_stubs.stubout_glance_client(self.stubs)
fake_utils.stub_out_utils_execute(self.stubs)
@ -480,6 +474,9 @@ class XenAPIVMTestCase(test.TestCase):
if empty_dns:
network_info[0][1]['dns'] = []
# 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,
'disk_format': 'vhd'}
self.conn.spawn(self.context, instance, image_meta, network_info)
@ -778,6 +775,7 @@ class XenAPIVMTestCase(test.TestCase):
image_meta = {'id': glance_stubs.FakeGlance.IMAGE_VHD,
'disk_format': 'vhd'}
if spawn:
instance.admin_pass = 'herp'
self.conn.spawn(self.context, instance, image_meta, network_info)
return instance

View File

@ -389,9 +389,6 @@ def stub_out_migration_methods(stubs):
def fake_destroy(*args, **kwargs):
pass
def fake_reset_network(*args, **kwargs):
pass
@classmethod
def fake_generate_ephemeral(cls, *args):
pass
@ -402,5 +399,4 @@ def stub_out_migration_methods(stubs):
stubs.Set(vmops.VMOps, '_create_snapshot', fake_create_snapshot)
stubs.Set(vm_utils.VMHelper, 'get_vdi_for_vm_safely', fake_get_vdi)
stubs.Set(vm_utils.VMHelper, 'get_sr_path', fake_get_sr_path)
stubs.Set(vmops.VMOps, 'reset_network', fake_reset_network)
stubs.Set(vm_utils.VMHelper, 'generate_ephemeral', fake_generate_ephemeral)

View File

@ -489,12 +489,20 @@ class SessionBase(object):
def host_call_plugin(self, _1, _2, plugin, method, _5):
if (plugin, method) == ('agent', 'version'):
return as_json(returncode='0', message='1.0')
elif (plugin, method) == ('agent', 'key_init'):
return as_json(returncode='D0', message='1')
elif (plugin, method) == ('agent', 'password'):
return as_json(returncode='0', message='success')
elif (plugin, method) == ('agent', 'resetnetwork'):
return as_json(returncode='0', message='success')
elif (plugin, method) == ('glance', 'copy_kernel_vdi'):
return ''
elif (plugin, method) == ('glance', 'upload_vhd'):
return ''
elif (plugin, method) == ('glance', 'create_kernel_ramdisk'):
return ''
elif (plugin, method) == ('glance', 'remove_kernel_ramdisk'):
return ''
elif (plugin, method) == ('migration', 'move_vhds_into_sr'):
return ''
elif (plugin, method) == ('migration', 'transfer_vhd'):

View File

@ -330,7 +330,7 @@ class VMOps(object):
@step
def boot_instance_step(undo_mgr, vm_ref):
self._spawn(instance, vm_ref)
self._boot_new_instance(instance, vm_ref)
@step
def apply_security_group_filters_step(undo_mgr):
@ -493,45 +493,8 @@ class VMOps(object):
userdevice, bootable=False)
userdevice += 1
def _configure_instance(self, ctx, instance, vm_ref,
skip_set_password=False):
# Inject files, if necessary
injected_files = instance.injected_files
if injected_files:
# Check if this is a JSON-encoded string and convert if needed.
if isinstance(injected_files, basestring):
try:
injected_files = json.loads(injected_files)
except ValueError:
LOG.exception(_("Invalid value for injected_files: %r"),
injected_files, instance=instance)
injected_files = []
# Inject any files, if specified
for path, contents in instance.injected_files:
LOG.debug(_("Injecting file path: '%s'") % path,
instance=instance)
self.inject_file(instance, path, contents)
admin_password = instance.admin_pass
# Set admin password, if necessary
if admin_password and not skip_set_password:
LOG.debug(_("Setting admin password"), instance=instance)
self.set_admin_password(instance, admin_password)
# Reset network config
LOG.debug(_("Resetting network"), instance=instance)
self.reset_network(instance, vm_ref)
# Set VCPU weight
inst_type = db.instance_type_get(ctx, instance.instance_type_id)
vcpu_weight = inst_type['vcpu_weight']
if vcpu_weight is not None:
LOG.debug(_("Setting VCPU weight"), instance=instance)
self._session.call_xenapi("VM.add_to_VCPUs_params", vm_ref,
"weight", str(vcpu_weight))
def _spawn(self, instance, vm_ref):
"""Spawn a new instance."""
def _boot_new_instance(self, instance, vm_ref):
"""Boot a new instance and configure it."""
LOG.debug(_('Starting VM'), instance=instance)
self._start(instance, vm_ref)
@ -577,7 +540,41 @@ class VMOps(object):
# if the guest agent is not available, configure the
# instance, but skip the admin password configuration
no_agent = version is None
self._configure_instance(ctx, instance, vm_ref, no_agent)
# Inject files, if necessary
injected_files = instance.injected_files
if injected_files:
# Check if this is a JSON-encoded string and convert if needed.
if isinstance(injected_files, basestring):
try:
injected_files = json.loads(injected_files)
except ValueError:
LOG.exception(_("Invalid value for injected_files: %r"),
injected_files, instance=instance)
injected_files = []
# Inject any files, if specified
for path, contents in instance.injected_files:
LOG.debug(_("Injecting file path: '%s'") % path,
instance=instance)
self.inject_file(instance, path, contents)
admin_password = instance.admin_pass
# Set admin password, if necessary
if admin_password and not no_agent:
LOG.debug(_("Setting admin password"), instance=instance)
self.set_admin_password(instance, admin_password)
# Reset network config
LOG.debug(_("Resetting network"), instance=instance)
self.reset_network(instance, vm_ref)
# Set VCPU weight
inst_type = db.instance_type_get(ctx, instance.instance_type_id)
vcpu_weight = inst_type['vcpu_weight']
if vcpu_weight is not None:
LOG.debug(_("Setting VCPU weight"), instance=instance)
self._session.call_xenapi('VM.add_to_VCPUs_params', vm_ref,
'weight', str(vcpu_weight))
def _get_vm_opaque_ref(self, instance):
vm_ref = VMHelper.lookup(self._session, instance['name'])