Debian Codegraph @

mistralclient/tests/unit/test_client.py

raw symbols SCIP
1 # Copyright 2015 - Huawei Technologies Co., Ltd.
2 # Copyright 2016 - StackStorm, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17 import tempfile
18 from unittest import mock
19
20 from oslo_serialization import jsonutils
21 from oslo_utils import uuidutils
22 from oslotest import base
23 import osprofiler.profiler
24
25 from mistralclient.api import client
26
27 AUTH_HTTP_URL_v3 = 'http://localhost:35357/v3'
28 AUTH_HTTP_URL_v2_0 = 'http://localhost:35357/v2.0'
29 AUTH_HTTPS_URL = AUTH_HTTP_URL_v3.replace('http', 'https')
30 MISTRAL_HTTP_URL = 'http://localhost:8989/v2'
31 MISTRAL_HTTPS_URL = MISTRAL_HTTP_URL.replace('http', 'https')
32 PROFILER_HMAC_KEY = 'SECRET_HMAC_KEY'
33
34
35 class BaseClientTest(base.BaseTestCase):
36
37 @staticmethod
38 def setup_keystone_mock(session_mock):
39 keystone_client_instance = session_mock.return_value
40 keystone_client_instance.auth_token = uuidutils.generate_uuid()
41 keystone_client_instance.project_id = uuidutils.generate_uuid()
42 keystone_client_instance.user_id = uuidutils.generate_uuid()
43 keystone_client_instance.auth_ref = str(jsonutils.dumps({}))
44 return keystone_client_instance
45
46 @mock.patch('keystoneauth1.session.Session')
47 def test_mistral_url_from_catalog_v2(self, session_mock):
48 session = mock.Mock()
49 session_mock.side_effect = [session]
50
51 get_endpoint = mock.Mock(return_value='http://mistral_host:8989/v2')
52 session.get_endpoint = get_endpoint
53
54 mistralclient = client.client(
55 username='mistral',
56 project_name='mistral',
57 api_key='password',
58 auth_url=AUTH_HTTP_URL_v2_0,
59 service_type='workflowv2'
60 )
61
62 self.assertEqual(
63 'http://mistral_host:8989/v2',
64 mistralclient.actions.http_client.base_url
65 )
66
67 @mock.patch('keystoneauth1.session.Session')
68 def test_mistral_url_from_catalog(self, session_mock):
69 session = mock.Mock()
70 session_mock.side_effect = [session]
71
72 get_endpoint = mock.Mock(return_value='http://mistral_host:8989/v2')
73 session.get_endpoint = get_endpoint
74
75 mistralclient = client.client(
76 username='mistral',
77 project_name='mistral',
78 api_key='password',
79 user_domain_name='Default',
80 project_domain_name='Default',
81 auth_url=AUTH_HTTP_URL_v3,
82 service_type='workflowv2'
83 )
84
85 self.assertEqual(
86 'http://mistral_host:8989/v2',
87 mistralclient.actions.http_client.base_url
88 )
89
90 @mock.patch('keystoneauth1.session.Session')
91 @mock.patch('mistralclient.api.httpclient.HTTPClient')
92 def test_mistral_url_default(self, http_client_mock, session_mock):
93 session = mock.Mock()
94 session_mock.side_effect = [session]
95
96 get_endpoint = mock.Mock(side_effect=Exception)
97 session.get_endpoint = get_endpoint
98
99 client.client(
100 username='mistral',
101 project_name='mistral',
102 api_key='password',
103 user_domain_name='Default',
104 project_domain_name='Default',
105 auth_url=AUTH_HTTP_URL_v3
106 )
107
108 self.assertTrue(http_client_mock.called)
109 mistral_url_for_http = http_client_mock.call_args[0][0]
110 self.assertEqual(MISTRAL_HTTP_URL, mistral_url_for_http)
111
112 @mock.patch('mistralclient.auth.keystone.KeystoneAuthHandler'
113 '._is_service_catalog_v2', return_value=True)
114 @mock.patch('keystoneauth1.identity.generic.Password')
115 @mock.patch('keystoneauth1.session.Session')
116 @mock.patch('mistralclient.api.httpclient.HTTPClient')
117 def test_target_parameters_processed(
118 self,
119 http_client_mock,
120 session_mock,
121 password_mock,
122 catalog_type_mock
123 ):
124
125 session = mock.MagicMock()
126 target_session = mock.MagicMock()
127 session_mock.side_effect = [session, target_session]
128 auth = mock.MagicMock()
129 target_auth = mock.MagicMock()
130 target_auth._plugin.auth_url = AUTH_HTTP_URL_v3
131
132 password_mock.side_effect = [auth, target_auth]
133
134 get_endpoint = mock.Mock(return_value='http://mistral_host:8989/v2')
135 session.get_endpoint = get_endpoint
136
137 target_session.get_project_id = mock.Mock(return_value='projectid')
138 target_session.get_user_id = mock.Mock(return_value='userid')
139 target_session.get_auth_headers = mock.Mock(return_value={
140 'X-Auth-Token': 'authtoken'
141 })
142
143 mock_access = mock.MagicMock()
144 mock_catalog = mock.MagicMock()
145 mock_catalog.catalog = {}
146 mock_access.service_catalog = mock_catalog
147 auth.get_access = mock.Mock(return_value=mock_access)
148
149 client.client(
150 auth_url=AUTH_HTTP_URL_v3,
151 username='user',
152 api_key='password',
153 user_domain_name='Default',
154 project_domain_name='Default',
155 target_username='tmistral',
156 target_project_name='tmistralp',
157 target_auth_url=AUTH_HTTP_URL_v3,
158 target_api_key='tpassword',
159 target_user_domain_name='Default',
160 target_project_domain_name='Default',
161 target_region_name='tregion'
162 )
163
164 self.assertTrue(http_client_mock.called)
165 mistral_url_for_http = http_client_mock.call_args[0][0]
166 kwargs = http_client_mock.call_args[1]
167 self.assertEqual('http://mistral_host:8989/v2', mistral_url_for_http)
168
169 expected_values = {
170 'target_project_id': 'projectid',
171 'target_auth_token': 'authtoken',
172 'target_user_id': 'userid',
173 'target_auth_url': AUTH_HTTP_URL_v3,
174 'target_project_name': 'tmistralp',
175 'target_username': 'tmistral',
176 'target_region_name': 'tregion',
177 'target_service_catalog': "{}"
178 }
179
180 for key in expected_values:
181 self.assertEqual(expected_values[key], kwargs[key])
182
183 @mock.patch('keystoneauth1.session.Session')
184 @mock.patch('mistralclient.api.httpclient.HTTPClient')
185 def test_mistral_url_https_insecure(self, http_client_mock, session_mock):
186 keystone_client_instance = self.setup_keystone_mock( # noqa
187 session_mock
188 )
189
190 expected_args = (
191 MISTRAL_HTTPS_URL,
192 )
193
194 client.client(
195 mistral_url=MISTRAL_HTTPS_URL,
196 username='mistral',
197 project_name='mistral',
198 api_key='password',
199 user_domain_name='Default',
200 project_domain_name='Default',
201 auth_url=AUTH_HTTP_URL_v3,
202 cacert=None,
203 insecure=True
204 )
205
206 self.assertTrue(http_client_mock.called)
207 self.assertEqual(http_client_mock.call_args[0], expected_args)
208 self.assertEqual(http_client_mock.call_args[1]['insecure'], True)
209
210 @mock.patch('keystoneauth1.session.Session')
211 @mock.patch('mistralclient.api.httpclient.HTTPClient')
212 def test_mistral_url_https_secure(self, http_client_mock, session_mock):
213 fd, cert_path = tempfile.mkstemp(suffix='.pem')
214
215 keystone_client_instance = self.setup_keystone_mock( # noqa
216 session_mock
217 )
218
219 expected_args = (
220 MISTRAL_HTTPS_URL,
221 )
222
223 try:
224 client.client(
225 mistral_url=MISTRAL_HTTPS_URL,
226 username='mistral',
227 project_name='mistral',
228 api_key='password',
229 user_domain_name='Default',
230 project_domain_name='Default',
231 auth_url=AUTH_HTTP_URL_v3,
232 cacert=cert_path,
233 insecure=False
234 )
235 finally:
236 os.close(fd)
237 os.unlink(cert_path)
238
239 self.assertTrue(http_client_mock.called)
240 self.assertEqual(http_client_mock.call_args[0], expected_args)
241 self.assertEqual(http_client_mock.call_args[1]['cacert'], cert_path)
242
243 @mock.patch('keystoneauth1.session.Session')
244 def test_mistral_url_https_bad_cacert(self, session_mock):
245 keystone_client_instance = self.setup_keystone_mock( # noqa
246 session_mock
247 )
248
249 self.assertRaises(
250 ValueError,
251 client.client,
252 mistral_url=MISTRAL_HTTPS_URL,
253 username='mistral',
254 project_name='mistral',
255 api_key='password',
256 user_domain_name='Default',
257 project_domain_name='Default',
258 auth_url=AUTH_HTTP_URL_v3,
259 cacert='/path/to/foobar',
260 insecure=False
261 )
262
263 @mock.patch('logging.Logger.warning')
264 @mock.patch('keystoneauth1.session.Session')
265 def test_mistral_url_https_bad_insecure(self, session_mock,
266 log_warning_mock):
267 fd, path = tempfile.mkstemp(suffix='.pem')
268
269 keystone_client_instance = self.setup_keystone_mock(
270 session_mock
271 )
272
273 try:
274 client.client(
275 mistral_url=MISTRAL_HTTPS_URL,
276 user_id=keystone_client_instance.user_id,
277 project_id=keystone_client_instance.project_id,
278 api_key='password',
279 user_domain_name='Default',
280 project_domain_name='Default',
281 auth_url=AUTH_HTTP_URL_v3,
282 cacert=path,
283 insecure=True
284 )
285 finally:
286 os.close(fd)
287 os.unlink(path)
288
289 self.assertTrue(log_warning_mock.called)
290
291 @mock.patch('keystoneauth1.session.Session')
292 @mock.patch('mistralclient.api.httpclient.HTTPClient')
293 def test_mistral_profile_enabled(self, http_client_mock, session_mock):
294 keystone_client_instance = self.setup_keystone_mock( # noqa
295 session_mock
296 )
297
298 client.client(
299 username='mistral',
300 project_name='mistral',
301 api_key='password',
302 user_domain_name='Default',
303 project_domain_name='Default',
304 auth_url=AUTH_HTTP_URL_v3,
305 profile=PROFILER_HMAC_KEY
306 )
307
308 self.assertTrue(http_client_mock.called)
309
310 profiler = osprofiler.profiler.get()
311
312 self.assertEqual(profiler.hmac_key, PROFILER_HMAC_KEY)
313
314 @mock.patch('mistralclient.auth.get_auth_handler')
315 def test_mistral_no_auth(self, get_auth_handler_mock):
316 # Test that we don't authenticate if auth url wasn't provided
317
318 client.client(
319 username='mistral',
320 project_name='mistral',
321 api_key='password',
322 service_type='workflowv2'
323 )
324
325 self.assertEqual(0, get_auth_handler_mock.call_count)