Merge pull request #107 from ramielrowe/usage_seed

Fixing some bugs
This commit is contained in:
Sandy Walsh
2013-06-04 09:46:20 -07:00
3 changed files with 56 additions and 8 deletions

View File

@@ -148,7 +148,7 @@ def get_usage_exist(request, exist_id):
@api_call
def exists_send_status(request, message_id):
if request.method != 'PUT':
if request.method not in ['PUT', 'POST']:
raise BadRequestException(message="Invalid method")
if request.body is None or request.body == '':

View File

@@ -404,6 +404,24 @@ class DBAPITestCase(unittest.TestCase):
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_not_found(self):
fake_request = self.mox.CreateMockAnything()
fake_request.method = 'PUT'
@@ -517,6 +535,33 @@ class DBAPITestCase(unittest.TestCase):
trans_obj.__exit__(None, None, None)
self.mox.ReplayAll()
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

View File

@@ -106,7 +106,7 @@ def _usage_for_instance(instance, task=None):
def _delete_for_instance(instance):
delete = {
'instance': instance['uuid'],
'deleted_at': dt.dt_to_decimal(instance.get('deleted_at')),
'deleted_at': dt.dt_to_decimal(instance.get('terminated_at')),
}
launched_at = instance.get('launched_at')
@@ -121,15 +121,18 @@ def get_active_instances(period_length):
session = api.get_session()
computes = novadb.service_get_all_by_topic(context, 'compute')
active_instances = []
yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1)
for compute in computes:
query = session.query(novamodels.Instance)
if compute.updated_at > yesterday:
query = session.query(novamodels.Instance)
query = query.filter(api.or_(novamodels.Instance.terminated_at == None,
novamodels.Instance.terminated_at > start))
query = query.filter_by(host=compute.host)
active_filter = api.or_(novamodels.Instance.terminated_at == None,
novamodels.Instance.terminated_at > start)
query = query.filter(active_filter)
query = query.filter_by(host=compute.host)
for instance in query.all():
active_instances.append(instance)
for instance in query.all():
active_instances.append(instance)
return active_instances