Merge "Configuration for containerized MySQL clients"
[apex-tripleo-heat-templates.git] / tripleo_heat_templates / tests / test_environment_generator.py
1 # Copyright 2015 Red Hat Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import io
16 import tempfile
17
18 import mock
19 from oslotest import base
20 import six
21 import testscenarios
22
23 from tripleo_heat_templates import environment_generator
24
25 load_tests = testscenarios.load_tests_apply_scenarios
26
27 basic_template = '''
28 parameters:
29   FooParam:
30     default: foo
31     description: Foo description
32     type: string
33   BarParam:
34     default: 42
35     description: Bar description
36     type: number
37   EndpointMap:
38     default: {}
39     description: Parameter that should not be included by default
40     type: json
41 resources:
42   # None
43 '''
44 basic_private_template = '''
45 parameters:
46   FooParam:
47     default: foo
48     description: Foo description
49     type: string
50   _BarParam:
51     default: 42
52     description: Bar description
53     type: number
54 resources:
55   # None
56 '''
57 mandatory_template = '''
58 parameters:
59   FooParam:
60     description: Mandatory param
61     type: string
62 resources:
63   # None
64 '''
65 index_template = '''
66 parameters:
67   FooParam:
68     description: Param with %index% as its default
69     type: string
70     default: '%index%'
71 resources:
72   # None
73 '''
74 multiline_template = '''
75 parameters:
76   FooParam:
77     description: |
78       Parameter with
79       multi-line description
80     type: string
81     default: ''
82 resources:
83   # None
84 '''
85
86
87 class GeneratorTestCase(base.BaseTestCase):
88     content_scenarios = [
89         ('basic',
90          {'template': basic_template,
91           'exception': None,
92           'nested_output': '',
93           'input_file': '''environments:
94   -
95     name: basic
96     title: Basic Environment
97     description: Basic description
98     files:
99       foo.yaml:
100         parameters: all
101 ''',
102           'expected_output': '''# title: Basic Environment
103 # description: |
104 #   Basic description
105 parameter_defaults:
106   # Bar description
107   # Type: number
108   BarParam: 42
109
110   # Foo description
111   # Type: string
112   FooParam: foo
113
114 ''',
115           }),
116         ('basic-one-param',
117          {'template': basic_template,
118           'exception': None,
119           'nested_output': '',
120           'input_file': '''environments:
121   -
122     name: basic
123     title: Basic Environment
124     description: Basic description
125     files:
126       foo.yaml:
127         parameters:
128           - FooParam
129 ''',
130           'expected_output': '''# title: Basic Environment
131 # description: |
132 #   Basic description
133 parameter_defaults:
134   # Foo description
135   # Type: string
136   FooParam: foo
137
138 ''',
139           }),
140         ('basic-static-param',
141          {'template': basic_template,
142           'exception': None,
143           'nested_output': '',
144           'input_file': '''environments:
145   -
146     name: basic
147     title: Basic Environment
148     description: Basic description
149     files:
150       foo.yaml:
151         parameters: all
152     static:
153       - BarParam
154 ''',
155           'expected_output': '''# title: Basic Environment
156 # description: |
157 #   Basic description
158 parameter_defaults:
159   # Foo description
160   # Type: string
161   FooParam: foo
162
163   # ******************************************************
164   # Static parameters - these are values that must be
165   # included in the environment but should not be changed.
166   # ******************************************************
167   # Bar description
168   # Type: number
169   BarParam: 42
170
171   # *********************
172   # End static parameters
173   # *********************
174 ''',
175           }),
176         ('basic-static-param-sample',
177          {'template': basic_template,
178           'exception': None,
179           'nested_output': '',
180           'input_file': '''environments:
181   -
182     name: basic
183     title: Basic Environment
184     description: Basic description
185     files:
186       foo.yaml:
187         parameters: all
188     static:
189       - BarParam
190     sample_values:
191       BarParam: 1
192       FooParam: ''
193 ''',
194           'expected_output': '''# title: Basic Environment
195 # description: |
196 #   Basic description
197 parameter_defaults:
198   # Foo description
199   # Type: string
200   FooParam: ''
201
202   # ******************************************************
203   # Static parameters - these are values that must be
204   # included in the environment but should not be changed.
205   # ******************************************************
206   # Bar description
207   # Type: number
208   BarParam: 1
209
210   # *********************
211   # End static parameters
212   # *********************
213 ''',
214           }),
215         ('basic-private',
216          {'template': basic_private_template,
217           'exception': None,
218           'nested_output': '',
219           'input_file': '''environments:
220   -
221     name: basic
222     title: Basic Environment
223     description: Basic description
224     files:
225       foo.yaml:
226         parameters: all
227 ''',
228           'expected_output': '''# title: Basic Environment
229 # description: |
230 #   Basic description
231 parameter_defaults:
232   # Foo description
233   # Type: string
234   FooParam: foo
235
236 ''',
237           }),
238         ('mandatory',
239          {'template': mandatory_template,
240           'exception': None,
241           'nested_output': '',
242           'input_file': '''environments:
243   -
244     name: basic
245     title: Basic Environment
246     description: Basic description
247     files:
248       foo.yaml:
249         parameters: all
250 ''',
251           'expected_output': '''# title: Basic Environment
252 # description: |
253 #   Basic description
254 parameter_defaults:
255   # Mandatory param
256   # Mandatory. This parameter must be set by the user.
257   # Type: string
258   FooParam: <None>
259
260 ''',
261           }),
262         ('basic-sample',
263          {'template': basic_template,
264           'exception': None,
265           'nested_output': '',
266           'input_file': '''environments:
267   -
268     name: basic
269     title: Basic Environment
270     description: Basic description
271     files:
272       foo.yaml:
273         parameters: all
274     sample_values:
275       FooParam: baz
276 ''',
277           'expected_output': '''# title: Basic Environment
278 # description: |
279 #   Basic description
280 parameter_defaults:
281   # Bar description
282   # Type: number
283   BarParam: 42
284
285   # Foo description
286   # Type: string
287   FooParam: baz
288
289 ''',
290           }),
291         ('basic-resource-registry',
292          {'template': basic_template,
293           'exception': None,
294           'nested_output': '',
295           'input_file': '''environments:
296   -
297     name: basic
298     title: Basic Environment
299     description: Basic description
300     files:
301       foo.yaml:
302         parameters: all
303     resource_registry:
304       OS::TripleO::FakeResource: fake-filename.yaml
305 ''',
306           'expected_output': '''# title: Basic Environment
307 # description: |
308 #   Basic description
309 parameter_defaults:
310   # Bar description
311   # Type: number
312   BarParam: 42
313
314   # Foo description
315   # Type: string
316   FooParam: foo
317
318 resource_registry:
319   OS::TripleO::FakeResource: fake-filename.yaml
320 ''',
321           }),
322         ('basic-hidden',
323          {'template': basic_template,
324           'exception': None,
325           'nested_output': '',
326           'input_file': '''environments:
327   -
328     name: basic
329     title: Basic Environment
330     description: Basic description
331     files:
332       foo.yaml:
333         parameters: all
334     sample_values:
335       EndpointMap: |-2
336
337             foo: bar
338 ''',
339           'expected_output': '''# title: Basic Environment
340 # description: |
341 #   Basic description
342 parameter_defaults:
343   # Bar description
344   # Type: number
345   BarParam: 42
346
347   # Parameter that should not be included by default
348   # Type: json
349   EndpointMap:
350     foo: bar
351
352   # Foo description
353   # Type: string
354   FooParam: foo
355
356 ''',
357           }),
358         ('missing-param',
359          {'template': basic_template,
360           'exception': RuntimeError,
361           'nested_output': '',
362           'input_file': '''environments:
363   -
364     name: basic
365     title: Basic Environment
366     description: Basic description
367     files:
368       foo.yaml:
369         parameters:
370           - SomethingNonexistent
371 ''',
372           'expected_output': None,
373           }),
374         ('percent-index',
375          {'template': index_template,
376           'exception': None,
377           'nested_output': '',
378           'input_file': '''environments:
379   -
380     name: basic
381     title: Basic Environment
382     description: Basic description
383     files:
384       foo.yaml:
385         parameters: all
386 ''',
387           'expected_output': '''# title: Basic Environment
388 # description: |
389 #   Basic description
390 parameter_defaults:
391   # Param with %index% as its default
392   # Type: string
393   FooParam: '%index%'
394
395 ''',
396           }),
397         ('nested',
398          {'template': multiline_template,
399           'exception': None,
400           'input_file': '''environments:
401   -
402     name: basic
403     title: Basic Environment
404     description: Basic description
405     files:
406       foo.yaml:
407         parameters: all
408     children:
409       - name: nested
410         title: Nested Environment
411         description: Nested description
412         sample_values:
413           FooParam: bar
414 ''',
415           'expected_output': '''# title: Basic Environment
416 # description: |
417 #   Basic description
418 parameter_defaults:
419   # Parameter with
420   # multi-line description
421   # Type: string
422   FooParam: ''
423
424 ''',
425           'nested_output': '''# title: Nested Environment
426 # description: |
427 #   Nested description
428 parameter_defaults:
429   # Parameter with
430   # multi-line description
431   # Type: string
432   FooParam: bar
433
434 ''',
435           }),
436         ('multi-line-desc',
437          {'template': multiline_template,
438           'exception': None,
439           'nested_output': '',
440           'input_file': '''environments:
441   -
442     name: basic
443     title: Basic Environment
444     description: Basic description
445     files:
446       foo.yaml:
447         parameters: all
448 ''',
449           'expected_output': '''# title: Basic Environment
450 # description: |
451 #   Basic description
452 parameter_defaults:
453   # Parameter with
454   # multi-line description
455   # Type: string
456   FooParam: ''
457
458 ''',
459           }),
460         ]
461
462     @classmethod
463     def generate_scenarios(cls):
464         cls.scenarios = testscenarios.multiply_scenarios(
465             cls.content_scenarios)
466
467     def test_generator(self):
468         fake_input = io.StringIO(six.text_type(self.input_file))
469         fake_template = io.StringIO(six.text_type(self.template))
470         _, fake_output_path = tempfile.mkstemp()
471         fake_output = open(fake_output_path, 'w')
472         with mock.patch('tripleo_heat_templates.environment_generator.open',
473                         create=True) as mock_open:
474             mock_se = [fake_input, fake_template, fake_output]
475             if self.nested_output:
476                 _, fake_nested_output_path = tempfile.mkstemp()
477                 fake_nested_output = open(fake_nested_output_path, 'w')
478                 fake_template2 = io.StringIO(six.text_type(self.template))
479                 mock_se = [fake_input, fake_template, fake_output,
480                            fake_template2, fake_nested_output]
481             mock_open.side_effect = mock_se
482             if not self.exception:
483                 environment_generator.generate_environments('ignored.yaml')
484             else:
485                 self.assertRaises(self.exception,
486                                   environment_generator.generate_environments,
487                                   'ignored.yaml')
488                 return
489         expected = environment_generator._FILE_HEADER + self.expected_output
490         with open(fake_output_path) as f:
491             self.assertEqual(expected, f.read())
492         if self.nested_output:
493             with open(fake_nested_output_path) as f:
494                 expected = (environment_generator._FILE_HEADER +
495                             self.nested_output)
496                 self.assertEqual(expected, f.read())
497
498 GeneratorTestCase.generate_scenarios()