From 2aa31ddf4f90517695831379eee919bbf72d4ff7 Mon Sep 17 00:00:00 2001 From: Nate Potter Date: Fri, 29 Sep 2017 14:31:01 -0700 Subject: [PATCH] Add int_or_none adapter function Add a function that allows a value to either be translated to an int or left as None if the json value is null to account for null values without throwing an error. Change-Id: I03c76d01740cfb36648f09112611111a3b316467 Closes-bug: #1720443 --- sushy/resources/system/system.py | 3 ++- sushy/tests/unit/resources/system/test_system.py | 5 +++++ sushy/tests/unit/test_utils.py | 4 ++++ sushy/utils.py | 12 ++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/sushy/resources/system/system.py b/sushy/resources/system/system.py index f9eee1a3..11c2f832 100644 --- a/sushy/resources/system/system.py +++ b/sushy/resources/system/system.py @@ -21,6 +21,7 @@ from sushy.resources import common from sushy.resources.system import constants as sys_cons from sushy.resources.system import mappings as sys_maps from sushy.resources.system import processor +from sushy import utils LOG = logging.getLogger(__name__) @@ -52,7 +53,7 @@ class MemorySummaryField(base.CompositeField): This signifies health state of memory along with its dependent resources. """ - size_gib = base.Field('TotalSystemMemoryGiB', adapter=int) + size_gib = base.Field('TotalSystemMemoryGiB', adapter=utils.int_or_none) """The size of memory of the system in GiB. This signifies the total installed, operating system-accessible memory diff --git a/sushy/tests/unit/resources/system/test_system.py b/sushy/tests/unit/resources/system/test_system.py index b3c602f4..30a98ebb 100644 --- a/sushy/tests/unit/resources/system/test_system.py +++ b/sushy/tests/unit/resources/system/test_system.py @@ -79,6 +79,11 @@ class SystemTestCase(base.TestCase): 'attribute Actions/#ComputerSystem.Reset/target', self.sys_inst._parse_attributes) + def test__parse_attributes_null_memory_capacity(self): + self.sys_inst.json['MemorySummary']['TotalSystemMemoryGiB'] = None + self.sys_inst._parse_attributes() + self.assertIsNone(self.sys_inst.memory_summary.size_gib) + def test_get__reset_action_element(self): value = self.sys_inst._get_reset_action_element() self.assertEqual("/redfish/v1/Systems/437XR1138R2/Actions/" diff --git a/sushy/tests/unit/test_utils.py b/sushy/tests/unit/test_utils.py index f53731a3..20032760 100644 --- a/sushy/tests/unit/test_utils.py +++ b/sushy/tests/unit/test_utils.py @@ -35,3 +35,7 @@ class UtilsTestCase(base.TestCase): expected = ('/redfish/v1/Systems/FOO', '/redfish/v1/Systems/BAR') self.assertEqual(expected, utils.get_members_identities(members)) self.assertEqual(1, log_mock.call_count) + + def test_int_or_none(self): + self.assertEqual(1, utils.int_or_none('1')) + self.assertIsNone(None, utils.int_or_none(None)) diff --git a/sushy/utils.py b/sushy/utils.py index f1e243cf..689a2960 100644 --- a/sushy/utils.py +++ b/sushy/utils.py @@ -45,3 +45,15 @@ def get_members_identities(members): members_list.append(path.rstrip('/')) return tuple(members_list) + + +def int_or_none(x): + """Given a value x it cast as int or None + + :param x: The value to transform and return + :returns: Either None or x cast to an int + + """ + if x is None: + return None + return int(x)