From bbaabaf887f1b87a23aaf3cd73b7b68ac20f13a5 Mon Sep 17 00:00:00 2001 From: ananya23d Date: Tue, 22 Dec 2015 10:47:36 +0530 Subject: [PATCH] Set None explicitly to filter options. When user set None in query string value 'None' was set instead of None. Note: This fix is for mongodb only. I will add another patch for other database. Partial-Bug: #1388680 Change-Id: I2f42013b9c8fb9a3af9d6366f10dd8bb3906d73a --- ceilometer/storage/pymongo_base.py | 12 +++- .../functional/storage/test_pymongo_base.py | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/ceilometer/storage/pymongo_base.py b/ceilometer/storage/pymongo_base.py index 93c696d5..594302e9 100644 --- a/ceilometer/storage/pymongo_base.py +++ b/ceilometer/storage/pymongo_base.py @@ -66,11 +66,17 @@ class Connection(base.Connection): metaquery = pymongo_utils.improve_keys(metaquery, metaquery=True) or {} q = {} - if user is not None: + if user == 'None': + q['user_id'] = None + elif user is not None: q['user_id'] = user - if project is not None: + if project == 'None': + q['project_id'] = None + elif project is not None: q['project_id'] = project - if resource is not None: + if resource == 'None': + q['_id'] = None + elif resource is not None: q['_id'] = resource if source is not None: q['source'] = source diff --git a/ceilometer/tests/functional/storage/test_pymongo_base.py b/ceilometer/tests/functional/storage/test_pymongo_base.py index 43fdee39..86c1ed71 100644 --- a/ceilometer/tests/functional/storage/test_pymongo_base.py +++ b/ceilometer/tests/functional/storage/test_pymongo_base.py @@ -78,3 +78,69 @@ class CompatibilityTest(test_storage_scenarios.DBTestBase): def test_counter_unit(self): meters = list(self.conn.get_meters()) self.assertEqual(1, len(meters)) + + +# TODO(ananya) same test should be done for other databse +@tests_db.run_with('mongodb', 'db2') +class FilterQueryTestForMeters(test_storage_scenarios.DBTestBase): + def prepare_data(self): + def old_record_metering_data(self, data): + received_timestamp = datetime.datetime.utcnow() + self.db.resource.update( + {'_id': data['resource_id']}, + {'$set': {'project_id': data['project_id'], + 'user_id': data['user_id'], + # Current metadata being used and when it was + # last updated. + 'timestamp': data['timestamp'], + 'received_timestamp': received_timestamp, + 'metadata': data['resource_metadata'], + 'source': data['source'], + }, + '$addToSet': {'meter': {'counter_name': data['counter_name'], + 'counter_type': data['counter_type'], + }, + }, + }, + upsert=True, + ) + + record = copy.copy(data) + self.db.meter.insert(record) + + # Stubout with the old version DB schema, the one w/o 'counter_unit' + with mock.patch.object(self.conn, 'record_metering_data', + side_effect=old_record_metering_data): + self.counters = [] + c = sample.Sample( + 'volume.size', + 'gauge', + 'GiB', + 5, + None, + None, + None, + 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(self.conn, 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_resource(self): + meters = list(self.conn.get_meters(resource='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))