Merge "Raise exception if BuildRequest deleted twice"

This commit is contained in:
Jenkins 2016-06-13 01:31:40 +00:00 committed by Gerrit Code Review
commit 872fac5e37
3 changed files with 26 additions and 6 deletions

View File

@ -132,10 +132,12 @@ class BuildRequest(base.NovaObject):
@staticmethod
@db.api_context_manager.writer
def _destroy_in_db(context, id):
context.session.query(api_models.BuildRequest).filter_by(
id=id).delete()
def _destroy_in_db(context, instance_uuid):
result = context.session.query(api_models.BuildRequest).filter_by(
instance_uuid=instance_uuid).delete()
if not result:
raise exception.BuildRequestNotFound(uuid=instance_uuid)
@base.remotable
def destroy(self):
self._destroy_in_db(self._context, self.id)
self._destroy_in_db(self._context, self.instance_uuid)

View File

@ -90,3 +90,19 @@ class BuildRequestTestCase(test.NoDBTestCase):
'keypairs': obj_comp})
continue
self.assertEqual(expected, db_value)
def test_destroy(self):
self._create_req()
db_req = self.build_req_obj.get_by_instance_uuid(self.context,
self.instance_uuid)
db_req.destroy()
self.assertRaises(exception.BuildRequestNotFound,
self.build_req_obj._get_by_instance_uuid_from_db, self.context,
self.instance_uuid)
def test_destroy_twice_raises(self):
self._create_req()
db_req = self.build_req_obj.get_by_instance_uuid(self.context,
self.instance_uuid)
db_req.destroy()
self.assertRaises(exception.BuildRequestNotFound, db_req.destroy)

View File

@ -20,6 +20,7 @@ from nova.objects import build_request
from nova.tests.unit import fake_build_request
from nova.tests.unit import fake_instance
from nova.tests.unit.objects import test_objects
from nova.tests import uuidsentinel as uuids
class _TestBuildRequestObject(object):
@ -94,10 +95,11 @@ class _TestBuildRequestObject(object):
@mock.patch.object(build_request.BuildRequest, '_destroy_in_db')
def test_destroy(self, destroy_in_db):
req_obj = build_request.BuildRequest(self.context)
req_obj.id = 1
req_obj.instance_uuid = uuids.instance
req_obj.destroy()
destroy_in_db.assert_called_once_with(self.context, req_obj.id)
destroy_in_db.assert_called_once_with(self.context,
req_obj.instance_uuid)
class TestBuildRequestObject(test_objects._LocalTest,