Fix the return of statistic with getting no sample

In mysql scenario, when getting statistics with no sample
(this can be presented with invalid value of sample filters), ceilometer
will raise an error about NoneType.
Like other API of ceilometer, it should return an empty list.

Change-Id: I07cbc3f03f7a5e6751e6851f14ed1cdc16ee4a4c
Closes-bug: #1292322
This commit is contained in:
liu-sheng 2014-03-14 11:26:33 +08:00
parent f46a8b160e
commit d92d25ab06
2 changed files with 12 additions and 0 deletions

View File

@ -734,6 +734,10 @@ class Connection(base.Connection):
res = self._make_stats_query(sample_filter,
None,
aggregate).first()
if not res:
# NOTE(liusheng):The 'res' may be NoneType, because no
# sample has found with sample filter(s).
return
query = self._make_stats_query(sample_filter, groupby, aggregate)
# HACK(jd) This is an awful method to compute stats by period, but

View File

@ -1364,6 +1364,14 @@ class StatisticsTest(DBTestBase,
self.assertEqual(results.sum, 18)
self.assertEqual(results.avg, 6)
def test_with_no_sample(self):
f = storage.SampleFilter(
user='user-not-exists',
meter='volume.size',
)
results = list(self.conn.get_meter_statistics(f, period=1800))
self.assertEqual([], results)
class StatisticsGroupByTest(DBTestBase,
tests_db.MixinTestsWithBackendScenarios):