summaryrefslogtreecommitdiff
path: root/heat/tests/engine/test_resource_type.py
blob: db3023bc15793abb78a752961c5fbd104264fe7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock

from heat.common import exception
from heat.engine import environment
from heat.engine import resource as res
from heat.engine import service
from heat.tests import common
from heat.tests import generic_resource as generic_rsrc
from heat.tests import utils


class ResourceTypeTest(common.HeatTestCase):

    def setUp(self):
        super(ResourceTypeTest, self).setUp()
        self.ctx = utils.dummy_context(tenant_id='stack_service_test_tenant')
        self.eng = service.EngineService('a-host', 'a-topic')

    @mock.patch.object(res.Resource, 'is_service_available')
    def test_list_resource_types(self, mock_is_service_available):
        mock_is_service_available.return_value = (True, None)
        resources = self.eng.list_resource_types(self.ctx)
        self.assertIsInstance(resources, list)
        self.assertIn('AWS::EC2::Instance', resources)
        self.assertIn('AWS::RDS::DBInstance', resources)

    @mock.patch.object(res.Resource, 'is_service_available')
    def test_list_resource_types_deprecated(self,
                                            mock_is_service_available):
        mock_is_service_available.return_value = (True, None)
        resources = self.eng.list_resource_types(self.ctx, "DEPRECATED")
        self.assertEqual(set(['OS::Aodh::Alarm',
                              'OS::Glance::Image']),
                         set(resources))

    @mock.patch.object(res.Resource, 'is_service_available')
    def test_list_resource_types_supported(self,
                                           mock_is_service_available):
        mock_is_service_available.return_value = (True, None)
        resources = self.eng.list_resource_types(self.ctx, "SUPPORTED")
        self.assertNotIn(['OS::Neutron::RouterGateway'], resources)
        self.assertIn('AWS::EC2::Instance', resources)

    @mock.patch.object(res.Resource, 'is_service_available')
    def test_list_resource_types_unavailable(
            self,
            mock_is_service_available):
        mock_is_service_available.return_value = (
            False, 'Service endpoint not in service catalog.')
        resources = self.eng.list_resource_types(self.ctx)
        # Check for a known resource, not listed
        self.assertNotIn('OS::Nova::Server', resources)

    @mock.patch.object(res.Resource, 'is_service_available')
    def test_list_resource_types_with_descr(self, mock_is_service_available):
        mock_is_service_available.return_value = (True, None)
        resources = self.eng.list_resource_types(self.ctx,
                                                 with_description=True)
        self.assertIsInstance(resources, list)
        self.assertIn({'resource_type': 'AWS::RDS::DBInstance',
                       'description': 'Builtin AWS::RDS::DBInstance'},
                      resources)
        self.assertIn({'resource_type': 'AWS::EC2::Instance',
                       'description': 'No description available'},
                      resources)

    def test_resource_schema(self):
        type_name = 'ResourceWithPropsType'
        expected = {
            'resource_type': type_name,
            'properties': {
                'Foo': {
                    'type': 'string',
                    'required': False,
                    'update_allowed': False,
                    'immutable': False,
                },
                'FooInt': {
                    'type': 'integer',
                    'required': False,
                    'update_allowed': False,
                    'immutable': False,
                },
            },
            'attributes': {
                'foo': {'description': 'A generic attribute'},
                'Foo': {'description': 'Another generic attribute'},
                'show': {
                    'description': 'Detailed information about resource.',
                    'type': 'map'},
            },
            'support_status': {
                'status': 'SUPPORTED',
                'version': None,
                'message': None,
                'previous_status': None
            },
            'description': 'No description available'
        }

        schema = self.eng.resource_schema(self.ctx, type_name=type_name,
                                          with_description=True)
        self.assertEqual(expected, schema)

    def test_resource_schema_for_hidden_type(self):
        type_name = 'ResourceTypeHidden'
        self.assertRaises(exception.NotSupported, self.eng.resource_schema,
                          self.ctx, type_name)

    def test_generate_template_for_hidden_type(self):
        type_name = 'ResourceTypeHidden'
        self.assertRaises(exception.NotSupported, self.eng.generate_template,
                          self.ctx, type_name)

    def test_resource_schema_with_attr_type(self):

        type_name = 'ResourceWithAttributeType'
        expected = {
            'resource_type': type_name,
            'properties': {},
            'attributes': {
                'attr1': {'description': 'A generic attribute',
                          'type': 'string'},
                'attr2': {'description': 'Another generic attribute',
                          'type': 'map'},
                'show': {
                    'description': 'Detailed information about resource.',
                    'type': 'map'},
            },
            'support_status': {
                'status': 'SUPPORTED',
                'version': None,
                'message': None,
                'previous_status': None
            }
        }
        schema = self.eng.resource_schema(self.ctx, type_name=type_name)
        self.assertEqual(expected, schema)

    def test_resource_schema_with_hidden(self):

        type_name = 'ResourceWithHiddenPropertyAndAttribute'
        expected = {
            'resource_type': type_name,
            'properties': {
                'supported': {
                    'description': "Supported property.",
                    'type': 'list',
                    'immutable': False,
                    'required': False,
                    'update_allowed': False
                }
            },
            'attributes': {
                'supported': {'description': 'Supported attribute.',
                              'type': 'string'},
                'show': {
                    'description': 'Detailed information about resource.',
                    'type': 'map'},
            },
            'support_status': {
                'status': 'SUPPORTED',
                'version': None,
                'message': None,
                'previous_status': None
            }
        }
        schema = self.eng.resource_schema(self.ctx, type_name=type_name)
        self.assertEqual(expected, schema)

    def _no_template_file(self, function):
        env = environment.Environment()
        info = environment.ResourceInfo(env.registry,
                                        ['ResourceWithWrongRefOnFile'],
                                        'not_existing.yaml')
        mock_iterable = mock.MagicMock(return_value=iter([info]))
        with mock.patch('heat.engine.environment.ResourceRegistry.iterable_by',
                        new=mock_iterable):
            ex = self.assertRaises(exception.InvalidGlobalResource,
                                   function,
                                   self.ctx,
                                   type_name='ResourceWithWrongRefOnFile')
            msg = ('There was an error loading the definition of the global '
                   'resource type ResourceWithWrongRefOnFile.')
            self.assertIn(msg, str(ex))

    def test_resource_schema_no_template_file(self):
        self._no_template_file(self.eng.resource_schema)

    def test_generate_template_no_template_file(self):
        self._no_template_file(self.eng.generate_template)

    def test_resource_schema_nonexist(self):
        ex = self.assertRaises(exception.EntityNotFound,
                               self.eng.resource_schema,
                               self.ctx, type_name='Bogus')
        msg = 'The Resource Type (Bogus) could not be found.'
        self.assertEqual(msg, str(ex))

    def test_resource_schema_unavailable(self):
        type_name = 'ResourceWithDefaultClientName'

        with mock.patch.object(
                generic_rsrc.ResourceWithDefaultClientName,
                'is_service_available') as mock_is_service_available:
            mock_is_service_available.return_value = (
                False, 'Service endpoint not in service catalog.')
            ex = self.assertRaises(exception.ResourceTypeUnavailable,
                                   self.eng.resource_schema,
                                   self.ctx,
                                   type_name)
            msg = ('HEAT-E99001 Service sample is not available for resource '
                   'type ResourceWithDefaultClientName, reason: '
                   'Service endpoint not in service catalog.')
            self.assertEqual(msg,
                             str(ex),
                             'invalid exception message')

            mock_is_service_available.assert_called_once_with(self.ctx)