2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
17
from keystoneclient import exceptions as keystone_exceptions
19
from heat.common import exception
20
from heat.engine.clients.os import keystone as client
21
from heat.tests import common
24
class KeystoneRoleConstraintTest(common.HeatTestCase):
26
def test_expected_exceptions(self):
27
self.assertEqual((exception.EntityNotFound,),
28
client.KeystoneRoleConstraint.expected_exceptions,
29
"KeystoneRoleConstraint expected exceptions error")
31
def test_constrain(self):
32
constrain = client.KeystoneRoleConstraint()
33
client_mock = mock.MagicMock()
34
client_plugin_mock = mock.MagicMock()
35
client_plugin_mock.get_role_id.return_value = None
36
client_mock.client_plugin.return_value = client_plugin_mock
38
self.assertIsNone(constrain.validate_with_client(client_mock,
41
client_plugin_mock.get_role_id.assert_called_once_with('role_1')
44
class KeystoneProjectConstraintTest(common.HeatTestCase):
46
def test_expected_exceptions(self):
47
self.assertEqual((exception.EntityNotFound,),
48
client.KeystoneProjectConstraint.expected_exceptions,
49
"KeystoneProjectConstraint expected exceptions error")
51
def test_constrain(self):
52
constrain = client.KeystoneProjectConstraint()
53
client_mock = mock.MagicMock()
54
client_plugin_mock = mock.MagicMock()
55
client_plugin_mock.get_project_id.return_value = None
56
client_mock.client_plugin.return_value = client_plugin_mock
58
self.assertIsNone(constrain.validate_with_client(client_mock,
61
client_plugin_mock.get_project_id.assert_called_once_with('project_1')
64
class KeystoneGroupConstraintTest(common.HeatTestCase):
66
def test_expected_exceptions(self):
67
self.assertEqual((exception.EntityNotFound,),
68
client.KeystoneGroupConstraint.expected_exceptions,
69
"KeystoneGroupConstraint expected exceptions error")
71
def test_constrain(self):
72
constrain = client.KeystoneGroupConstraint()
73
client_mock = mock.MagicMock()
74
client_plugin_mock = mock.MagicMock()
75
client_plugin_mock.get_group_id.return_value = None
76
client_mock.client_plugin.return_value = client_plugin_mock
78
self.assertIsNone(constrain.validate_with_client(client_mock,
81
client_plugin_mock.get_group_id.assert_called_once_with('group_1')
84
class KeystoneDomainConstraintTest(common.HeatTestCase):
86
def test_expected_exceptions(self):
87
self.assertEqual((exception.EntityNotFound,),
88
client.KeystoneDomainConstraint.expected_exceptions,
89
"KeystoneDomainConstraint expected exceptions error")
91
def test_constrain(self):
92
constrain = client.KeystoneDomainConstraint()
93
client_mock = mock.MagicMock()
94
client_plugin_mock = mock.MagicMock()
95
client_plugin_mock.get_domain_id.return_value = None
96
client_mock.client_plugin.return_value = client_plugin_mock
98
self.assertIsNone(constrain.validate_with_client(client_mock,
101
client_plugin_mock.get_domain_id.assert_called_once_with('domain_1')
104
class KeystoneServiceConstraintTest(common.HeatTestCase):
106
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
108
def test_expected_exceptions(self):
109
self.assertEqual((exception.EntityNotFound,
110
exception.KeystoneServiceNameConflict,),
111
client.KeystoneServiceConstraint.expected_exceptions,
112
"KeystoneServiceConstraint expected exceptions error")
114
def test_constrain(self):
115
constrain = client.KeystoneServiceConstraint()
116
client_mock = mock.MagicMock()
117
client_plugin_mock = mock.MagicMock()
118
client_plugin_mock.get_service_id.return_value = self.sample_uuid
119
client_mock.client_plugin.return_value = client_plugin_mock
121
self.assertIsNone(constrain.validate_with_client(client_mock,
124
client_plugin_mock.get_service_id.assert_called_once_with(
129
class KeystoneClientPluginServiceTest(common.HeatTestCase):
131
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
132
sample_name = 'sample_service'
134
def _get_mock_service(self):
135
srv = mock.MagicMock()
136
srv.id = self.sample_uuid
137
srv.name = self.sample_name
141
super(KeystoneClientPluginServiceTest, self).setUp()
142
self._client = mock.MagicMock()
144
@mock.patch.object(client.KeystoneClientPlugin, 'client')
145
def test_get_service_id(self, client_keystone):
147
self._client.client.services.get.return_value = (self
148
._get_mock_service())
150
client_keystone.return_value = self._client
151
client_plugin = client.KeystoneClientPlugin(
152
context=mock.MagicMock()
155
self.assertEqual(self.sample_uuid,
156
client_plugin.get_service_id(self.sample_uuid))
157
self._client.client.services.get.assert_called_once_with(
160
@mock.patch.object(client.KeystoneClientPlugin, 'client')
161
def test_get_service_id_with_name(self, client_keystone):
162
self._client.client.services.get.side_effect = (keystone_exceptions
164
self._client.client.services.list.return_value = [
165
self._get_mock_service()
168
client_keystone.return_value = self._client
169
client_plugin = client.KeystoneClientPlugin(
170
context=mock.MagicMock()
173
self.assertEqual(self.sample_uuid,
174
client_plugin.get_service_id(self.sample_name))
175
self.assertRaises(keystone_exceptions.NotFound,
176
self._client.client.services.get,
178
self._client.client.services.list.assert_called_once_with(
179
name=self.sample_name)
181
@mock.patch.object(client.KeystoneClientPlugin, 'client')
182
def test_get_service_id_with_name_conflict(self, client_keystone):
183
self._client.client.services.get.side_effect = (keystone_exceptions
185
self._client.client.services.list.return_value = [
186
self._get_mock_service(),
187
self._get_mock_service()
190
client_keystone.return_value = self._client
191
client_plugin = client.KeystoneClientPlugin(
192
context=mock.MagicMock()
195
ex = self.assertRaises(exception.KeystoneServiceNameConflict,
196
client_plugin.get_service_id,
198
msg = ("Keystone has more than one service with same name "
199
"%s. Please use service id instead of name" %
201
self.assertEqual(msg, six.text_type(ex))
202
self.assertRaises(keystone_exceptions.NotFound,
203
self._client.client.services.get,
205
self._client.client.services.list.assert_called_once_with(
206
name=self.sample_name)
208
@mock.patch.object(client.KeystoneClientPlugin, 'client')
209
def test_get_service_id_not_found(self, client_keystone):
210
self._client.client.services.get.side_effect = (keystone_exceptions
212
self._client.client.services.list.return_value = [
215
client_keystone.return_value = self._client
216
client_plugin = client.KeystoneClientPlugin(
217
context=mock.MagicMock()
220
ex = self.assertRaises(exception.EntityNotFound,
221
client_plugin.get_service_id,
223
msg = ("The KeystoneService (%(name)s) could not be found." %
224
{'name': self.sample_name})
225
self.assertEqual(msg, six.text_type(ex))
226
self.assertRaises(keystone_exceptions.NotFound,
227
self._client.client.services.get,
229
self._client.client.services.list.assert_called_once_with(
230
name=self.sample_name)
233
class KeystoneClientPluginRoleTest(common.HeatTestCase):
235
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
236
sample_name = 'sample_role'
238
def _get_mock_role(self):
239
role = mock.MagicMock()
240
role.id = self.sample_uuid
241
role.name = self.sample_name
245
super(KeystoneClientPluginRoleTest, self).setUp()
246
self._client = mock.MagicMock()
248
@mock.patch.object(client.KeystoneClientPlugin, 'client')
249
def test_get_role_id(self, client_keystone):
250
self._client.client.roles.get.return_value = (self
253
client_keystone.return_value = self._client
254
client_plugin = client.KeystoneClientPlugin(
255
context=mock.MagicMock()
258
self.assertEqual(self.sample_uuid,
259
client_plugin.get_role_id(self.sample_uuid))
260
self._client.client.roles.get.assert_called_once_with(
263
@mock.patch.object(client.KeystoneClientPlugin, 'client')
264
def test_get_role_id_with_name(self, client_keystone):
265
self._client.client.roles.get.side_effect = (keystone_exceptions
267
self._client.client.roles.list.return_value = [
268
self._get_mock_role()
271
client_keystone.return_value = self._client
272
client_plugin = client.KeystoneClientPlugin(
273
context=mock.MagicMock()
276
self.assertEqual(self.sample_uuid,
277
client_plugin.get_role_id(self.sample_name))
278
self.assertRaises(keystone_exceptions.NotFound,
279
self._client.client.roles.get,
281
self._client.client.roles.list.assert_called_once_with(
282
name=self.sample_name)
284
@mock.patch.object(client.KeystoneClientPlugin, 'client')
285
def test_get_role_id_not_found(self, client_keystone):
286
self._client.client.roles.get.side_effect = (keystone_exceptions
288
self._client.client.roles.list.return_value = [
291
client_keystone.return_value = self._client
292
client_plugin = client.KeystoneClientPlugin(
293
context=mock.MagicMock()
296
ex = self.assertRaises(exception.EntityNotFound,
297
client_plugin.get_role_id,
299
msg = ("The KeystoneRole (%(name)s) could not be found." %
300
{'name': self.sample_name})
301
self.assertEqual(msg, six.text_type(ex))
302
self.assertRaises(keystone_exceptions.NotFound,
303
self._client.client.roles.get,
305
self._client.client.roles.list.assert_called_once_with(
306
name=self.sample_name)
309
class KeystoneClientPluginProjectTest(common.HeatTestCase):
311
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
312
sample_name = 'sample_project'
314
def _get_mock_project(self):
315
project = mock.MagicMock()
316
project.id = self.sample_uuid
317
project.name = self.sample_name
321
super(KeystoneClientPluginProjectTest, self).setUp()
322
self._client = mock.MagicMock()
324
@mock.patch.object(client.KeystoneClientPlugin, 'client')
325
def test_get_project_id(self, client_keystone):
326
self._client.client.projects.get.return_value = (self
327
._get_mock_project())
329
client_keystone.return_value = self._client
330
client_plugin = client.KeystoneClientPlugin(
331
context=mock.MagicMock()
334
self.assertEqual(self.sample_uuid,
335
client_plugin.get_project_id(self.sample_uuid))
336
self._client.client.projects.get.assert_called_once_with(
339
@mock.patch.object(client.KeystoneClientPlugin, 'client')
340
def test_get_project_id_with_name(self, client_keystone):
341
self._client.client.projects.get.side_effect = (keystone_exceptions
343
self._client.client.projects.list.return_value = [
344
self._get_mock_project()
347
client_keystone.return_value = self._client
348
client_plugin = client.KeystoneClientPlugin(
349
context=mock.MagicMock()
352
self.assertEqual(self.sample_uuid,
353
client_plugin.get_project_id(self.sample_name))
354
self.assertRaises(keystone_exceptions.NotFound,
355
self._client.client.projects.get,
357
self._client.client.projects.list.assert_called_once_with(
358
name=self.sample_name)
360
@mock.patch.object(client.KeystoneClientPlugin, 'client')
361
def test_get_project_id_not_found(self, client_keystone):
362
self._client.client.projects.get.side_effect = (keystone_exceptions
364
self._client.client.projects.list.return_value = [
367
client_keystone.return_value = self._client
368
client_plugin = client.KeystoneClientPlugin(
369
context=mock.MagicMock()
372
ex = self.assertRaises(exception.EntityNotFound,
373
client_plugin.get_project_id,
375
msg = ("The KeystoneProject (%(name)s) could not be found." %
376
{'name': self.sample_name})
377
self.assertEqual(msg, six.text_type(ex))
378
self.assertRaises(keystone_exceptions.NotFound,
379
self._client.client.projects.get,
381
self._client.client.projects.list.assert_called_once_with(
382
name=self.sample_name)
385
class KeystoneClientPluginDomainTest(common.HeatTestCase):
387
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
388
sample_name = 'sample_domain'
390
def _get_mock_domain(self):
391
domain = mock.MagicMock()
392
domain.id = self.sample_uuid
393
domain.name = self.sample_name
397
super(KeystoneClientPluginDomainTest, self).setUp()
398
self._client = mock.MagicMock()
400
@mock.patch.object(client.KeystoneClientPlugin, 'client')
401
def test_get_domain_id(self, client_keystone):
402
self._client.client.domains.get.return_value = (self
405
client_keystone.return_value = self._client
406
client_plugin = client.KeystoneClientPlugin(
407
context=mock.MagicMock()
410
self.assertEqual(self.sample_uuid,
411
client_plugin.get_domain_id(self.sample_uuid))
412
self._client.client.domains.get.assert_called_once_with(
415
@mock.patch.object(client.KeystoneClientPlugin, 'client')
416
def test_get_domain_id_with_name(self, client_keystone):
417
self._client.client.domains.get.side_effect = (keystone_exceptions
419
self._client.client.domains.list.return_value = [
420
self._get_mock_domain()
423
client_keystone.return_value = self._client
424
client_plugin = client.KeystoneClientPlugin(
425
context=mock.MagicMock()
428
self.assertEqual(self.sample_uuid,
429
client_plugin.get_domain_id(self.sample_name))
430
self.assertRaises(keystone_exceptions.NotFound,
431
self._client.client.domains.get,
433
self._client.client.domains.list.assert_called_once_with(
434
name=self.sample_name)
436
@mock.patch.object(client.KeystoneClientPlugin, 'client')
437
def test_get_domain_id_not_found(self, client_keystone):
438
self._client.client.domains.get.side_effect = (keystone_exceptions
440
self._client.client.domains.list.return_value = [
443
client_keystone.return_value = self._client
444
client_plugin = client.KeystoneClientPlugin(
445
context=mock.MagicMock()
448
ex = self.assertRaises(exception.EntityNotFound,
449
client_plugin.get_domain_id,
451
msg = ("The KeystoneDomain (%(name)s) could not be found." %
452
{'name': self.sample_name})
453
self.assertEqual(msg, six.text_type(ex))
454
self.assertRaises(keystone_exceptions.NotFound,
455
self._client.client.domains.get,
457
self._client.client.domains.list.assert_called_once_with(
458
name=self.sample_name)
461
class KeystoneClientPluginGroupTest(common.HeatTestCase):
463
sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
464
sample_name = 'sample_group'
466
def _get_mock_group(self):
467
group = mock.MagicMock()
468
group.id = self.sample_uuid
469
group.name = self.sample_name
473
super(KeystoneClientPluginGroupTest, self).setUp()
474
self._client = mock.MagicMock()
476
@mock.patch.object(client.KeystoneClientPlugin, 'client')
477
def test_get_group_id(self, client_keystone):
478
self._client.client.groups.get.return_value = (self
481
client_keystone.return_value = self._client
482
client_plugin = client.KeystoneClientPlugin(
483
context=mock.MagicMock()
486
self.assertEqual(self.sample_uuid,
487
client_plugin.get_group_id(self.sample_uuid))
488
self._client.client.groups.get.assert_called_once_with(
491
@mock.patch.object(client.KeystoneClientPlugin, 'client')
492
def test_get_group_id_with_name(self, client_keystone):
493
self._client.client.groups.get.side_effect = (keystone_exceptions
495
self._client.client.groups.list.return_value = [
496
self._get_mock_group()
499
client_keystone.return_value = self._client
500
client_plugin = client.KeystoneClientPlugin(
501
context=mock.MagicMock()
504
self.assertEqual(self.sample_uuid,
505
client_plugin.get_group_id(self.sample_name))
506
self.assertRaises(keystone_exceptions.NotFound,
507
self._client.client.groups.get,
509
self._client.client.groups.list.assert_called_once_with(
510
name=self.sample_name)
512
@mock.patch.object(client.KeystoneClientPlugin, 'client')
513
def test_get_group_id_not_found(self, client_keystone):
514
self._client.client.groups.get.side_effect = (keystone_exceptions
516
self._client.client.groups.list.return_value = [
519
client_keystone.return_value = self._client
520
client_plugin = client.KeystoneClientPlugin(
521
context=mock.MagicMock()
524
ex = self.assertRaises(exception.EntityNotFound,
525
client_plugin.get_group_id,
527
msg = ("The KeystoneGroup (%(name)s) could not be found." %
528
{'name': self.sample_name})
529
self.assertEqual(msg, six.text_type(ex))
530
self.assertRaises(keystone_exceptions.NotFound,
531
self._client.client.groups.get,
533
self._client.client.groups.list.assert_called_once_with(
534
name=self.sample_name)