From 9b4116a1ae930f21a18490d8a477a6d08dc76946 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Mon, 1 Aug 2016 15:22:00 +0800 Subject: [PATCH] Check whether action value is string before calling format() AttributeError: 'int' object has no attribute 'format' will be raised if we pass an integer value, so we should check whether the value is a string or not. Change-Id: I14ed4d404a9be1233493083bef49218cf0f45867 Closes-Bug: #1608393 --- ironic_inspector/rules.py | 3 ++- ironic_inspector/test/unit/test_rules.py | 14 ++++++++++++++ ...tted-value-from-nonstring-3d851cb42ce3a0ac.yaml | 5 +++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/check-formatted-value-from-nonstring-3d851cb42ce3a0ac.yaml diff --git a/ironic_inspector/rules.py b/ironic_inspector/rules.py index 23d60330c..60b752a16 100644 --- a/ironic_inspector/rules.py +++ b/ironic_inspector/rules.py @@ -19,6 +19,7 @@ import jsonschema from oslo_db import exception as db_exc from oslo_utils import timeutils from oslo_utils import uuidutils +import six from sqlalchemy import orm from ironic_inspector.common.i18n import _, _LE, _LI @@ -202,7 +203,7 @@ class IntrospectionRule(object): ext = ext_mgr[act.action].obj for formatted_param in ext.FORMATTED_PARAMS: value = act.params.get(formatted_param) - if not value: + if not value or not isinstance(value, six.string_types): continue # NOTE(aarefiev): verify provided value with introspection diff --git a/ironic_inspector/test/unit/test_rules.py b/ironic_inspector/test/unit/test_rules.py index 8524d971c..b90fae738 100644 --- a/ironic_inspector/test/unit/test_rules.py +++ b/ironic_inspector/test/unit/test_rules.py @@ -419,6 +419,20 @@ class TestApplyActions(BaseTest): self.assertRaises(utils.Error, self.rule.apply_actions, self.node_info, data=self.data) + def test_apply_data_non_format_value(self, mock_ext_mgr): + self.rule = rules.create(actions_json=[ + {'action': 'set-attribute', + 'path': '/driver_info/ipmi_address', + 'value': 1}], + conditions_json=self.conditions_json + ) + mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock + + self.rule.apply_actions(self.node_info, data=self.data) + + self.assertEqual(1, self.act_mock.apply.call_count) + self.assertFalse(self.act_mock.rollback.called) + def test_rollback(self, mock_ext_mgr): mock_ext_mgr.return_value.__getitem__.return_value = self.ext_mock diff --git a/releasenotes/notes/check-formatted-value-from-nonstring-3d851cb42ce3a0ac.yaml b/releasenotes/notes/check-formatted-value-from-nonstring-3d851cb42ce3a0ac.yaml new file mode 100644 index 000000000..90d9069d7 --- /dev/null +++ b/releasenotes/notes/check-formatted-value-from-nonstring-3d851cb42ce3a0ac.yaml @@ -0,0 +1,5 @@ +--- +fixes: + This fixes setting non string 'value' field for rule's + actions. As non string value is obviously not a formatted + value, this adds a check to avoid AttributeError exception.