summaryrefslogtreecommitdiff
path: root/heat/tests/test_parameters.py
blob: 9a5b5073d06776ec35e4e54677de35a786d7619f (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#
#    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 testtools
import json

from heat.common import exception
from heat.common import identifier
from heat.engine import parameters
from heat.engine import template
from heat.engine import constraints as constr


class ParameterTest(testtools.TestCase):

    def new_parameter(self, name, schema, value=None,
                      validate_value=True):
        tmpl = template.Template({'Parameters': {name: schema}})
        schema = tmpl.param_schemata()[name]
        return parameters.Parameter(name, schema, value,
                                    validate_value)

    def test_new_string(self):
        p = self.new_parameter('p', {'Type': 'String'}, validate_value=False)
        self.assertIsInstance(p, parameters.StringParam)

    def test_new_number(self):
        p = self.new_parameter('p', {'Type': 'Number'}, validate_value=False)
        self.assertIsInstance(p, parameters.NumberParam)

    def test_new_list(self):
        p = self.new_parameter('p', {'Type': 'CommaDelimitedList'},
                               validate_value=False)
        self.assertIsInstance(p, parameters.CommaDelimitedListParam)

    def test_new_json(self):
        p = self.new_parameter('p', {'Type': 'Json'}, validate_value=False)
        self.assertIsInstance(p, parameters.JsonParam)

    def test_new_bad_type(self):
        self.assertRaises(constr.InvalidSchemaError, self.new_parameter, 'p',
                          {'Type': 'List'}, validate_value=False)

    def test_new_no_type(self):
        self.assertRaises(KeyError, self.new_parameter,
                          'p', {'Default': 'blarg'})

    def test_default_no_override(self):
        p = self.new_parameter('defaulted', {'Type': 'String',
                                             'Default': 'blarg'})
        self.assertTrue(p.has_default())
        self.assertEqual('blarg', p.default())
        self.assertEqual('blarg', p.value())

    def test_default_override(self):
        p = self.new_parameter('defaulted',
                               {'Type': 'String',
                                'Default': 'blarg'},
                               'wibble')
        self.assertTrue(p.has_default())
        self.assertEqual('blarg', p.default())
        self.assertEqual('wibble', p.value())

    def test_default_invalid(self):
        schema = {'Type': 'String',
                  'AllowedValues': ['foo'],
                  'ConstraintDescription': 'wibble',
                  'Default': 'bar'}
        err = self.assertRaises(constr.InvalidSchemaError,
                                self.new_parameter, 'p', schema, 'foo')
        self.assertIn('wibble', str(err))

    def test_no_echo_true(self):
        p = self.new_parameter('anechoic',
                               {'Type': 'String',
                                'NoEcho': 'true'},
                               'wibble')
        self.assertTrue(p.hidden())
        self.assertNotEqual(str(p), 'wibble')

    def test_no_echo_true_caps(self):
        p = self.new_parameter('anechoic',
                               {'Type': 'String',
                                'NoEcho': 'TrUe'},
                               'wibble')
        self.assertTrue(p.hidden())
        self.assertNotEqual(str(p), 'wibble')

    def test_no_echo_false(self):
        p = self.new_parameter('echoic',
                               {'Type': 'String',
                                'NoEcho': 'false'},
                               'wibble')
        self.assertFalse(p.hidden())
        self.assertEqual('wibble', str(p))

    def test_description(self):
        description = 'Description of the parameter'
        p = self.new_parameter('p', {'Type': 'String',
                                     'Description': description},
                               validate_value=False)
        self.assertEqual(description, p.description())

    def test_no_description(self):
        p = self.new_parameter('p', {'Type': 'String'}, validate_value=False)
        self.assertEqual('', p.description())

    def test_string_len_good(self):
        schema = {'Type': 'String',
                  'MinLength': '3',
                  'MaxLength': '3'}
        p = self.new_parameter('p', schema, 'foo')
        self.assertEqual('foo', p.value())

    def test_string_underflow(self):
        schema = {'Type': 'String',
                  'ConstraintDescription': 'wibble',
                  'MinLength': '4'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, 'foo')
        self.assertIn('wibble', str(err))

    def test_string_overflow(self):
        schema = {'Type': 'String',
                  'ConstraintDescription': 'wibble',
                  'MaxLength': '2'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, 'foo')
        self.assertIn('wibble', str(err))

    def test_string_pattern_good(self):
        schema = {'Type': 'String',
                  'AllowedPattern': '[a-z]*'}
        p = self.new_parameter('p', schema, 'foo')
        self.assertEqual('foo', p.value())

    def test_string_pattern_bad_prefix(self):
        schema = {'Type': 'String',
                  'ConstraintDescription': 'wibble',
                  'AllowedPattern': '[a-z]*'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, '1foo')
        self.assertIn('wibble', str(err))

    def test_string_pattern_bad_suffix(self):
        schema = {'Type': 'String',
                  'ConstraintDescription': 'wibble',
                  'AllowedPattern': '[a-z]*'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, 'foo1')
        self.assertIn('wibble', str(err))

    def test_string_value_list_good(self):
        schema = {'Type': 'String',
                  'AllowedValues': ['foo', 'bar', 'baz']}
        p = self.new_parameter('p', schema, 'bar')
        self.assertEqual('bar', p.value())

    def test_string_value_list_bad(self):
        schema = {'Type': 'String',
                  'ConstraintDescription': 'wibble',
                  'AllowedValues': ['foo', 'bar', 'baz']}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, 'blarg')
        self.assertIn('wibble', str(err))

    def test_number_int_good(self):
        schema = {'Type': 'Number',
                  'MinValue': '3',
                  'MaxValue': '3'}
        p = self.new_parameter('p', schema, '3')
        self.assertEqual(3, p.value())

    def test_number_float_good(self):
        schema = {'Type': 'Number',
                  'MinValue': '3.0',
                  'MaxValue': '4.0'}
        p = self.new_parameter('p', schema, '3.5')
        self.assertEqual(3.5, p.value())

    def test_number_low(self):
        schema = {'Type': 'Number',
                  'ConstraintDescription': 'wibble',
                  'MinValue': '4'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, '3')
        self.assertIn('wibble', str(err))

    def test_number_high(self):
        schema = {'Type': 'Number',
                  'ConstraintDescription': 'wibble',
                  'MaxValue': '2'}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, '3')
        self.assertIn('wibble', str(err))

    def test_number_value_list_good(self):
        schema = {'Type': 'Number',
                  'AllowedValues': ['1', '3', '5']}
        p = self.new_parameter('p', schema, '5')
        self.assertEqual(5, p.value())

    def test_number_value_list_bad(self):
        schema = {'Type': 'Number',
                  'ConstraintDescription': 'wibble',
                  'AllowedValues': ['1', '3', '5']}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, '2')
        self.assertIn('wibble', str(err))

    def test_list_value_list_good(self):
        schema = {'Type': 'CommaDelimitedList',
                  'AllowedValues': ['foo', 'bar', 'baz']}
        p = self.new_parameter('p', schema, 'baz,foo,bar')
        self.assertEqual('baz,foo,bar'.split(','), p.value())
        schema['Default'] = []
        p = self.new_parameter('p', schema)
        self.assertEqual([], p.value())
        schema['Default'] = 'baz,foo,bar'
        p = self.new_parameter('p', schema)
        self.assertEqual('baz,foo,bar'.split(','), p.value())

    def test_list_value_list_bad(self):
        schema = {'Type': 'CommaDelimitedList',
                  'ConstraintDescription': 'wibble',
                  'AllowedValues': ['foo', 'bar', 'baz']}
        err = self.assertRaises(ValueError, self.new_parameter,
                                'p', schema, 'foo,baz,blarg')
        self.assertIn('wibble', str(err))

    def test_map_value(self):
        '''Happy path for value thats already a map.'''
        schema = {'Type': 'Json'}
        val = {"foo": "bar", "items": [1, 2, 3]}
        p = self.new_parameter('p', schema, val)
        self.assertEqual(val, p.value())
        self.assertEqual(val, p.parsed)

    def test_map_value_bad(self):
        '''Map value is not JSON parsable.'''
        schema = {'Type': 'Json',
                  'ConstraintDescription': 'wibble'}
        val = {"foo": "bar", "not_json": len}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, val)
        self.assertIn('Value must be valid JSON', str(err))

    def test_map_value_parse(self):
        '''Happy path for value that's a string.'''
        schema = {'Type': 'Json'}
        val = {"foo": "bar", "items": [1, 2, 3]}
        val_s = json.dumps(val)
        p = self.new_parameter('p', schema, val_s)
        self.assertEqual(val, p.value())
        self.assertEqual(val, p.parsed)

    def test_map_value_bad_parse(self):
        '''Test value error for unparsable string value.'''
        schema = {'Type': 'Json',
                  'ConstraintDescription': 'wibble'}
        val = "I am not a map"
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, val)
        self.assertIn('Value must be valid JSON', str(err))

    def test_map_underrun(self):
        '''Test map length under MIN_LEN.'''
        schema = {'Type': 'Json',
                  'MinLength': 3}
        val = {"foo": "bar", "items": [1, 2, 3]}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, val)
        self.assertIn('out of range', str(err))

    def test_map_overrun(self):
        '''Test map length over MAX_LEN.'''
        schema = {'Type': 'Json',
                  'MaxLength': 1}
        val = {"foo": "bar", "items": [1, 2, 3]}
        err = self.assertRaises(ValueError,
                                self.new_parameter, 'p', schema, val)
        self.assertIn('out of range', str(err))

    def test_missing_param(self):
        '''Test missing user parameter.'''
        self.assertRaises(exception.UserParameterMissing,
                          self.new_parameter, 'p',
                          {'Type': 'String'})


