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:
Isham Ibrahim 2017-06-19 01:52:11 +05:30
parent d6acee808d
commit b7aa9c8980
3 changed files with 30 additions and 47 deletions

View File

@ -10,3 +10,4 @@ requests
south
sphinxcontrib-httpdomain
pbr
mock==2.0.0

View File

@ -297,13 +297,12 @@ def _ping_processing_with_service(pings, service, version=1):
exists.save()
except exists_model.DoesNotExist:
msg = "Could not find Exists record with message_id = '%s' for %s"
msg = msg % (msg_id, service)
raise NotFoundException(message=msg)
msg %= (msg_id, service)
stacklog.error(msg) # continuing loop
except exists_model.MultipleObjectsReturned:
msg = "Multiple Exists records with message_id = '%s' for %s"
msg = msg % (msg_id, service)
print msg
raise APIException(message=msg)
msg %= (msg_id, service)
stacklog.error(msg) # continuing loop
def _exists_send_status_batch(request):

View File

@ -21,6 +21,7 @@ from django.db.models import Count
from django.db.models import FieldDoesNotExist
from django.db import transaction
import mox
from mock import Mock, patch
from stacktach import dbapi
from stacktach import models
@ -816,8 +817,10 @@ class DBAPITestCase(StacktachBaseTestCase):
self.assertEqual(resp.status_code, 200)
self.mox.VerifyAll()
def test_send_status_batch_not_found(self):
fake_request = self.mox.CreateMockAnything()
@patch('stacktach.models.InstanceExists.objects.select_for_update')
@patch('django.db.transaction')
def test_send_status_batch_not_found(self, django_transaction, mock_model):
fake_request = Mock()
fake_request.method = 'PUT'
messages = {
MESSAGE_ID_1: '201',
@ -825,30 +828,21 @@ class DBAPITestCase(StacktachBaseTestCase):
body_dict = {'messages': messages}
body = json.dumps(body_dict)
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')
self.assertEqual(resp.status_code, 404)
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()
self.assertEqual(resp.status_code, 200)
def test_send_status_batch_multiple_results(self):
fake_request = self.mox.CreateMockAnything()
@patch('stacktach.models.InstanceExists.objects.select_for_update')
@patch('django.db.transaction')
def test_send_status_batch_multiple_results(self, django_transaction, mock_model):
fake_request = Mock()
fake_request.method = 'PUT'
messages = {
MESSAGE_ID_1: 201,
@ -856,27 +850,16 @@ class DBAPITestCase(StacktachBaseTestCase):
body_dict = {'messages': messages}
body = json.dumps(body_dict)
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')
self.assertEqual(resp.status_code, 500)
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()
self.assertEqual(resp.status_code, 200)
def test_send_status_batch_wrong_method(self):
fake_request = self.mox.CreateMockAnything()