Merge "Use flavor stored with instance in vmware driver"

This commit is contained in:
Jenkins 2015-03-09 12:13:19 +00:00 committed by Gerrit Code Review
commit 9802c9bc6f
5 changed files with 20 additions and 55 deletions

View File

@ -22,8 +22,6 @@ import contextlib
import mock
from oslo_vmware import exceptions as vexc
from nova import db
from nova.tests.unit import test_flavors
from nova.tests.unit.virt.vmwareapi import fake
from nova.virt.vmwareapi import driver
from nova.virt.vmwareapi import images
@ -70,18 +68,6 @@ def fake_session_permission_exception():
raise vexc.VimFaultException(fault_list, fault_string, details=details)
def _fake_flavor_get(context, id):
for instance_type in test_flavors.DEFAULT_FLAVORS:
if instance_type['id'] == id:
return instance_type
return {'memory_mb': 128, 'root_gb': 0, 'deleted_at': None,
'name': 'm1.micro', 'deleted': 0, 'created_at': None,
'ephemeral_gb': 0, 'updated_at': None,
'disabled': False, 'vcpus': 1, 'extra_specs': {},
'swap': 0, 'rxtx_factor': 1.0, 'is_public': True,
'flavorid': '1', 'vcpu_weight': None, 'id': 2}
def set_stubs(stubs):
"""Set the stubs."""
stubs.Set(network_util, 'get_network_with_the_name',
@ -91,7 +77,6 @@ def set_stubs(stubs):
stubs.Set(driver.VMwareAPISession, "vim", fake_vim_prop)
stubs.Set(driver.VMwareAPISession, "_is_vim_object",
fake_is_vim_object)
stubs.Set(db, 'flavor_get', _fake_flavor_get)
def fake_suds_context(calls=None):

View File

@ -19,6 +19,7 @@ from mox3 import mox
from nova import context
from nova.image import glance
from nova import objects
from nova import test
from nova.tests.unit import fake_instance
import nova.tests.unit.image.fake
@ -81,6 +82,7 @@ class ConfigDriveTestCase(test.NoDBTestCase):
}
self.test_instance = fake_instance.fake_instance_obj(self.context,
**instance_values)
self.test_instance.flavor = objects.Flavor(extra_specs={})
(image_service, image_id) = glance.get_remote_image_service(context,
image_ref)

View File

@ -43,7 +43,6 @@ from nova.compute import power_state
from nova.compute import task_states
from nova.compute import vm_states
from nova import context
from nova import db
from nova import exception
from nova.image import glance
from nova.network import model as network_model
@ -366,12 +365,14 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
def _create_instance(self, node=None, set_image_ref=True,
uuid=None, instance_type='m1.large',
ephemeral=None):
ephemeral=None, instance_type_updates=None):
if not node:
node = self.node_name
if not uuid:
uuid = uuidutils.generate_uuid()
self.type_data = self._get_instance_type_by_name(instance_type)
if instance_type_updates:
self.type_data.update(instance_type_updates)
if ephemeral is not None:
self.type_data['ephemeral_gb'] = ephemeral
values = {'name': 'fake_name',
@ -397,16 +398,18 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
self.uuid = uuid
self.instance = fake_instance.fake_instance_obj(
self.context, **values)
self.instance.flavor = objects.Flavor(**self.type_data)
def _create_vm(self, node=None, num_instances=1, uuid=None,
instance_type='m1.large', powered_on=True,
ephemeral=None, bdi=None):
ephemeral=None, bdi=None, instance_type_updates=None):
"""Create and spawn the VM."""
if not node:
node = self.node_name
self._create_instance(node=node, uuid=uuid,
instance_type=instance_type,
ephemeral=ephemeral)
ephemeral=ephemeral,
instance_type_updates=instance_type_updates)
self.assertIsNone(vm_util.vm_ref_cache_get(self.uuid))
self.conn.spawn(self.context, self.instance, self.image,
injected_files=[], admin_password=None,
@ -1149,13 +1152,8 @@ class VMwareAPIVMTestCase(test.NoDBTestCase):
block_device_info=block_device_info)
def test_spawn_hw_versions(self):
def _fake_flavor_get(context, id):
flavor = stubs._fake_flavor_get(context, id)
flavor['extra_specs'] = {'vmware:hw_version': 'vmx-08'}
return flavor
with mock.patch.object(db, 'flavor_get', _fake_flavor_get):
self._create_vm()
updates = {'extra_specs': {'vmware:hw_version': 'vmx-08'}}
self._create_vm(instance_type_updates=updates)
vm = self._get_vm_record()
version = vm.get("version")
self.assertEqual('vmx-08', version)

View File

