Fix using a variable which is not defined

There is a variable which is not defined but used, we should define it
before using. Add a test for get_resource_manager_extra_kwargs_hook
function, and change the exception to a more detail instead broad
'Exception'.

Change-Id: I7a798dad55a65f06ebbe175925a00028940bb168
This commit is contained in:
huangtianhua 2014-06-24 17:50:27 +08:00
parent 1a7f84cb46
commit 65b0d36f3c
2 changed files with 26 additions and 5 deletions

View File

@ -301,3 +301,23 @@ class ValidationsTestCase(test_utils.TestCase):
self.fail("Invalid key passed validation: %s" % key)
except exceptions.CommandError as ce:
self.assertTrue(key in str(ce))
class ResourceManagerExtraKwargsHookTestCase(test_utils.TestCase):
def test_get_resource_manager_extra_kwargs_hook_test(self):
do_foo = mock.MagicMock()
def hook1(args):
return {'kwarg1': 'v_hook1'}
def hook2(args):
return {'kwarg1': 'v_hook2'}
do_foo.resource_manager_kwargs_hooks = [hook1, hook2]
args = {}
exc = self.assertRaises(exceptions.NoUniqueMatch,
utils.get_resource_manager_extra_kwargs,
do_foo,
args)
except_error = ("Hook 'hook2' is attempting to redefine "
"attributes")
self.assertIn(except_error, six.text_type(exc))

View File

@ -57,13 +57,14 @@ def get_resource_manager_extra_kwargs(f, args, allow_conflicts=False):
extra_kwargs = {}
for hook in hooks:
hook_kwargs = hook(args)
hook_name = hook.__name__
conflicting_keys = set(hook_kwargs.keys()) & set(extra_kwargs.keys())
if conflicting_keys and not allow_conflicts:
raise Exception(_("Hook '%(hook_name)s' is attempting to redefine"
" attributes '%(conflicting_keys)s'") %
{'hook_name': hook_name,
'conflicting_keys': conflicting_keys})
msg = (_("Hook '%(hook_name)s' is attempting to redefine "
"attributes '%(conflicting_keys)s'") %
{'hook_name': hook_name,
'conflicting_keys': conflicting_keys})
raise exceptions.NoUniqueMatch(msg)
extra_kwargs.update(hook_kwargs)