Merge "Trim the fat from InstanceInfo"

This commit is contained in:
Jenkins 2017-09-06 15:34:04 +00:00 committed by Gerrit Code Review
commit 7615b52dac
22 changed files with 64 additions and 215 deletions

View File

@ -283,12 +283,10 @@ driver-impl-powervm=unknown
[operation.get-guest-info]
title=Guest instance status
status=mandatory
notes=Provides a quick report on information about the guest instance,
including the power state, memory allocation, CPU allocation, number
of vCPUs and cumulative CPU execution time. As well as being
informational, the power state is used by the compute manager for
tracking changes in guests. Therefore this operation is considered
mandatory to support.
notes=Provides realtime information about the power state of the guest
instance. Since the power state is used by the compute manager for
tracking changes in guests, this operation is considered mandatory to
support.
cli=
driver-impl-xenserver=complete
driver-impl-libvirt-kvm-x86=complete

View File

@ -101,11 +101,7 @@ class VMOpsTestCase(test_base.HyperVBaseTestCase):
return fake_info[key]
mock_info.__getitem__.side_effect = getitem
expected = hardware.InstanceInfo(state=constants.HYPERV_POWER_STATE[2],
max_mem_kb=mock.sentinel.FAKE_MEM_KB,
mem_kb=mock.sentinel.FAKE_MEM_KB,
num_cpu=mock.sentinel.FAKE_NUM_CPU,
cpu_time_ns=mock.sentinel.FAKE_CPU_NS)
expected = hardware.InstanceInfo(state=constants.HYPERV_POWER_STATE[2])
self._vmops._vmutils.vm_exists.return_value = vm_exists
self._vmops._vmutils.get_vm_summary_info.return_value = mock_info

View File

