Unit tests for RawData parsing

This commit is contained in:
Andrew Melton 2013-01-25 14:55:12 -05:00
parent f8005284c6
commit a30725a46d
9 changed files with 220 additions and 13 deletions

BIN
.coverage Normal file

Binary file not shown.

6
local_settings.py.bak Normal file
View File

@ -0,0 +1,6 @@
STACKTACH_DB_ENGINE='django.db.backends.sqlite3'
STACKTACH_DB_NAME='/tmp/stacktach.sqlite'
STACKTACH_DB_HOST=''
STACKTACH_DB_USERNAME=''
STACKTACH_DB_PASSWORD=''
STACKTACH_INSTALL_DIR='/home/andrewmelton/publicgit/stacktach-app/'

4
run_integration_tests.sh Executable file
View File

@ -0,0 +1,4 @@
#! /bin/bash
workingdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
django-admin.py test --pythonpath=$workingdir --settings=tests.integration.settings stacktach

2
run_tests.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
nosetests tests --exclude-dir=stacktach --with-coverage --cover-package=stacktach

View File

@ -1,5 +1,8 @@
import models import models
def create_rawdata(**kwargs):
return models.RawData(**kwargs)
def create_lifecycle(**kwargs): def create_lifecycle(**kwargs):
return models.Lifecycle(**kwargs) return models.Lifecycle(**kwargs)

View File

@ -347,8 +347,8 @@ def process_raw_data(deployment, args, json_args):
values['when'] = dt.dt_to_decimal(when) values['when'] = dt.dt_to_decimal(when)
values['routing_key'] = routing_key values['routing_key'] = routing_key
values['json'] = json_args values['json'] = json_args
record = models.RawData(**values) record = STACKDB.create_rawdata(**values)
record.save() STACKDB.save(record)
aggregate_lifecycle(record) aggregate_lifecycle(record)
aggregate_usage(record) aggregate_usage(record)

View File

@ -3,7 +3,6 @@ import decimal
import unittest import unittest
import utils import utils
utils.setup_sys_path()
from stacktach import datetime_to_decimal from stacktach import datetime_to_decimal
class DatetimeToDecimalTestCase(unittest.TestCase): class DatetimeToDecimalTestCase(unittest.TestCase):

View File

