Fix set_default() when used before config init

Using set_default() before initializing OpenStackConfig would cause
an error since the _defaults dict would still be None and not an
actual dict. This corrects that by calling get_defaults() to make
sure it is initialized properly, and also adds a warning to note
that the method is now deprecated.

Change-Id: I81803c680b614f9bee47c6f69a4efffa638dcebc
This commit is contained in:
David Shrewsbury 2015-07-14 10:54:21 -04:00
parent 27dff22c6b
commit b74df460a8
2 changed files with 23 additions and 1 deletions

View File

@ -64,6 +64,11 @@ BOOL_KEYS = ('insecure', 'cache')
# Remove this sometime in June 2015 once OSC is comfortably
# changed-over and global-defaults is updated.
def set_default(key, value):
warnings.warn(
"Use of set_default() is deprecated. Defaults should be set with the "
"`override_defaults` parameter of OpenStackConfig."
)
defaults.get_defaults() # make sure the dict is initialized
defaults._defaults[key] = value

View File

@ -252,9 +252,26 @@ class TestConfigArgparse(base.TestCase):
class TestConfigDefault(base.TestCase):
def setUp(self):
super(TestConfigDefault, self).setUp()
# Reset defaults after each test so that other tests are
# not affected by any changes.
self.addCleanup(self._reset_defaults)
def _reset_defaults(self):
defaults._defaults = None
def test_set_no_default(self):
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
cc = c.get_one_cloud(cloud='_test-cloud_', argparse=None)
self._assert_cloud_details(cc)
self.assertEqual(cc.auth_type, 'password')
self.assertEqual('password', cc.auth_type)
def test_set_default_before_init(self):
config.set_default('auth_type', 'token')
c = config.OpenStackConfig(config_files=[self.cloud_yaml],
vendor_files=[self.vendor_yaml])
cc = c.get_one_cloud(cloud='_test-cloud_', argparse=None)
self.assertEqual('token', cc.auth_type)