Raise a reasonable error on non-nullable flags with empty text

Now it fails with AttributeError: 'NoneType' object has no attribute 'strip'.
Raise a new exception with proper explanation instead.

Partial-Bug: #1550328
Change-Id: Ie9f9d212c934bb75e8224dd64ec8df7f788b86d3
This commit is contained in:
Dmitry Tantsur 2016-11-07 15:36:04 +01:00
parent 02a486d89a
commit ba1eb4aa99
5 changed files with 102 additions and 1 deletions

View File

@ -35,6 +35,10 @@ class DRACUnexpectedReturnValue(DRACRequestFailed):
'%(expected_return_value)s') '%(expected_return_value)s')
class DRACEmptyResponseField(BaseClientException):
msg_fmt = ("Attribute '%(attr)s' is not nullable, but no value received")
class InvalidParameterValue(BaseClientException): class InvalidParameterValue(BaseClientException):
msg_fmt = '%(reason)s' msg_fmt = '%(reason)s'

View File

@ -15,6 +15,7 @@ import re
from lxml import etree from lxml import etree
from dracclient import exceptions
from dracclient.resources import uris from dracclient.resources import uris
from dracclient.tests import base from dracclient.tests import base
from dracclient.tests import utils as test_utils from dracclient.tests import utils as test_utils
@ -63,3 +64,17 @@ class UtilsTestCase(base.BaseTest):
allow_missing=True) allow_missing=True)
self.assertIsNone(val) self.assertIsNone(val)
def test_get_wsman_resource_attr_missing_text(self):
expected_message = ("Attribute 'HyperThreadingEnabled' is not nullable"
", but no value received")
doc = etree.fromstring(
test_utils.InventoryEnumerations[
uris.DCIM_CPUView]['empty_flag'])
cpus = utils.find_xml(doc, 'DCIM_CPUView', uris.DCIM_CPUView,
find_all=True)
self.assertRaisesRegexp(
exceptions.DRACEmptyResponseField, re.escape(expected_message),
utils.get_wsman_resource_attr, cpus[0], uris.DCIM_CPUView,
'HyperThreadingEnabled', allow_missing=False)

View File

