From 37ec9b99c7ca85467f5d274505e86436d86657dc Mon Sep 17 00:00:00 2001 From: Sylvain Bauza Date: Mon, 28 Nov 2016 18:36:53 +0100 Subject: [PATCH] Default deleted if the instance from BuildRequest is not having it We should already have the rehydrated instance having the deleted field being set. That field is also a BooleanField type, so we should never try to cast it from an integer (even if we support it). Instead, we need to make sure that if the field is not set for any reason (mostly because an Instance wasn't persisted before we persisted the BuildRequest object that was nesting it), then we would default the deleted field to False. Change-Id: I828191f750a5978fed65a86e797388d11042b4a0 Related-Bug: #1644513 --- nova/objects/build_request.py | 6 +++++- nova/tests/unit/objects/test_build_request.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/nova/objects/build_request.py b/nova/objects/build_request.py index 3186a14c3bd..4dbe5a06dac 100644 --- a/nova/objects/build_request.py +++ b/nova/objects/build_request.py @@ -80,9 +80,13 @@ def _load_instance(self, db_instance): LOG.exception(_LE('Could not deserialize instance in ' 'BuildRequest')) raise exception.BuildRequestNotFound(uuid=self.instance_uuid) + # NOTE(sbauza): The instance primitive should already have the deleted + # field being set, so when hydrating it back here, we should get the + # right value but in case we don't have it, let's suppose that the + # instance is not deleted, which is the default value for that field. + self.instance.obj_set_defaults('deleted') # NOTE(alaski): Set some fields on instance that are needed by the api, # not lazy-loadable, and don't change. - self.instance.deleted = 0 self.instance.disable_terminate = False self.instance.terminated_at = None self.instance.host = None diff --git a/nova/tests/unit/objects/test_build_request.py b/nova/tests/unit/objects/test_build_request.py index 26b34afab0d..0812c98aee2 100644 --- a/nova/tests/unit/objects/test_build_request.py +++ b/nova/tests/unit/objects/test_build_request.py @@ -12,6 +12,7 @@ import mock from oslo_serialization import jsonutils +from oslo_versionedobjects import base as o_vo_base from nova import exception from nova import objects @@ -147,6 +148,17 @@ def test_get_new_instance_show_changed_fields(self): self.assertEqual(getattr(build_request.instance, field), getattr(instance, field)) + def test_from_db_object_set_deleted(self): + # Assert that if we persisted an instance not yet having the deleted + # field being set, we still return a value for that field. + fake_req = fake_build_request.fake_db_req() + with mock.patch.object(o_vo_base.VersionedObject, + 'obj_set_defaults') as mock_obj_set_defaults: + build_request = objects.BuildRequest._from_db_object( + self.context, objects.BuildRequest(), fake_req) + mock_obj_set_defaults.assert_called_once_with('deleted') + self.assertFalse(build_request.instance.deleted) + class TestBuildRequestObject(test_objects._LocalTest, _TestBuildRequestObject):