Allow missing flags on inventory.CPU

Change-Id: Idd485fd11ab9d784bc8c24a9b8279bcce76f0f68
Closes-Bug: #1602173
This commit is contained in:
Imre Farkas 2016-07-12 11:51:40 +02:00
parent 5a617b9e8d
commit f96744daf3
8 changed files with 174 additions and 9 deletions

View File

@ -101,15 +101,17 @@ class InventoryManagement(object):
speed_mhz=int(self._get_cpu_attr(cpu, 'CurrentClockSpeed')),
model=self._get_cpu_attr(cpu, 'Model'),
status=PRIMARY_STATUS[self._get_cpu_attr(cpu, 'PrimaryStatus')],
ht_enabled=bool(self._get_cpu_attr(cpu, 'HyperThreadingEnabled')),
turbo_enabled=bool(self._get_cpu_attr(cpu, 'TurboModeEnabled')),
vt_enabled=bool(self._get_cpu_attr(cpu,
'VirtualizationTechnologyEnabled')),
ht_enabled=bool(self._get_cpu_attr(cpu, 'HyperThreadingEnabled',
allow_missing=True)),
turbo_enabled=bool(self._get_cpu_attr(cpu, 'TurboModeEnabled',
allow_missing=True)),
vt_enabled=bool(self._get_cpu_attr(
cpu, 'VirtualizationTechnologyEnabled', allow_missing=True)),
arch64=arch64)
def _get_cpu_attr(self, cpu, attr_name):
def _get_cpu_attr(self, cpu, attr_name, allow_missing=False):
return utils.get_wsman_resource_attr(
cpu, uris.DCIM_CPUView, attr_name)
cpu, uris.DCIM_CPUView, attr_name, allow_missing=allow_missing)
def list_memory(self):
"""Returns the list of installed memory

View File

@ -1074,6 +1074,27 @@ class ClientInventoryManagementTestCase(base.BaseTest):
expected_cpu,
self.drac_client.list_cpus())
def test_list_cpus_with_missing_flags(self, mock_requests):
expected_cpu = [inventory.CPU(
id='CPU.Socket.1',
cores=8,
speed_mhz=1900,
model='Intel(R) Xeon(R) CPU E5-2440 v2 @ 1.90GHz',
status='OK',
ht_enabled=False,
turbo_enabled=False,
vt_enabled=False,
arch64=False)]
mock_requests.post(
'https://1.2.3.4:443/wsman',
text=test_utils.InventoryEnumerations[
uris.DCIM_CPUView]['missing_flags'])
self.assertEqual(
expected_cpu,
self.drac_client.list_cpus())
def test_list_memory(self, mock_requests):
expected_memory = [inventory.Memory(
id='DIMM.Socket.A1',

View File

@ -0,0 +1,65 @@
#
# 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.
import re
from lxml import etree
from dracclient.resources import uris
from dracclient.tests import base
from dracclient.tests import utils as test_utils
from dracclient import utils
class UtilsTestCase(base.BaseTest):
def setUp(self):
super(UtilsTestCase, self).setUp()
def test_get_wsman_resource_attr(self):
doc = etree.fromstring(
test_utils.InventoryEnumerations[uris.DCIM_CPUView]['ok'])
cpus = utils.find_xml(doc, 'DCIM_CPUView', uris.DCIM_CPUView,
find_all=True)
val = utils.get_wsman_resource_attr(
cpus[0], uris.DCIM_CPUView, 'HyperThreadingEnabled',
allow_missing=False)
self.assertEqual('1', val)
def test_get_wsman_resource_attr_missing_attr(self):
expected_message = ("Could not find attribute 'HyperThreadingEnabled'")
doc = etree.fromstring(
test_utils.InventoryEnumerations[
uris.DCIM_CPUView]['missing_flags'])
cpus = utils.find_xml(doc, 'DCIM_CPUView', uris.DCIM_CPUView,
find_all=True)
self.assertRaisesRegexp(
AttributeError, re.escape(expected_message),
utils.get_wsman_resource_attr, cpus[0], uris.DCIM_CPUView,
'HyperThreadingEnabled', allow_missing=False)
def test_get_wsman_resource_attr_missing_attr_allowed(self):
doc = etree.fromstring(
test_utils.InventoryEnumerations[
uris.DCIM_CPUView]['missing_flags'])
cpus = utils.find_xml(doc, 'DCIM_CPUView', uris.DCIM_CPUView,
find_all=True)
val = utils.get_wsman_resource_attr(
cpus[0], uris.DCIM_CPUView, 'HyperThreadingEnabled',
allow_missing=True)
self.assertIsNone(val)

View File

@ -99,10 +99,11 @@ BIOSInvocations = {
InventoryEnumerations = {
uris.DCIM_CPUView: {
'ok': load_wsman_xml('cpu-enumeration-enum-ok')
'ok': load_wsman_xml('cpu_view-enum-ok'),
'missing_flags': load_wsman_xml('cpu_view-enum-missing_flags')
},
uris.DCIM_MemoryView: {
'ok': load_wsman_xml('memory-enumeration-enum-ok')
'ok': load_wsman_xml('memory_view-enum-ok')
},
uris.DCIM_NICView: {
'ok': load_wsman_xml('nic_view-enum-ok')

View File

@ -0,0 +1,64 @@
<s:Envelope xmlns:n1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_CPUView"
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<s:Header>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse</wsa:Action>
<wsa:RelatesTo>uuid:bde5656e-1253-4545-87dd-a111346efebe</wsa:RelatesTo>
<wsa:MessageID>uuid:35d42397-376d-176d-8062-a36fc6fe83b0</wsa:MessageID>
</s:Header>
<s:Body>
<wsen:EnumerateResponse>
<wsman:Items>
<n1:DCIM_CPUView>
<n1:CPUFamily>B3</n1:CPUFamily>
<n1:CPUStatus>1</n1:CPUStatus>
<n1:Cache1Associativity>7</n1:Cache1Associativity>
<n1:Cache1ErrorMethodology>5</n1:Cache1ErrorMethodology>
<n1:Cache1Level>0</n1:Cache1Level>
<n1:Cache1PrimaryStatus>1</n1:Cache1PrimaryStatus>
<n1:Cache1SRAMType>2</n1:Cache1SRAMType>
<n1:Cache1Size>256</n1:Cache1Size>
<n1:Cache1Type>4</n1:Cache1Type>
<n1:Cache1WritePolicy>1</n1:Cache1WritePolicy>
<n1:Cache2Associativity>7</n1:Cache2Associativity>
<n1:Cache2ErrorMethodology>5</n1:Cache2ErrorMethodology>
<n1:Cache2Level>1</n1:Cache2Level>
<n1:Cache2PrimaryStatus>1</n1:Cache2PrimaryStatus>
<n1:Cache2SRAMType>2</n1:Cache2SRAMType>
<n1:Cache2Size>2048</n1:Cache2Size>
<n1:Cache2Type>5</n1:Cache2Type>
<n1:Cache2WritePolicy>1</n1:Cache2WritePolicy>
<n1:Cache3Associativity>14</n1:Cache3Associativity>
<n1:Cache3ErrorMethodology>5</n1:Cache3ErrorMethodology>
<n1:Cache3Level>2</n1:Cache3Level>
<n1:Cache3PrimaryStatus>1</n1:Cache3PrimaryStatus>
<n1:Cache3SRAMType>2</n1:Cache3SRAMType>
<n1:Cache3Size>20480</n1:Cache3Size>
<n1:Cache3Type>5</n1:Cache3Type>
<n1:Cache3WritePolicy>1</n1:Cache3WritePolicy>
<n1:Characteristics>2</n1:Characteristics>
<n1:CurrentClockSpeed>1900</n1:CurrentClockSpeed>
<n1:DeviceDescription>CPU 1</n1:DeviceDescription>
<n1:ExternalBusClockSpeed>6400</n1:ExternalBusClockSpeed>
<n1:FQDD>CPU.Socket.1</n1:FQDD>
<n1:InstanceID>CPU.Socket.1</n1:InstanceID>
<n1:LastSystemInventoryTime>20160706154809.000000+000</n1:LastSystemInventoryTime>
<n1:LastUpdateTime>20141016232016.000000+000</n1:LastUpdateTime>
<n1:Manufacturer>Intel</n1:Manufacturer>
<n1:MaxClockSpeed>3600</n1:MaxClockSpeed>
<n1:Model>Intel(R) Xeon(R) CPU E5-2440 v2 @ 1.90GHz</n1:Model>
<n1:NumberOfEnabledCores>8</n1:NumberOfEnabledCores>
<n1:NumberOfEnabledThreads>16</n1:NumberOfEnabledThreads>
<n1:NumberOfProcessorCores>8</n1:NumberOfProcessorCores>
<n1:PrimaryStatus>1</n1:PrimaryStatus>
<n1:Voltage>1.2</n1:Voltage>
</n1:DCIM_CPUView>
</wsman:Items>
<wsen:EnumerationContext/>
<wsman:EndOfSequence/>
</wsen:EnumerateResponse>
</s:Body>
</s:Envelope>

View File

@ -44,7 +44,8 @@ def find_xml(doc, item, namespace, find_all=False):
return doc.find(query)
def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False):
def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
allow_missing=False):
"""Find an attribute of a resource in an ElementTree object.
:param doc: the element tree object.
@ -53,10 +54,21 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False):
:param nullable: enables checking if the element contains an
XMLSchema-instance namespaced nil attribute that has a
value of True. In this case, it will return None.
:param allow_missing: if set to True, attributes missing from the XML
document will return None instead of raising
AttributeError.
:raises: AttributeError if the attribute is missing from the XML doc and
allow_missing is False.
:returns: value of the attribute
"""
item = find_xml(doc, attr_name, resource_uri)
if item is None:
if allow_missing:
return
else:
raise AttributeError("Could not find attribute '%s'" % (attr_name))
if not nullable:
return item.text.strip()
else: