summaryrefslogtreecommitdiff
path: root/oslo_config/tests/test_sources.py
blob: 656f61ef84ce36e4e02e706fe1c5163f0d314c8e (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# 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 os

from oslotest import base
from requests import HTTPError
import requests_mock

from oslo_config import _list_opts
from oslo_config import cfg
from oslo_config import fixture
from oslo_config import sources
from oslo_config.sources import _uri


class TestProcessingSources(base.BaseTestCase):

    # NOTE(dhellmann): These tests use the config() method of the
    # fixture because that invokes set_override() on the option. The
    # load_raw_values() method injects data underneath the option, but
    # only after invoking __call__ on the ConfigOpts instance, which
    # is when the 'config_source' option is processed.

    def setUp(self):
        super(TestProcessingSources, self).setUp()
        self.conf = cfg.ConfigOpts()
        self.conf_fixture = self.useFixture(fixture.Config(self.conf))

    def test_no_sources_default(self):
        with base.mock.patch.object(
                self.conf,
                '_open_source_from_opt_group') as open_source:
            open_source.side_effect = AssertionError('should not be called')
            self.conf([])

    def test_no_sources(self):
        self.conf_fixture.config(
            config_source=[],
        )
        with base.mock.patch.object(
                self.conf,
                '_open_source_from_opt_group') as open_source:
            open_source.side_effect = AssertionError('should not be called')
            self.conf([])

    def test_source_named(self):
        self.conf_fixture.config(
            config_source=['missing_source'],
        )
        with base.mock.patch.object(
                self.conf,
                '_open_source_from_opt_group') as open_source:
            self.conf([])
            open_source.assert_called_once_with('missing_source')

    def test_multiple_sources_named(self):
        self.conf_fixture.config(
            config_source=['source1', 'source2'],
        )
        with base.mock.patch.object(
                self.conf,
                '_open_source_from_opt_group') as open_source:
            self.conf([])
            open_source.assert_has_calls([
                base.mock.call('source1'),
                base.mock.call('source2'),
            ])


class TestLoading(base.BaseTestCase):

    # NOTE(dhellmann): These tests can use load_raw_values() because
    # they explicitly call _open_source_from_opt_group() after the
    # ConfigOpts setup is done in __call__().

    def setUp(self):
        super(TestLoading, self).setUp()
        self.conf = cfg.ConfigOpts()
        self.conf_fixture = self.useFixture(fixture.Config(self.conf))

    def test_source_missing(self):
        # The group being loaded does not exist at all.
        source = self.conf._open_source_from_opt_group('missing_source')
        self.assertIsNone(source)

    def test_driver_missing(self):
        # The group exists and has other options, but does not specify
        # a driver.
        self.conf_fixture.load_raw_values(
            group='missing_driver',
            not_driver='foo',
        )
        source = self.conf._open_source_from_opt_group('missing_driver')
        self.assertIsNone(source)

    def test_unknown_driver(self):
        # The group exists, but does not specify a valid driver.
        self.conf_fixture.load_raw_values(
            group='unknown_driver',
            driver='very_unlikely_to_exist_driver_name',
        )
        source = self.conf._open_source_from_opt_group('unknown_driver')
        self.assertIsNone(source)


class TestEnvironmentConfigurationSource(base.BaseTestCase):

    def setUp(self):
        super(TestEnvironmentConfigurationSource, self).setUp()
        self.conf = cfg.ConfigOpts()
        self.conf_fixture = self.useFixture(fixture.Config(self.conf))
        self.conf.register_opt(cfg.StrOpt('bar'), 'foo')

        def cleanup():
            if 'OS_FOO__BAR' in os.environ:
                del os.environ['OS_FOO__BAR']
        self.addCleanup(cleanup)

    def test_simple_environment_get(self):
        self.conf(args=[])
        env_value = 'goodbye'
        os.environ['OS_FOO__BAR'] = env_value

        self.assertEqual(env_value, self.conf['foo']['bar'])

    def test_env_beats_files(self):
        file_value = 'hello'
        env_value = 'goodbye'
        self.conf(args=[])
        self.conf_fixture.load_raw_values(
            group='foo',
            bar=file_value,
        )

        self.assertEqual(file_value, self.conf['foo']['bar'])
        self.conf.reload_config_files()
        os.environ['OS_FOO__BAR'] = env_value
        self.assertEqual(env_value, self.conf['foo']['bar'])

    def test_cli_beats_env(self):
        env_value = 'goodbye'
        cli_value = 'cli'
        os.environ['OS_FOO__BAR'] = env_value
        self.conf.register_cli_opt(cfg.StrOpt('bar'), 'foo')
        self.conf(args=['--foo=%s' % cli_value])

        self.assertEqual(cli_value, self.conf['foo']['bar'])

    def test_use_env_false_allows_files(self):
        file_value = 'hello'
        env_value = 'goodbye'
        os.environ['OS_FOO__BAR'] = env_value
        self.conf(args=[], use_env=False)
        self.conf_fixture.load_raw_values(
            group='foo',
            bar=file_value,
        )

        self.assertEqual(file_value, self.conf['foo']['bar'])
        self.conf.reset()
        self.conf(args=[], use_env=True)
        self.assertEqual(env_value, self.conf['foo']['bar'])


def make_uri(name):
    return "https://oslo.config/{}.conf".format(name)


_extra_configs = {
    make_uri("types"): {
        "name": "types",
        "data": {
            "DEFAULT": {
                "foo": (cfg.StrOpt, "bar")
            },
            "test": {
                "opt_str": (cfg.StrOpt, "a nice string"),
                "opt_bool": (cfg.BoolOpt, True),
                "opt_int": (cfg.IntOpt, 42),
                "opt_float": (cfg.FloatOpt, 3.14),
                "opt_ip": (cfg.IPOpt, "127.0.0.1"),
                "opt_port": (cfg.PortOpt, 443),
                "opt_host": (cfg.HostnameOpt, "www.openstack.org"),
                "opt_uri": (cfg.URIOpt, "https://www.openstack.org"),
                "opt_multi": (cfg.MultiStrOpt, ["abc", "def", "ghi"])
            }
        }
    },
    make_uri("ini_1"): {
        "name": "ini_1",
        "data": {
            "DEFAULT": {
                "abc": (cfg.StrOpt, "abc")
            }
        }
    },
    make_uri("ini_2"): {
        "name": "ini_2",
        "data": {
            "DEFAULT": {
                "abc": (cfg.StrOpt, "foo"),
                "def": (cfg.StrOpt, "def")
            }
        }
    },
    make_uri("ini_3"): {
        "name": "ini_3",
        "data": {
            "DEFAULT": {
                "abc": (cfg.StrOpt, "bar"),
                "def": (cfg.StrOpt, "bar"),
                "ghi": (cfg.StrOpt, "ghi")
            }
        }
    }
}


def opts_to_ini(uri, *args, **kwargs):
    opts = _extra_configs[uri]["data"]
    result = ""

    # 'g': group, 'o': option, 't': type, and 'v': value
    for g in opts.keys():
        result += "[{}]\n".format(g)
        for o, (t, v) in opts[g].items():
            if t == cfg.MultiStrOpt:
                for i in v:
                    result += "{} = {}\n".format(o, i)
            else:
                result += "{} = {}\n".format(o, v)

    return result


class URISourceTestCase(base.BaseTestCase):

    def setUp(self):
        super(URISourceTestCase, self).setUp()
        self.conf = cfg.ConfigOpts()
        self.conf_fixture = self.useFixture(fixture.Config(self.conf))

    def _register_opts(self, opts):
        # 'g': group, 'o': option, and 't': type
        for g in opts.keys():
            for o, (t, _) in opts[g].items():
                self.conf.register_opt(t(o), g if g != "DEFAULT" else None)

    def test_incomplete_driver(self):
        # The group exists, but does not specify the
        # required options for this driver.
        self.conf_fixture.load_raw_values(
            group='incomplete_ini_driver',
            driver='remote_file',
        )
        source = self.conf._open_source_from_opt_group('incomplete_ini_driver')
        self.assertIsNone(source)

    @requests_mock.mock()
    def test_fetch_uri(self, m):
        m.get("https://bad.uri", status_code=404)

        self.assertRaises(
            HTTPError, _uri.URIConfigurationSource, "https://bad.uri")

        m.get("https://good.uri", text="[DEFAULT]\nfoo=bar\n")
        source = _uri.URIConfigurationSource("https://good.uri")

        self.assertEqual(
            "bar", source.get("DEFAULT", "foo", cfg.StrOpt("foo"))[0])

    @base.mock.patch(
        "oslo_config.sources._uri.URIConfigurationSource._fetch_uri",
        side_effect=opts_to_ini)
    def test_configuration_source(self, mock_fetch_uri):
        group = "types"
        uri = make_uri(group)

        self.conf_fixture.load_raw_values(
            group=group,
            driver='remote_file',
            uri=uri
        )
        self.conf_fixture.config(config_source=[group])

        # testing driver loading
        self.assertEqual(self.conf._sources, [])
        self.conf._load_alternative_sources()
        self.assertEqual(type(self.conf._sources[0]),
                         _uri.URIConfigurationSource)

        source = self.conf._open_source_from_opt_group(group)

        self._register_opts(_extra_configs[uri]["data"])

        # non-existing option
        self.assertIs(sources._NoValue,
                      source.get("DEFAULT", "bar", cfg.StrOpt("bar"))[0])

        # 'g': group, 'o': option, 't': type, and 'v': value
        for g in _extra_configs[uri]["data"]:
            for o, (t, v) in _extra_configs[uri]["data"][g].items():
                self.assertEqual(str(v), str(source.get(g, o, t(o))[0]))
                self.assertEqual(v,
                                 self.conf[g][o] if g != "DEFAULT" else
                                 self.conf[o])

    @base.mock.patch(
        "oslo_config.sources._uri.URIConfigurationSource._fetch_uri",
        side_effect=opts_to_ini)
    def test_multiple_configuration_sources(self, mock_fetch_uri):
        groups = ["ini_1", "ini_2", "ini_3"]
        uri = make_uri("ini_3")

        for group in groups:
            self.conf_fixture.load_raw_values(
                group=group,
                driver='remote_file',
                uri=make_uri(group)
            )

        self.conf_fixture.config(config_source=groups)
        self.conf._load_alternative_sources()

        # ini_3 contains all options to be tested
        self._register_opts(_extra_configs[uri]["data"])

        # where options are 'abc', 'def', and 'ghi'
        # if the extra configs are loaded in the right order
        # the option name and option value will match correctly
        for option in _extra_configs[uri]["data"]["DEFAULT"]:
            self.assertEqual(option, self.conf[option])

    def test_list_opts(self):
        discovered_group = None
        for group in _list_opts.list_opts():
            if group[0] is not None:
                if group[0].name == "sample_remote_file_source":
                    discovered_group = group
                    break

        self.assertIsNotNone(discovered_group)

        self.assertEqual(
            _uri.URIConfigurationSourceDriver().list_options_for_discovery(),
            # NOTE: Ignore 'driver' option inserted automatically.
            discovered_group[1][1:],
        )