diff options
| author | Mark McLoughlin <markmc@redhat.com> | 2013-02-03 21:17:02 +0000 |
|---|---|---|
| committer | Mark McLoughlin <markmc@redhat.com> | 2013-02-18 16:28:21 +0000 |
| commit | bcba9e708e2067caca316a8499aa659f9802eeec (patch) | |
| tree | a7e69a8dd15fb5a3f60aed1688db4c22468abc32 /tests | |
| parent | 538721d3fdab613ed3d3ab33df123f90da173e93 (diff) | |
| download | oslo-bcba9e708e2067caca316a8499aa659f9802eeec.tar.gz oslo-bcba9e708e2067caca316a8499aa659f9802eeec.tar.xz oslo-bcba9e708e2067caca316a8499aa659f9802eeec.zip | |
Add ConfigFilter wrapper class
Implements blueprint cfg-filter-view
At the moment, if a module requires a configuration option from another
module, we do:
CONF.import_opt('source.module', 'option_name')
but, in fact, all options from the imported module are available for
use.
The new ConfigFilter class makes it possible to enforce which options
are available within a module e.g. with
CONF = cfgfilter.ConfigFilter(cfg.CONF)
CONF.import_opt('foo', 'source.module')
CONF.register_opt(StrOpt('bar'))
then the foo and bar options would be the only options available via
this CONF object while still being available via the global cfg.CONF
object.
Change-Id: Ie3aa2cd090a626da8afd27ecb78853cbf279bc8b
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/testmods/baar_baa_opt.py | 21 | ||||
| -rw-r--r-- | tests/testmods/fbar_foo_opt.py | 21 | ||||
| -rw-r--r-- | tests/testmods/fblaa_opt.py | 21 | ||||
| -rw-r--r-- | tests/unit/test_cfgfilter.py | 119 |
4 files changed, 182 insertions, 0 deletions
diff --git a/tests/testmods/baar_baa_opt.py b/tests/testmods/baar_baa_opt.py new file mode 100644 index 0000000..bd0cef1 --- /dev/null +++ b/tests/testmods/baar_baa_opt.py @@ -0,0 +1,21 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 Red Hat, Inc. +# +# 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. + +from oslo.config import cfg + +CONF = cfg.CONF + +CONF.register_opt(cfg.StrOpt('baa'), group='baar') diff --git a/tests/testmods/fbar_foo_opt.py b/tests/testmods/fbar_foo_opt.py new file mode 100644 index 0000000..fb206c5 --- /dev/null +++ b/tests/testmods/fbar_foo_opt.py @@ -0,0 +1,21 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 Red Hat, Inc. +# +# 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. + +from oslo.config import cfg + +CONF = cfg.CONF + +CONF.register_opt(cfg.StrOpt('foo'), group='bbar') diff --git a/tests/testmods/fblaa_opt.py b/tests/testmods/fblaa_opt.py new file mode 100644 index 0000000..0c886af --- /dev/null +++ b/tests/testmods/fblaa_opt.py @@ -0,0 +1,21 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 Red Hat, Inc. +# +# 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. + +from oslo.config import cfg + +CONF = cfg.CONF + +CONF.register_opt(cfg.StrOpt('fblaa')) diff --git a/tests/unit/test_cfgfilter.py b/tests/unit/test_cfgfilter.py new file mode 100644 index 0000000..e78acc8 --- /dev/null +++ b/tests/unit/test_cfgfilter.py @@ -0,0 +1,119 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2013 Red Hat, Inc. +# +# 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. + +from oslo.config.cfg import * + +from openstack.common.cfgfilter import * +from tests import utils + + +class ConfigFilterTestCase(utils.BaseTestCase): + + def setUp(self): + super(ConfigFilterTestCase, self).setUp() + self.conf = ConfigOpts() + self.fconf = ConfigFilter(self.conf) + + def test_register_opt_default(self): + self.fconf.register_opt(StrOpt('foo', default='bar')) + + self.assertEquals(self.fconf.foo, 'bar') + self.assertEquals(self.fconf['foo'], 'bar') + self.assertTrue('foo' in self.fconf) + self.assertEquals(list(self.fconf), ['foo']) + self.assertEquals(len(self.fconf), 1) + + def test_register_opt_none_default(self): + self.fconf.register_opt(StrOpt('foo')) + + self.assertTrue(self.fconf.foo is None) + self.assertTrue(self.fconf['foo'] is None) + self.assertTrue('foo' in self.fconf) + self.assertEquals(list(self.fconf), ['foo']) + self.assertEquals(len(self.fconf), 1) + + def test_register_grouped_opt_default(self): + self.fconf.register_opt(StrOpt('foo', default='bar'), group='blaa') + + self.assertEquals(self.fconf.blaa.foo, 'bar') + self.assertEquals(self.fconf['blaa']['foo'], 'bar') + self.assertTrue('blaa' in self.fconf) + self.assertTrue('foo' in self.fconf.blaa) + self.assertEquals(list(self.fconf), ['blaa']) + self.assertEquals(list(self.fconf.blaa), ['foo']) + self.assertEquals(len(self.fconf), 1) + self.assertEquals(len(self.fconf.blaa), 1) + + def test_register_grouped_opt_none_default(self): + self.fconf.register_opt(StrOpt('foo'), group='blaa') + + self.assertTrue(self.fconf.blaa.foo is None) + self.assertTrue(self.fconf['blaa']['foo'] is None) + self.assertTrue('blaa' in self.fconf) + self.assertTrue('foo' in self.fconf.blaa) + self.assertEquals(list(self.fconf), ['blaa']) + self.assertEquals(list(self.fconf.blaa), ['foo']) + self.assertEquals(len(self.fconf), 1) + self.assertEquals(len(self.fconf.blaa), 1) + + def test_register_group(self): + group = OptGroup('blaa') + self.fconf.register_group(group) + self.fconf.register_opt(StrOpt('foo'), group=group) + + self.assertTrue(self.fconf.blaa.foo is None) + self.assertTrue(self.fconf['blaa']['foo'] is None) + self.assertTrue('blaa' in self.fconf) + self.assertTrue('foo' in self.fconf.blaa) + self.assertEquals(list(self.fconf), ['blaa']) + self.assertEquals(list(self.fconf.blaa), ['foo']) + self.assertEquals(len(self.fconf), 1) + self.assertEquals(len(self.fconf.blaa), 1) + + def test_unknown_opt(self): + self.assertFalse('foo' in self.fconf) + self.assertEquals(len(self.fconf), 0) + self.assertRaises(NoSuchOptError, getattr, self.fconf, 'foo') + + def test_blocked_opt(self): + self.conf.register_opt(StrOpt('foo')) + + self.assertTrue('foo' in self.conf) + self.assertEquals(len(self.conf), 1) + self.assertTrue(self.conf.foo is None) + self.assertFalse('foo' in self.fconf) + self.assertEquals(len(self.fconf), 0) + self.assertRaises(NoSuchOptError, getattr, self.fconf, 'foo') + + def test_import_opt(self): + self.fconf = ConfigFilter(CONF) + self.assertFalse(hasattr(self.fconf, 'blaa')) + self.fconf.import_opt('blaa', 'tests.testmods.blaa_opt') + self.assertTrue(hasattr(self.fconf, 'blaa')) + + def test_import_opt_in_group(self): + self.fconf = ConfigFilter(CONF) + self.assertFalse(hasattr(self.fconf, 'bar')) + self.fconf.import_opt('foo', 'tests.testmods.bar_foo_opt', group='bar') + self.assertTrue(hasattr(self.fconf, 'bar')) + self.assertTrue(hasattr(self.fconf.bar, 'foo')) + + def test_import_group(self): + self.fconf = ConfigFilter(CONF) + self.assertFalse(hasattr(self.fconf, 'baar')) + self.fconf.import_group('baar', 'tests.testmods.baar_baa_opt') + self.assertTrue(hasattr(self.fconf, 'baar')) + self.assertTrue(hasattr(self.fconf.baar, 'baa')) |
