~ubuntu-branches/ubuntu/trusty/heat/trusty-security

« back to all changes in this revision

Viewing changes to heat/tests/test_hot.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-10-03 09:43:04 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20131003094304-k2c4qcsfn7cv6eos
Tags: 2013.2~rc1-0ubuntu1
* New upstream release.
* debian/control: Dropped python-d2to1 build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
            self.fail('Expected KeyError for invalid section')
47
47
 
48
48
        # test defaults for valid sections
49
 
        self.assertEquals(tmpl[hot.VERSION], '2013-05-23')
50
 
        self.assertEquals(tmpl[hot.DESCRIPTION], 'No description')
51
 
        self.assertEquals(tmpl[hot.PARAMETERS], {})
52
 
        self.assertEquals(tmpl[hot.RESOURCES], {})
53
 
        self.assertEquals(tmpl[hot.OUTPUTS], {})
 
49
        self.assertEqual(tmpl[hot.VERSION], '2013-05-23')
 
50
        self.assertEqual(tmpl[hot.DESCRIPTION], 'No description')
 
51
        self.assertEqual(tmpl[hot.PARAMETERS], {})
 
52
        self.assertEqual(tmpl[hot.RESOURCES], {})
 
53
        self.assertEqual(tmpl[hot.OUTPUTS], {})
54
54
 
55
55
    def test_translate_parameters(self):
56
56
        """Test translation of parameters into internal engine format."""
92
92
        tmpl = parser.Template(hot_tpl)
93
93
        self.assertEqual(tmpl[hot.PARAMETERS], expected)
94
94
 
95
 
    def test_translate_parameters_length_range(self):
96
 
        hot_tpl = template_format.parse('''
97
 
        heat_template_version: 2013-05-23
98
 
        parameters:
99
 
          wait_time:
100
 
            description: application wait time
101
 
            type: number
102
 
            default: 150
103
 
            constraints:
104
 
              - range: { min: 120, max: 600}
105
 
                description: min value 120 seconds, max value 600 seconds
106
 
          key_name:
107
 
            description: Name of an existing EC2 KeyPair
108
 
            type: string
109
 
            default: heat_key
110
 
            constraints:
111
 
              - length: {min: 1, max: 32}
112
 
                description: length should be between 1 and 32
113
 
        ''')
114
 
 
115
 
        expected = {
116
 
            'wait_time': {
117
 
                'Description': 'application wait time',
118
 
                'Type': 'Number',
119
 
                'Default': 150,
120
 
                'MaxValue': [
121
 
                    (600, 'min value 120 seconds, max value 600 seconds')],
122
 
                'MinValue': [
123
 
                    (120, 'min value 120 seconds, max value 600 seconds')]
124
 
            },
125
 
            'key_name': {
126
 
                'Description': 'Name of an existing EC2 KeyPair',
127
 
                'Type': 'String',
128
 
                'Default': 'heat_key',
129
 
                'MaxLength': [(32, u'length should be between 1 and 32')],
130
 
                'MinLength': [(1, u'length should be between 1 and 32')]
131
 
            }}
132
 
 
133
 
        tmpl = parser.Template(hot_tpl)
134
 
        self.assertEqual(expected, tmpl[hot.PARAMETERS])
135
 
 
136
 
    def test_translate_parameters_allowed_values(self):
137
 
        hot_tpl = template_format.parse('''
138
 
        heat_template_version: 2013-05-23
139
 
        parameters:
140
 
          instance_type:
141
 
            description: instance type
142
 
            type: string
143
 
            default: m1.small
144
 
            constraints:
145
 
              - allowed_values: ["m1.tiny",
146
 
                                 "m1.small",
147
 
                                 "m1.medium", "m1.large", "m1.xlarge"]
148
 
                description: must be a valid EC2 instance type.
149
 
        ''')
