From c06bbe99734f2fea35cfb4bdd854814c9119b617 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 12:30:55 -0700 Subject: Added monkey patching notification code function w --- bin/nova-api | 2 +- bin/nova-compute | 1 + bin/nova-network | 1 + bin/nova-objectstore | 1 + bin/nova-scheduler | 1 + nova/flags.py | 8 ++++++++ nova/notifier/api.py | 18 ++++++++++++++++++ nova/utils.py | 24 +++++++++++++++++++++++- tools/pip-requires | 1 + 9 files changed, 55 insertions(+), 2 deletions(-) diff --git a/bin/nova-api b/bin/nova-api index fe8e83366..4edf77c7f 100755 --- a/bin/nova-api +++ b/bin/nova-api @@ -45,7 +45,7 @@ FLAGS = flags.FLAGS def main(): """Launch EC2 and OSAPI services.""" nova.utils.Bootstrapper.bootstrap_binary(sys.argv) - + nova.utils.monkey_patch() launcher = nova.service.Launcher() for api in FLAGS.enabled_apis: diff --git a/bin/nova-compute b/bin/nova-compute index cd7c78def..1de53c075 100755 --- a/bin/nova-compute +++ b/bin/nova-compute @@ -45,5 +45,6 @@ if __name__ == '__main__': utils.default_flagfile() flags.FLAGS(sys.argv) logging.setup() + utils.monkey_patch() service.serve() service.wait() diff --git a/bin/nova-network b/bin/nova-network index 101761ef7..5b66b8f2b 100755 --- a/bin/nova-network +++ b/bin/nova-network @@ -45,5 +45,6 @@ if __name__ == '__main__': utils.default_flagfile() flags.FLAGS(sys.argv) logging.setup() + utils.monkey_patch() service.serve() service.wait() diff --git a/bin/nova-objectstore b/bin/nova-objectstore index 4d5aec445..c2860f534 100755 --- a/bin/nova-objectstore +++ b/bin/nova-objectstore @@ -49,6 +49,7 @@ if __name__ == '__main__': utils.default_flagfile() FLAGS(sys.argv) logging.setup() + utils.monkey_patch() router = s3server.S3Application(FLAGS.buckets_path) server = wsgi.Server("S3 Objectstore", router, diff --git a/bin/nova-scheduler b/bin/nova-scheduler index 0c205a80f..ae3db51bc 100755 --- a/bin/nova-scheduler +++ b/bin/nova-scheduler @@ -45,5 +45,6 @@ if __name__ == '__main__': utils.default_flagfile() flags.FLAGS(sys.argv) logging.setup() + utils.monkey_patch() service.serve() service.wait() diff --git a/nova/flags.py b/nova/flags.py index 48d5e8168..09c30c7c0 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -402,3 +402,11 @@ DEFINE_bool('resume_guests_state_on_host_boot', False, DEFINE_string('root_helper', 'sudo', 'Command prefix to use for running commands as root') + +DEFINE_bool('monkey_patch', False, + 'Whether to monkey patch') + +DEFINE_list('monkey_patch_modules', + ['nova.api.ec2.cloud:nova.notifier.api.notify_decorator', + 'nova.compute.api:nova.notifier.api.notify_decorator'], + 'Module list representng monkey patched module and decorator') diff --git a/nova/notifier/api.py b/nova/notifier/api.py index e18f3e280..ca0ff60a4 100644 --- a/nova/notifier/api.py +++ b/nova/notifier/api.py @@ -39,6 +39,24 @@ class BadPriorityException(Exception): pass +def notify_decorator(name, fn): + def wrapped_func(*args, **kwarg): + body = {} + body['args'] = [] + body['kwarg'] = {} + for arg in args: + body['args'].append(arg) + for key in kwarg: + body['kwarg'][key] = kwarg[key] + LOG.debug("Notify Decorator: %s %r" % (name, body)) + notify(FLAGS.host, + name, + DEBUG, + body) + fn(*args, **kwarg) + return wrapped_func + + def publisher_id(service, host=None): if not host: host = FLAGS.host diff --git a/nova/utils.py b/nova/utils.py index 7276b6bd5..8024a3517 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -35,6 +35,7 @@ import sys import time import types import uuid +import pyclbr from xml.sax import saxutils from eventlet import event @@ -634,7 +635,7 @@ def synchronized(name, external=False): Different methods can share the same lock: @synchronized('mylock') - def foo(self, *args): + Gdef foo(self, *args): ... @synchronized('mylock') @@ -873,3 +874,24 @@ class Bootstrapper(object): for key in FLAGS: value = FLAGS.get(key, None) logging.audit(_("%(key)s : %(value)s" % locals())) + + +def monkey_patch(): + if not FLAGS.monkey_patch: + return + for module_and_decorator in FLAGS.monkey_patch_modules: + module, decorator_name = module_and_decorator.split(':') + decorator = import_class(decorator_name) + __import__(module) + module_data = pyclbr.readmodule_ex(module) + for key in module_data.keys(): + if isinstance(module_data[key], pyclbr.Class): + clz = import_class("%s.%s" % (module, key)) + for method, func in inspect.getmembers(clz, inspect.ismethod): + setattr(clz, method,\ + decorator("%s.%s" % (module, key), func)) + if isinstance(module_data[key], pyclbr.Function): + func = import_class("%s.%s" % (module, key)) + setattr(sys.modules[module], key,\ + setattr(sys.modules[module], key, \ + decorator("%s.%s" % (module, key), func))) diff --git a/tools/pip-requires b/tools/pip-requires index 60b502ffd..7eb220a95 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -34,3 +34,4 @@ coverage nosexcover GitPython paramiko +pyclbr -- cgit From 46ba1b111bfff27edcb963bc43869f26b02d569a Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 13:31:54 -0700 Subject: Fixed mistake on mergew --- bin/nova-scheduler | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/nova-scheduler b/bin/nova-scheduler index ae3db51bc..c1033a304 100755 --- a/bin/nova-scheduler +++ b/bin/nova-scheduler @@ -46,5 +46,6 @@ if __name__ == '__main__': flags.FLAGS(sys.argv) logging.setup() utils.monkey_patch() - service.serve() + server = service.Service.create(binary='nova-scheduler') + service.serve(server) service.wait() -- cgit From 75eb485c3a0e53380b9247d45e2a66159928dcd2 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 14:04:58 -0700 Subject: Fixed NoneType returned bugw --- nova/notifier/api.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nova/notifier/api.py b/nova/notifier/api.py index ca0ff60a4..99b8b0102 100644 --- a/nova/notifier/api.py +++ b/nova/notifier/api.py @@ -48,12 +48,11 @@ def notify_decorator(name, fn): body['args'].append(arg) for key in kwarg: body['kwarg'][key] = kwarg[key] - LOG.debug("Notify Decorator: %s %r" % (name, body)) notify(FLAGS.host, name, DEBUG, body) - fn(*args, **kwarg) + return fn(*args, **kwarg) return wrapped_func -- cgit From 27723b95f7b3a64226ebe431e17cbf681b40303b Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 14:13:39 -0700 Subject: Fixed typo --- nova/flags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/flags.py b/nova/flags.py index 09c30c7c0..4e6e3ad40 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -409,4 +409,4 @@ DEFINE_bool('monkey_patch', False, DEFINE_list('monkey_patch_modules', ['nova.api.ec2.cloud:nova.notifier.api.notify_decorator', 'nova.compute.api:nova.notifier.api.notify_decorator'], - 'Module list representng monkey patched module and decorator') + 'Module list representing monkey patched module and decorator') -- cgit From e10aa40bd6c2f96b2f5bba8b38b9605f019328e9 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 14:22:53 -0700 Subject: Fixed typo --- nova/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/utils.py b/nova/utils.py index 171035068..d7e14b1b0 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -636,7 +636,7 @@ def synchronized(name, external=False): Different methods can share the same lock: @synchronized('mylock') - Gdef foo(self, *args): + def foo(self, *args): ... @synchronized('mylock') -- cgit From 5a288485215a13f3892ae17a46b9644ed84fc089 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Mon, 22 Aug 2011 14:24:37 -0700 Subject: Added Test Code, doc string, and fixed pip-requiresw --- nova/flags.py | 5 +++-- nova/notifier/api.py | 8 ++++++++ nova/tests/test_notifier.py | 22 ++++++++++++++++++++++ nova/tests/test_utils.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ nova/utils.py | 26 +++++++++++++++++++++++--- tools/pip-requires | 1 - 6 files changed, 100 insertions(+), 6 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index 4e6e3ad40..e9bf6e334 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -404,9 +404,10 @@ DEFINE_string('root_helper', 'sudo', 'Command prefix to use for running commands as root') DEFINE_bool('monkey_patch', False, - 'Whether to monkey patch') + 'Whether to log monkey patching') DEFINE_list('monkey_patch_modules', ['nova.api.ec2.cloud:nova.notifier.api.notify_decorator', 'nova.compute.api:nova.notifier.api.notify_decorator'], - 'Module list representing monkey patched module and decorator') + 'Module list representing monkey\ + patched module and decorator') diff --git a/nova/notifier/api.py b/nova/notifier/api.py index 99b8b0102..a98f17dbe 100644 --- a/nova/notifier/api.py +++ b/nova/notifier/api.py @@ -40,6 +40,14 @@ class BadPriorityException(Exception): def notify_decorator(name, fn): + """ decorator for notify which is used from utils.monkey_patch() + Parameters: + + name - name of the function + function - object of the function + + + """ def wrapped_func(*args, **kwarg): body = {} body['args'] = [] diff --git a/nova/tests/test_notifier.py b/nova/tests/test_notifier.py index 64b799a2c..ab5dfb692 100644 --- a/nova/tests/test_notifier.py +++ b/nova/tests/test_notifier.py @@ -134,3 +134,25 @@ class NotifierTestCase(test.TestCase): self.assertEqual(msg['event_type'], 'error_notification') self.assertEqual(msg['priority'], 'ERROR') self.assertEqual(msg['payload']['error'], 'foo') + + def test_send_notification_by_decorator(self): + self.notify_called = False + + def example_api(arg1, arg2): + return arg1 + arg2 + + example_api =\ + nova.notifier.api.notify_decorator( + 'example_api', + example_api) + + def mock_notify(cls, *args): + self.notify_called = True + + self.stubs.Set(nova.notifier.no_op_notifier, 'notify', + mock_notify) + + class Mock(object): + pass + self.assertEqual(3, example_api(1, 2)) + self.assertEqual(self.notify_called, True) diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index ec5098a37..317ec46d0 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -18,6 +18,7 @@ import datetime import os import tempfile +import nova from nova import exception from nova import test from nova import utils @@ -384,3 +385,46 @@ class ToPrimitiveTestCase(test.TestCase): def test_typeerror(self): x = bytearray # Class, not instance self.assertEquals(utils.to_primitive(x), u"") + + +class MonkeyPatchTestCase(test.TestCase): + """Unit test for utils.monkey_patch().""" + def setUp(self): + super(MonkeyPatchTestCase, self).setUp() + self.flags( + monkey_patch=True, + monkey_patch_modules=['nova.tests.example.example_a' + ':' + + 'nova.tests.example.example_decorator']) + + def test_monkey_patch(self): + utils.monkey_patch() + nova.tests.example.CALLED_FUNCTION = [] + from nova.tests.example import example_a, example_b + + self.assertEqual('Example function', example_a.example_function_a()) + exampleA = example_a.ExampleClassA() + exampleA.example_method() + ret_a = exampleA.example_method_add(3, 5) + self.assertEqual(ret_a, 8) + + self.assertEqual('Example function', example_b.example_function_b()) + exampleB = example_b.ExampleClassB() + exampleB.example_method() + ret_b = exampleB.example_method_add(3, 5) + + self.assertEqual(ret_b, 8) + package_a = 'nova.tests.example.example_a.' + self.assertTrue(package_a + 'example_function_a' + in nova.tests.example.CALLED_FUNCTION) + + self.assertTrue(package_a + 'ExampleClassA.example_method' + in nova.tests.example.CALLED_FUNCTION) + self.assertTrue(package_a + 'ExampleClassA.example_method_add' + in nova.tests.example.CALLED_FUNCTION) + package_b = 'nova.tests.example.example_b.' + self.assertFalse(package_b + 'example_function_b' + in nova.tests.example.CALLED_FUNCTION) + self.assertFalse(package_b + 'ExampleClassB.example_method' + in nova.tests.example.CALLED_FUNCTION) + self.assertFalse(package_b + 'ExampleClassB.example_method_add' + in nova.tests.example.CALLED_FUNCTION) diff --git a/nova/utils.py b/nova/utils.py index d7e14b1b0..be2ba68f9 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -842,21 +842,41 @@ def bool_from_str(val): def monkey_patch(): + """ If the Flags.monkey_patch set as True, + this functuion patches a decorator + for all functions in specified modules. + You can set decorators for each modules + using FLAGS.monkey_patch_modules. + The format is "Module path:Decorator function". + Example: 'nova.api.ec2.cloud:nova.notifier.api.notify_decorator' + + Parameters of the decorator is as follows. + (See nova.notifier.api.notify_decorator) + + name - name of the function + function - object of the function + """ + + # If FLAGS.monkey_patch is not True, this function do nothing. if not FLAGS.monkey_patch: return + # Get list of moudles and decorators for module_and_decorator in FLAGS.monkey_patch_modules: module, decorator_name = module_and_decorator.split(':') + # import decorator function decorator = import_class(decorator_name) __import__(module) + # Retrive module information using pyclbr module_data = pyclbr.readmodule_ex(module) for key in module_data.keys(): + # set the decorator for the class methods if isinstance(module_data[key], pyclbr.Class): clz = import_class("%s.%s" % (module, key)) for method, func in inspect.getmembers(clz, inspect.ismethod): setattr(clz, method,\ - decorator("%s.%s" % (module, key), func)) + decorator("%s.%s.%s" % (module, key, method), func)) + # set the decorator for the function if isinstance(module_data[key], pyclbr.Function): func = import_class("%s.%s" % (module, key)) setattr(sys.modules[module], key,\ - setattr(sys.modules[module], key, \ - decorator("%s.%s" % (module, key), func))) + decorator("%s.%s" % (module, key), func)) diff --git a/tools/pip-requires b/tools/pip-requires index 7eb220a95..60b502ffd 100644 --- a/tools/pip-requires +++ b/tools/pip-requires @@ -34,4 +34,3 @@ coverage nosexcover GitPython paramiko -pyclbr -- cgit From 5ae44219fd82d843cc5e715c318d9e80ab20b1a2 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 08:07:25 -0700 Subject: Fixed typo and docstring and example class name --- nova/notifier/api.py | 7 +++--- nova/tests/example/__init__.py | 34 ---------------------------- nova/tests/example/example_a.py | 32 -------------------------- nova/tests/example/example_b.py | 32 -------------------------- nova/tests/monkey_patch_example/__init__.py | 34 ++++++++++++++++++++++++++++ nova/tests/monkey_patch_example/example_a.py | 32 ++++++++++++++++++++++++++ nova/tests/monkey_patch_example/example_b.py | 32 ++++++++++++++++++++++++++ nova/tests/test_utils.py | 25 ++++++++++---------- nova/utils.py | 4 ++-- 9 files changed, 116 insertions(+), 116 deletions(-) delete mode 100644 nova/tests/example/__init__.py delete mode 100644 nova/tests/example/example_a.py delete mode 100644 nova/tests/example/example_b.py create mode 100644 nova/tests/monkey_patch_example/__init__.py create mode 100644 nova/tests/monkey_patch_example/example_a.py create mode 100644 nova/tests/monkey_patch_example/example_b.py diff --git a/nova/notifier/api.py b/nova/notifier/api.py index a98f17dbe..f5cf95d2a 100644 --- a/nova/notifier/api.py +++ b/nova/notifier/api.py @@ -41,11 +41,10 @@ class BadPriorityException(Exception): def notify_decorator(name, fn): """ decorator for notify which is used from utils.monkey_patch() - Parameters: - - name - name of the function - function - object of the function + :param name: name of the function + :param function: - object of the function + :returns: function -- decorated function """ def wrapped_func(*args, **kwarg): diff --git a/nova/tests/example/__init__.py b/nova/tests/example/__init__.py deleted file mode 100644 index 1cfdf8a7e..000000000 --- a/nova/tests/example/__init__.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# 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. -"""Example Module for testing utils.monkey_patch() -""" - - -CALLED_FUNCTION = [] - - -def example_decorator(name, function): - """ decorator for notify which is used from utils.monkey_patch() - Parameters: - - name - name of the function - function - object of the function - - - """ - def wrapped_func(*args, **kwarg): - CALLED_FUNCTION.append(name) - return function(*args, **kwarg) - return wrapped_func diff --git a/nova/tests/example/example_a.py b/nova/tests/example/example_a.py deleted file mode 100644 index 91bf048e4..000000000 --- a/nova/tests/example/example_a.py +++ /dev/null @@ -1,32 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# 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. - -"""Example Module A for testing utils.monkey_patch() -""" - - -def example_function_a(): - return 'Example function' - - -class ExampleClassA(): - def example_method(self): - return 'Example method' - - def example_method_add(self, arg1, arg2): - return arg1 + arg2 diff --git a/nova/tests/example/example_b.py b/nova/tests/example/example_b.py deleted file mode 100644 index edd267c4f..000000000 --- a/nova/tests/example/example_b.py +++ /dev/null @@ -1,32 +0,0 @@ -# vim: tabstop=4 shiftwidth=4 softtabstop=4 - -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. -# 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. - -"""Example Module B for testing utils.monkey_patch() -""" - - -def example_function_b(): - return 'Example function' - - -class ExampleClassB(): - def example_method(self): - return 'Example method' - - def example_method_add(self, arg1, arg2): - return arg1 + arg2 diff --git a/nova/tests/monkey_patch_example/__init__.py b/nova/tests/monkey_patch_example/__init__.py new file mode 100644 index 000000000..1cfdf8a7e --- /dev/null +++ b/nova/tests/monkey_patch_example/__init__.py @@ -0,0 +1,34 @@ +# Copyright 2011 OpenStack LLC. +# 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. +"""Example Module for testing utils.monkey_patch() +""" + + +CALLED_FUNCTION = [] + + +def example_decorator(name, function): + """ decorator for notify which is used from utils.monkey_patch() + Parameters: + + name - name of the function + function - object of the function + + + """ + def wrapped_func(*args, **kwarg): + CALLED_FUNCTION.append(name) + return function(*args, **kwarg) + return wrapped_func diff --git a/nova/tests/monkey_patch_example/example_a.py b/nova/tests/monkey_patch_example/example_a.py new file mode 100644 index 000000000..91bf048e4 --- /dev/null +++ b/nova/tests/monkey_patch_example/example_a.py @@ -0,0 +1,32 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +"""Example Module A for testing utils.monkey_patch() +""" + + +def example_function_a(): + return 'Example function' + + +class ExampleClassA(): + def example_method(self): + return 'Example method' + + def example_method_add(self, arg1, arg2): + return arg1 + arg2 diff --git a/nova/tests/monkey_patch_example/example_b.py b/nova/tests/monkey_patch_example/example_b.py new file mode 100644 index 000000000..edd267c4f --- /dev/null +++ b/nova/tests/monkey_patch_example/example_b.py @@ -0,0 +1,32 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# 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. + +"""Example Module B for testing utils.monkey_patch() +""" + + +def example_function_b(): + return 'Example function' + + +class ExampleClassB(): + def example_method(self): + return 'Example method' + + def example_method_add(self, arg1, arg2): + return arg1 + arg2 diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index f80ffb179..1ba794a1a 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -401,15 +401,16 @@ class MonkeyPatchTestCase(test.TestCase): """Unit test for utils.monkey_patch().""" def setUp(self): super(MonkeyPatchTestCase, self).setUp() + self.example_package = 'nova.tests.monkey_patch_example.' self.flags( monkey_patch=True, - monkey_patch_modules=['nova.tests.example.example_a' + ':' - + 'nova.tests.example.example_decorator']) + monkey_patch_modules=[self.example_package + 'example_a' + ':' + + self.example_package + 'example_decorator']) def test_monkey_patch(self): utils.monkey_patch() - nova.tests.example.CALLED_FUNCTION = [] - from nova.tests.example import example_a, example_b + nova.tests.monkey_patch_example.CALLED_FUNCTION = [] + from nova.tests.monkey_patch_example import example_a, example_b self.assertEqual('Example function', example_a.example_function_a()) exampleA = example_a.ExampleClassA() @@ -423,18 +424,18 @@ class MonkeyPatchTestCase(test.TestCase): ret_b = exampleB.example_method_add(3, 5) self.assertEqual(ret_b, 8) - package_a = 'nova.tests.example.example_a.' + package_a = self.example_package + 'example_a.' self.assertTrue(package_a + 'example_function_a' - in nova.tests.example.CALLED_FUNCTION) + in nova.tests.monkey_patch_example.CALLED_FUNCTION) self.assertTrue(package_a + 'ExampleClassA.example_method' - in nova.tests.example.CALLED_FUNCTION) + in nova.tests.monkey_patch_example.CALLED_FUNCTION) self.assertTrue(package_a + 'ExampleClassA.example_method_add' - in nova.tests.example.CALLED_FUNCTION) - package_b = 'nova.tests.example.example_b.' + in nova.tests.monkey_patch_example.CALLED_FUNCTION) + package_b = self.example_package + 'example_b.' self.assertFalse(package_b + 'example_function_b' - in nova.tests.example.CALLED_FUNCTION) + in nova.tests.monkey_patch_example.CALLED_FUNCTION) self.assertFalse(package_b + 'ExampleClassB.example_method' - in nova.tests.example.CALLED_FUNCTION) + in nova.tests.monkey_patch_example.CALLED_FUNCTION) self.assertFalse(package_b + 'ExampleClassB.example_method_add' - in nova.tests.example.CALLED_FUNCTION) + in nova.tests.monkey_patch_example.CALLED_FUNCTION) diff --git a/nova/utils.py b/nova/utils.py index 44a0d4398..edf67384d 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -866,13 +866,13 @@ def monkey_patch(): # If FLAGS.monkey_patch is not True, this function do nothing. if not FLAGS.monkey_patch: return - # Get list of moudles and decorators + # Get list of modules and decorators for module_and_decorator in FLAGS.monkey_patch_modules: module, decorator_name = module_and_decorator.split(':') # import decorator function decorator = import_class(decorator_name) __import__(module) - # Retrive module information using pyclbr + # Retrieve module information using pyclbr module_data = pyclbr.readmodule_ex(module) for key in module_data.keys(): # set the decorator for the class methods -- cgit From f380b65cdce439d440b68b0f4a65be45d13ce453 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 08:51:44 -0700 Subject: Removed blank line --- nova/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nova/utils.py b/nova/utils.py index 5c7d52c70..21e6221b2 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -851,7 +851,6 @@ def is_valid_ipv4(address): """valid the address strictly as per format xxx.xxx.xxx.xxx. where xxx is a value between 0 and 255. """ - parts = address.split(".") if len(parts) != 4: return False -- cgit From 76f02277a3677d40a13a8b05a12f9d83053808c3 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 09:46:49 -0700 Subject: Fixed some docstring Added default publisher_id flagw --- nova/notifier/api.py | 7 +++++-- nova/tests/monkey_patch_example/__init__.py | 13 ++++++------- nova/tests/monkey_patch_example/example_a.py | 7 ++----- nova/tests/monkey_patch_example/example_b.py | 6 ++---- nova/tests/test_notifier.py | 3 +-- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/nova/notifier/api.py b/nova/notifier/api.py index f5cf95d2a..6ef4a050e 100644 --- a/nova/notifier/api.py +++ b/nova/notifier/api.py @@ -25,6 +25,9 @@ FLAGS = flags.FLAGS flags.DEFINE_string('default_notification_level', 'INFO', 'Default notification level for outgoing notifications') +flags.DEFINE_string('default_publisher_id', FLAGS.host, + 'Default publisher_id for outgoing notifications') + WARN = 'WARN' INFO = 'INFO' @@ -55,9 +58,9 @@ def notify_decorator(name, fn): body['args'].append(arg) for key in kwarg: body['kwarg'][key] = kwarg[key] - notify(FLAGS.host, + notify(FLAGS.default_publisher_id, name, - DEBUG, + FLAGS.default_notification_level, body) return fn(*args, **kwarg) return wrapped_func diff --git a/nova/tests/monkey_patch_example/__init__.py b/nova/tests/monkey_patch_example/__init__.py index 1cfdf8a7e..25cf9ccfe 100644 --- a/nova/tests/monkey_patch_example/__init__.py +++ b/nova/tests/monkey_patch_example/__init__.py @@ -1,3 +1,5 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + # Copyright 2011 OpenStack LLC. # All Rights Reserved. # @@ -12,8 +14,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. -"""Example Module for testing utils.monkey_patch() -""" +"""Example Module for testing utils.monkey_patch().""" CALLED_FUNCTION = [] @@ -21,12 +22,10 @@ CALLED_FUNCTION = [] def example_decorator(name, function): """ decorator for notify which is used from utils.monkey_patch() - Parameters: - - name - name of the function - function - object of the function - + :param name: name of the function + :param function: - object of the function + :returns: function -- decorated function """ def wrapped_func(*args, **kwarg): CALLED_FUNCTION.append(name) diff --git a/nova/tests/monkey_patch_example/example_a.py b/nova/tests/monkey_patch_example/example_a.py index 91bf048e4..21e79bcb0 100644 --- a/nova/tests/monkey_patch_example/example_a.py +++ b/nova/tests/monkey_patch_example/example_a.py @@ -1,7 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. +# Copyright 2011 OpenStack LLC. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -15,9 +14,7 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. - -"""Example Module A for testing utils.monkey_patch() -""" +"""Example Module A for testing utils.monkey_patch().""" def example_function_a(): diff --git a/nova/tests/monkey_patch_example/example_b.py b/nova/tests/monkey_patch_example/example_b.py index edd267c4f..9d8f6d339 100644 --- a/nova/tests/monkey_patch_example/example_b.py +++ b/nova/tests/monkey_patch_example/example_b.py @@ -1,7 +1,6 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2010 United States Government as represented by the -# Administrator of the National Aeronautics and Space Administration. +# Copyright 2011 OpenStack LLC. # All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); you may @@ -16,8 +15,7 @@ # License for the specific language governing permissions and limitations # under the License. -"""Example Module B for testing utils.monkey_patch() -""" +"""Example Module B for testing utils.monkey_patch().""" def example_function_b(): diff --git a/nova/tests/test_notifier.py b/nova/tests/test_notifier.py index ab5dfb692..7de3a4a99 100644 --- a/nova/tests/test_notifier.py +++ b/nova/tests/test_notifier.py @@ -141,8 +141,7 @@ class NotifierTestCase(test.TestCase): def example_api(arg1, arg2): return arg1 + arg2 - example_api =\ - nova.notifier.api.notify_decorator( + example_api = nova.notifier.api.notify_decorator( 'example_api', example_api) -- cgit From 6bbef7627200f6c6ef27b5ae5c9b114e8e6d0f52 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 12:06:25 -0700 Subject: Fixed doc string --- nova/flags.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nova/flags.py b/nova/flags.py index 05d0db8af..95000df1b 100644 --- a/nova/flags.py +++ b/nova/flags.py @@ -409,7 +409,7 @@ DEFINE_bool('monkey_patch', False, 'Whether to log monkey patching') DEFINE_list('monkey_patch_modules', - ['nova.api.ec2.cloud:nova.notifier.api.notify_decorator', - 'nova.compute.api:nova.notifier.api.notify_decorator'], - 'Module list representing monkey\ - patched module and decorator') + ['nova.api.ec2.cloud:nova.notifier.api.notify_decorator', + 'nova.compute.api:nova.notifier.api.notify_decorator'], + 'Module list representing monkey ' + 'patched module and decorator') -- cgit