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

1.1.23 by James Page
Import upstream version 2015.1~b3
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
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
21
22
from ..resources.role import KeystoneRole  # noqa
23
from ..resources.role import resource_mapping  # noqa
24
25
keystone_role_template = {
26
    'heat_template_version': '2013-05-23',
27
    'resources': {
28
        'test_role': {
29
            'type': 'OS::Keystone::Role',
30
            'properties': {
31
                'name': 'test_role_1'
32
            }
33
        }
34
    }
35
}
36
37
RESOURCE_TYPE = 'OS::Keystone::Role'
38
39
40
class KeystoneRoleTest(common.HeatTestCase):
41
    def setUp(self):
42
        super(KeystoneRoleTest, self).setUp()
43
44
        self.ctx = utils.dummy_context()
45
46
        # For unit testing purpose. Register resource provider explicitly.
47
        resource._register_class(RESOURCE_TYPE, KeystoneRole)
48
49
        self.stack = parser.Stack(
50
            self.ctx, 'test_stack_keystone',
51
            template.Template(keystone_role_template)
52
        )
53
54
        self.test_role = self.stack['test_role']
55
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
60
61
    def _get_mock_role(self):
62
        value = mock.MagicMock()
63
        role_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
64
        value.id = role_id
65
66
        return value
67
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)
73
74
    def test_role_handle_create(self):
75
        mock_role = self._get_mock_role()
76
        self.roles.create.return_value = mock_role
77
78
        # validate the properties
79
        self.assertEqual('test_role_1',
80
                         self.test_role.properties.get(KeystoneRole.NAME))
81
82
        self.test_role.handle_create()
83
84
        # validate role creation with given name
85
        self.roles.create.assert_called_once_with(name='test_role_1')
86
87
        # validate physical resource id
88
        self.assertEqual(mock_role.id, self.test_role.resource_id)
89
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
95
96
        self.test_role.handle_create()
97
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)
101
102
    def test_role_handle_update(self):
103
        self.test_role.resource_id = '477e8273-60a7-4c41-b683-fdb0bc7cd151'
104
105
        # update the name property
106
        prop_diff = {KeystoneRole.NAME: 'test_role_1_updated'}
107
108
        self.test_role.handle_update(json_snippet=None,
109
                                     tmpl_diff=None,
110
                                     prop_diff=prop_diff)
111
112
        self.roles.update.assert_called_once_with(
113
            role=self.test_role.resource_id,
114
            name=prop_diff[KeystoneRole.NAME]
115
        )
116
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
120
121
        self.assertIsNone(self.test_role.handle_delete())
122
        self.roles.delete.assert_called_once_with(
123
            self.test_role.resource_id
124
        )
125
126
    def test_role_handle_delete_resource_id_is_none(self):
127
        self.resource_id = None
128
        self.assertIsNone(self.test_role.handle_delete())
129
130
        assert not self.roles.delete.called
131
132
    def test_role_handle_delete_not_found(self):
133
        exc = self.keystoneclient.NotFound
134
        self.roles.delete.side_effect = exc
135
136
        self.assertIsNone(self.test_role.handle_delete())