150
 
        expected = {
151
 
            'instance_type': {
152
 
                'Description': 'instance type',
153
 
                'Type': 'String',
154
 
                'Default': 'm1.small',
155
 
                'AllowedValues': [(["m1.tiny",
156
 
                                    "m1.small",
157
 
                                    "m1.medium",
158
 
                                    "m1.large",
159
 
                                    "m1.xlarge"],
160
 
                                   'must be a valid EC2 instance type.')]}}
161
 
 
162
 
        tmpl = parser.Template(hot_tpl)
163
 
        self.assertEqual(expected, tmpl[hot.PARAMETERS])
164
 
 
165
 
    def test_translate_parameters_allowed_patterns(self):
166
 
        hot_tpl = template_format.parse('''
167
 
        heat_template_version: 2013-05-23
168
 
        parameters:
169
 
          db_name:
170
 
            description: The WordPress database name
171
 
            type: string
172
 
            default: wordpress
173
 
            constraints:
174
 
              - length: { min: 1, max: 64 }
175
 
                description: string lenght should between 1 and 64
176
 
              - allowed_pattern: "[a-zA-Z]+"
177
 
                description: Value must consist of characters only
178
 
              - allowed_pattern: "[a-z]+[a-zA-Z]*"
179
 
                description: Value must start with a lowercase character
180
 
        ''')
181
 
        expected = {
182
 
            'db_name': {
183
 
                'Description': 'The WordPress database name',
184
 
                'Type': 'String',
185
 
                'Default': 'wordpress',
186
 
                'MinLength': [(1, 'string lenght should between 1 and 64')],
187
 
                'MaxLength': [(64, 'string lenght should between 1 and 64')],
188
 
                'AllowedPattern': [
189
 
                    ('[a-zA-Z]+',
190
 
                     'Value must consist of characters only'),
191
 
                    ('[a-z]+[a-zA-Z]*',
192
 
                     'Value must start with a lowercase character')]}}
193
 
        tmpl = parser.Template(hot_tpl)
194
 
        self.assertEqual(expected, tmpl[hot.PARAMETERS])
195
 
 
196
95
    def test_translate_parameters_hidden(self):
