From 065326c0f55a73c99706148f7bfbbec87ea863bc Mon Sep 17 00:00:00 2001 From: Kyrylo Romanenko Date: Fri, 16 Sep 2016 18:00:43 +0300 Subject: [PATCH] 'updated_at' field value after node is updated node.save() doesn't update object from db because 'updated_at' field for the node is not up-to-date after a save(). Change-Id: I58076d71e5977dded3f5485f295b3235d467872d Partial-Bug: #1281638 Co-Authored-By: Galyna Zholtkevych --- ironic/objects/node.py | 8 +++- ironic/tests/unit/objects/test_node.py | 37 ++++++++++++++++++- ...ated-at-object-field-a74466f7c4541072.yaml | 4 ++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/change-updated-at-object-field-a74466f7c4541072.yaml diff --git a/ironic/objects/node.py b/ironic/objects/node.py index 1fcef73e8f..dcdd3e9595 100644 --- a/ironic/objects/node.py +++ b/ironic/objects/node.py @@ -359,7 +359,13 @@ class Node(base.IronicObject, object_base.VersionedObjectDictCompat): # Clean driver_internal_info when changes driver self.driver_internal_info = {} updates = self.obj_get_changes() - self.dbapi.update_node(self.uuid, updates) + db_node = self.dbapi.update_node(self.uuid, updates) + + # TODO(galyna): updating specific field not touching others to not + # change default behaviour. Otherwise it will break a bunch of tests + # This can be updated in other way when more fields like `updated_at` + # will appear + self.updated_at = db_node['updated_at'] self.obj_reset_changes() # NOTE(xek): We don't want to enable RPC on this call just yet. Remotable diff --git a/ironic/tests/unit/objects/test_node.py b/ironic/tests/unit/objects/test_node.py index 467dcea8a4..29ef4878f3 100644 --- a/ironic/tests/unit/objects/test_node.py +++ b/ironic/tests/unit/objects/test_node.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import mock from testtools import matchers @@ -76,7 +77,7 @@ class TestNodeObject(base.DbTestCase): mock_get_node.return_value = self.fake_node with mock.patch.object(self.dbapi, 'update_node', autospec=True) as mock_update_node: - + mock_update_node.return_value = utils.get_test_node() n = objects.Node.get(self.context, uuid) self.assertEqual({"private_state": "secret value"}, n.driver_internal_info) @@ -92,6 +93,36 @@ class TestNodeObject(base.DbTestCase): self.assertEqual(self.context, n._context) self.assertEqual({}, n.driver_internal_info) + def test_save_updated_at_field(self): + uuid = self.fake_node['uuid'] + extra = {"test": 123} + test_time = datetime.datetime(2000, 1, 1, 0, 0) + with mock.patch.object(self.dbapi, 'get_node_by_uuid', + autospec=True) as mock_get_node: + mock_get_node.return_value = self.fake_node + with mock.patch.object(self.dbapi, 'update_node', + autospec=True) as mock_update_node: + mock_update_node.return_value = ( + utils.get_test_node(extra=extra, updated_at=test_time)) + n = objects.Node.get(self.context, uuid) + self.assertEqual({"private_state": "secret value"}, + n.driver_internal_info) + n.properties = {"fake": "property"} + n.extra = extra + n.driver = "fake-driver" + n.driver_internal_info = {} + n.save() + + mock_get_node.assert_called_once_with(uuid) + mock_update_node.assert_called_once_with( + uuid, {'properties': {"fake": "property"}, + 'driver': 'fake-driver', + 'driver_internal_info': {}, + 'extra': {'test': 123}}) + self.assertEqual(self.context, n._context) + res_updated_at = n.updated_at.replace(tzinfo=None) + self.assertEqual(test_time, res_updated_at) + def test_refresh(self): uuid = self.fake_node['uuid'] returns = [dict(self.fake_node, properties={"fake": "first"}), @@ -163,6 +194,10 @@ class TestNodeObject(base.DbTestCase): node.touch_provisioning() mock_touch.assert_called_once_with(node.id) + def test_create(self): + node = objects.Node(self.context, **self.fake_node) + node.create() + def test_create_with_invalid_properties(self): node = objects.Node(self.context, **self.fake_node) node.properties = {"local_gb": "5G"} diff --git a/releasenotes/notes/change-updated-at-object-field-a74466f7c4541072.yaml b/releasenotes/notes/change-updated-at-object-field-a74466f7c4541072.yaml new file mode 100644 index 0000000000..7e08782924 --- /dev/null +++ b/releasenotes/notes/change-updated-at-object-field-a74466f7c4541072.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - Set correct node's "updated_at" field after + node has been updated.