@ -7,7 +7,6 @@ import unittest
import mox import mox
import utils import utils
utils.setup_sys_path()
from utils import INSTANCE_ID_1 from utils import INSTANCE_ID_1
from utils import INSTANCE_ID_2 from utils import INSTANCE_ID_2
from utils import MESSAGE_ID_1 from utils import MESSAGE_ID_1
@ -15,8 +14,200 @@ from utils import MESSAGE_ID_2
from utils import REQUEST_ID_1 from utils import REQUEST_ID_1
from utils import REQUEST_ID_2 from utils import REQUEST_ID_2
from utils import REQUEST_ID_3 from utils import REQUEST_ID_3
from utils import TENANT_ID_1
from stacktach import views from stacktach import views
class StacktachRawParsingTestCase(unittest.TestCase):
def setUp(self):
self.mox = mox.Mox()
views.STACKDB = self.mox.CreateMockAnything()
def tearDown(self):
self.mox.UnsetStubs()
def assertOnHandlerResponce(self, resp, **kwargs):
for key in kwargs:
self.assertTrue(key in resp, msg='%s not in response' % key)
self.assertEqual(resp[key], kwargs[key])
def test_monitor_message(self):
body = {
'event_type': 'compute.instance.create.start',
'publisher_id': 'compute.cpu1-n01.example.com',
'_context_request_id': REQUEST_ID_1,
'_context_project_id': TENANT_ID_1,
'payload': {
'instance_id': INSTANCE_ID_1,
'state': 'active',
'old_state': 'building',
'old_task_state': 'build',
},
}
resp = views._monitor_message(None, body)
self.assertOnHandlerResponce(resp, host='cpu1-n01.example.com',
instance=INSTANCE_ID_1,
publisher=body['publisher_id'],
service='compute',
event=body['event_type'],
tenant=TENANT_ID_1,
request_id=REQUEST_ID_1,
state='active',
old_state='building',
old_task='build')
def test_monitor_message_no_host(self):
body = {
'event_type': 'compute.instance.create.start',
'publisher_id': 'compute',
'_context_request_id': REQUEST_ID_1,
'_context_project_id': TENANT_ID_1,
'payload': {
'instance_id': INSTANCE_ID_1,
'state': 'active',
'old_state': 'building',
'old_task_state': 'build',
},
}
resp = views._monitor_message(None, body)
self.assertOnHandlerResponce(resp, host=None, instance=INSTANCE_ID_1,
publisher=body['publisher_id'],
service='compute',
event=body['event_type'],
tenant=TENANT_ID_1,
request_id=REQUEST_ID_1, state='active',
old_state='building', old_task='build')
def test_monitor_message_exception(self):
body = {
'event_type': 'compute.instance.create.start',
'publisher_id': 'compute.cpu1-n01.example.com',
'_context_request_id': REQUEST_ID_1,
'_context_project_id': TENANT_ID_1,
'payload': {
'exception': {'kwargs':{'uuid': INSTANCE_ID_1}},
'state': 'active',
'old_state': 'building',
'old_task_state': 'build',
},
}
resp = views._monitor_message(None, body)
self.assertOnHandlerResponce(resp, host='cpu1-n01.example.com',
instance=INSTANCE_ID_1,
publisher=body['publisher_id'],
service='compute',
event=body['event_type'],
tenant=TENANT_ID_1,
request_id=REQUEST_ID_1,
state='active', old_state='building',
old_task='build')
def test_monitor_message_exception(self):
body = {
'event_type': 'compute.instance.create.start',
'publisher_id': 'compute.cpu1-n01.example.com',
'_context_request_id': REQUEST_ID_1,
'_context_project_id': TENANT_ID_1,
'payload': {
'instance': {'uuid': INSTANCE_ID_1},
'state': 'active',
'old_state': 'building',
'old_task_state': 'build',
},
}
resp = views._monitor_message(None, body)
self.assertOnHandlerResponce(resp, host='cpu1-n01.example.com',
instance=INSTANCE_ID_1,
publisher=body['publisher_id'],
service='compute',
event=body['event_type'],
tenant=TENANT_ID_1,
request_id=REQUEST_ID_1,
state='active', old_state='building',
old_task='build')
def test_compute_update_message(self):
body = {
'_context_request_id': REQUEST_ID_1,
'method': 'some_method',
'args': {
'host': 'compute',
'service_name': 'compute',
'_context_project_id': TENANT_ID_1
},
'payload': {
'state': 'active',
'old_state': 'building',
'old_task_state': 'build',
}
}
resp = views._compute_update_message(None, body)
print resp
self.assertOnHandlerResponce(resp, publisher=None, instance=None,
host='compute', tenant=TENANT_ID_1,
event='some_method',
request_id=REQUEST_ID_1, state='active',
old_state='building', old_task='build')
def test_process_raw_data(self):
deployment = self.mox.CreateMockAnything()
when = '2013-1-25 13:38:23.123'
dict = {
'timestamp': when,
}
args = ('monitor.info', dict)
json_args = json.dumps(args)
old_info_handler = views.HANDLERS['monitor.info']
views.HANDLERS['monitor.info'] = lambda key, mess: {'host': 'api'}
raw_values = {
'deployment': deployment,
'when': utils.decimal_utc(datetime.datetime.strptime(when, "%Y-%m-%d %H:%M:%S.%f")),
'host': 'api',
'routing_key': 'monitor.info',
'json': json_args
}
raw = self.mox.CreateMockAnything()
views.STACKDB.create_rawdata(**raw_values).AndReturn(raw)
views.STACKDB.save(raw)
self.mox.StubOutWithMock(views, "aggregate_lifecycle")
views.aggregate_lifecycle(raw)
self.mox.StubOutWithMock(views, "aggregate_usage")
views.aggregate_usage(raw)
self.mox.ReplayAll()
views.process_raw_data(deployment, args, json_args)
self.mox.VerifyAll()
views.HANDLERS['monitor.info'] = old_info_handler
def test_process_raw_data_old_timestamp(self):
deployment = self.mox.CreateMockAnything()
when = '2013-1-25T13:38:23.123'
dict = {
'_context_timestamp': when,
}
args = ('monitor.info', dict)
json_args = json.dumps(args)
old_info_handler = views.HANDLERS['monitor.info']
views.HANDLERS['monitor.info'] = lambda key, mess: {'host': 'api'}
raw_values = {
'deployment': deployment,
'when': utils.decimal_utc(datetime.datetime.strptime(when, "%Y-%m-%dT%H:%M:%S.%f")),
'host': 'api',
'routing_key': 'monitor.info',
'json': json_args
}
raw = self.mox.CreateMockAnything()
views.STACKDB.create_rawdata(**raw_values).AndReturn(raw)
views.STACKDB.save(raw)
self.mox.StubOutWithMock(views, "aggregate_lifecycle")
views.aggregate_lifecycle(raw)
self.mox.StubOutWithMock(views, "aggregate_usage")
views.aggregate_usage(raw)
self.mox.ReplayAll()
views.process_raw_data(deployment, args, json_args)
self.mox.VerifyAll()
views.HANDLERS['monitor.info'] = old_info_handler
class StacktachLifecycleTestCase(unittest.TestCase): class StacktachLifecycleTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.mox = mox.Mox() self.mox = mox.Mox()

View File

@ -3,15 +3,7 @@ import os
import sys import sys
import unittest import unittest
INSTANCE_ID_1 = 'testinstanceid1' TENANT_ID_1 = 'testtenantid1'
INSTANCE_ID_2 = 'testinstanceid2'
MESSAGE_ID_1 = 'testmessageid1'
MESSAGE_ID_2 = 'testmessageid2'
REQUEST_ID_1 = 'testrequestid1'
REQUEST_ID_2 = 'testrequestid2'
REQUEST_ID_3 = 'testrequestid3'
def setup_sys_path(): def setup_sys_path():
sys.path = [os.path.abspath(os.path.dirname('stacktach'))] + sys.path sys.path = [os.path.abspath(os.path.dirname('stacktach'))] + sys.path
@ -31,6 +23,16 @@ setup_sys_path()
setup_environment() setup_environment()
from stacktach import datetime_to_decimal as dt from stacktach import datetime_to_decimal as dt
INSTANCE_ID_1 = 'testinstanceid1'
INSTANCE_ID_2 = 'testinstanceid2'
MESSAGE_ID_1 = 'testmessageid1'
MESSAGE_ID_2 = 'testmessageid2'
REQUEST_ID_1 = 'testrequestid1'
REQUEST_ID_2 = 'testrequestid2'
REQUEST_ID_3 = 'testrequestid3'
def decimal_utc(t = datetime.datetime.utcnow()): def decimal_utc(t = datetime.datetime.utcnow()):
return dt.dt_to_decimal(t) return dt.dt_to_decimal(t)