From b7aa9c898006fd447e390744685c9a1cff7046a1 Mon Sep 17 00:00:00 2001 From: Isham Ibrahim Date: Mon, 19 Jun 2017 01:52:11 +0530 Subject: [PATCH] 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 --- etc/pip-requires.txt | 1 + stacktach/dbapi.py | 9 +++--- tests/unit/test_dbapi.py | 67 +++++++++++++++------------------------- 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/etc/pip-requires.txt b/etc/pip-requires.txt index 2e40f82..f16ae77 100644 --- a/etc/pip-requires.txt +++ b/etc/pip-requires.txt @@ -10,3 +10,4 @@ requests south sphinxcontrib-httpdomain pbr +mock==2.0.0 diff --git a/stacktach/dbapi.py b/stacktach/dbapi.py index f3a3349..199f20b 100644 --- a/stacktach/dbapi.py +++ b/stacktach/dbapi.py @@ -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): diff --git a/tests/unit/test_dbapi.py b/tests/unit/test_dbapi.py index 9ebc85d..24c55ed 100644 --- a/tests/unit/test_dbapi.py +++ b/tests/unit/test_dbapi.py @@ -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()