1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5#      http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13from unittest import mock
14import uuid
15
16from oslo_config import cfg
17from oslo_config import fixture as config
18import stevedore
19
20from keystoneauth1 import exceptions
21from keystoneauth1 import loading
22from keystoneauth1.loading._plugins.identity import v2
23from keystoneauth1.loading._plugins.identity import v3
24from keystoneauth1.tests.unit.loading import utils
25
26
27class ConfTests(utils.TestCase):
28
29    def setUp(self):
30        super(ConfTests, self).setUp()
31        self.conf_fixture = self.useFixture(config.Config())
32
33        # NOTE(jamielennox): we register the basic config options first because
34        # we need them in place before we can stub them. We will need to run
35        # the register again after we stub the auth section and auth plugin so
36        # it can load the plugin specific options.
37        loading.register_auth_conf_options(self.conf_fixture.conf,
38                                           group=self.GROUP)
39
40    def test_loading_v2(self):
41        section = uuid.uuid4().hex
42        auth_url = uuid.uuid4().hex
43        username = uuid.uuid4().hex
44        password = uuid.uuid4().hex
45        trust_id = uuid.uuid4().hex
46        tenant_id = uuid.uuid4().hex
47
48        self.conf_fixture.config(auth_section=section, group=self.GROUP)
49        loading.register_auth_conf_options(self.conf_fixture.conf,
50                                           group=self.GROUP)
51
52        opts = loading.get_auth_plugin_conf_options(v2.Password())
53        self.conf_fixture.register_opts(opts, group=section)
54
55        self.conf_fixture.config(auth_type=self.V2PASS,
56                                 auth_url=auth_url,
57                                 username=username,
58                                 password=password,
59                                 trust_id=trust_id,
60                                 tenant_id=tenant_id,
61                                 group=section)
62
63        a = loading.load_auth_from_conf_options(self.conf_fixture.conf,
64                                                self.GROUP)
65
66        self.assertEqual(auth_url, a.auth_url)
67        self.assertEqual(username, a.username)
68        self.assertEqual(password, a.password)
69        self.assertEqual(trust_id, a.trust_id)
70        self.assertEqual(tenant_id, a.tenant_id)
71
72    def test_loading_v3(self):
73        section = uuid.uuid4().hex
74        auth_url = uuid.uuid4().hex,
75        token = uuid.uuid4().hex
76        trust_id = uuid.uuid4().hex
77        project_id = uuid.uuid4().hex
78        project_domain_name = uuid.uuid4().hex
79
80        self.conf_fixture.config(auth_section=section, group=self.GROUP)
81        loading.register_auth_conf_options(self.conf_fixture.conf,
82                                           group=self.GROUP)
83
84        opts = loading.get_auth_plugin_conf_options(v3.Token())
85        self.conf_fixture.register_opts(opts, group=section)
86
87        self.conf_fixture.config(auth_type=self.V3TOKEN,
88                                 auth_url=auth_url,
89                                 token=token,
90                                 trust_id=trust_id,
91                                 project_id=project_id,
92                                 project_domain_name=project_domain_name,
93                                 group=section)
94
95        a = loading.load_auth_from_conf_options(self.conf_fixture.conf,
96                                                self.GROUP)
97
98        self.assertEqual(token, a.auth_methods[0].token)
99        self.assertEqual(trust_id, a.trust_id)
100        self.assertEqual(project_id, a.project_id)
101        self.assertEqual(project_domain_name, a.project_domain_name)
102
103    def test_loading_invalid_plugin(self):
104        auth_type = uuid.uuid4().hex
105        self.conf_fixture.config(auth_type=auth_type,
106                                 group=self.GROUP)
107
108        e = self.assertRaises(exceptions.NoMatchingPlugin,
109                              loading.load_auth_from_conf_options,
110                              self.conf_fixture.conf,
111                              self.GROUP)
112
113        self.assertEqual(auth_type, e.name)
114
115    def test_loading_with_no_data(self):
116        lo = loading.load_auth_from_conf_options(self.conf_fixture.conf,
117                                                 self.GROUP)
118        self.assertIsNone(lo)
119
120    @mock.patch('stevedore.DriverManager')
121    def test_other_params(self, m):
122        m.return_value = utils.MockManager(utils.MockLoader())
123        driver_name = uuid.uuid4().hex
124
125        opts = loading.get_auth_plugin_conf_options(utils.MockLoader())
126        self.conf_fixture.register_opts(opts, group=self.GROUP)
127        self.conf_fixture.config(auth_type=driver_name,
128                                 group=self.GROUP,
129                                 **self.TEST_VALS)
130
131        a = loading.load_auth_from_conf_options(self.conf_fixture.conf,
132                                                self.GROUP)
133        self.assertTestVals(a)
134
135        m.assert_called_once_with(namespace=loading.PLUGIN_NAMESPACE,
136                                  name=driver_name,
137                                  invoke_on_load=True)
138
139    @utils.mock_plugin()
140    def test_same_section(self, m):
141        opts = loading.get_auth_plugin_conf_options(utils.MockLoader())
142        self.conf_fixture.register_opts(opts, group=self.GROUP)
143
144        loading.register_auth_conf_options(self.conf_fixture.conf,
145                                           group=self.GROUP)
146        self.conf_fixture.config(auth_type=uuid.uuid4().hex,
147                                 group=self.GROUP,
148                                 **self.TEST_VALS)
149
150        a = loading.load_auth_from_conf_options(self.conf_fixture.conf,
151                                                self.GROUP)
152        self.assertTestVals(a)
153
154    @utils.mock_plugin()
155    def test_diff_section(self, m):
156        section = uuid.uuid4().hex
157
158        self.conf_fixture.config(auth_section=section, group=self.GROUP)
159        loading.register_auth_conf_options(self.conf_fixture.conf,
160                                           group=self.GROUP)
161
162        opts = loading.get_auth_plugin_conf_options(utils.MockLoader())
163        self.conf_fixture.register_opts(opts, group=section)
164        self.conf_fixture.config(group=section,
165                                 auth_type=uuid.uuid4().hex,
166                                 **self.TEST_VALS)
167
168        a = loading.load_auth_from_conf_options(self.conf_fixture.conf,
169                                                self.GROUP)
170        self.assertTestVals(a)
171
172    def test_plugins_are_all_opts(self):
173        manager = stevedore.ExtensionManager(loading.PLUGIN_NAMESPACE,
174                                             propagate_map_exceptions=True)
175
176        def inner(driver):
177            for p in driver.plugin().get_options():
178                self.assertIsInstance(p, loading.Opt)
179
180        manager.map(inner)
181
182    def test_get_common(self):
183        opts = loading.get_auth_common_conf_options()
184        for opt in opts:
185            self.assertIsInstance(opt, cfg.Opt)
186        self.assertEqual(2, len(opts))
187
188    def test_get_named(self):
189        loaded_opts = loading.get_plugin_options('v2password')
190        plugin_opts = v2.Password().get_options()
191
192        loaded_names = set([o.name for o in loaded_opts])
193        plugin_names = set([o.name for o in plugin_opts])
194
195        self.assertEqual(plugin_names, loaded_names)
196
197    def test_register_cfg(self):
198        loading.register_auth_conf_options(self.conf_fixture.conf,
199                                           group=self.GROUP)
200
201    def test_common_conf_options(self):
202        opts = loading.get_auth_common_conf_options()
203
204        self.assertEqual(2, len(opts))
205        auth_type = [o for o in opts if o.name == 'auth_type'][0]
206        self.assertEqual(1, len(auth_type.deprecated_opts))
207        self.assertIsInstance(auth_type.deprecated_opts[0], cfg.DeprecatedOpt)
208