VIRT-2985: Continuing the loop for a batch update of the exists
pingback from yagi in case of django.ObjectNotExist and django.MultipleObjectExist errors. unit tests for batch update containing django errors have also been updated.
SECOND ITERATION - Added another mock patch to fake Django DB transactions. Change-Id: Ia953d9b5393ee315c6296f0f0e9d98ff4c456e0c
This commit is contained in:
parent
d6acee808d
commit
b7aa9c8980
@ -10,3 +10,4 @@ requests
|
|||||||
south
|
south
|
||||||
sphinxcontrib-httpdomain
|
sphinxcontrib-httpdomain
|
||||||
pbr
|
pbr
|
||||||
|
mock==2.0.0
|
||||||
|
@ -297,13 +297,12 @@ def _ping_processing_with_service(pings, service, version=1):
|
|||||||
exists.save()
|
exists.save()
|
||||||
except exists_model.DoesNotExist:
|
except exists_model.DoesNotExist:
|
||||||
msg = "Could not find Exists record with message_id = '%s' for %s"
|
msg = "Could not find Exists record with message_id = '%s' for %s"
|
||||||
msg = msg % (msg_id, service)
|
msg %= (msg_id, service)
|
||||||
raise NotFoundException(message=msg)
|
stacklog.error(msg) # continuing loop
|
||||||
except exists_model.MultipleObjectsReturned:
|
except exists_model.MultipleObjectsReturned:
|
||||||
msg = "Multiple Exists records with message_id = '%s' for %s"
|
msg = "Multiple Exists records with message_id = '%s' for %s"
|
||||||
msg = msg % (msg_id, service)
|
msg %= (msg_id, service)
|
||||||
print msg
|
stacklog.error(msg) # continuing loop
|
||||||
raise APIException(message=msg)
|
|
||||||
|
|
||||||
|
|
||||||
def _exists_send_status_batch(request):
|
def _exists_send_status_batch(request):
|
||||||
|
@ -21,6 +21,7 @@ from django.db.models import Count
|
|||||||
from django.db.models import FieldDoesNotExist
|
from django.db.models import FieldDoesNotExist
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
import mox
|
import mox
|
||||||
|
from mock import Mock, patch
|
||||||
|
|
||||||
from stacktach import dbapi
|
from stacktach import dbapi
|
||||||
from stacktach import models
|
from stacktach import models
|
||||||
@ -816,8 +817,10 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
self.assertEqual(resp.status_code, 200)
|
self.assertEqual(resp.status_code, 200)
|
||||||
self.mox.VerifyAll()
|
self.mox.VerifyAll()
|
||||||
|
|
||||||
def test_send_status_batch_not_found(self):
|
@patch('stacktach.models.InstanceExists.objects.select_for_update')
|
||||||
fake_request = self.mox.CreateMockAnything()
|
@patch('django.db.transaction')
|
||||||
|
def test_send_status_batch_not_found(self, django_transaction, mock_model):
|
||||||
|
fake_request = Mock()
|
||||||
fake_request.method = 'PUT'
|
fake_request.method = 'PUT'
|
||||||
messages = {
|
messages = {
|
||||||
MESSAGE_ID_1: '201',
|
MESSAGE_ID_1: '201',
|
||||||
@ -825,30 +828,21 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
body_dict = {'messages': messages}
|
body_dict = {'messages': messages}
|
||||||
body = json.dumps(body_dict)
|
body = json.dumps(body_dict)
|
||||||
fake_request.body = body
|
fake_request.body = body
|
||||||
self.mox.StubOutWithMock(transaction, 'commit_on_success')
|
|
||||||
trans_obj = self.mox.CreateMockAnything()
|
|
||||||
transaction.commit_on_success().AndReturn(trans_obj)
|
|
||||||
trans_obj.__enter__()
|
|
||||||
results = self.mox.CreateMockAnything()
|
|
||||||
models.InstanceExists.objects.select_for_update().AndReturn(results)
|
|
||||||
exception = models.InstanceExists.DoesNotExist()
|
|
||||||
results.get(message_id=MESSAGE_ID_1).AndRaise(exception)
|
|
||||||
trans_obj.__exit__(dbapi.NotFoundException().__class__,
|
|
||||||
mox.IgnoreArg(),
|
|
||||||
mox.IgnoreArg())
|
|
||||||
self.mox.ReplayAll()
|
|
||||||
|
|
||||||
|
from stacktach import dbapi
|
||||||
|
from stacktach.dbapi import models
|
||||||
|
dbapi.transaction = django_transaction
|
||||||
|
exception = models.InstanceExists.DoesNotExist("Object does not exist")
|
||||||
|
mock_model.side_effect = exception
|
||||||
|
# Since the batch update does not return any response message,
|
||||||
|
# only status code is checked.
|
||||||
resp = dbapi.exists_send_status(fake_request, 'batch')
|
resp = dbapi.exists_send_status(fake_request, 'batch')
|
||||||
self.assertEqual(resp.status_code, 404)
|
self.assertEqual(resp.status_code, 200)
|
||||||
body = json.loads(resp.content)
|
|
||||||
self.assertEqual(body.get("status"), 404)
|
|
||||||
msg = "Could not find Exists record with message_id = '%s' for nova"
|
|
||||||
msg = msg % MESSAGE_ID_1
|
|
||||||
self.assertEqual(body.get("message"), msg)
|
|
||||||
self.mox.VerifyAll()
|
|
||||||
|
|
||||||
def test_send_status_batch_multiple_results(self):
|
@patch('stacktach.models.InstanceExists.objects.select_for_update')
|
||||||
fake_request = self.mox.CreateMockAnything()
|
@patch('django.db.transaction')
|
||||||
|
def test_send_status_batch_multiple_results(self, django_transaction, mock_model):
|
||||||
|
fake_request = Mock()
|
||||||
fake_request.method = 'PUT'
|
fake_request.method = 'PUT'
|
||||||
messages = {
|
messages = {
|
||||||
MESSAGE_ID_1: 201,
|
MESSAGE_ID_1: 201,
|
||||||
@ -856,27 +850,16 @@ class DBAPITestCase(StacktachBaseTestCase):
|
|||||||
body_dict = {'messages': messages}
|
body_dict = {'messages': messages}
|
||||||
body = json.dumps(body_dict)
|
body = json.dumps(body_dict)
|
||||||
fake_request.body = body
|
fake_request.body = body
|
||||||
self.mox.StubOutWithMock(transaction, 'commit_on_success')
|
|
||||||
trans_obj = self.mox.CreateMockAnything()
|
|
||||||
transaction.commit_on_success().AndReturn(trans_obj)
|
|
||||||
trans_obj.__enter__()
|
|
||||||
results = self.mox.CreateMockAnything()
|
|
||||||
models.InstanceExists.objects.select_for_update().AndReturn(results)
|
|
||||||
exception = models.InstanceExists.MultipleObjectsReturned()
|
|
||||||
results.get(message_id=MESSAGE_ID_1).AndRaise(exception)
|
|
||||||
trans_obj.__exit__(dbapi.APIException().__class__,
|
|
||||||
mox.IgnoreArg(),
|
|
||||||
mox.IgnoreArg())
|
|
||||||
self.mox.ReplayAll()
|
|
||||||
|
|
||||||
|
from stacktach import dbapi
|
||||||
|
from stacktach.dbapi import models
|
||||||
|
dbapi.transaction = django_transaction
|
||||||
|
|
||||||
|
exception = models.InstanceExists.MultipleObjectsReturned("Multiple Exists records found")
|
||||||
|
mock_model.side_effect = exception
|
||||||
resp = dbapi.exists_send_status(fake_request, 'batch')
|
resp = dbapi.exists_send_status(fake_request, 'batch')
|
||||||
self.assertEqual(resp.status_code, 500)
|
self.assertEqual(resp.status_code, 200)
|
||||||
body = json.loads(resp.content)
|
|
||||||
self.assertEqual(body.get("status"), 500)
|
|
||||||
msg = "Multiple Exists records with message_id = '%s' for nova"
|
|
||||||
msg = msg % MESSAGE_ID_1
|
|
||||||
self.assertEqual(body.get("message"), msg)
|
|
||||||
self.mox.VerifyAll()
|
|
||||||
|
|
||||||
def test_send_status_batch_wrong_method(self):
|
def test_send_status_batch_wrong_method(self):
|
||||||
fake_request = self.mox.CreateMockAnything()
|
fake_request = self.mox.CreateMockAnything()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user