From afb29a398cc73849ded2ef191513346cebb14834 Mon Sep 17 00:00:00 2001 From: Peter Stachowski Date: Thu, 14 Jul 2016 22:32:05 +0000 Subject: [PATCH] Support flavor ids with leading '0' When Trove moved to support alphanumeric flavor ids, the case of having a numeric string id with leading zeros was not taken into account. On the server side the code checks if the flavor id can be converted to an int, and if so then sets it as such - the manifestation being that if the str_id is '01' then the id will be set to '1' instead of None as it should be. This behavior has been fixed and unit tests created. Change-Id: I87a8ac98d4358b75182c987f584c6098398a0c2a Depends-On: I5acdec576a2e7da6cbfbb1cfc61c49fbbf7379af Closes-Bug: #1603187 --- trove/flavor/views.py | 8 ++- trove/tests/unittests/flavor/__init__.py | 0 .../unittests/flavor/test_flavor_views.py | 68 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 trove/tests/unittests/flavor/__init__.py create mode 100644 trove/tests/unittests/flavor/test_flavor_views.py diff --git a/trove/flavor/views.py b/trove/flavor/views.py index b5be2494e0..c850af1eee 100644 --- a/trove/flavor/views.py +++ b/trove/flavor/views.py @@ -28,11 +28,13 @@ class FlavorView(object): def data(self): - # If the flavor id cannot be cast to an int, we simply return + # If the flavor id is not an int, we simply return # no id and rely on str_id instead. - try: + if isinstance(self.flavor.id, int) or ( + self.flavor.id.isdigit() and + not self.flavor.id.startswith('0')): f_id = int(self.flavor.id) - except ValueError: + else: f_id = None flavor = { diff --git a/trove/tests/unittests/flavor/__init__.py b/trove/tests/unittests/flavor/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/trove/tests/unittests/flavor/test_flavor_views.py b/trove/tests/unittests/flavor/test_flavor_views.py new file mode 100644 index 0000000000..017b78a1eb --- /dev/null +++ b/trove/tests/unittests/flavor/test_flavor_views.py @@ -0,0 +1,68 @@ +# Copyright 2016 Tesora, Inc. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +from mock import Mock, patch +from trove.flavor.views import FlavorView +from trove.tests.unittests import trove_testtools + + +class FlavorViewsTest(trove_testtools.TestCase): + + def setUp(self): + super(FlavorViewsTest, self).setUp() + self.flavor = Mock() + self.flavor.id = 10 + self.flavor.str_id = '10' + self.flavor.name = 'test_flavor' + self.flavor.ram = 512 + self.links = 'my_links' + + def tearDown(self): + super(FlavorViewsTest, self).tearDown() + + def test_data(self): + data = [ + {'flavor_id': 10, + 'expected_id': 10, + 'expected_str_id': '10'}, + {'flavor_id': 'uuid-10', + 'expected_id': None, + 'expected_str_id': 'uuid-10'}, + {'flavor_id': '02', + 'expected_id': None, + 'expected_str_id': '02'}, + ] + + for datum in data: + flavor_id = datum['flavor_id'] + expected_id = datum['expected_id'] + expected_str_id = datum['expected_str_id'] + msg = "Testing flavor_id: %s - " % flavor_id + self.flavor.id = flavor_id + with patch.object(FlavorView, '_build_links', + Mock(return_value=(self.links))): + view = FlavorView(self.flavor) + result = view.data() + self.assertEqual(expected_id, result['flavor']['id'], + msg + 'invalid id') + self.assertEqual(expected_str_id, result['flavor']['str_id'], + msg + 'invalid str_id') + self.assertEqual(self.flavor.name, result['flavor']['name'], + msg + 'invalid name') + self.assertEqual(self.flavor.ram, result['flavor']['ram'], + msg + 'invalid ram') + self.assertEqual(self.links, result['flavor']['links'], + msg + 'invalid links')