@ -100,7 +100,8 @@ BIOSInvocations = {
InventoryEnumerations = { InventoryEnumerations = {
uris.DCIM_CPUView: { uris.DCIM_CPUView: {
'ok': load_wsman_xml('cpu_view-enum-ok'), 'ok': load_wsman_xml('cpu_view-enum-ok'),
'missing_flags': load_wsman_xml('cpu_view-enum-missing_flags') 'missing_flags': load_wsman_xml('cpu_view-enum-missing_flags'),
'empty_flag': load_wsman_xml('cpu_view-enum-empty_flag'),
}, },
uris.DCIM_MemoryView: { uris.DCIM_MemoryView: {
'ok': load_wsman_xml('memory_view-enum-ok') 'ok': load_wsman_xml('memory_view-enum-ok')

View File

@ -0,0 +1,75 @@
<s:Envelope 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"
xmlns:n1="http://schemas.dell.com/wbem/wscim/1/cim-schema/2/DCIM_CPUView">
<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:dd7ea19a-8633-4fa4-a43a-66ae8220f689</wsa:RelatesTo>
<wsa:MessageID>uuid:247e710c-29de-19de-93c9-f148d4fe83b0</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>4</n1:Cache1ErrorMethodology>
<n1:Cache1Level>0</n1:Cache1Level>
<n1:Cache1Location>0</n1:Cache1Location>
<n1:Cache1PrimaryStatus>1</n1:Cache1PrimaryStatus>
<n1:Cache1SRAMType>2</n1:Cache1SRAMType>
<n1:Cache1Size>384</n1:Cache1Size>
<n1:Cache1Type>5</n1:Cache1Type>
<n1:Cache1WritePolicy>1</n1:Cache1WritePolicy>
<n1:Cache2Associativity>7</n1:Cache2Associativity>
<n1:Cache2ErrorMethodology>5</n1:Cache2ErrorMethodology>
<n1:Cache2Level>1</n1:Cache2Level>
<n1:Cache2Location>0</n1:Cache2Location>
<n1:Cache2PrimaryStatus>1</n1:Cache2PrimaryStatus>
<n1:Cache2SRAMType>2</n1:Cache2SRAMType>
<n1:Cache2Size>1536</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:Cache3Location>0</n1:Cache3Location>
<n1:Cache3PrimaryStatus>1</n1:Cache3PrimaryStatus>
<n1:Cache3SRAMType>2</n1:Cache3SRAMType>
<n1:Cache3Size>15360</n1:Cache3Size>
<n1:Cache3Type>5</n1:Cache3Type>
<n1:Cache3WritePolicy>1</n1:Cache3WritePolicy>
<n1:Characteristics>4</n1:Characteristics>
<n1:CurrentClockSpeed>2400</n1:CurrentClockSpeed>
<n1:DeviceDescription>CPU 1</n1:DeviceDescription>
<n1:ExecuteDisabledCapable>0</n1:ExecuteDisabledCapable>
<n1:ExecuteDisabledEnabled>0</n1:ExecuteDisabledEnabled>
<n1:ExternalBusClockSpeed>6400</n1:ExternalBusClockSpeed>
<n1:FQDD>CPU.Socket.1</n1:FQDD>
<n1:HyperThreadingCapable />
<n1:HyperThreadingEnabled />
<n1:InstanceID>CPU.Socket.1</n1:InstanceID>
<n1:LastSystemInventoryTime>20160107171416.000000+000</n1:LastSystemInventoryTime>
<n1:LastUpdateTime>20151112172601.000000+000</n1:LastUpdateTime>
<n1:Manufacturer>Intel</n1:Manufacturer>
<n1:MaxClockSpeed>4000</n1:MaxClockSpeed>
<n1:Model>Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz</n1:Model>
<n1:NumberOfEnabledCores>6</n1:NumberOfEnabledCores>
<n1:NumberOfEnabledThreads>12</n1:NumberOfEnabledThreads>
<n1:NumberOfProcessorCores>6</n1:NumberOfProcessorCores>
<n1:PrimaryStatus>1</n1:PrimaryStatus>
<n1:TurboModeCapable>1</n1:TurboModeCapable>
<n1:TurboModeEnabled>1</n1:TurboModeEnabled>
<n1:VirtualizationTechnologyCapable>1</n1:VirtualizationTechnologyCapable>
<n1:VirtualizationTechnologyEnabled>1</n1:VirtualizationTechnologyEnabled>
<n1:Voltage>1.3</n1:Voltage>
</n1:DCIM_CPUView>
</wsman:Items>
<wsen:EnumerationContext/>
<wsman:EndOfSequence/>
</wsen:EnumerateResponse>
</s:Body>
</s:Envelope>

View File

@ -15,6 +15,8 @@
Common functionalities shared between different DRAC modules. Common functionalities shared between different DRAC modules.
""" """
from dracclient import exceptions
NS_XMLSchema_Instance = 'http://www.w3.org/2001/XMLSchema-instance' NS_XMLSchema_Instance = 'http://www.w3.org/2001/XMLSchema-instance'
# ReturnValue constants # ReturnValue constants
@ -59,6 +61,8 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
AttributeError. AttributeError.
:raises: AttributeError if the attribute is missing from the XML doc and :raises: AttributeError if the attribute is missing from the XML doc and
allow_missing is False. allow_missing is False.
:raises: DRACEmptyResponseField if the attribute is present in the XML doc
but it has no text and nullable is False.
:returns: value of the attribute :returns: value of the attribute
""" """
item = find_xml(doc, attr_name, resource_uri) item = find_xml(doc, attr_name, resource_uri)
@ -70,6 +74,8 @@ def get_wsman_resource_attr(doc, resource_uri, attr_name, nullable=False,
raise AttributeError("Could not find attribute '%s'" % (attr_name)) raise AttributeError("Could not find attribute '%s'" % (attr_name))
if not nullable: if not nullable:
if item.text is None:
raise exceptions.DRACEmptyResponseField(attr=attr_name)
return item.text.strip() return item.text.strip()
else: else:
nil_attr = item.attrib.get('{%s}nil' % NS_XMLSchema_Instance) nil_attr = item.attrib.get('{%s}nil' % NS_XMLSchema_Instance)