Corrected get_raw_event() in sqlalchemy.

This fixed bug 1101100. The returned dict of get_raw_event() in sqlalchemy
should be consistent with one generated by
collector.meter.meter_message_from_counter().

Change-Id: I6948fdbd0c1e03d46be7d3a05faa53ca70698ce8
This commit is contained in:
Lianhao Lu 2013-01-22 16:45:48 +08:00
parent 83ef54a891
commit ce1a4b57a5
4 changed files with 29 additions and 60 deletions

View File

@ -335,6 +335,11 @@ class Connection(base.Connection):
# detail that should not leak outside of the driver.
e = row2dict(e)
del e['id']
# Replace 'sources' with 'source' to meet the caller's
# expectation, Meter.sources contains one and only one
# source in the current implementation.
e['source'] = e['sources'][0]['id']
del e['sources']
yield e
def _make_volume_query(self, event_filter, counter_volume_func):

View File

@ -331,6 +331,28 @@ class MeterTest(DBTestBase):
class RawEventTest(DBTestBase):
def test_get_raw_events_by_user(self):
f = storage.EventFilter(user='user-id')
results = list(self.conn.get_raw_events(f))
assert len(results) == 2
for meter in results:
assert meter in [self.msg1, self.msg2]
def test_get_raw_events_by_project(self):
f = storage.EventFilter(project='project-id')
results = list(self.conn.get_raw_events(f))
assert results
for meter in results:
assert meter in [self.msg1, self.msg2, self.msg3]
def test_get_raw_events_by_resource(self):
f = storage.EventFilter(user='user-id', resource='resource-id')
results = list(self.conn.get_raw_events(f))
assert results
meter = results[0]
assert meter is not None
assert meter == self.msg1
def test_get_events_by_metaquery(self):
q = {'metadata.display_name': 'test-server'}
f = storage.EventFilter(metaquery=q)

View File

@ -112,28 +112,7 @@ class MeterTest(base.MeterTest, MongoDBEngineTestBase):
class RawEventTest(base.RawEventTest, MongoDBEngineTestBase):
def test_get_raw_events_by_user(self):
f = storage.EventFilter(user='user-id')
results = list(self.conn.get_raw_events(f))
assert len(results) == 2
for meter in results:
assert meter in [self.msg1, self.msg2]
def test_get_raw_events_by_project(self):
f = storage.EventFilter(project='project-id')
results = list(self.conn.get_raw_events(f))
assert results
for meter in results:
assert meter in [self.msg1, self.msg2, self.msg3]
def test_get_raw_events_by_resource(self):
f = storage.EventFilter(user='user-id', resource='resource-id')
results = list(self.conn.get_raw_events(f))
assert results
meter = results[0]
assert meter is not None
assert meter == self.msg1
pass
class SumTest(base.SumTest, MongoDBEngineTestBase):

View File

@ -112,44 +112,7 @@ class MeterTest(base.MeterTest, SQLAlchemyEngineTestBase):
class RawEventTest(base.RawEventTest, SQLAlchemyEngineTestBase):
def _compare_raw(self, msg_dict, result_dict):
for k, v in msg_dict.items():
if k in ['timestamp', 'source']:
continue
if k == 'resource_metadata':
key = result_dict[k]
value = v
else:
key = str(result_dict[k])
value = str(v)
assert key == value
def _iterate_msgs(self, results):
for meter in results:
labels = map(lambda x: x['id'], meter['sources'])
# should only have one source
assert len(labels) == 1
count = re.match('test-(\d+)', labels[0]).group(1)
self._compare_raw(getattr(self, 'msg' + count), meter)
def test_get_raw_events_by_user(self):
f = storage.EventFilter(user='user-id')
results = list(self.conn.get_raw_events(f))
assert len(results) == 2
self._iterate_msgs(results)
def test_get_raw_events_by_project(self):
f = storage.EventFilter(project='project-id')
results = list(self.conn.get_raw_events(f))
assert len(results) == 3
self._iterate_msgs(results)
def test_get_raw_events_by_resource(self):
f = storage.EventFilter(user='user-id', resource='resource-id')
results = list(self.conn.get_raw_events(f))
assert len(results) == 1
self._compare_raw(self.msg1, results[0])
pass
class TestGetEventInterval(base.TestGetEventInterval,