~ubuntu-branches/ubuntu/wily/heat/wily

« back to all changes in this revision

Viewing changes to heat/tests/keystone/test_client.py

  • Committer: Package Import Robot
  • Author(s): Corey Bryant
  • Date: 2015-08-19 08:11:50 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20150819081150-m969fd35xn8bdmfu
Tags: 1:5.0.0~b2-0ubuntu1
* New upstream milestone for OpenStack Liberty.
* d/control: Align (build-)depends with upstream.
* d/p/fix-requirements.patch: Dropped. No longer needed.
* d/p/fixup-assert-regex.patch: Rebased.
* d/rules: Remove .eggs directory in override_dh_auto_clean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
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
5
 
#
6
 
#         http://www.apache.org/licenses/LICENSE-2.0
7
 
#
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
12
 
#    under the License.
13
 
 
14
 
import mock
15
 
import six
16
 
 
17
 
from keystoneclient import exceptions as keystone_exceptions
18
 
 
19
 
from heat.common import exception
20
 
from heat.engine.clients.os import keystone as client
21
 
from heat.tests import common
22
 
 
23
 
 
24
 
class KeystoneRoleConstraintTest(common.HeatTestCase):
25
 
 
26
 
    def test_expected_exceptions(self):
27
 
        self.assertEqual((exception.EntityNotFound,),
28
 
                         client.KeystoneRoleConstraint.expected_exceptions,
29
 
                         "KeystoneRoleConstraint expected exceptions error")
30
 
 
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
37
 
 
38
 
        self.assertIsNone(constrain.validate_with_client(client_mock,
39
 
                                                         'role_1'))
40
 
 
41
 
        client_plugin_mock.get_role_id.assert_called_once_with('role_1')
42
 
 
43
 
 
44
 
class KeystoneProjectConstraintTest(common.HeatTestCase):
45
 
 
46
 
    def test_expected_exceptions(self):
47
 
        self.assertEqual((exception.EntityNotFound,),
48
 
                         client.KeystoneProjectConstraint.expected_exceptions,
49
 
                         "KeystoneProjectConstraint expected exceptions error")
50
 
 
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
57
 
 
58
 
        self.assertIsNone(constrain.validate_with_client(client_mock,
59
 
                                                         'project_1'))
60
 
 
61
 
        client_plugin_mock.get_project_id.assert_called_once_with('project_1')
62
 
 
63
 
 
64
 
class KeystoneGroupConstraintTest(common.HeatTestCase):
65
 
 
66
 
    def test_expected_exceptions(self):
67
 
        self.assertEqual((exception.EntityNotFound,),
68
 
                         client.KeystoneGroupConstraint.expected_exceptions,
69
 
                         "KeystoneGroupConstraint expected exceptions error")
70
 
 
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
77
 
 
78
 
        self.assertIsNone(constrain.validate_with_client(client_mock,
79
 
                                                         'group_1'))
80
 
 
81
 
        client_plugin_mock.get_group_id.assert_called_once_with('group_1')
82
 
 
83
 
 
84
 
class KeystoneDomainConstraintTest(common.HeatTestCase):
85
 
 
86
 
    def test_expected_exceptions(self):
87
 
        self.assertEqual((exception.EntityNotFound,),
88
 
                         client.KeystoneDomainConstraint.expected_exceptions,
89
 
                         "KeystoneDomainConstraint expected exceptions error")
90
 
 
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
97
 
 
98
 
        self.assertIsNone(constrain.validate_with_client(client_mock,
99
 
                                                         'domain_1'))
100
 
 
101
 
        client_plugin_mock.get_domain_id.assert_called_once_with('domain_1')
102
 
 
103
 
 
104
 
class KeystoneServiceConstraintTest(common.HeatTestCase):
105
 
 
106
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
107
 
 
108
 
    def test_expected_exceptions(self):
109
 
        self.assertEqual((exception.EntityNotFound,
110
 
                          exception.KeystoneServiceNameConflict,),
111
 
                         client.KeystoneServiceConstraint.expected_exceptions,
112
 
                         "KeystoneServiceConstraint expected exceptions error")
