From 870bc4e14cd2f711fe22adb870a09eda818fc594 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Wed, 14 Jun 2017 05:12:54 -0700 Subject: [PATCH] Add sanity check to receives decorator Add simple assertion to prevent simple mistakes like I4a39f2e9d2f10639a209e7311fe44b94d369c5b1. Change-Id: I3a18da4949dcd97096817c5e157cac99bb37c04d --- neutron_lib/callbacks/registry.py | 2 ++ neutron_lib/tests/unit/callbacks/test_registry.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/neutron_lib/callbacks/registry.py b/neutron_lib/callbacks/registry.py index 49cb847..7ea0faa 100644 --- a/neutron_lib/callbacks/registry.py +++ b/neutron_lib/callbacks/registry.py @@ -68,6 +68,8 @@ def receives(resource, events): @has_registry_receivers decorator to setup the __new__ method to actually register the instance methods after initialization. """ + assert isinstance(events, (list, tuple, set)), 'events must be collection' + def decorator(f): for e in events: _REGISTERED_CLASS_METHODS[f].append((resource, e)) diff --git a/neutron_lib/tests/unit/callbacks/test_registry.py b/neutron_lib/tests/unit/callbacks/test_registry.py index 6d78274..e7668f0 100644 --- a/neutron_lib/tests/unit/callbacks/test_registry.py +++ b/neutron_lib/tests/unit/callbacks/test_registry.py @@ -13,6 +13,7 @@ # under the License. import mock +import testtools from oslotest import base @@ -99,6 +100,10 @@ class CallBacksManagerTestCase(base.BaseTestCase): def test_object_new_not_broken(self): CallbackClassWithParameters('dummy') + def test_no_strings_in_events_arg(self): + with testtools.ExpectedException(AssertionError): + registry.receives(resources.PORT, events.AFTER_CREATE) + class TestCallbackRegistryDispatching(base.BaseTestCase):