Add set_default() method to fixture
Add a set_default() method to the fixture to allow it to be passed to set_default() functions in other oslo libraries that expect to be given a ConfigObj instance. This lets tests in one project use a fixture to set defaults for options in code they don't own, and unset those defaults when the test is cleaned up. Change-Id: Ifa9e6e2ab4ab4f93a7dc22d786ee6fd6ae7224ae
This commit is contained in:
parent
d86fd9b03f
commit
427de804c3
oslo_config
@ -177,3 +177,27 @@ class Config(fixtures.Fixture):
|
||||
|
||||
self.conf.default_config_files = config_files
|
||||
self.conf.reload_config_files()
|
||||
|
||||
def set_default(self, name, default, group=None):
|
||||
"""Set a default value for an option.
|
||||
|
||||
This method is not necessarily meant to be invoked
|
||||
directly. It is here to allow the set_defaults() functions in
|
||||
various Oslo libraries to work with a Config fixture instead
|
||||
of a ConfigOpts instance.
|
||||
|
||||
Use it like::
|
||||
|
||||
class MyTest(testtools.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(MyTest, self).setUp()
|
||||
self.conf = self.useFixture(fixture.Config())
|
||||
|
||||
def test_something(self):
|
||||
some_library.set_defaults(self.conf, name='value')
|
||||
some_library.do_something_exciting()
|
||||
|
||||
"""
|
||||
self.conf.set_default(name, default, group)
|
||||
self.addCleanup(self.conf.clear_default, name, group)
|
||||
|
@ -29,79 +29,79 @@ class ConfigTestCase(base.BaseTestCase):
|
||||
config_fixture.setUp()
|
||||
config_fixture.register_opt(cfg.StrOpt(
|
||||
'testing_option', default='initial_value'))
|
||||
return (config_fixture, conf)
|
||||
return config_fixture
|
||||
|
||||
def test_overridden_value(self):
|
||||
f, conf = self._make_fixture()
|
||||
self.assertEqual(conf.get('testing_option'), 'initial_value')
|
||||
f = self._make_fixture()
|
||||
self.assertEqual(f.conf.get('testing_option'), 'initial_value')
|
||||
f.config(testing_option='changed_value')
|
||||
self.assertEqual(conf.get('testing_option'),
|
||||
self.assertEqual('changed_value',
|
||||
f.conf.get('testing_option'))
|
||||
|
||||
def test_cleanup(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
f.config(testing_option='changed_value')
|
||||
self.assertEqual(f.conf.get('testing_option'),
|
||||
'changed_value')
|
||||
f.conf.reset()
|
||||
self.assertEqual(conf.get('testing_option'), 'initial_value')
|
||||
self.assertEqual(f.conf.get('testing_option'), 'initial_value')
|
||||
|
||||
def test_register_option(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
f.register_opt(opt)
|
||||
self.assertEqual(conf.get('new_test_opt'),
|
||||
self.assertEqual(f.conf.get('new_test_opt'),
|
||||
opt.default)
|
||||
|
||||
def test_register_options(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
|
||||
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
|
||||
f.register_opts([opt1, opt2])
|
||||
self.assertEqual(conf.get('first_test_opt'), opt1.default)
|
||||
self.assertEqual(conf.get('second_test_opt'), opt2.default)
|
||||
self.assertEqual(f.conf.get('first_test_opt'), opt1.default)
|
||||
self.assertEqual(f.conf.get('second_test_opt'), opt2.default)
|
||||
|
||||
def test_cleanup_unregister_option(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
f.register_opt(opt)
|
||||
self.assertEqual(conf.get('new_test_opt'),
|
||||
self.assertEqual(f.conf.get('new_test_opt'),
|
||||
opt.default)
|
||||
f.cleanUp()
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt')
|
||||
|
||||
def test_register_cli_option(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
f.register_cli_opt(opt)
|
||||
self.assertEqual(conf.get('new_test_opt'),
|
||||
self.assertEqual(f.conf.get('new_test_opt'),
|
||||
opt.default)
|
||||
|
||||
def test_register_cli_options(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
|
||||
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
|
||||
f.register_cli_opts([opt1, opt2])
|
||||
self.assertEqual(conf.get('first_test_opt'), opt1.default)
|
||||
self.assertEqual(conf.get('second_test_opt'), opt2.default)
|
||||
self.assertEqual(f.conf.get('first_test_opt'), opt1.default)
|
||||
self.assertEqual(f.conf.get('second_test_opt'), opt2.default)
|
||||
|
||||
def test_cleanup_unregister_cli_option(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
f.register_cli_opt(opt)
|
||||
self.assertEqual(conf.get('new_test_opt'),
|
||||
self.assertEqual(f.conf.get('new_test_opt'),
|
||||
opt.default)
|
||||
f.cleanUp()
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'new_test_opt')
|
||||
|
||||
def test_load_raw_values(self):
|
||||
f, conf = self._make_fixture()
|
||||
f = self._make_fixture()
|
||||
f.load_raw_values(first_test_opt='loaded_value_1',
|
||||
second_test_opt='loaded_value_2')
|
||||
|
||||
# Must not be registered.
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'first_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'second_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt')
|
||||
|
||||
opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1')
|
||||
opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2')
|
||||
@ -109,38 +109,38 @@ class ConfigTestCase(base.BaseTestCase):
|
||||
f.register_opt(opt1)
|
||||
f.register_opt(opt2)
|
||||
|
||||
self.assertEqual(conf.first_test_opt, 'loaded_value_1')
|
||||
self.assertEqual(conf.second_test_opt, 'loaded_value_2')
|
||||
self.assertEqual(f.conf.first_test_opt, 'loaded_value_1')
|
||||
self.assertEqual(f.conf.second_test_opt, 'loaded_value_2')
|
||||
|
||||
# Cleanup.
|
||||
f.cleanUp()
|
||||
|
||||
# Must no longer be registered.
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'first_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, conf.get, 'second_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'first_test_opt')
|
||||
self.assertRaises(cfg.NoSuchOptError, f.conf.get, 'second_test_opt')
|
||||
|
||||
# Even when registered, must be default.
|
||||
f.register_opt(opt1)
|
||||
f.register_opt(opt2)
|
||||
self.assertEqual(conf.first_test_opt, 'initial_value_1')
|
||||
self.assertEqual(conf.second_test_opt, 'initial_value_2')
|
||||
self.assertEqual(f.conf.first_test_opt, 'initial_value_1')
|
||||
self.assertEqual(f.conf.second_test_opt, 'initial_value_2')
|
||||
|
||||
def test_assert_default_files_cleanup(self):
|
||||
"""Assert that using the fixture forces a clean list."""
|
||||
f, conf = self._make_fixture()
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
f = self._make_fixture()
|
||||
self.assertNotIn('default_config_files', f.conf)
|
||||
|
||||
config_files = ['./test_fixture.conf']
|
||||
f.set_config_files(config_files)
|
||||
|
||||
self.assertEqual(conf.default_config_files, config_files)
|
||||
self.assertEqual(f.conf.default_config_files, config_files)
|
||||
f.cleanUp()
|
||||
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
self.assertNotIn('default_config_files', f.conf)
|
||||
|
||||
def test_load_custom_files(self):
|
||||
f, conf = self._make_fixture()
|
||||
self.assertNotIn('default_config_files', conf)
|
||||
f = self._make_fixture()
|
||||
self.assertNotIn('default_config_files', f.conf)
|
||||
config_files = ['./oslo_config/tests/test_fixture.conf']
|
||||
f.set_config_files(config_files)
|
||||
|
||||
@ -150,5 +150,34 @@ class ConfigTestCase(base.BaseTestCase):
|
||||
f.register_opt(opt1)
|
||||
f.register_opt(opt2)
|
||||
|
||||
self.assertEqual('loaded_value_1', conf.get('first_test_opt'))
|
||||
self.assertEqual('loaded_value_2', conf.get('second_test_opt'))
|
||||
self.assertEqual('loaded_value_1', f.conf.get('first_test_opt'))
|
||||
self.assertEqual('loaded_value_2', f.conf.get('second_test_opt'))
|
||||
|
||||
def test_set_default(self):
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
# Register the option directly so it is not cleaned up by the
|
||||
# fixture.
|
||||
f.conf.register_opt(opt)
|
||||
f.set_default(
|
||||
name='new_test_opt',
|
||||
default='alternate_value',
|
||||
)
|
||||
self.assertEqual('alternate_value', f.conf.new_test_opt)
|
||||
f.cleanUp()
|
||||
self.assertEqual('initial_value', f.conf.new_test_opt)
|
||||
|
||||
def test_set_default_group(self):
|
||||
f = self._make_fixture()
|
||||
opt = cfg.StrOpt('new_test_opt', default='initial_value')
|
||||
# Register the option directly so it is not cleaned up by the
|
||||
# fixture.
|
||||
f.conf.register_opt(opt, group='foo')
|
||||
f.set_default(
|
||||
name='new_test_opt',
|
||||
default='alternate_value',
|
||||
group='foo',
|
||||
)
|
||||
self.assertEqual('alternate_value', f.conf.foo.new_test_opt)
|
||||
f.cleanUp()
|
||||
self.assertEqual('initial_value', f.conf.foo.new_test_opt)
|
||||
|
Loading…
x
Reference in New Issue
Block a user