Skip to content

Commit 37ec9b9

Browse files
committed
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
1 parent 14073a3 commit 37ec9b9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

nova/objects/build_request.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ def _load_instance(self, db_instance):
8080
LOG.exception(_LE('Could not deserialize instance in '
8181
'BuildRequest'))
8282
raise exception.BuildRequestNotFound(uuid=self.instance_uuid)
83+
# NOTE(sbauza): The instance primitive should already have the deleted
84+
# field being set, so when hydrating it back here, we should get the
85+
# right value but in case we don't have it, let's suppose that the
86+
# instance is not deleted, which is the default value for that field.
87+
self.instance.obj_set_defaults('deleted')
8388
# NOTE(alaski): Set some fields on instance that are needed by the api,
8489
# not lazy-loadable, and don't change.
85-
self.instance.deleted = 0
8690
self.instance.disable_terminate = False
8791
self.instance.terminated_at = None
8892
self.instance.host = None

nova/tests/unit/objects/test_build_request.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import mock
1414
from oslo_serialization import jsonutils
15+
from oslo_versionedobjects import base as o_vo_base
1516

1617
from nova import exception
1718
from nova import objects
@@ -147,6 +148,17 @@ def test_get_new_instance_show_changed_fields(self):
147148
self.assertEqual(getattr(build_request.instance, field),
148149
getattr(instance, field))
149150

151+
def test_from_db_object_set_deleted(self):
152+
# Assert that if we persisted an instance not yet having the deleted
153+
# field being set, we still return a value for that field.
154+
fake_req = fake_build_request.fake_db_req()
155+
with mock.patch.object(o_vo_base.VersionedObject,
156+
'obj_set_defaults') as mock_obj_set_defaults:
157+
build_request = objects.BuildRequest._from_db_object(
158+
self.context, objects.BuildRequest(), fake_req)
159+
mock_obj_set_defaults.assert_called_once_with('deleted')
160+
self.assertFalse(build_request.instance.deleted)
161+
150162

151163
class TestBuildRequestObject(test_objects._LocalTest,
152164
_TestBuildRequestObject):

0 commit comments

Comments
 (0)