@ -968,14 +968,10 @@ class IronicDriverTestCase(test.NoDBTestCase):
# ironic_states.POWER_ON should be mapped to
# nova_states.RUNNING
memory_kib = properties['memory_mb'] * 1024
instance = fake_instance.fake_instance_obj('fake-context',
uuid=self.instance_uuid)
result = self.driver.get_info(instance)
self.assertEqual(hardware.InstanceInfo(state=nova_states.RUNNING,
max_mem_kb=memory_kib,
mem_kb=memory_kib,
num_cpu=properties['cpus']),
self.assertEqual(hardware.InstanceInfo(state=nova_states.RUNNING),
result)
@mock.patch.object(FAKE_CLIENT.node, 'get_by_instance_uuid')

View File

@ -12323,7 +12323,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
mock_get_domain.return_value = dom_mock
mock_get_info.return_value = hardware.InstanceInfo(
state=power_state.SHUTDOWN, id=-1)
state=power_state.SHUTDOWN, internal_id=-1)
mock_delete_instance_files.return_value = None
instance = objects.Instance(self.context, **self.test_instance)
@ -12384,7 +12384,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr._has_uefi_support = mock.Mock(return_value=False)
drvr.delete_instance_files = mock.Mock(return_value=None)
drvr.get_info = mock.Mock(return_value=
hardware.InstanceInfo(state=power_state.SHUTDOWN, id=-1)
hardware.InstanceInfo(state=power_state.SHUTDOWN, internal_id=-1)
)
instance = objects.Instance(self.context, **self.test_instance)
@ -12407,7 +12407,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr._has_uefi_support = mock.Mock(return_value=False)
drvr.delete_instance_files = mock.Mock(return_value=None)
drvr.get_info = mock.Mock(return_value=
hardware.InstanceInfo(state=power_state.SHUTDOWN, id=-1)
hardware.InstanceInfo(state=power_state.SHUTDOWN, internal_id=-1)
)
instance = objects.Instance(self.context, **self.test_instance)
@ -12433,7 +12433,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr._has_uefi_support = mock.Mock(return_value=False)
drvr.delete_instance_files = mock.Mock(return_value=None)
drvr.get_info = mock.Mock(return_value=
hardware.InstanceInfo(state=power_state.SHUTDOWN, id=-1)
hardware.InstanceInfo(state=power_state.SHUTDOWN, internal_id=-1)
)
instance = objects.Instance(self.context, **self.test_instance)
@ -12455,9 +12455,8 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr._host.get_domain = mock.Mock(return_value=mock_domain)
drvr._has_uefi_support = mock.Mock(return_value=True)
drvr.delete_instance_files = mock.Mock(return_value=None)
drvr.get_info = mock.Mock(return_value=
hardware.InstanceInfo(state=power_state.SHUTDOWN, id=-1)
)
drvr.get_info = mock.Mock(return_value=hardware.InstanceInfo(
state=power_state.SHUTDOWN, internal_id=-1))
instance = objects.Instance(self.context, **self.test_instance)
drvr.destroy(self.context, instance, [])
@ -12530,7 +12529,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
mock_domain.destroy.side_effect = ex
mock_info = mock.MagicMock()
mock_info.id = 1
mock_info.internal_id = 1
mock_info.state = power_state.SHUTDOWN
mock_get_info.return_value = mock_info
@ -12591,7 +12590,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
mock_guest = mock.Mock(libvirt_guest.Guest, id=1)
mock_guest.poweroff = mock.Mock(side_effect=[ex, None])
inst_info = hardware.InstanceInfo(power_state.SHUTDOWN, id=1)
inst_info = hardware.InstanceInfo(power_state.SHUTDOWN, internal_id=1)
instance = objects.Instance(**self.test_instance)
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
@ -13906,11 +13905,7 @@ class LibvirtConnTestCase(test.NoDBTestCase,
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
info = drvr.get_info(instance)
self.assertEqual(1, info.state)
self.assertEqual(2048, info.max_mem_kb)
self.assertEqual(737, info.mem_kb)
self.assertEqual(8, info.num_cpu)
self.assertEqual(12345, info.cpu_time_ns)
self.assertEqual(mock.sentinel.instance_id, info.id)
self.assertEqual(mock.sentinel.instance_id, info.internal_id)
dom_mock.info.assert_called_once_with()
dom_mock.ID.assert_called_once_with()
mock_get_domain.assert_called_once_with(instance)

View File

@ -554,11 +554,7 @@ class GuestTestCase(test.NoDBTestCase):
info = self.guest.get_info(self.host)
self.domain.info.assert_called_once_with()
self.assertEqual(1, info.state)
self.assertEqual(2, info.max_mem_kb)
self.assertEqual(3, info.mem_kb)
self.assertEqual(4, info.num_cpu)
self.assertEqual(5, info.cpu_time_ns)
self.assertEqual(6, info.id)
self.assertEqual(6, info.internal_id)
def test_get_power_state(self):
self.domain.info.return_value = (1, 2, 3, 4, 5)

View File

@ -75,12 +75,16 @@ class TestPowerVMDriver(test.NoDBTestCase):
self.assertEqual(mock_img.return_value, self.drv.image_api)
@mock.patch('nova.virt.powervm.vm.get_pvm_uuid')
def test_get_info(self, mock_uuid):
mock_uuid.return_value = 'uuid'
info = self.drv.get_info('inst')
self.assertIsInstance(info, hardware.InstanceInfo)
self.assertEqual('uuid', info.id)
@mock.patch('nova.virt.powervm.vm.get_vm_qp')
@mock.patch('nova.virt.powervm.vm._translate_vm_state')
def test_get_info(self, mock_tx_state, mock_qp, mock_uuid):
mock_tx_state.return_value = 'fake-state'
self.assertEqual(hardware.InstanceInfo('fake-state'),
self.drv.get_info('inst'))
mock_uuid.assert_called_once_with('inst')
mock_qp.assert_called_once_with(
self.drv.adapter, mock_uuid.return_value, 'PartitionState')
mock_tx_state.assert_called_once_with(mock_qp.return_value)
@mock.patch('nova.virt.powervm.vm.get_lpar_names')
def test_list_instances(self, mock_names):

View File

@ -241,42 +241,6 @@ class TestVM(test.NoDBTestCase):
self.assertEqual(power_state.CRASHED,
vm._translate_vm_state('error'))
def test_instance_info(self):
inst_info = vm.InstanceInfo(self.apt, 'inst')
self.get_pvm_uuid.assert_called_once_with('inst')
# Test the static properties
self.assertEqual(inst_info.id, self.get_pvm_uuid.return_value)
# Check that we raise an exception if the instance is gone.
self.apt.read.side_effect = pvm_exc.HttpError(
mock.MagicMock(status=404))
self.assertRaises(exception.InstanceNotFound,
inst_info.__getattribute__, 'state')
# Reset the test inst_info
inst_info = vm.InstanceInfo(self.apt, 'inst')
class FakeResp2(object):
def __init__(self, body):
self.body = '"%s"' % body
resp = FakeResp2('running')
def return_resp(*args, **kwds):
return resp
self.apt.read.side_effect = return_resp
self.assertEqual(inst_info.state, power_state.RUNNING)
# Check the __eq__ method
self.get_pvm_uuid.return_value = 'pvm-uuid1'
inst_info1 = vm.InstanceInfo(self.apt, 'inst')
inst_info2 = vm.InstanceInfo(self.apt, 'inst')
self.assertEqual(inst_info1, inst_info2)
self.get_pvm_uuid.return_value = 'pvm-uuid2'
inst_info2 = vm.InstanceInfo(self.apt, 'inst2')
self.assertNotEqual(inst_info1, inst_info2)
@mock.patch('pypowervm.wrappers.logical_partition.LPAR', autospec=True)
def test_get_lpar_names(self, mock_lpar):
inst1 = mock.Mock()

View File

@ -34,47 +34,23 @@ from nova.virt import hardware as hw
class InstanceInfoTests(test.NoDBTestCase):
def test_instance_info_default(self):
ii = hw.InstanceInfo()
self.assertIsNone(ii.state)
self.assertIsNone(ii.id)
self.assertEqual(0, ii.max_mem_kb)
self.assertEqual(0, ii.mem_kb)
self.assertEqual(0, ii.num_cpu)
self.assertEqual(0, ii.cpu_time_ns)
ii = hw.InstanceInfo('fake-state')
self.assertEqual('fake-state', ii.state)
self.assertIsNone(ii.internal_id)
def test_instance_info(self):
ii = hw.InstanceInfo(state='fake-state',
max_mem_kb=1,
mem_kb=2,
num_cpu=3,
cpu_time_ns=4,
id='fake-id')
internal_id='fake-id')
self.assertEqual('fake-state', ii.state)
self.assertEqual('fake-id', ii.id)
self.assertEqual(1, ii.max_mem_kb)
self.assertEqual(2, ii.mem_kb)
self.assertEqual(3, ii.num_cpu)
self.assertEqual(4, ii.cpu_time_ns)
self.assertEqual('fake-id', ii.internal_id)
def test_instance_infoi_equals(self):
def test_instance_info_equals(self):
ii1 = hw.InstanceInfo(state='fake-state',
max_mem_kb=1,
mem_kb=2,
num_cpu=3,
cpu_time_ns=4,
id='fake-id')
internal_id='fake-id')
ii2 = hw.InstanceInfo(state='fake-state',
max_mem_kb=1,
mem_kb=2,
num_cpu=3,
cpu_time_ns=4,
id='fake-id')
internal_id='fake-id')
ii3 = hw.InstanceInfo(state='fake-estat',
max_mem_kb=11,
mem_kb=22,
num_cpu=33,
cpu_time_ns=44,
id='fake-di')
internal_id='fake-di')
self.assertEqual(ii1, ii2)
self.assertNotEqual(ii1, ii3)