113
 
 
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
120
 
 
121
 
        self.assertIsNone(constrain.validate_with_client(client_mock,
122
 
                                                         self.sample_uuid))
123
 
 
124
 
        client_plugin_mock.get_service_id.assert_called_once_with(
125
 
            self.sample_uuid
126
 
        )
127
 
 
128
 
 
129
 
class KeystoneClientPluginServiceTest(common.HeatTestCase):
130
 
 
131
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
132
 
    sample_name = 'sample_service'
133
 
 
134
 
    def _get_mock_service(self):
135
 
        srv = mock.MagicMock()
136
 
        srv.id = self.sample_uuid
137
 
        srv.name = self.sample_name
138
 
        return srv
139
 
 
140
 
    def setUp(self):
141
 
        super(KeystoneClientPluginServiceTest, self).setUp()
142
 
        self._client = mock.MagicMock()
143
 
 
144
 
    @mock.patch.object(client.KeystoneClientPlugin, 'client')
145
 
    def test_get_service_id(self, client_keystone):
146
 
 
147
 
        self._client.client.services.get.return_value = (self
148
 
                                                         ._get_mock_service())
149
 
 
150
 
        client_keystone.return_value = self._client
151
 
        client_plugin = client.KeystoneClientPlugin(
152
 
            context=mock.MagicMock()
153
 
        )
154
 
 
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(
158
 
            self.sample_uuid)
159
 
 
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
163
 
                                                        .NotFound)
164
 
        self._client.client.services.list.return_value = [
165
 
            self._get_mock_service()
166
 
        ]
167
 
 
168
 
        client_keystone.return_value = self._client
169
 
        client_plugin = client.KeystoneClientPlugin(
170
 
            context=mock.MagicMock()
171
 
        )
172
 
 
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,
177
 
                          self.sample_name)
178
 
        self._client.client.services.list.assert_called_once_with(
179
 
            name=self.sample_name)
180
 
 
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
184
 
                                                        .NotFound)
185
 
        self._client.client.services.list.return_value = [
186
 
            self._get_mock_service(),
187
 
            self._get_mock_service()
188
 
        ]
189
 
 
190
 
        client_keystone.return_value = self._client
191
 
        client_plugin = client.KeystoneClientPlugin(
192
 
            context=mock.MagicMock()
193
 
        )
194
 
 
195
 
        ex = self.assertRaises(exception.KeystoneServiceNameConflict,
196
 
                               client_plugin.get_service_id,
197
 
                               self.sample_name)
198
 
        msg = ("Keystone has more than one service with same name "
199
 
               "%s. Please use service id instead of name" %
200
 
               self.sample_name)
201
 
        self.assertEqual(msg, six.text_type(ex))
202
 
        self.assertRaises(keystone_exceptions.NotFound,
203
 
                          self._client.client.services.get,
204
 
                          self.sample_name)
205
 
        self._client.client.services.list.assert_called_once_with(
206
 
            name=self.sample_name)
207
 
 
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
211
 
                                                        .NotFound)
212
 
        self._client.client.services.list.return_value = [
213
 
        ]
214
 
 
215
 
        client_keystone.return_value = self._client
216
 
        client_plugin = client.KeystoneClientPlugin(
217
 
            context=mock.MagicMock()
218
 
        )
219
 
 
220
 
        ex = self.assertRaises(exception.EntityNotFound,
221
 
                               client_plugin.get_service_id,
222
 
                               self.sample_name)
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,
228
 
                          self.sample_name)
229
 
        self._client.client.services.list.assert_called_once_with(
230
 
            name=self.sample_name)
231
 
 
232
 
 
233
 
class KeystoneClientPluginRoleTest(common.HeatTestCase):
234
 
 
235
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
236
 
    sample_name = 'sample_role'
237
 
 
238
 
    def _get_mock_role(self):