197
96
        hot_tpl = template_format.parse('''
198
97
        heat_template_version: 2013-05-23
258
157
        tmpl = parser.Template(hot_tpl_empty)
259
158
        self.assertEqual(tmpl.resolve_param_refs(snippet, params),
260
159
                         snippet_resolved)
 
160
        snippet = {'properties': {'key1': {'Ref': 'foo'},
 
161
                                  'key2': {'Ref': 'blarg'}}}
 
162
        snippet_resolved = {'properties': {'key1': 'bar',
 
163
                                           'key2': 'wibble'}}
 
164
        tmpl = parser.Template(hot_tpl_empty)
 
165
        self.assertEqual(snippet_resolved,
 
166
                         tmpl.resolve_param_refs(snippet, params))
261
167
 
262
168
    def test_str_replace(self):
263
169
        """Test str_replace function."""
264
170
 
265
 
        snippet = {'str_replace': {'template': 'Template $var1 string $var2',
 
171
        snippet = {'str_replace': {'template': 'Template var1 string var2',
266
172
                                   'params': {'var1': 'foo', 'var2': 'bar'}}}
267
173
        snippet_resolved = 'Template foo string bar'
268
174
 
269
175
        tmpl = parser.Template(hot_tpl_empty)
270
176
 
 
177
        self.assertEqual(snippet_resolved,
 
178
                         tmpl.resolve_replace(snippet))
 
179
 
 
180
    def test_str_fn_replace(self):
 
181
        """Test Fn:Replace function."""
 
182
 
 
183
        snippet = {'Fn::Replace': [{'$var1': 'foo', '$var2': 'bar'},
 
184
                                   'Template $var1 string $var2']}
 
185
        snippet_resolved = 'Template foo string bar'
 
186
 
 
187
        tmpl = parser.Template(hot_tpl_empty)
 
188
 
271
189
        self.assertEqual(tmpl.resolve_replace(snippet), snippet_resolved)
272
190
 
273
191
    def test_str_replace_syntax(self):
278
196
        validate that we get a TypeError.
279
197
        """
280
198
 
281
 
        snippet = {'str_replace': [{'template': 'Template $var1 string $var2'},
 
199
        snippet = {'str_replace': [{'template': 'Template var1 string var2'},
282
200
                                   {'params': {'var1': 'foo', 'var2': 'bar'}}]}
283
201
 
284
202
        tmpl = parser.Template(hot_tpl_empty)
293
211
        a KeyError.
294
212
        """
295
213
 
296
 
        snippet = {'str_replace': {'tmpl': 'Template $var1 string $var2',
 
214
        snippet = {'str_replace': {'tmpl': 'Template var1 string var2',
297
215
                                   'params': {'var1': 'foo', 'var2': 'bar'}}}
298
216
 
299
217
        tmpl = parser.Template(hot_tpl_empty)
300
218
 
301
219
        self.assertRaises(KeyError, tmpl.resolve_replace, snippet)
302
220
 
303
 
        snippet = {'str_replace': {'tmpl': 'Template $var1 string $var2',
 
221
        snippet = {'str_replace': {'tmpl': 'Template var1 string var2',
304
222
                                   'parms': {'var1': 'foo', 'var2': 'bar'}}}
305
223
 
306
224
        self.assertRaises(KeyError, tmpl.resolve_replace, snippet)
320
238
 
321
239
        self.assertRaises(TypeError, tmpl.resolve_replace, snippet)
322
240
 
323
 
        snippet = {'str_replace': {'template': 'Template $var1 string $var2',
 
241
        snippet = {'str_replace': {'template': 'Template var1 string var2',
324
242
                                   'params': ['var1', 'foo', 'var2', 'bar']}}
325
243
 
326
244
        self.assertRaises(TypeError, tmpl.resolve_replace, snippet)
348
266
                         (parser.Stack.CREATE, parser.Stack.COMPLETE))
349
267
 
350
268
        snippet = {'Value': {'get_attr': ['resource1', 'foo']}}
351
 
        resolved = hot.HOTemplate.resolve_attributes(snippet, self.stack)
352
 
        # GenericResourceType has an attribute 'foo' which yields the resource
353
 
        # name.
354
 
        self.assertEqual(resolved, {'Value': 'resource1'})
355
 
        # test invalid reference
 
269
        rsrc = self.stack['resource1']
 
270
        for action, status in (
 
271
                (rsrc.CREATE, rsrc.IN_PROGRESS),
 
272
                (rsrc.CREATE, rsrc.COMPLETE),
 
273
                (rsrc.RESUME, rsrc.IN_PROGRESS),
 
274
                (rsrc.RESUME, rsrc.COMPLETE),
 
275
                (rsrc.UPDATE, rsrc.IN_PROGRESS),
 
276
                (rsrc.UPDATE, rsrc.COMPLETE)):
 
277
            rsrc.state_set(action, status)
 
278
 
 
279
            resolved = hot.HOTemplate.resolve_attributes(snippet, self.stack)
 
280
            # GenericResourceType has an attribute 'foo' which yields the
 
281
            # resource name.
 
282
            self.assertEqual(resolved, {'Value': 'resource1'})
 
283
            # test invalid reference
356
284
        self.assertRaises(exception.InvalidTemplateAttribute,
357
285
                          hot.HOTemplate.resolve_attributes,
358
286
                          {'Value': {'get_attr': ['resource1', 'NotThere']}},
359
287
                          self.stack)
360
288
 
 
289
        snippet = {'Value': {'Fn::GetAtt': ['resource1', 'foo']}}
 
290
        resolved = hot.HOTemplate.resolve_attributes(snippet, self.stack)
 
291
        self.assertEqual({'Value': 'resource1'}, resolved)
 
292
 
361
293
    @utils.stack_delete_after
362
294
    def test_get_resource(self):
363
295
        """Test resolution of get_resource occurrences in HOT template."""
382
314
 
383
315
 
384
316
class HOTParamValidatorTest(HeatTestCase):
385
 
    "Test HOTParamValidator"
 
317
    """Test HOTParamValidator"""
386
318
 
387
319
    def test_multiple_constraint_descriptions(self):
388
 
        len_desc = 'string length should between 8 and 16'
 
320
        len_desc = 'string length should be between 8 and 16'
389
321
        pattern_desc1 = 'Value must consist of characters only'
390
322
        pattern_desc2 = 'Value must start with a lowercase character'
391
323
        param = {
393
325
                'Description': 'The WordPress database name',
394
326
                'Type': 'String',
395
327
                'Default': 'wordpress',
396
 
                'MinLength': [(8, len_desc)],
397
 
                'MaxLength': [(16, len_desc)],
398
 
                'AllowedPattern': [
399
 
                    ('[a-zA-Z]+', pattern_desc1),
400
 
                    ('[a-z]+[a-zA-Z]*', pattern_desc2)]}}
 
328
                'constraints': [
 
329
                    {'length': {'min': 6, 'max': 16},
 
330
                     'description': len_desc},
 
331
                    {'allowed_pattern': '[a-zA-Z]+',
 
332
                     'description': pattern_desc1},
 
333
                    {'allowed_pattern': '[a-z]+[a-zA-Z]*',
 
334
                     'description': pattern_desc2}]}}
401
335
 
402
336
        name = 'db_name'
403
337
        schema = param['db_name']
404
338
 
405
339
        def v(value):
406
 
            hot.HOTParamSchema(schema).do_check(name, value,
407
 
                                                [parameters.ALLOWED_VALUES,
408
 
                                                 parameters.ALLOWED_PATTERN,
409
 
                                                 parameters.MAX_LENGTH,
410
 
                                                 parameters.MIN_LENGTH])
 
340
            hot.HOTParamSchema(schema).validate(name, value)
411
341
            return True
412
342
 
413
343
        value = 'wp'
433
363
        self.assertTrue(v(value))
434
364
 
435
365
    def test_hot_template_validate_param(self):
436
 
        len_desc = 'string length should between 8 and 16'
 
366
        len_desc = 'string length should be between 8 and 16'
437
367
        pattern_desc1 = 'Value must consist of characters only'
438
368
        pattern_desc2 = 'Value must start with a lowercase character'
439
369
        hot_tpl = template_format.parse('''
478
408
 
479
409
        value = 'abcdefghI'
480
410
        self.assertTrue(run_parameters(value))
 
411
 
 
412
    def test_range_constraint(self):
 
413
        range_desc = 'Value must be between 30000 and 50000'
 
414
        param = {
 
415
            'db_port': {
 
416
                'Description': 'The database port',
 
417
                'Type': 'Number',
 
418
                'Default': 15,
 
419
                'constraints': [
 
420
                    {'range': {'min': 30000, 'max': 50000},
 
421
                     'description': range_desc}]}}
 
422
 
 
423
        name = 'db_port'
 
424
        schema = param['db_port']
 
425
 
 
426
        def v(value):
 
427
            hot.HOTParamSchema(schema).validate(name, value)
 
428
            return True
 
429
 
 
430
        value = 29999
 
431
        err = self.assertRaises(ValueError, v, value)
 
432
        self.assertIn(range_desc, str(err))
 
433
 
 
434
        value = 50001
 
435
        err = self.assertRaises(ValueError, v, value)
 
436
        self.assertIn(range_desc, str(err))
 
437
 
 
438
        value = 30000
 
439
        self.assertTrue(v(value))
 
440
 
 
441
        value = 40000
 
442
        self.assertTrue(v(value))
 
443
 
 
444
        value = 50000
 
445
        self.assertTrue(v(value))