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
This commit is contained in:
Peter Stachowski 2016-07-14 22:32:05 +00:00
parent ff49045744
commit afb29a398c
3 changed files with 73 additions and 3 deletions

View File

@ -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 = {

View File

View File

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