Skip to content

Commit

Permalink
Default deleted if the instance from BuildRequest is not having it
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbauza committed Nov 28, 2016
1 parent 14073a3 commit 37ec9b9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nova/objects/build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions nova/tests/unit/objects/test_build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 37ec9b9

Please sign in to comment.