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
16
from heat.engine import parser
17
from heat.engine import resource
18
from heat.engine import template
19
from heat.tests import common
20
from heat.tests import utils
22
from ..resources.role import KeystoneRole # noqa
23
from ..resources.role import resource_mapping # noqa
25
keystone_role_template = {
26
'heat_template_version': '2013-05-23',
29
'type': 'OS::Keystone::Role',
37
RESOURCE_TYPE = 'OS::Keystone::Role'
40
class KeystoneRoleTest(common.HeatTestCase):
42
super(KeystoneRoleTest, self).setUp()
44
self.ctx = utils.dummy_context()
46
# For unit testing purpose. Register resource provider explicitly.
47
resource._register_class(RESOURCE_TYPE, KeystoneRole)
49
self.stack = parser.Stack(
50
self.ctx, 'test_stack_keystone',
51
template.Template(keystone_role_template)
54
self.test_role = self.stack['test_role']
56
self.keystoneclient = mock.MagicMock()
57
self.test_role.keystone = mock.MagicMock()
58
self.test_role.keystone.return_value = self.keystoneclient
59
self.roles = self.keystoneclient.client.roles
61
def _get_mock_role(self):
62
value = mock.MagicMock()
63
role_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
68
def test_resource_mapping(self):
69
mapping = resource_mapping()
70
self.assertEqual(1, len(mapping))
71
self.assertEqual(KeystoneRole, mapping[RESOURCE_TYPE])
72
self.assertIsInstance(self.test_role, KeystoneRole)
74
def test_role_handle_create(self):
75
mock_role = self._get_mock_role()
76
self.roles.create.return_value = mock_role
78
# validate the properties
79
self.assertEqual('test_role_1',
80
self.test_role.properties.get(KeystoneRole.NAME))
82
self.test_role.handle_create()
84
# validate role creation with given name
85
self.roles.create.assert_called_once_with(name='test_role_1')
87
# validate physical resource id
88
self.assertEqual(mock_role.id, self.test_role.resource_id)
90
def test_role_handle_create_default_name(self):
91
# reset the NAME value to None, to make sure role is
92
# created with physical_resource_name
93
self.test_role.properties = mock.MagicMock()
94
self.test_role.properties.get.return_value = None
96
self.test_role.handle_create()
98
# validate role creation with default name
99
physical_resource_name = self.test_role.physical_resource_name()
100
self.roles.create.assert_called_once_with(name=physical_resource_name)
102
def test_role_handle_update(self):
103
self.test_role.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
105
# update the name property
106
prop_diff = {KeystoneRole.NAME: 'test_role_1_updated'}
108
self.test_role.handle_update(json_snippet=None,
112
self.roles.update.assert_called_once_with(
113
role=self.test_role.resource_id,
114
name=prop_diff[KeystoneRole.NAME]
117
def test_role_handle_delete(self):
118
self.test_role.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
119
self.roles.delete.return_value = None
121
self.assertIsNone(self.test_role.handle_delete())
122
self.roles.delete.assert_called_once_with(
123
self.test_role.resource_id
126
def test_role_handle_delete_resource_id_is_none(self):
127
self.resource_id = None
128
self.assertIsNone(self.test_role.handle_delete())
130
assert not self.roles.delete.called
132
def test_role_handle_delete_not_found(self):
133
exc = self.keystoneclient.NotFound
134
self.roles.delete.side_effect = exc
136
self.assertIsNone(self.test_role.handle_delete())