From e30131a1ef47fa5b8962cefbcf970de0dd6785fa Mon Sep 17 00:00:00 2001 From: Michael Krotscheck Date: Thu, 9 Apr 2015 07:55:48 -0700 Subject: [PATCH] Added Raw Value Loading to Test Fixture This patch adds a method by which raw key/value pairs may be loaded into the test fixture, without them being registered first. Tests have been provided. Change-Id: I53d77e4782bf97aefee11edd489ff1c9ff6c69a3 --- oslo_config/fixture.py | 24 ++++++++++++++++++++++++ oslo_config/tests/test_fixture.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/oslo_config/fixture.py b/oslo_config/fixture.py index 02d570d9..bb0f287d 100644 --- a/oslo_config/fixture.py +++ b/oslo_config/fixture.py @@ -116,3 +116,27 @@ class Config(fixtures.Fixture): """ for opt in opts: self.register_cli_opt(opt, group=group) + + def load_raw_values(self, group=None, **kwargs): + """Load raw values into the configuration without registering them. + + This method adds a series of parameters into the current config + instance, as if they had been loaded by a ConfigParser. This method + does not require that you register the configuration options first, + however the values loaded will not be accessible until you do. + """ + + # Make sure the namespace exists for our tests. + if not self.conf._namespace: + self.conf.__call__(args=[]) + + # Default out the group name + group = 'DEFAULT' if not group else group + + raw_config = dict() + raw_config[group] = dict() + for key, value in six.iteritems(kwargs): + # Parsed values are an array of raw strings. + raw_config[group][key] = [str(value)] + + self.conf._namespace._add_parsed_config_file(raw_config, raw_config) diff --git a/oslo_config/tests/test_fixture.py b/oslo_config/tests/test_fixture.py index 6d8b2a7b..671bea98 100644 --- a/oslo_config/tests/test_fixture.py +++ b/oslo_config/tests/test_fixture.py @@ -85,3 +85,33 @@ class ConfigTestCase(base.BaseTestCase): opt.default) self.config_fixture.cleanUp() self.assertRaises(cfg.NoSuchOptError, conf.get, 'new_test_opt') + + def test_load_raw_values(self): + self.config_fixture.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') + + opt1 = cfg.StrOpt('first_test_opt', default='initial_value_1') + opt2 = cfg.StrOpt('second_test_opt', default='initial_value_2') + + self.config_fixture.register_opt(opt1) + self.config_fixture.register_opt(opt2) + + self.assertEqual(conf.first_test_opt, 'loaded_value_1') + self.assertEqual(conf.second_test_opt, 'loaded_value_2') + + # Cleanup. + self.config_fixture.cleanUp() + + # Must no longer be registered. + self.assertRaises(cfg.NoSuchOptError, conf.get, 'first_test_opt') + self.assertRaises(cfg.NoSuchOptError, conf.get, 'second_test_opt') + + # Even when registered, must be default. + self.config_fixture.register_opt(opt1) + self.config_fixture.register_opt(opt2) + self.assertEqual(conf.first_test_opt, 'initial_value_1') + self.assertEqual(conf.second_test_opt, 'initial_value_2')