diff options
author | Mark McLoughlin <markmc@redhat.com> | 2014-06-02 14:35:02 +0300 |
---|---|---|
committer | Mark McLoughlin <markmc@redhat.com> | 2014-07-03 15:07:35 +0100 |
commit | bcb8b7b8f61bd43e7d314c6d743c436501f1728f (patch) | |
tree | cafffe4ca506bd7571640b9a515be46080568117 /tests | |
parent | dcef87a2828187f70f7f291f94840f702f104628 (diff) | |
download | oslo-config-bcb8b7b8f61bd43e7d314c6d743c436501f1728f.tar.gz |
Add cfgfilter.ConfigFilter
Add a new class designed to wrap cfg.ConfigOpts, with the following use cases
in mind:
1. Help enforce that a given module does not access options registered by
another module, without first declaring those cross-module dependencies
using import_opt().
2. Prevent private configuration opts from being visible to modules other than
the one which registered it.
blueprint: oslo-config-cfgfilter
Change-Id: I477f7027806d7630ce91159fb274f41c0c203770
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_cfgfilter.py | 280 | ||||
-rw-r--r-- | tests/testmods/fbaar_baa_opt.py | 21 | ||||
-rw-r--r-- | tests/testmods/fbar_foo_opt.py | 21 | ||||
-rw-r--r-- | tests/testmods/fblaa_opt.py | 21 |
4 files changed, 343 insertions, 0 deletions
diff --git a/tests/test_cfgfilter.py b/tests/test_cfgfilter.py new file mode 100644 index 0000000..b2ec902 --- /dev/null +++ b/tests/test_cfgfilter.py @@ -0,0 +1,280 @@ +# Copyright 2014 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 oslotest import base as test_base + +from oslo.config import cfg +from oslo.config import cfgfilter + + +class BaseTestCase(test_base.BaseTestCase): + + def setUp(self, conf=None): + super(BaseTestCase, self).setUp() + if conf is None: + self.conf = cfg.ConfigOpts() + else: + self.conf = conf + self.fconf = cfgfilter.ConfigFilter(self.conf) + + +class RegisterTestCase(BaseTestCase): + + def test_register_opt_default(self): + self.fconf.register_opt(cfg.StrOpt('foo', default='bar')) + + self.assertEqual('bar', self.fconf.foo) + self.assertEqual('bar', self.fconf['foo']) + self.assertIn('foo', self.fconf) + self.assertEqual(['foo'], list(self.fconf)) + self.assertEqual(1, len(self.fconf)) + + self.assertNotIn('foo', self.conf) + self.assertEqual(0, len(self.conf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo') + + def test_register_opt_none_default(self): + self.fconf.register_opt(cfg.StrOpt('foo')) + + self.assertIsNone(self.fconf.foo) + self.assertIsNone(self.fconf['foo']) + self.assertIn('foo', self.fconf) + self.assertEqual(['foo'], list(self.fconf)) + self.assertEqual(1, len(self.fconf)) + + self.assertNotIn('foo', self.conf) + self.assertEqual(0, len(self.conf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'foo') + + def test_register_grouped_opt_default(self): + self.fconf.register_opt(cfg.StrOpt('foo', default='bar'), + group='blaa') + + self.assertEqual('bar', self.fconf.blaa.foo) + self.assertEqual('bar', self.fconf['blaa']['foo']) + self.assertIn('blaa', self.fconf) + self.assertIn('foo', self.fconf.blaa) + self.assertEqual(['blaa'], list(self.fconf)) + self.assertEqual(['foo'], list(self.fconf.blaa)) + self.assertEqual(1, len(self.fconf)) + self.assertEqual(1, len(self.fconf.blaa)) + + self.assertNotIn('blaa', self.conf) + self.assertEqual(0, len(self.conf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') + + def test_register_grouped_opt_none_default(self): + self.fconf.register_opt(cfg.StrOpt('foo'), group='blaa') + + self.assertIsNone(self.fconf.blaa.foo) + self.assertIsNone(self.fconf['blaa']['foo']) + self.assertIn('blaa', self.fconf) + self.assertIn('foo', self.fconf.blaa) + self.assertEqual(['blaa'], list(self.fconf)) + self.assertEqual(['foo'], list(self.fconf.blaa)) + self.assertEqual(1, len(self.fconf)) + self.assertEqual(1, len(self.fconf.blaa)) + + self.assertNotIn('blaa', self.conf) + self.assertEqual(0, len(self.conf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') + + def test_register_group(self): + group = cfg.OptGroup('blaa') + self.fconf.register_group(group) + self.fconf.register_opt(cfg.StrOpt('foo'), group=group) + + self.assertIsNone(self.fconf.blaa.foo) + self.assertIsNone(self.fconf['blaa']['foo']) + self.assertIn('blaa', self.fconf) + self.assertIn('foo', self.fconf.blaa) + self.assertEqual(['blaa'], list(self.fconf)) + self.assertEqual(['foo'], list(self.fconf.blaa)) + self.assertEqual(1, len(self.fconf)) + self.assertEqual(1, len(self.fconf.blaa)) + + self.assertNotIn('blaa', self.conf) + self.assertEqual(0, len(self.conf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.conf, 'blaa') + + def test_register_opts(self): + self.fconf.register_opts([cfg.StrOpt('foo'), + cfg.StrOpt('bar')]) + self.assertIn('foo', self.fconf) + self.assertIn('bar', self.fconf) + self.assertNotIn('foo', self.conf) + self.assertNotIn('bar', self.conf) + + def test_register_cli_opt(self): + self.fconf.register_cli_opt(cfg.StrOpt('foo')) + self.assertIn('foo', self.fconf) + self.assertNotIn('foo', self.conf) + + def test_register_cli_opts(self): + self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')]) + self.assertIn('foo', self.fconf) + self.assertIn('bar', self.fconf) + self.assertNotIn('foo', self.conf) + self.assertNotIn('bar', self.conf) + + def test_register_opts_grouped(self): + self.fconf.register_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], + group='blaa') + self.assertIn('foo', self.fconf.blaa) + self.assertIn('bar', self.fconf.blaa) + self.assertNotIn('blaa', self.conf) + + def test_register_cli_opt_grouped(self): + self.fconf.register_cli_opt(cfg.StrOpt('foo'), group='blaa') + self.assertIn('foo', self.fconf.blaa) + self.assertNotIn('blaa', self.conf) + + def test_register_cli_opts_grouped(self): + self.fconf.register_cli_opts([cfg.StrOpt('foo'), cfg.StrOpt('bar')], + group='blaa') + self.assertIn('foo', self.fconf.blaa) + self.assertIn('bar', self.fconf.blaa) + self.assertNotIn('blaa', self.conf) + + def test_unknown_opt(self): + self.assertNotIn('foo', self.fconf) + self.assertEqual(0, len(self.fconf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo') + self.assertNotIn('blaa', self.conf) + + def test_blocked_opt(self): + self.conf.register_opt(cfg.StrOpt('foo')) + + self.assertIn('foo', self.conf) + self.assertEqual(1, len(self.conf)) + self.assertIsNone(self.conf.foo) + self.assertNotIn('foo', self.fconf) + self.assertEqual(0, len(self.fconf)) + self.assertRaises(cfg.NoSuchOptError, getattr, self.fconf, 'foo') + + def test_already_registered_opt(self): + self.conf.register_opt(cfg.StrOpt('foo')) + self.fconf.register_opt(cfg.StrOpt('foo')) + + self.assertIn('foo', self.conf) + self.assertEqual(1, len(self.conf)) + self.assertIsNone(self.conf.foo) + self.assertIn('foo', self.fconf) + self.assertEqual(1, len(self.fconf)) + self.assertIsNone(self.fconf.foo) + + self.conf.set_override('foo', 'bar') + + self.assertEqual('bar', self.conf.foo) + self.assertEqual('bar', self.fconf.foo) + + def test_already_registered_opts(self): + self.conf.register_opts([cfg.StrOpt('foo'), + cfg.StrOpt('fu')]) + self.fconf.register_opts([cfg.StrOpt('foo'), + cfg.StrOpt('bu')]) + + self.assertIn('foo', self.conf) + self.assertIn('fu', self.conf) + self.assertNotIn('bu', self.conf) + self.assertEqual(2, len(self.conf)) + self.assertIsNone(self.conf.foo) + self.assertIsNone(self.conf.fu) + self.assertIn('foo', self.fconf) + self.assertIn('bu', self.fconf) + self.assertNotIn('fu', self.fconf) + self.assertEqual(2, len(self.fconf)) + self.assertIsNone(self.fconf.foo) + self.assertIsNone(self.fconf.bu) + + self.conf.set_override('foo', 'bar') + + self.assertEqual('bar', self.conf.foo) + self.assertEqual('bar', self.fconf.foo) + + def test_already_registered_cli_opt(self): + self.conf.register_cli_opt(cfg.StrOpt('foo')) + self.fconf.register_cli_opt(cfg.StrOpt('foo')) + + self.assertIn('foo', self.conf) + self.assertEqual(1, len(self.conf)) + self.assertIsNone(self.conf.foo) + self.assertIn('foo', self.fconf) + self.assertEqual(1, len(self.fconf)) + self.assertIsNone(self.fconf.foo) + + self.conf.set_override('foo', 'bar') + + self.assertEqual('bar', self.conf.foo) + self.assertEqual('bar', self.fconf.foo) + + def test_already_registered_cli_opts(self): + self.conf.register_cli_opts([cfg.StrOpt('foo'), + cfg.StrOpt('fu')]) + self.fconf.register_cli_opts([cfg.StrOpt('foo'), + cfg.StrOpt('bu')]) + + self.assertIn('foo', self.conf) + self.assertIn('fu', self.conf) + self.assertNotIn('bu', self.conf) + self.assertEqual(2, len(self.conf)) + self.assertIsNone(self.conf.foo) + self.assertIsNone(self.conf.fu) + self.assertIn('foo', self.fconf) + self.assertIn('bu', self.fconf) + self.assertNotIn('fu', self.fconf) + self.assertEqual(2, len(self.fconf)) + self.assertIsNone(self.fconf.foo) + self.assertIsNone(self.fconf.bu) + + self.conf.set_override('foo', 'bar') + + self.assertEqual('bar', self.conf.foo) + self.assertEqual('bar', self.fconf.foo) + + +class ImportTestCase(BaseTestCase): + + def setUp(self): + super(ImportTestCase, self).setUp(cfg.CONF) + + def test_import_opt(self): + self.assertFalse(hasattr(self.conf, 'fblaa')) + self.conf.import_opt('fblaa', 'tests.testmods.fblaa_opt') + self.assertTrue(hasattr(self.conf, 'fblaa')) + self.assertFalse(hasattr(self.fconf, 'fblaa')) + self.fconf.import_opt('fblaa', 'tests.testmods.fblaa_opt') + self.assertTrue(hasattr(self.fconf, 'fblaa')) + + def test_import_opt_in_group(self): + self.assertFalse(hasattr(self.conf, 'fbar')) + self.conf.import_opt('foo', 'tests.testmods.fbar_foo_opt', + group='fbar') + self.assertTrue(hasattr(self.conf, 'fbar')) + self.assertTrue(hasattr(self.conf.fbar, 'foo')) + self.assertFalse(hasattr(self.fconf, 'fbar')) + self.fconf.import_opt('foo', 'tests.testmods.fbar_foo_opt', + group='fbar') + self.assertTrue(hasattr(self.fconf, 'fbar')) + self.assertTrue(hasattr(self.fconf.fbar, 'foo')) + + def test_import_group(self): + self.assertFalse(hasattr(self.conf, 'fbaar')) + self.conf.import_group('fbaar', 'tests.testmods.fbaar_baa_opt') + self.assertTrue(hasattr(self.conf, 'fbaar')) + self.assertTrue(hasattr(self.conf.fbaar, 'baa')) + self.assertFalse(hasattr(self.fconf, 'fbaar')) + self.fconf.import_group('fbaar', 'tests.testmods.fbaar_baa_opt') + self.assertTrue(hasattr(self.fconf, 'fbaar')) + self.assertTrue(hasattr(self.fconf.fbaar, 'baa')) diff --git a/tests/testmods/fbaar_baa_opt.py b/tests/testmods/fbaar_baa_opt.py new file mode 100644 index 0000000..68875b1 --- /dev/null +++ b/tests/testmods/fbaar_baa_opt.py @@ -0,0 +1,21 @@ +# 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 + +opt = cfg.StrOpt('baa') + +CONF.register_opt(opt, group='fbaar') diff --git a/tests/testmods/fbar_foo_opt.py b/tests/testmods/fbar_foo_opt.py new file mode 100644 index 0000000..c0280a4 --- /dev/null +++ b/tests/testmods/fbar_foo_opt.py @@ -0,0 +1,21 @@ +# 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 + +opt = cfg.StrOpt('foo') + +CONF.register_opt(opt, group='fbar') diff --git a/tests/testmods/fblaa_opt.py b/tests/testmods/fblaa_opt.py new file mode 100644 index 0000000..8b5258d --- /dev/null +++ b/tests/testmods/fblaa_opt.py @@ -0,0 +1,21 @@ +# 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 + +opt = cfg.StrOpt('fblaa') + +CONF.register_opt(opt) |