From 37a779065f73c47ccdd87b09b59030c01ff190ba Mon Sep 17 00:00:00 2001 From: Doug Hellmann Date: Fri, 6 Sep 2013 10:33:41 -0400 Subject: [PATCH] Make type guessing for query args more robust Improve the error handling of the type-guesser for queries against metadata fields, and add some test coverage for specific cases that came out of the discussion of the source of the bug. Fixes bug #1221736 Change-Id: I27f84aa2538d2a69a3d7b7e93c36813df8d35204 --- ceilometer/api/controllers/v2.py | 2 +- tests/api/v2/test_query.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ceilometer/api/controllers/v2.py b/ceilometer/api/controllers/v2.py index 5f0ad2e8a..88db116f9 100644 --- a/ceilometer/api/controllers/v2.py +++ b/ceilometer/api/controllers/v2.py @@ -175,7 +175,7 @@ class Query(_Base): if not self.type: try: converted_value = ast.literal_eval(self.value) - except ValueError: + except (ValueError, SyntaxError): msg = _('Failed to convert the metadata value %s' ' automatically') % (self.value) LOG.debug(msg) diff --git a/tests/api/v2/test_query.py b/tests/api/v2/test_query.py index 8a0a2a9d9..432bb8bfb 100644 --- a/tests/api/v2/test_query.py +++ b/tests/api/v2/test_query.py @@ -101,6 +101,40 @@ class TestQuery(tests_base.TestCase): type='integer') self.assertRaises(wsme.exc.ClientSideError, query._get_value_as_type) + def test_get_value_as_type_integer_expression_without_type(self): + # bug 1221736 + query = Query(field='should_be_a_string', + op='eq', + value='123-1') + expected = '123-1' + self.assertEqual(query._get_value_as_type(), expected) + + def test_get_value_as_type_boolean_expression_without_type(self): + # bug 1221736 + query = Query(field='should_be_a_string', + op='eq', + value='True or False') + expected = 'True or False' + self.assertEqual(query._get_value_as_type(), expected) + + def test_get_value_as_type_with_syntax_error(self): + # bug 1221736 + value = 'WWW-Layer-4a80714f-0232-4580-aa5e-81494d1a4147-uolhh25p5xxm' + query = Query(field='group_id', + op='eq', + value=value) + expected = value + self.assertEqual(query._get_value_as_type(), expected) + + def test_get_value_as_type_with_syntax_error_colons(self): + # bug 1221736 + value = 'Ref::StackId' + query = Query(field='field_name', + op='eq', + value=value) + expected = value + self.assertEqual(query._get_value_as_type(), expected) + def _fake_db_func(self, resource, on_behalf_of, x, y, metaquery={}, user=None, project=None, start_timestamp=None, start_timestamp_op=None,