From 17274a613f981b33a1d0ec010dc8dbb92fbc5226 Mon Sep 17 00:00:00 2001 From: Anton Arefiev Date: Thu, 4 Feb 2016 18:08:22 +0200 Subject: [PATCH] Add forward slash for node path patch Ironic cli allow to patch attributes w/o forward slash at the beginning: ironic node-update add 'driver_info/attr'='my_attr' Inspector fails if driver info in rules provided in same manner, this change add head forward slash to path if it's missing. Closes-Bug: #1529809 Change-Id: I4423184e15d5779c59a903b50554db0550ba3d83 --- ironic_inspector/node_cache.py | 6 ++++ ironic_inspector/test/test_node_cache.py | 29 +++++++++++++++++-- ...patch-head-backslash-24bcdd03ba254bf2.yaml | 4 +++ 3 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/patch-head-backslash-24bcdd03ba254bf2.yaml diff --git a/ironic_inspector/node_cache.py b/ironic_inspector/node_cache.py index 9f46cd5fb..85e3491a0 100644 --- a/ironic_inspector/node_cache.py +++ b/ironic_inspector/node_cache.py @@ -264,6 +264,12 @@ class NodeInfo(object): :param patches: JSON patches to apply :raises: ironicclient exceptions """ + # NOTE(aarefiev): support path w/o ahead forward slash + # as Ironic cli does + for patch in patches: + if patch.get('path') and not patch['path'].startswith('/'): + patch['path'] = '/' + patch['path'] + LOG.debug('Updating node with patches %s', patches, node_info=self) self._node = self.ironic.node.update(self.uuid, patches) diff --git a/ironic_inspector/test/test_node_cache.py b/ironic_inspector/test/test_node_cache.py index a84f20c4e..8f662aa9a 100644 --- a/ironic_inspector/test/test_node_cache.py +++ b/ironic_inspector/test/test_node_cache.py @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import copy import json import time import unittest @@ -502,9 +503,33 @@ class TestUpdate(test_base.NodeTest): def test_patch(self): self.ironic.node.update.return_value = mock.sentinel.node - self.node_info.patch(['patch']) + self.node_info.patch([{'patch': 'patch'}]) - self.ironic.node.update.assert_called_once_with(self.uuid, ['patch']) + self.ironic.node.update.assert_called_once_with(self.uuid, + [{'patch': 'patch'}]) + self.assertIs(mock.sentinel.node, self.node_info.node()) + + def test_patch_path_wo_leading_slash(self): + self.ironic.node.update.return_value = mock.sentinel.node + + patch = [{'op': 'add', 'path': 'driver_info/test', 'value': 42}] + expected_patch = copy.deepcopy(patch) + expected_patch[0]['path'] = '/' + 'driver_info/test' + + self.node_info.patch(patch) + + self.ironic.node.update.assert_called_once_with(self.uuid, + expected_patch) + self.assertIs(mock.sentinel.node, self.node_info.node()) + + def test_patch_path_with_leading_slash(self): + self.ironic.node.update.return_value = mock.sentinel.node + + patch = [{'op': 'add', 'path': '/driver_info/test', 'value': 42}] + + self.node_info.patch(patch) + + self.ironic.node.update.assert_called_once_with(self.uuid, patch) self.assertIs(mock.sentinel.node, self.node_info.node()) def test_update_properties(self): diff --git a/releasenotes/notes/patch-head-backslash-24bcdd03ba254bf2.yaml b/releasenotes/notes/patch-head-backslash-24bcdd03ba254bf2.yaml new file mode 100644 index 000000000..b32e4d7f9 --- /dev/null +++ b/releasenotes/notes/patch-head-backslash-24bcdd03ba254bf2.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - Introspection rules (e.g. set-attribute action) now accept 'path' + field without leading forward slash as Ironic cli does.