View File

@ -428,10 +428,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase,
vm = self._get_vm_record()
# Check that m1.large above turned into the right thing.
mem_kib = int(self.type_data['memory_mb']) << 10
vcpus = self.type_data['vcpus']
self.assertEqual(vm_info.max_mem_kb, mem_kib)
self.assertEqual(vm_info.mem_kb, mem_kib)
self.assertEqual(vm.get("summary.config.instanceUuid"), self.uuid)
self.assertEqual(vm.get("summary.config.numCpu"), vcpus)
self.assertEqual(vm.get("summary.config.memorySizeMB"),
@ -469,11 +466,7 @@ class VMwareAPIVMTestCase(test.NoDBTestCase,
"""Check if the get_info returned values correspond to the instance
object in the db.
"""
mem_kib = int(self.type_data['memory_mb']) << 10
self.assertEqual(info.state, pwr_state)
self.assertEqual(info.max_mem_kb, mem_kib)
self.assertEqual(info.mem_kb, mem_kib)
self.assertEqual(info.num_cpu, self.type_data['vcpus'])
def test_instance_exists(self):
self._create_vm()

View File

@ -301,10 +301,7 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
info = self._vmops.get_info(self._instance)
mock_get_vm_ref.assert_called_once_with(self._session,
self._instance)
expected = hardware.InstanceInfo(state=power_state.RUNNING,
max_mem_kb=128 * 1024,
mem_kb=128 * 1024,
num_cpu=4)
expected = hardware.InstanceInfo(state=power_state.RUNNING)
self.assertEqual(expected, info)
@mock.patch.object(vm_util, 'get_vm_ref', return_value='fake_ref')

View File

@ -2353,7 +2353,5 @@ class VMInfoTests(VMUtilsTestBase):
self.session.call_xenapi.side_effect = call_xenapi
info = vm_utils.compile_info(self.session, "dummy")
self.assertEqual(hardware.InstanceInfo(state=power_state.RUNNING,
max_mem_kb=10, mem_kb=9,
num_cpu='5', cpu_time_ns=0),
self.assertEqual(hardware.InstanceInfo(state=power_state.RUNNING),
info)

View File

@ -623,8 +623,6 @@ class XenAPIVMTestCase(stubs.XenAPITestBase,
vcpus = flavor['vcpus']
vcpu_weight = flavor['vcpu_weight']
self.assertEqual(self.vm_info.max_mem_kb, mem_kib)
self.assertEqual(self.vm_info.mem_kb, mem_kib)
self.assertEqual(self.vm['memory_static_max'], mem_bytes)
self.assertEqual(self.vm['memory_dynamic_max'], mem_bytes)
self.assertEqual(self.vm['memory_dynamic_min'], mem_bytes)

View File

@ -333,11 +333,7 @@ class FakeDriver(driver.ComputeDriver):
if instance.uuid not in self.instances:
raise exception.InstanceNotFound(instance_id=instance.uuid)
i = self.instances[instance.uuid]
return hardware.InstanceInfo(state=i.state,
max_mem_kb=0,
mem_kb=0,
num_cpu=2,
cpu_time_ns=0)
return hardware.InstanceInfo(state=i.state)
def get_diagnostics(self, instance):
return {'cpu0_time': 17300000000,

View File

@ -197,24 +197,15 @@ def get_number_of_serial_ports(flavor, image_meta):
class InstanceInfo(object):
def __init__(self, state=None, max_mem_kb=0, mem_kb=0, num_cpu=0,
cpu_time_ns=0, id=None):
def __init__(self, state, internal_id=None):
"""Create a new Instance Info object
:param state: the running state, one of the power_state codes
:param max_mem_kb: (int) the maximum memory in KBytes allowed
:param mem_kb: (int) the memory in KBytes used by the instance
:param num_cpu: (int) the number of virtual CPUs for the
instance
:param cpu_time_ns: (int) the CPU time used in nanoseconds
:param id: a unique ID for the instance
:param state: Required. The running state, one of the power_state codes
:param internal_id: Optional. A unique ID for the instance. Need not be
related to the Instance.uuid.
"""
self.state = state
self.max_mem_kb = max_mem_kb
self.mem_kb = mem_kb
self.num_cpu = num_cpu
self.cpu_time_ns = cpu_time_ns
self.id = id
self.internal_id = internal_id
def __eq__(self, other):
return (self.__class__ == other.__class__ and

View File

@ -134,11 +134,7 @@ class VMOps(object):
info = self._vmutils.get_vm_summary_info(instance_name)
state = constants.HYPERV_POWER_STATE[info['EnabledState']]
return hardware.InstanceInfo(state=state,
max_mem_kb=info['MemoryUsage'],
mem_kb=info['MemoryUsage'],
num_cpu=info['NumberOfProcessors'],
cpu_time_ns=info['UpTime'])
return hardware.InstanceInfo(state=state)
def _create_root_device(self, context, instance, root_disk_info, vm_gen):
path = None

View File

@ -877,10 +877,7 @@ class IronicDriver(virt_driver.ComputeDriver):
{'instance': instance.uuid,
'node': instance.node})
return hardware.InstanceInfo(state=map_power_state(node.power_state),
max_mem_kb=memory_kib,
mem_kb=memory_kib,
num_cpu=num_cpu)
return hardware.InstanceInfo(state=map_power_state(node.power_state))
def deallocate_networks_on_reschedule(self, instance):
"""Does the driver want networks deallocated on reschedule?

View File

@ -862,7 +862,7 @@ class LibvirtDriver(driver.ComputeDriver):
try:
dom_info = self.get_info(instance)
state = dom_info.state
new_domid = dom_info.id
new_domid = dom_info.internal_id
except exception.InstanceNotFound:
LOG.debug("During wait destroy, instance disappeared.",
instance=instance)

View File

@ -548,11 +548,7 @@ class Guest(object):
return hardware.InstanceInfo(
state=LIBVIRT_POWER_STATE[dom_info[0]],
max_mem_kb=dom_info[1],
mem_kb=dom_info[2],
num_cpu=dom_info[3],
cpu_time_ns=dom_info[4],
id=self.id)
internal_id=self.id)
def get_power_state(self, host):
return self.get_info(host).state

View File

@ -92,16 +92,9 @@ class PowerVMDriver(driver.ComputeDriver):
"""Get the current status of an instance.
:param instance: nova.objects.instance.Instance object
:returns: An InstanceInfo object containing:
:state: the running state, one of the power_state codes
:max_mem_kb: (int) the maximum memory in KBytes allowed
:mem_kb: (int) the memory in KBytes used by the domain
:num_cpu: (int) the number of virtual CPUs for the domain
:cpu_time_ns: (int) the CPU time used in nanoseconds
:id: a unique ID for the instance
:returns: An InstanceInfo object.
"""
return vm.InstanceInfo(self.adapter, instance)
return vm.get_vm_info(self.adapter, instance)
def list_instances(self):
"""Return the names of all the instances known to the virt host.

View File

@ -308,6 +308,19 @@ def get_vm_qp(adapter, lpar_uuid, qprop=None, log_errors=True):
return jsonutils.loads(resp.body)
def get_vm_info(adapter, instance):
"""Get the InstanceInfo for an instance.
:param adapter: The pypowervm.adapter.Adapter for the PowerVM REST API.
:param instance: nova.objects.instance.Instance object
:returns: An InstanceInfo object.
"""
pvm_uuid = get_pvm_uuid(instance)
pvm_state = get_vm_qp(adapter, pvm_uuid, 'PartitionState')
nova_state = _translate_vm_state(pvm_state)
return hardware.InstanceInfo(nova_state)
class VMBuilder(object):
"""Converts a Nova Instance/Flavor into a pypowervm LPARBuilder."""
_PVM_PROC_COMPAT = 'powervm:processor_compatibility'
@ -488,32 +501,3 @@ class VMBuilder(object):
# Return the singular pool id.
return pool_wraps[0].id
class InstanceInfo(hardware.InstanceInfo):
"""Instance Information
This object tries to lazy load the attributes since the compute
manager retrieves it a lot just to check the status and doesn't need
all the attributes.
:param adapter: pypowervm adapter
:param instance: nova instance
"""
_QP_STATE = 'PartitionState'
def __init__(self, adapter, instance):
self._adapter = adapter
# This is the PowerVM LPAR UUID (not the instance (UU)ID).
self.id = get_pvm_uuid(instance)
self._state = None
@property
def state(self):
# return the state if we previously loaded it
if self._state is not None:
return self._state
# otherwise, fetch the value now
pvm_state = get_vm_qp(self._adapter, self.id, self._QP_STATE)
self._state = _translate_vm_state(pvm_state)
return self._state

View File

@ -1497,9 +1497,7 @@ class VMwareVMOps(object):
"""Return data about the VM instance."""
vm_ref = vm_util.get_vm_ref(self._session, instance)
lst_properties = ["summary.config.numCpu",
"summary.config.memorySizeMB",
"runtime.powerState"]
lst_properties = ["runtime.powerState"]
try:
vm_props = self._session._call_method(vutil,
"get_object_properties_dict",
@ -1507,13 +1505,8 @@ class VMwareVMOps(object):
lst_properties)
except vexc.ManagedObjectNotFoundException:
raise exception.InstanceNotFound(instance_id=instance.uuid)
max_mem = int(vm_props.get('summary.config.memorySizeMB', 0)) * 1024
num_cpu = int(vm_props.get('summary.config.numCpu', 0))
return hardware.InstanceInfo(
state=constants.POWER_STATES[vm_props['runtime.powerState']],
max_mem_kb=max_mem,
mem_kb=max_mem,
num_cpu=num_cpu)
state=constants.POWER_STATES[vm_props['runtime.powerState']])
def _get_diagnostics(self, instance):
"""Return data about VM diagnostics."""

View File

@ -1685,15 +1685,7 @@ def _vm_query_data_source(session, *args):
def compile_info(session, vm_ref):
"""Fill record with VM status information."""
power_state = get_power_state(session, vm_ref)
max_mem = session.call_xenapi("VM.get_memory_static_max", vm_ref)
mem = session.call_xenapi("VM.get_memory_dynamic_max", vm_ref)
num_cpu = session.call_xenapi("VM.get_VCPUs_max", vm_ref)
return hardware.InstanceInfo(state=power_state,
max_mem_kb=int(max_mem) >> 10,
mem_kb=int(mem) >> 10,
num_cpu=num_cpu)
return hardware.InstanceInfo(state=get_power_state(session, vm_ref))
def compile_instance_diagnostics(session, instance, vm_ref):