diff --git a/ceilometer/storage/impl_sqlalchemy.py b/ceilometer/storage/impl_sqlalchemy.py index 7977572d..ec9f8044 100644 --- a/ceilometer/storage/impl_sqlalchemy.py +++ b/ceilometer/storage/impl_sqlalchemy.py @@ -157,8 +157,12 @@ def make_query_from_filter(session, query, sample_filter, require_meter=True): else: query = query.filter(models.Sample.timestamp < ts_end) if sample_filter.user: + if sample_filter.user == 'None': + sample_filter.user = None query = query.filter(models.Resource.user_id == sample_filter.user) if sample_filter.project: + if sample_filter.project == 'None': + sample_filter.project = None query = query.filter( models.Resource.project_id == sample_filter.project) if sample_filter.resource: diff --git a/ceilometer/tests/functional/storage/test_impl_sqlalchemy.py b/ceilometer/tests/functional/storage/test_impl_sqlalchemy.py index b8096d40..e5492afb 100644 --- a/ceilometer/tests/functional/storage/test_impl_sqlalchemy.py +++ b/ceilometer/tests/functional/storage/test_impl_sqlalchemy.py @@ -28,6 +28,8 @@ from six.moves import reprlib from ceilometer.event.storage import impl_sqlalchemy as impl_sqla_event from ceilometer.event.storage import models +from ceilometer.publisher import utils +from ceilometer import sample from ceilometer.storage import impl_sqlalchemy from ceilometer.storage.sqlalchemy import models as sql_models from ceilometer.tests import base as test_base @@ -190,3 +192,37 @@ class CapabilitiesTest(test_base.BaseTestCase): actual_capabilities = (impl_sqlalchemy. Connection.get_storage_capabilities()) self.assertEqual(expected_capabilities, actual_capabilities) + + +@tests_db.run_with('sqlite', 'mysql', 'pgsql') +class FilterQueryTestForMeters(scenarios.DBTestBase): + def prepare_data(self): + self.counters = [] + c = sample.Sample( + 'volume.size', + 'gauge', + 'GiB', + 5, + user_id=None, + project_id=None, + resource_id='fake_id', + timestamp=datetime.datetime(2012, 9, 25, 10, 30), + resource_metadata={'display_name': 'test-volume', + 'tag': 'self.counter', + }, + source='test', + ) + + self.counters.append(c) + msg = utils.meter_message_from_counter( + c, + secret='not-so-secret') + self.conn.record_metering_data(msg) + + def test_get_meters_by_user(self): + meters = list(self.conn.get_meters(user='None')) + self.assertEqual(1, len(meters)) + + def test_get_meters_by_project(self): + meters = list(self.conn.get_meters(project='None')) + self.assertEqual(1, len(meters))