From 0fd8816aa51b69034929c56d5e053d87d93ce021 Mon Sep 17 00:00:00 2001 From: Tomofumi Hayashi Date: Thu, 5 Feb 2015 02:32:58 +0000 Subject: [PATCH] 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 --- novaclient/tests/unit/test_utils.py | 20 ++++++++++++++++++++ novaclient/tests/unit/v2/test_shell.py | 17 +++++++---------- novaclient/utils.py | 12 ++++++------ 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/novaclient/tests/unit/test_utils.py b/novaclient/tests/unit/test_utils.py index fe0452636..51b824f20 100644 --- a/novaclient/tests/unit/test_utils.py +++ b/novaclient/tests/unit/test_utils.py @@ -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') diff --git a/novaclient/tests/unit/v2/test_shell.py b/novaclient/tests/unit/v2/test_shell.py index 49e1581aa..bdf958659 100644 --- a/novaclient/tests/unit/v2/test_shell.py +++ b/novaclient/tests/unit/v2/test_shell.py @@ -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) diff --git a/novaclient/utils.py b/novaclient/utils.py index fbc5da604..289321c1b 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -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(),