Debian Codegraph @

keystoneclient/tests/unit/auth/test_cli.py

raw symbols SCIP
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
13 import argparse
14 from unittest import mock
15 import uuid
16
17 import fixtures
18 from oslo_config import cfg
19
20 from keystoneclient.auth import base
21 from keystoneclient.auth import cli
22 from keystoneclient.tests.unit.auth import utils
23
24
25 class TesterPlugin(base.BaseAuthPlugin):
26
27 def get_token(self, *args, **kwargs):
28 return None
29
30 @classmethod
31 def get_options(cls):
32 # NOTE(jamielennox): this is kind of horrible. If you specify this as
33 # a deprecated_name= value it will convert - to _ which is not what we
34 # want for a CLI option.
35 deprecated = [cfg.DeprecatedOpt('test-other')]
36 return [
37 cfg.StrOpt('test-opt', help='tester', deprecated_opts=deprecated)
38 ]
39
40
41 class CliTests(utils.TestCase):
42
43 def setUp(self):
44 super(CliTests, self).setUp()
45 self.deprecations.expect_deprecations()
46 self.p = argparse.ArgumentParser()
47
48 def env(self, name, value=None):
49 if value is not None:
50 # environment variables are always strings
51 value = str(value)
52
53 return self.useFixture(fixtures.EnvironmentVariable(name, value))
54
55 def test_creating_with_no_args(self):
56 ret = cli.register_argparse_arguments(self.p, [])
57 self.assertIsNone(ret)
58 self.assertIn('--os-auth-plugin', self.p.format_usage())
59
60 def test_load_with_nothing(self):
61 cli.register_argparse_arguments(self.p, [])
62 opts = self.p.parse_args([])
63 self.assertIsNone(cli.load_from_argparse_arguments(opts))
64
65 @utils.mock_plugin
66 def test_basic_params_added(self, m):
67 name = uuid.uuid4().hex
68 argv = ['--os-auth-plugin', name]
69 ret = cli.register_argparse_arguments(self.p, argv)
70 self.assertIs(utils.MockPlugin, ret)
71
72 for n in ('--os-a-int', '--os-a-bool', '--os-a-float'):
73 self.assertIn(n, self.p.format_usage())
74
75 m.assert_called_once_with(name)
76
77 @utils.mock_plugin
78 def test_param_loading(self, m):
79 name = uuid.uuid4().hex
80 argv = ['--os-auth-plugin', name,
81 '--os-a-int', str(self.a_int),
82 '--os-a-float', str(self.a_float),
83 '--os-a-bool', str(self.a_bool)]
84
85 klass = cli.register_argparse_arguments(self.p, argv)
86 self.assertIs(utils.MockPlugin, klass)
87
88 opts = self.p.parse_args(argv)
89 self.assertEqual(name, opts.os_auth_plugin)
90
91 a = cli.load_from_argparse_arguments(opts)
92 self.assertTestVals(a)
93
94 self.assertEqual(name, opts.os_auth_plugin)
95 self.assertEqual(str(self.a_int), opts.os_a_int)
96 self.assertEqual(str(self.a_float), opts.os_a_float)
97 self.assertEqual(str(self.a_bool), opts.os_a_bool)
98
99 @utils.mock_plugin
100 def test_default_options(self, m):
101 name = uuid.uuid4().hex
102 argv = ['--os-auth-plugin', name,
103 '--os-a-float', str(self.a_float)]
104
105 klass = cli.register_argparse_arguments(self.p, argv)
106 self.assertIs(utils.MockPlugin, klass)
107
108 opts = self.p.parse_args(argv)
109 self.assertEqual(name, opts.os_auth_plugin)
110
111 a = cli.load_from_argparse_arguments(opts)
112
113 self.assertEqual(self.a_float, a['a_float'])
114 self.assertEqual(3, a['a_int'])
115
116 @utils.mock_plugin
117 def test_with_default_string_value(self, m):
118 name = uuid.uuid4().hex
119 klass = cli.register_argparse_arguments(self.p, [], default=name)
120 self.assertIs(utils.MockPlugin, klass)
121 m.assert_called_once_with(name)
122
123 @utils.mock_plugin
124 def test_overrides_default_string_value(self, m):
125 name = uuid.uuid4().hex
126 default = uuid.uuid4().hex
127 argv = ['--os-auth-plugin', name]
128 klass = cli.register_argparse_arguments(self.p, argv, default=default)
129 self.assertIs(utils.MockPlugin, klass)
130 m.assert_called_once_with(name)
131
132 @utils.mock_plugin
133 def test_with_default_type_value(self, m):
134 klass = cli.register_argparse_arguments(self.p, [],
135 default=utils.MockPlugin)
136 self.assertIs(utils.MockPlugin, klass)
137 self.assertEqual(0, m.call_count)
138
139 @utils.mock_plugin
140 def test_overrides_default_type_value(self, m):
141 # using this test plugin would fail if called because there
142 # is no get_options() function
143 class TestPlugin(object):
144 pass
145 name = uuid.uuid4().hex
146 argv = ['--os-auth-plugin', name]
147 klass = cli.register_argparse_arguments(self.p, argv,
148 default=TestPlugin)
149 self.assertIs(utils.MockPlugin, klass)
150 m.assert_called_once_with(name)
151
152 @utils.mock_plugin
153 def test_env_overrides_default_opt(self, m):
154 name = uuid.uuid4().hex
155 val = uuid.uuid4().hex
156 self.env('OS_A_STR', val)
157
158 klass = cli.register_argparse_arguments(self.p, [], default=name)
159 opts = self.p.parse_args([])
160 a = klass.load_from_argparse_arguments(opts)
161
162 self.assertEqual(val, a['a_str'])
163
164 def test_deprecated_cli_options(self):
165 TesterPlugin.register_argparse_arguments(self.p)
166 val = uuid.uuid4().hex
167 opts = self.p.parse_args(['--os-test-other', val])
168 self.assertEqual(val, opts.os_test_opt)
169
170 def test_deprecated_multi_cli_options(self):
171 TesterPlugin.register_argparse_arguments(self.p)
172 val1 = uuid.uuid4().hex
173 val2 = uuid.uuid4().hex
174 # argarse rules say that the last specified wins.
175 opts = self.p.parse_args(['--os-test-other', val2,
176 '--os-test-opt', val1])
177 self.assertEqual(val1, opts.os_test_opt)
178
179 def test_deprecated_env_options(self):
180 val = uuid.uuid4().hex
181
182 with mock.patch.dict('os.environ', {'OS_TEST_OTHER': val}):
183 TesterPlugin.register_argparse_arguments(self.p)
184
185 opts = self.p.parse_args([])
186 self.assertEqual(val, opts.os_test_opt)
187
188 def test_deprecated_env_multi_options(self):
189 val1 = uuid.uuid4().hex
190 val2 = uuid.uuid4().hex
191
192 with mock.patch.dict('os.environ', {'OS_TEST_OPT': val1,
193 'OS_TEST_OTHER': val2}):
194 TesterPlugin.register_argparse_arguments(self.p)
195
196 opts = self.p.parse_args([])
197 self.assertEqual(val1, opts.os_test_opt)