239
 
        role = mock.MagicMock()
240
 
        role.id = self.sample_uuid
241
 
        role.name = self.sample_name
242
 
        return role
243
 
 
244
 
    def setUp(self):
245
 
        super(KeystoneClientPluginRoleTest, self).setUp()
246
 
        self._client = mock.MagicMock()
247
 
 
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
251
 
                                                      ._get_mock_role())
252
 
 
253
 
        client_keystone.return_value = self._client
254
 
        client_plugin = client.KeystoneClientPlugin(
255
 
            context=mock.MagicMock()
256
 
        )
257
 
 
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(
261
 
            self.sample_uuid)
262
 
 
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
266
 
                                                     .NotFound)
267
 
        self._client.client.roles.list.return_value = [
268
 
            self._get_mock_role()
269
 
        ]
270
 
 
271
 
        client_keystone.return_value = self._client
272
 
        client_plugin = client.KeystoneClientPlugin(
273
 
            context=mock.MagicMock()
274
 
        )
275
 
 
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,
280
 
                          self.sample_name)
281
 
        self._client.client.roles.list.assert_called_once_with(
282
 
            name=self.sample_name)
283
 
 
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
287
 
                                                     .NotFound)
288
 
        self._client.client.roles.list.return_value = [
289
 
        ]
290
 
 
291
 
        client_keystone.return_value = self._client
292
 
        client_plugin = client.KeystoneClientPlugin(
293
 
            context=mock.MagicMock()
294
 
        )
295
 
 
296
 
        ex = self.assertRaises(exception.EntityNotFound,
297
 
                               client_plugin.get_role_id,
298
 
                               self.sample_name)
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,
304
 
                          self.sample_name)
305
 
        self._client.client.roles.list.assert_called_once_with(
306
 
            name=self.sample_name)
307
 
 
308
 
 
309
 
class KeystoneClientPluginProjectTest(common.HeatTestCase):
310
 
 
311
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
312
 
    sample_name = 'sample_project'
313
 
 
314
 
    def _get_mock_project(self):
315
 
        project = mock.MagicMock()
316
 
        project.id = self.sample_uuid
317
 
        project.name = self.sample_name
318
 
        return project
319
 
 
320
 
    def setUp(self):
321
 
        super(KeystoneClientPluginProjectTest, self).setUp()
322
 
        self._client = mock.MagicMock()
323
 
 
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())
328
 
 
329
 
        client_keystone.return_value = self._client
330
 
        client_plugin = client.KeystoneClientPlugin(
331
 
            context=mock.MagicMock()
332
 
        )
333
 
 
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(
337
 
            self.sample_uuid)
338
 
 
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
342
 
                                                        .NotFound)
343
 
        self._client.client.projects.list.return_value = [
344
 
            self._get_mock_project()
345
 
        ]
346
 
 
347
 
        client_keystone.return_value = self._client
348
 
        client_plugin = client.KeystoneClientPlugin(
349
 
            context=mock.MagicMock()
350
 
        )
351
 
 
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,
356
 
                          self.sample_name)
357
 
        self._client.client.projects.list.assert_called_once_with(
358
 
            name=self.sample_name)
359
 
 
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
363
 
                                                        .NotFound)
364
 
        self._client.client.projects.list.return_value = [
365
 
        ]
366
 
 
367
 
        client_keystone.return_value = self._client
368
 
        client_plugin = client.KeystoneClientPlugin(
369
 
            context=mock.MagicMock()
370
 
        )
371
 
 
372
 
        ex = self.assertRaises(exception.EntityNotFound,
373
 
                               client_plugin.get_project_id,
374
 
                               self.sample_name)
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,
380
 
                          self.sample_name)
381
 
        self._client.client.projects.list.assert_called_once_with(
382
 
            name=self.sample_name)
383
 
 
384
 
 
385
 
class KeystoneClientPluginDomainTest(common.HeatTestCase):
386
 
 
387
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
388
 
    sample_name = 'sample_domain'