params_schema = json.loads('''{
  "Parameters" : {
    "User" : { "Type": "String" },
    "Defaulted" : {
      "Type": "String",
      "Default": "foobar"
    }
  }
}''')


class ParametersTest(testtools.TestCase):
    def new_parameters(self, stack_name, tmpl, user_params={}, stack_id=None,
                       validate_value=True):
        tmpl = template.Template(tmpl)
        return tmpl.parameters(
            identifier.HeatIdentifier('', stack_name, stack_id),
            user_params, validate_value)

    def test_pseudo_params(self):
        stack_name = 'test_stack'
        params = self.new_parameters(stack_name, {"Parameters": {}})

        self.assertEqual('test_stack', params['AWS::StackName'])
        self.assertEqual(
            'arn:openstack:heat:::stacks/{0}/{1}'.format(stack_name, 'None'),
            params['AWS::StackId'])

        self.assertIn('AWS::Region', params)

    def test_pseudo_param_stackid(self):
        stack_name = 'test_stack'
        params = self.new_parameters(stack_name, {'Parameters': {}},
                                     stack_id='abc123')

        self.assertEqual(
            'arn:openstack:heat:::stacks/{0}/{1}'.format(stack_name, 'abc123'),
            params['AWS::StackId'])
        stack_identifier = identifier.HeatIdentifier('', '', 'def456')
        params.set_stack_id(stack_identifier)
        self.assertEqual(stack_identifier.arn(), params['AWS::StackId'])

    def test_schema_invariance(self):
        params1 = self.new_parameters('test', params_schema,
                                      {'User': 'foo',
                                       'Defaulted': 'wibble'})
        self.assertEqual('wibble', params1['Defaulted'])

        params2 = self.new_parameters('test', params_schema, {'User': 'foo'})
        self.assertEqual('foobar', params2['Defaulted'])

    def test_to_dict(self):
        template = {'Parameters': {'Foo': {'Type': 'String'},
                                   'Bar': {'Type': 'Number', 'Default': '42'}}}
        params = self.new_parameters('test_params', template, {'Foo': 'foo'})

        as_dict = dict(params)
        self.assertEqual('foo', as_dict['Foo'])
        self.assertEqual(42, as_dict['Bar'])
        self.assertEqual('test_params', as_dict['AWS::StackName'])
        self.assertIn('AWS::Region', as_dict)

    def test_map(self):
        template = {'Parameters': {'Foo': {'Type': 'String'},
                                   'Bar': {'Type': 'Number', 'Default': '42'}}}
        params = self.new_parameters('test_params', template, {'Foo': 'foo'})

        expected = {'Foo': False,
                    'Bar': True,
                    'AWS::Region': True,
                    'AWS::StackId': True,
                    'AWS::StackName': True}

        self.assertEqual(expected, params.map(lambda p: p.has_default()))

    def test_map_str(self):
        template = {'Parameters': {'Foo': {'Type': 'String'},
                                   'Bar': {'Type': 'Number'}}}
        stack_name = 'test_params'
        params = self.new_parameters(stack_name, template,
                                     {'Foo': 'foo', 'Bar': '42'})

        expected = {'Foo': 'foo',
                    'Bar': '42',
                    'AWS::Region': 'ap-southeast-1',
                    'AWS::StackId':
                    'arn:openstack:heat:::stacks/{0}/{1}'.format(
                        stack_name,
                        'None'),
                    'AWS::StackName': 'test_params'}

        self.assertEqual(expected, params.map(str))

    def test_unknown_params(self):
        user_params = {'Foo': 'wibble'}
        self.assertRaises(exception.UnknownUserParameter,
                          self.new_parameters,
                          'test',
                          params_schema,
                          user_params)

    def test_missing_params(self):
        user_params = {}
        self.assertRaises(exception.UserParameterMissing,
                          self.new_parameters,
                          'test',
                          params_schema,
                          user_params)

    def test_missing_attribute_params(self):
        params = {'Parameters': {'Foo': {'Type': 'String'},
                                 'NoAttr': 'No attribute.',
                                 'Bar': {'Type': 'Number', 'Default': '1'}}}
        self.assertRaises(exception.InvalidTemplateParameter,
                          self.new_parameters,
                          'test',
                          params)