~ubuntu-branches/ubuntu/utopic/ceilometer/utopic-proposed

« back to all changes in this revision

Viewing changes to ceilometer/openstack/common/fixture/config.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-10-16 14:07:11 UTC
  • mfrom: (1.2.1) (28.1.5 utopic-proposed)
  • Revision ID: package-import@ubuntu.com-20141016140711-95mki6bdkivvfr2x
Tags: 2014.2-0ubuntu1
New upstream release. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright 2013 Mirantis, Inc.
3
 
# Copyright 2013 OpenStack Foundation
4
 
# All Rights Reserved.
5
 
#
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
9
 
#
10
 
#         http://www.apache.org/licenses/LICENSE-2.0
11
 
#
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
16
 
#    under the License.
17
 
 
18
 
import fixtures
19
 
from oslo.config import cfg
20
 
import six
21
 
 
22
 
 
23
 
class Config(fixtures.Fixture):
24
 
    """Allows overriding configuration settings for the test.
25
 
 
26
 
    `conf` will be reset on cleanup.
27
 
 
28
 
    """
29
 
 
30
 
    def __init__(self, conf=cfg.CONF):
31
 
        self.conf = conf
32
 
 
33
 
    def setUp(self):
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 = {}
41
 
 
42
 
    def config(self, **kw):
43
 
        """Override configuration values.
44
 
 
45
 
        The keyword arguments are the names of configuration options to
46
 
        override and their values.
47
 
 
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.
51
 
 
52
 
        """
53
 
 
54
 
        group = kw.pop('group', None)
55
 
        for k, v in six.iteritems(kw):
56
 
            self.conf.set_override(k, v, group)
57
 
 
58
 
    def _unregister_config_opts(self):
59
 
        for group in self._registered_config_opts:
60
 
            self.conf.unregister_opts(self._registered_config_opts[group],
61
 
                                      group=group)
62
 
 
63
 
    def register_opt(self, opt, group=None):
64
 
        """Register a single option for the test run.
65
 
 
66
 
        Options registered in this manner will automatically be unregistered
67
 
        during cleanup.
68
 
 
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``
71
 
        group.
72
 
        """
73
 
        self.conf.register_opt(opt, group=group)
74
 
        self._registered_config_opts.setdefault(group, set()).add(opt)
75
 
 
76
 
    def register_opts(self, opts, group=None):
77
 
        """Register multiple options for the test run.
78
 
 
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.
83
 
        """
84
 
        for opt in opts:
85
 
            self.register_opt(opt, group=group)