389
 
 
390
 
    def _get_mock_domain(self):
391
 
        domain = mock.MagicMock()
392
 
        domain.id = self.sample_uuid
393
 
        domain.name = self.sample_name
394
 
        return domain
395
 
 
396
 
    def setUp(self):
397
 
        super(KeystoneClientPluginDomainTest, self).setUp()
398
 
        self._client = mock.MagicMock()
399
 
 
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
403
 
                                                        ._get_mock_domain())
404
 
 
405
 
        client_keystone.return_value = self._client
406
 
        client_plugin = client.KeystoneClientPlugin(
407
 
            context=mock.MagicMock()
408
 
        )
409
 
 
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(
413
 
            self.sample_uuid)
414
 
 
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
418
 
                                                       .NotFound)
419
 
        self._client.client.domains.list.return_value = [
420
 
            self._get_mock_domain()
421
 
        ]
422
 
 
423
 
        client_keystone.return_value = self._client
424
 
        client_plugin = client.KeystoneClientPlugin(
425
 
            context=mock.MagicMock()
426
 
        )
427
 
 
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,
432
 
                          self.sample_name)
433
 
        self._client.client.domains.list.assert_called_once_with(
434
 
            name=self.sample_name)
435
 
 
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
439
 
                                                       .NotFound)
440
 
        self._client.client.domains.list.return_value = [
441
 
        ]
442
 
 
443
 
        client_keystone.return_value = self._client
444
 
        client_plugin = client.KeystoneClientPlugin(
445
 
            context=mock.MagicMock()
446
 
        )
447
 
 
448
 
        ex = self.assertRaises(exception.EntityNotFound,
449
 
                               client_plugin.get_domain_id,
450
 
                               self.sample_name)
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,
456
 
                          self.sample_name)
457
 
        self._client.client.domains.list.assert_called_once_with(
458
 
            name=self.sample_name)
459
 
 
460
 
 
461
 
class KeystoneClientPluginGroupTest(common.HeatTestCase):
462
 
 
463
 
    sample_uuid = '477e8273-60a7-4c41-b683-fdb0bc7cd152'
464
 
    sample_name = 'sample_group'
465
 
 
466
 
    def _get_mock_group(self):
467
 
        group = mock.MagicMock()
468
 
        group.id = self.sample_uuid
469
 
        group.name = self.sample_name
470
 
        return group
471
 
 
472
 
    def setUp(self):
473
 
        super(KeystoneClientPluginGroupTest, self).setUp()
474
 
        self._client = mock.MagicMock()
475
 
 
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
479
 
                                                       ._get_mock_group())
480
 
 
481
 
        client_keystone.return_value = self._client
482
 
        client_plugin = client.KeystoneClientPlugin(
483
 
            context=mock.MagicMock()
484
 
        )
485
 
 
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(
489
 
            self.sample_uuid)
490
 
 
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
494
 
                                                      .NotFound)
495
 
        self._client.client.groups.list.return_value = [
496
 
            self._get_mock_group()
497
 
        ]
498
 
 
499
 
        client_keystone.return_value = self._client
500
 
        client_plugin = client.KeystoneClientPlugin(
501
 
            context=mock.MagicMock()
502
 
        )
503
 
 
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,
508
 
                          self.sample_name)
509
 
        self._client.client.groups.list.assert_called_once_with(
510
 
            name=self.sample_name)
511
 
 
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
515
 
                                                      .NotFound)
516
 
        self._client.client.groups.list.return_value = [
517
 
        ]
518
 
 
519
 
        client_keystone.return_value = self._client
520
 
        client_plugin = client.KeystoneClientPlugin(
521
 
            context=mock.MagicMock()
522
 
        )
523
 
 
524
 
        ex = self.assertRaises(exception.EntityNotFound,
525
 
                               client_plugin.get_group_id,
526
 
                               self.sample_name)
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,
532
 
                          self.sample_name)
533
 
        self._client.client.groups.list.assert_called_once_with(
534
 
            name=self.sample_name)