From 65b0d36f3cd02d9068b0ded21898ca7ede0cc99c Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Tue, 24 Jun 2014 17:50:27 +0800 Subject: [PATCH] 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 --- novaclient/tests/test_utils.py | 20 ++++++++++++++++++++ novaclient/utils.py | 11 ++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/novaclient/tests/test_utils.py b/novaclient/tests/test_utils.py index 87c8c208f..3892820ed 100644 --- a/novaclient/tests/test_utils.py +++ b/novaclient/tests/test_utils.py @@ -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)) diff --git a/novaclient/utils.py b/novaclient/utils.py index def966992..aa3b5ab5d 100644 --- a/novaclient/utils.py +++ b/novaclient/utils.py @@ -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)