summaryrefslogtreecommitdiff
path: root/oslo_config/tests/test_get_location.py
blob: d06271ae14d33feef1d97008bcab7ecea326abc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import io
import tempfile
import textwrap

from oslotest import base

from oslo_config import cfg


class TestConfigOpts(cfg.ConfigOpts):
    def __call__(self, args=None, default_config_files=[],
                 default_config_dirs=[]):
        return cfg.ConfigOpts.__call__(
            self,
            args=args,
            prog='test',
            version='1.0',
            usage='%(prog)s FOO BAR',
            description='somedesc',
            epilog='tepilog',
            default_config_files=default_config_files,
            default_config_dirs=default_config_dirs,
            validate_default_values=True)


class LocationTestCase(base.BaseTestCase):

    def test_user_controlled(self):
        self.assertTrue(cfg.Locations.user.is_user_controlled)
        self.assertTrue(cfg.Locations.command_line.is_user_controlled)

    def test_not_user_controlled(self):
        self.assertFalse(cfg.Locations.opt_default.is_user_controlled)
        self.assertFalse(cfg.Locations.set_default.is_user_controlled)
        self.assertFalse(cfg.Locations.set_override.is_user_controlled)


class GetLocationTestCase(base.BaseTestCase):

    def setUp(self):
        super(GetLocationTestCase, self).setUp()

        def _clear():
            cfg._show_caller_details = False
        self.addCleanup(_clear)
        cfg._show_caller_details = True

        self.conf = TestConfigOpts()
        self.normal_opt = cfg.StrOpt(
            'normal_opt',
            default='normal_opt_default',
        )
        self.conf.register_opt(self.normal_opt)
        self.cli_opt = cfg.StrOpt(
            'cli_opt',
            default='cli_opt_default',
        )
        self.conf.register_cli_opt(self.cli_opt)
        self.group_opt = cfg.StrOpt(
            'group_opt',
            default='group_opt_default',
        )
        self.conf.register_opt(self.group_opt, group='group')

    def test_opt_default(self):
        self.conf([])
        loc = self.conf.get_location('normal_opt')
        self.assertEqual(
            cfg.Locations.opt_default,
            loc.location,
        )
        self.assertIn('test_get_location.py', loc.detail)

    def test_set_default_on_config_opt(self):
        self.conf.set_default('normal_opt', self.id())
        self.conf([])
        loc = self.conf.get_location('normal_opt')
        self.assertEqual(
            cfg.Locations.set_default,
            loc.location,
        )
        self.assertIn('test_get_location.py', loc.detail)

    def test_set_defaults_func(self):
        cfg.set_defaults([self.normal_opt], normal_opt=self.id())
        self.conf([])
        loc = self.conf.get_location('normal_opt')
        self.assertEqual(
            cfg.Locations.set_default,
            loc.location,
        )
        self.assertIn('test_get_location.py', loc.detail)

    def test_set_override(self):
        self.conf.set_override('normal_opt', self.id())
        self.conf([])
        loc = self.conf.get_location('normal_opt')
        self.assertEqual(
            cfg.Locations.set_override,
            loc.location,
        )
        self.assertIn('test_get_location.py', loc.detail)

    def test_user_cli(self):
        filename = self._write_opt_to_tmp_file(
            'DEFAULT', 'unknown_opt', self.id())
        self.conf(['--config-file', filename,
                   '--cli_opt', 'blah'])
        loc = self.conf.get_location('cli_opt')
        self.assertEqual(
            cfg.Locations.command_line,
            loc.location,
        )

    def test_default_cli(self):
        filename = self._write_opt_to_tmp_file(
            'DEFAULT', 'unknown_opt', self.id())
        self.conf(['--config-file', filename])
        loc = self.conf.get_location('cli_opt')
        self.assertEqual(
            cfg.Locations.opt_default,
            loc.location,
        )

    def _write_opt_to_tmp_file(self, group, option, value):
        filename = tempfile.mktemp()
        with io.open(filename, 'w', encoding='utf-8') as f:
            f.write(textwrap.dedent('''
            [{group}]
            {option} = {value}
            ''').format(
                group=group,
                option=option,
                value=value,
            ))
        return filename

    def test_user_cli_opt_in_file(self):
        filename = self._write_opt_to_tmp_file(
            'DEFAULT', 'cli_opt', self.id())
        self.conf(['--config-file', filename])
        loc = self.conf.get_location('cli_opt')
        self.assertEqual(
            cfg.Locations.user,
            loc.location,
        )
        self.assertEqual(
            filename,
            loc.detail,
        )

    def test_user_file(self):
        filename = self._write_opt_to_tmp_file(
            'DEFAULT', 'normal_opt', self.id())
        self.conf(['--config-file', filename])
        loc = self.conf.get_location('normal_opt')
        self.assertEqual(
            cfg.Locations.user,
            loc.location,
        )
        self.assertEqual(
            filename,
            loc.detail,
        )

    def test_duplicate_registration(self):
        # NOTE(dhellmann): The ZFS driver in Cinder uses multiple Opt
        # instances with the same settings but in different
        # modules. We don't want to break that case with the option
        # location stuff, so we need to test that it works. To
        # reproduce that test we have to simulate the option being
        # created in a different file, so we create a new Opt instance
        # and replace its _set_location data.
        dupe_opt = cfg.StrOpt(
            'normal_opt',
            default='normal_opt_default',
        )
        dupe_opt._set_location = cfg.LocationInfo(
            cfg.Locations.opt_default,
            'an alternative file',
        )
        # We expect register_opt() to return False to indicate that
        # the option was already registered.
        self.assertFalse(self.conf.register_opt(dupe_opt))

    def test_group_opt(self):
        self.conf([])
        loc = self.conf.get_location('group_opt', 'group')
        self.assertEqual(
            cfg.Locations.opt_default,
            loc.location,
        )
        self.assertIn('test_get_location.py', loc.detail)