Change logic in find_resource() to identify upper-case/lower-case name.

Currently find_resource() searches resource with human_id before does
with name_attr.  With this logic, find_resource() cannot identify
upper/lower case. In find_resource(), human_id always matches and
name_attr never used in find_resource() because human_id made from
small letters of name_attr string (see bug/1318503).

To identify upper/lower case, name_attr should be used before human_id.
This fix moves name_attr to ahead of human_id with unit-test cases.

Change-Id: I9b821d7111c11a97be38f19de06172daf410022d
Closes-Bug: #1318503
This commit is contained in:
Tomofumi Hayashi 2015-02-05 02:32:58 +00:00
parent 0a60aae852
commit 0fd8816aa5
3 changed files with 33 additions and 16 deletions

View File

@ -41,6 +41,10 @@ class FakeManager(base.ManagerWithFind):
resources = [
FakeResource('1234', {'name': 'entity_one'}),
FakeResource('12345', {'name': 'UPPER'}),
FakeResource('123456', {'name': 'lower'}),
FakeResource('1234567', {'name': 'Mixed'}),
FakeResource('12345678', {'name': 'mixed'}),
FakeResource(UUID, {'name': 'entity_two'}),
FakeResource('5678', {'name': '9876'}),
FakeResource('01234', {'name': 'entity_three'})
@ -118,6 +122,22 @@ class FindResourceTestCase(test_utils.TestCase):
output = utils.find_resource(self.manager, 'entity_one')
self.assertEqual(output, self.manager.get('1234'))
def test_find_by_str_upper_name(self):
output = utils.find_resource(self.manager, 'UPPER')
self.assertEqual(output, self.manager.get('12345'))
def test_find_by_str_name(self):
output = utils.find_resource(self.manager, 'lower')
self.assertEqual(output, self.manager.get('123456'))
def test_find_by_str_mix_name(self):
output = utils.find_resource(self.manager, 'Mixed')
self.assertEqual(output, self.manager.get('1234567'))
def test_find_by_str_lower_name(self):
output = utils.find_resource(self.manager, 'mixed')
self.assertEqual(output, self.manager.get('12345678'))
def test_find_by_str_displayname(self):
display_manager = FakeDisplayManager(None)
output = utils.find_resource(display_manager, 'entity_three')

View File

@ -673,8 +673,7 @@ class ShellTest(utils.TestCase):
self.assert_called('GET', '/images/1', pos=0)
self.assert_called('GET', '/flavors/512 MB Server', pos=1)
self.assert_called('GET', '/flavors?is_public=None', pos=2)
self.assert_called('GET', '/flavors?is_public=None', pos=3)
self.assert_called('GET', '/flavors/2', pos=4)
self.assert_called('GET', '/flavors/2', pos=3)
self.assert_called(
'POST', '/servers',
{
@ -685,7 +684,7 @@ class ShellTest(utils.TestCase):
'min_count': 1,
'max_count': 3,
}
}, pos=5)
}, pos=4)
def test_flavor_list(self):
self.run_command('flavor-list')
@ -712,17 +711,15 @@ class ShellTest(utils.TestCase):
self.run_command(['flavor-show', '128 MB Server'])
self.assert_called('GET', '/flavors/128 MB Server', pos=0)
self.assert_called('GET', '/flavors?is_public=None', pos=1)
self.assert_called('GET', '/flavors?is_public=None', pos=2)
self.assert_called('GET', '/flavors/aa1', pos=3)
self.assert_called('GET', '/flavors/aa1/os-extra_specs', pos=4)
self.assert_called('GET', '/flavors/aa1', pos=2)
self.assert_called('GET', '/flavors/aa1/os-extra_specs', pos=3)
def test_flavor_show_by_name_priv(self):
self.run_command(['flavor-show', '512 MB Server'])
self.assert_called('GET', '/flavors/512 MB Server', pos=0)
self.assert_called('GET', '/flavors?is_public=None', pos=1)
self.assert_called('GET', '/flavors?is_public=None', pos=2)
self.assert_called('GET', '/flavors/2', pos=3)
self.assert_called('GET', '/flavors/2/os-extra_specs', pos=4)
self.assert_called('GET', '/flavors/2', pos=2)
self.assert_called('GET', '/flavors/2/os-extra_specs', pos=3)
def test_flavor_key_set(self):
self.run_command('flavor-key 1 set k1=v1')
@ -2163,7 +2160,7 @@ class ShellTest(utils.TestCase):
self.run_command('volume-delete Work Work2')
self.assert_called('DELETE',
'/volumes/15e59938-07d5-11e1-90e3-e3dffe0c5983',
pos=-5)
pos=-4)
self.assert_called('DELETE',
'/volumes/15e59938-07d5-11e1-90e3-ee32ba30feaa',
pos=-1)

View File

@ -208,18 +208,18 @@ def find_resource(manager, name_or_id, **find_args):
pass
try:
try:
return manager.find(human_id=name_or_id, **find_args)
except exceptions.NotFound:
pass
# finally try to find entity by name
try:
resource = getattr(manager, 'resource_class', None)
name_attr = resource.NAME_ATTR if resource else 'name'
kwargs = {name_attr: name_or_id}
kwargs.update(find_args)
return manager.find(**kwargs)
except exceptions.NotFound:
pass
# finally try to find entity by human_id
try:
return manager.find(human_id=name_or_id, **find_args)
except exceptions.NotFound:
msg = (_("No %(class)s with a name or ID of '%(name)s' exists.") %
{'class': manager.resource_class.__name__.lower(),