@ -21,7 +21,6 @@ from oslo_vmware import exceptions as vexc
from nova.compute import power_state
from nova import context
from nova import db
from nova import exception
from nova.network import model as network_model
from nova import objects
@ -86,6 +85,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
self._flavor = objects.Flavor(name='m1.small', memory_mb=512, vcpus=1,
root_gb=10, ephemeral_gb=0, swap=0,
extra_specs={})
self._instance.flavor = self._flavor
self._vmops = vmops.VMwareVMOps(self._session, self._virtapi, None,
cluster=cluster.obj)
@ -774,7 +774,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
@mock.patch(
'nova.virt.vmwareapi.imagecache.ImageCacheManager.enlist_image')
@mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine')
@mock.patch.object(objects.Flavor, 'get_by_id')
@mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info')
@mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs')
@mock.patch.object(nova.virt.vmwareapi.images.VMwareImage,
@ -782,13 +781,11 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
def test_spawn_non_root_block_device(self, from_image,
get_extra_specs,
get_vm_config_info,
get_by_id,
build_virtual_machine,
enlist_image, fetch_image,
use_disk_image,
power_on_instance):
extra_specs = get_extra_specs.return_value
get_by_id.return_value = self._flavor
connection_info1 = {'data': 'fake-data1', 'serial': 'volume-fake-id1'}
connection_info2 = {'data': 'fake-data2', 'serial': 'volume-fake-id2'}
@ -832,7 +829,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
@mock.patch('nova.virt.vmwareapi.vm_util.power_on_instance')
@mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine')
@mock.patch.object(objects.Flavor, 'get_by_id')
@mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info')
@mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs')
@mock.patch.object(nova.virt.vmwareapi.images.VMwareImage,
@ -840,7 +836,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
def test_spawn_with_no_image_and_block_devices(self, from_image,
get_extra_specs,
get_vm_config_info,
get_by_id,
build_virtual_machine,
power_on_instance):
instance_values = {
@ -855,8 +850,8 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
}
instance = fake_instance.fake_instance_obj(
self._context, **instance_values)
instance.flavor = self._flavor
extra_specs = get_extra_specs.return_value
get_by_id.return_value = self._flavor
connection_info1 = {'data': 'fake-data1', 'serial': 'volume-fake-id1'}
connection_info2 = {'data': 'fake-data2', 'serial': 'volume-fake-id2'}
@ -901,7 +896,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
@mock.patch('nova.virt.vmwareapi.vm_util.power_on_instance')
@mock.patch.object(vmops.VMwareVMOps, 'build_virtual_machine')
@mock.patch.object(objects.Flavor, 'get_by_id')
@mock.patch.object(vmops.VMwareVMOps, '_get_vm_config_info')
@mock.patch.object(vmops.VMwareVMOps, '_get_extra_specs')
@mock.patch.object(nova.virt.vmwareapi.images.VMwareImage,
@ -909,7 +903,6 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
def test_spawn_unsupported_hardware(self, from_image,
get_extra_specs,
get_vm_config_info,
get_by_id,
build_virtual_machine,
power_on_instance):
instance_values = {
@ -924,8 +917,8 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
}
instance = fake_instance.fake_instance_obj(
self._context, **instance_values)
instance.flavor = self._flavor
extra_specs = get_extra_specs.return_value
get_by_id.return_value = self._flavor
connection_info = {'data': 'fake-data', 'serial': 'volume-fake-id'}
bdm = [{'boot_index': 0,
@ -1551,17 +1544,10 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
actual.cpu_limits.cpu_shares_share)
def _validate_flavor_extra_specs(self, flavor_extra_specs, expected):
def _fake_flavor_get(context, id):
flavor = stubs._fake_flavor_get(context, id)
flavor['extra_specs'] = flavor_extra_specs
return flavor
with mock.patch.object(db, 'flavor_get', _fake_flavor_get):
# Validate that the extra specs are parsed correctly
flavor = objects.Flavor.get_by_id(self._context, 1)
flavor_extra_specs = self._vmops._get_extra_specs(flavor)
self._validate_extra_specs(expected, flavor_extra_specs)
# Validate that the extra specs are parsed correctly
flavor = objects.Flavor(extra_specs=flavor_extra_specs)
flavor_extra_specs = self._vmops._get_extra_specs(flavor)
self._validate_extra_specs(expected, flavor_extra_specs)
def test_extra_specs_cpu_limit(self):
flavor_extra_specs = {'quota:cpu_limit': 7}

View File

@ -42,7 +42,6 @@ from nova.console import type as ctype
from nova import context as nova_context
from nova import exception
from nova.i18n import _, _LE, _LI, _LW
from nova import objects
from nova import utils
from nova.virt import configdrive
from nova.virt import diagnostics
@ -568,9 +567,7 @@ class VMwareVMOps(object):
image_meta)
# Read flavors for extra_specs
if flavor is None:
flavor = objects.Flavor.get_by_id(
nova_context.get_admin_context(read_deleted='yes'),
instance.instance_type_id)
flavor = instance.flavor
extra_specs = self._get_extra_specs(flavor)
@ -1229,10 +1226,7 @@ class VMwareVMOps(object):
vm_util.power_off_instance(self._session, instance, vm_ref)
client_factory = self._session.vim.client.factory
# Reconfigure the VM properties
flavor = objects.Flavor.get_by_id(
nova_context.get_admin_context(read_deleted='yes'),
instance.instance_type_id)
extra_specs = self._get_extra_specs(flavor)
extra_specs = self._get_extra_specs(instance.flavor)
vm_resize_spec = vm_util.get_vm_resize_spec(client_factory,
int(instance.vcpus),
int(instance.memory_mb),