setUp/tearDown decorator for set/clear override
Problem with recursion shows up only in full runs of Nova for example. So split the code that sets up the decorator and add a method to cleanup the decorated set_override during teardown. Also add a decorator for clear_override similar to the one for set_override. Added more tests for all the above. Change-Id: Ib16af2e770e96d971aef7f5c5d48ffd781477cfe
This commit is contained in:
parent
36fc947b15
commit
74a0ec8b1c
@ -71,6 +71,7 @@ class ConfFixture(fixtures.Fixture):
|
||||
'_notifier_opts',
|
||||
'oslo_messaging_notifications')
|
||||
|
||||
def _setup_decorator(self):
|
||||
# Support older test cases that still use the set_override
|
||||
# with the old config key names
|
||||
def decorator_for_set_override(wrapped_function):
|
||||
@ -84,13 +85,40 @@ class ConfFixture(fixtures.Fixture):
|
||||
elif args[0] == 'notification_topics':
|
||||
args = ('topics', args[1], group)
|
||||
return wrapped_function(*args, **kwargs)
|
||||
_wrapper.wrapped = wrapped_function
|
||||
return _wrapper
|
||||
|
||||
self.conf.set_override = decorator_for_set_override(
|
||||
self.conf.set_override)
|
||||
def decorator_for_clear_override(wrapped_function):
|
||||
@wraps(wrapped_function)
|
||||
def _wrapper(*args, **kwargs):
|
||||
group = 'oslo_messaging_notifications'
|
||||
if args[0] == 'notification_driver':
|
||||
args = ('driver', group)
|
||||
elif args[0] == 'notification_transport_url':
|
||||
args = ('transport_url', group)
|
||||
elif args[0] == 'notification_topics':
|
||||
args = ('topics', group)
|
||||
return wrapped_function(*args, **kwargs)
|
||||
_wrapper.wrapped = wrapped_function
|
||||
return _wrapper
|
||||
|
||||
if not hasattr(self.conf.set_override, 'wrapped'):
|
||||
self.conf.set_override = decorator_for_set_override(
|
||||
self.conf.set_override)
|
||||
if not hasattr(self.conf.clear_override, 'wrapped'):
|
||||
self.conf.clear_override = decorator_for_clear_override(
|
||||
self.conf.clear_override)
|
||||
|
||||
def _teardown_decorator(self):
|
||||
if hasattr(self.conf.set_override, 'wrapped'):
|
||||
self.conf.set_override = self.conf.set_override.wrapped
|
||||
if hasattr(self.conf.clear_override, 'wrapped'):
|
||||
self.conf.clear_override = self.conf.clear_override.wrapped
|
||||
|
||||
def setUp(self):
|
||||
super(ConfFixture, self).setUp()
|
||||
self._setup_decorator()
|
||||
self.addCleanup(self._teardown_decorator)
|
||||
self.addCleanup(self.conf.reset)
|
||||
|
||||
@property
|
||||
|
@ -12,10 +12,48 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
|
||||
from oslo_messaging import conffixture
|
||||
from oslo_messaging.tests import utils as test_utils
|
||||
|
||||
|
||||
class TestConfFixture(test_utils.BaseTestCase):
|
||||
|
||||
def test_fixture_wraps_set_override(self):
|
||||
conf = self.messaging_conf.conf
|
||||
self.assertIsNotNone(conf.set_override.wrapped)
|
||||
self.messaging_conf._teardown_decorator()
|
||||
self.assertFalse(hasattr(conf.set_override, 'wrapped'))
|
||||
|
||||
def test_fixture_wraps_clear_override(self):
|
||||
conf = self.messaging_conf.conf
|
||||
self.assertIsNotNone(conf.clear_override.wrapped)
|
||||
self.messaging_conf._teardown_decorator()
|
||||
self.assertFalse(hasattr(conf.clear_override, 'wrapped'))
|
||||
|
||||
def test_fixture_setup_teardown_decorator(self):
|
||||
conf = cfg.ConfigOpts()
|
||||
self.assertFalse(hasattr(conf.set_override, 'wrapped'))
|
||||
self.assertFalse(hasattr(conf.clear_override, 'wrapped'))
|
||||
fixture = conffixture.ConfFixture(conf)
|
||||
self.assertFalse(hasattr(conf.set_override, 'wrapped'))
|
||||
self.assertFalse(hasattr(conf.clear_override, 'wrapped'))
|
||||
self.useFixture(fixture)
|
||||
self.assertTrue(hasattr(conf.set_override, 'wrapped'))
|
||||
self.assertTrue(hasattr(conf.clear_override, 'wrapped'))
|
||||
fixture._teardown_decorator()
|
||||
self.assertFalse(hasattr(conf.set_override, 'wrapped'))
|
||||
self.assertFalse(hasattr(conf.clear_override, 'wrapped'))
|
||||
|
||||
def test_fixture_properties(self):
|
||||
conf = self.messaging_conf.conf
|
||||
self.messaging_conf.transport_driver = 'fake'
|
||||
self.assertEqual('fake',
|
||||
self.messaging_conf.transport_driver)
|
||||
self.assertEqual('fake',
|
||||
conf.rpc_backend)
|
||||
|
||||
def test_old_notifications_config_override(self):
|
||||
conf = self.messaging_conf.conf
|
||||
conf.set_override(
|
||||
@ -31,3 +69,14 @@ class TestConfFixture(test_utils.BaseTestCase):
|
||||
conf.oslo_messaging_notifications.transport_url)
|
||||
self.assertEqual(['topic1'],
|
||||
conf.oslo_messaging_notifications.topics)
|
||||
|
||||
conf.clear_override("notification_driver")
|
||||
conf.clear_override("notification_transport_url")
|
||||
conf.clear_override("notification_topics")
|
||||
|
||||
self.assertEqual([],
|
||||
conf.oslo_messaging_notifications.driver)
|
||||
self.assertEqual(None,
|
||||
conf.oslo_messaging_notifications.transport_url)
|
||||
self.assertEqual(['notifications'],
|
||||
conf.oslo_messaging_notifications.topics)
|
Loading…
Reference in New Issue
Block a user