def test_send_status_batch(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' messages = { MESSAGE_ID_1: 200, MESSAGE_ID_2: 400 } 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__() results1 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results1) exists1 = self.mox.CreateMockAnything() results1.get(message_id=MESSAGE_ID_2).AndReturn(exists1) exists1.save() results2 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results2) exists2 = self.mox.CreateMockAnything() results2.get(message_id=MESSAGE_ID_1).AndReturn(exists2) exists2.save() trans_obj.__exit__(None, None, None) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 200) exists1.send_status = 200 self.mox.VerifyAll()
def test_send_status_batch_multiple_results(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' messages = { MESSAGE_ID_1: 200, } 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() 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'" msg = msg % MESSAGE_ID_1 self.assertEqual(body.get("message"), msg) self.mox.VerifyAll()
def test_send_status_batch_accepts_post(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'POST' messages = {MESSAGE_ID_1: 200, MESSAGE_ID_2: 400} 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__() results1 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results1) exists1 = self.mox.CreateMockAnything() results1.get(message_id=MESSAGE_ID_2).AndReturn(exists1) exists1.save() results2 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results2) exists2 = self.mox.CreateMockAnything() results2.get(message_id=MESSAGE_ID_1).AndReturn(exists2) exists2.save() trans_obj.__exit__(None, None, None) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 200) exists1.send_status = 200 self.mox.VerifyAll()
def test_send_status_batch(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' messages = { MESSAGE_ID_1: 200, MESSAGE_ID_2: 400 } body_dict = {'messages': messages} body = json.dumps(body_dict) fake_request.body = body results1 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results1) exists1 = self.mox.CreateMockAnything() results1.get(message_id=MESSAGE_ID_2).AndReturn(exists1) exists1.save() results2 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results2) exists2 = self.mox.CreateMockAnything() results2.get(message_id=MESSAGE_ID_1).AndReturn(exists2) exists2.save() self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 200) exists1.send_status = 200 self.mox.VerifyAll()
def test_send_status(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' body_dict = {'send_status': 200} body = json.dumps(body_dict) fake_request.body = body exists = self.mox.CreateMockAnything() result = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(result) result.get(message_id=MESSAGE_ID_1).AndReturn(exists) exists.save() self.mox.ReplayAll() dbapi.exists_send_status(fake_request, MESSAGE_ID_1) self.assertEqual(exists.send_status, 200) self.mox.VerifyAll()
def test_send_status_accepts_post(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'POST' body_dict = {'send_status': 200} body = json.dumps(body_dict) fake_request.body = body exists = self.mox.CreateMockAnything() result = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(result) result.get(message_id=MESSAGE_ID_1).AndReturn(exists) exists.save() self.mox.ReplayAll() dbapi.exists_send_status(fake_request, MESSAGE_ID_1) self.assertEqual(exists.send_status, 200) self.mox.VerifyAll()
def test_send_status_batch_wrong_method(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 400) body = json.loads(resp.content) self.assertEqual(body.get('status'), 400) self.assertEqual(body.get('message'), "Invalid method") self.mox.VerifyAll()
def test_send_status_no_body(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' fake_request.body = None self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, MESSAGE_ID_1) self.assertEqual(resp.status_code, 400) body = json.loads(resp.content) self.assertEqual(body.get("status"), 400) self.assertEqual(body.get("message"), "Request body required") self.mox.VerifyAll()
def test_send_status_batch_empty_body(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' fake_request.body = '' self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 400) body = json.loads(resp.content) self.assertEqual(body.get('status'), 400) self.assertEqual(body.get('message'), "Request body required") self.mox.VerifyAll()
def test_send_status_batch_accepts_post_for_nova_and_glance_when_version_is_1( self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'POST' fake_request.GET = {'service': 'glance'} messages = { 'nova': { MESSAGE_ID_3: 201 }, 'glance': { MESSAGE_ID_1: 201, MESSAGE_ID_2: 201 } } body_dict = {'version': 1, '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__() results1 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results1) exists1 = self.mox.CreateMockAnything() results1.get(message_id=MESSAGE_ID_3).AndReturn(exists1) exists1.save() trans_obj.__exit__(None, None, None) trans_obj = self.mox.CreateMockAnything() transaction.commit_on_success().AndReturn(trans_obj) trans_obj.__enter__() results1 = self.mox.CreateMockAnything() models.ImageExists.objects.select_for_update().AndReturn(results1) exists1A = self.mox.CreateMockAnything() exists1B = self.mox.CreateMockAnything() results1.filter(message_id=MESSAGE_ID_2).AndReturn( [exists1A, exists1B]) exists1A.save() exists1B.save() results2 = self.mox.CreateMockAnything() models.ImageExists.objects.select_for_update().AndReturn(results2) exists2A = self.mox.CreateMockAnything() exists2B = self.mox.CreateMockAnything() results2.filter(message_id=MESSAGE_ID_1).AndReturn( [exists2A, exists2B]) exists2A.save() exists2B.save() trans_obj.__exit__(None, None, None) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 200) self.mox.VerifyAll()
def test_send_status_batch_bad_body(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' body_dict = {'bad': 'body'} fake_request.body = json.dumps(body_dict) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 400) body = json.loads(resp.content) self.assertEqual(body.get('status'), 400) msg = "'messages' missing from request body" self.assertEqual(body.get('message'), msg) self.mox.VerifyAll()
def test_send_status_batch_accepts_post_for_nova_and_glance_when_version_is_1( self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'POST' fake_request.GET = {'service': 'glance'} messages = { 'nova': {MESSAGE_ID_3: 201}, 'glance': {MESSAGE_ID_1: 201, MESSAGE_ID_2: 201} } body_dict = {'version': 1, '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__() results1 = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(results1) exists1 = self.mox.CreateMockAnything() results1.get(message_id=MESSAGE_ID_3).AndReturn(exists1) exists1.save() trans_obj.__exit__(None, None, None) trans_obj = self.mox.CreateMockAnything() transaction.commit_on_success().AndReturn(trans_obj) trans_obj.__enter__() results1 = self.mox.CreateMockAnything() models.ImageExists.objects.select_for_update().AndReturn(results1) exists1A = self.mox.CreateMockAnything() exists1B = self.mox.CreateMockAnything() results1.filter(message_id=MESSAGE_ID_2).AndReturn( [exists1A, exists1B]) exists1A.save() exists1B.save() results2 = self.mox.CreateMockAnything() models.ImageExists.objects.select_for_update().AndReturn(results2) exists2A = self.mox.CreateMockAnything() exists2B = self.mox.CreateMockAnything() results2.filter(message_id=MESSAGE_ID_1).AndReturn( [exists2A, exists2B]) exists2A.save() exists2B.save() trans_obj.__exit__(None, None, None) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, 'batch') self.assertEqual(resp.status_code, 200) self.mox.VerifyAll()
def test_send_status_multiple_results(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' body_dict = {'send_status': 200} body = json.dumps(body_dict) fake_request.body = body result = self.mox.CreateMockAnything() models.InstanceExists.objects.select_for_update().AndReturn(result) exception = models.InstanceExists.MultipleObjectsReturned() result.get(message_id=MESSAGE_ID_1).AndRaise(exception) self.mox.ReplayAll() resp = dbapi.exists_send_status(fake_request, MESSAGE_ID_1) 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'" msg = msg % MESSAGE_ID_1 self.assertEqual(body.get("message"), msg) self.mox.VerifyAll()
def test_send_status_batch_not_found(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'PUT' messages = { MESSAGE_ID_1: 200, } body_dict = {'messages': messages} body = json.dumps(body_dict) fake_request.body = body 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) self.mox.ReplayAll() 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'" msg = msg % MESSAGE_ID_1 self.assertEqual(body.get("message"), msg) self.mox.VerifyAll()