Fix unbound local when saving an unchanged RequestSpec

RequestSpec._get_update_primitives() tried to return the unitialized
db_updates field when the object has no changes. This patch initalizes
that local to None and handles that None in create() and save() where
the RequestSpec._get_update_primitives() is called.

Change-Id: Iad256079945bf8b5745ebdcc393c55115dbcab75
Closes-Bug: #1775863
(cherry picked from commit 08fb09e878)
(cherry picked from commit a31ebd87ba)
This commit is contained in:
Balazs Gibizer 2018-06-08 17:57:07 +02:00
parent df13125683
commit 6d60c74191
2 changed files with 19 additions and 4 deletions

View File

@ -497,6 +497,7 @@ class RequestSpec(base.NovaObject):
it was originally scheduled with.
"""
updates = self.obj_get_changes()
db_updates = None
# NOTE(alaski): The db schema is the full serialized object in a
# 'spec' column. If anything has changed we rewrite the full thing.
if updates:
@ -522,7 +523,9 @@ class RequestSpec(base.NovaObject):
reason='already created')
updates = self._get_update_primitives()
if not updates:
raise exception.ObjectActionError(action='create',
reason='no fields are set')
db_spec = self._create_in_db(self._context, updates)
self._from_db_object(self._context, self, db_spec)
@ -540,9 +543,11 @@ class RequestSpec(base.NovaObject):
@base.remotable
def save(self):
updates = self._get_update_primitives()
db_spec = self._save_in_db(self._context, self.instance_uuid, updates)
self._from_db_object(self._context, self, db_spec)
self.obj_reset_changes()
if updates:
db_spec = self._save_in_db(self._context, self.instance_uuid,
updates)
self._from_db_object(self._context, self, db_spec)
self.obj_reset_changes()
@staticmethod
@db.api_context_manager.writer

View File

@ -622,6 +622,16 @@ class _TestRequestSpecObject(object):
objects.SecurityGroupList)
self.assertIn('security_groups', req_obj)
def test_create_raises_on_unchanged_object(self):
ctxt = context.RequestContext(uuids.user_id, uuids.project_id)
req_obj = request_spec.RequestSpec(context=ctxt)
self.assertRaises(exception.ObjectActionError, req_obj.create)
def test_save_can_be_called_on_unchanged_object(self):
req_obj = fake_request_spec.fake_spec_obj(remove_id=True)
req_obj.create()
req_obj.save()
class TestRequestSpecObject(test_objects._LocalTest,
_TestRequestSpecObject):