2
# Copyright 2013 Mirantis, Inc.
3
# Copyright 2013 OpenStack Foundation
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
from oslo.config import cfg
23
class Config(fixtures.Fixture):
24
"""Allows overriding configuration settings for the test.
26
`conf` will be reset on cleanup.
30
def __init__(self, conf=cfg.CONF):
34
super(Config, self).setUp()
35
# NOTE(morganfainberg): unregister must be added to cleanup before
36
# reset is because cleanup works in reverse order of registered items,
37
# and a reset must occur before unregistering options can occur.
38
self.addCleanup(self._unregister_config_opts)
39
self.addCleanup(self.conf.reset)
40
self._registered_config_opts = {}
42
def config(self, **kw):
43
"""Override configuration values.
45
The keyword arguments are the names of configuration options to
46
override and their values.
48
If a `group` argument is supplied, the overrides are applied to
49
the specified configuration option group, otherwise the overrides
50
are applied to the ``default`` group.
54
group = kw.pop('group', None)
55
for k, v in six.iteritems(kw):
56
self.conf.set_override(k, v, group)
58
def _unregister_config_opts(self):
59
for group in self._registered_config_opts:
60
self.conf.unregister_opts(self._registered_config_opts[group],
63
def register_opt(self, opt, group=None):
64
"""Register a single option for the test run.
66
Options registered in this manner will automatically be unregistered
69
If a `group` argument is supplied, it will register the new option
70
to that group, otherwise the option is registered to the ``default``
73
self.conf.register_opt(opt, group=group)
74
self._registered_config_opts.setdefault(group, set()).add(opt)
76
def register_opts(self, opts, group=None):
77
"""Register multiple options for the test run.
79
This works in the same manner as register_opt() but takes a list of
80
options as the first argument. All arguments will be registered to the
81
same group if the ``group`` argument is supplied, otherwise all options
82
will be registered to the ``default`` group.
85
self.register_opt(opt, group=group)