Set None explicitly to filter options.

When user is set to None in query, string value 'None'
was set instead of None.

Note: This fix is for mysql.
Fix for mongo is provided in:
https://review.openstack.org/#/c/260331/

Note2: Here, test for resource_id is removed as,
in sqlalchemy implementation, resource_id cannot be None.

Change-Id: I4bcf9283912e857a9902c62df25d0d5090670b10
Partial-Bug: #1388680
This commit is contained in:
Ananya Chatterjee 2016-02-02 12:50:07 +05:30
parent 3776fb656d
commit c25c044741
2 changed files with 40 additions and 0 deletions

View File

@ -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:

View File

@ -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))