Merge "Add storage sample environments"
[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           'input_file': '''environments:
93   -
94     name: basic
95     title: Basic Environment
96     description: Basic description
97     files:
98       foo.yaml:
99         parameters: all
100 ''',
101           'expected_output': '''# title: Basic Environment
102 # description: |
103 #   Basic description
104 parameter_defaults:
105   # Bar description
106   # Type: number
107   BarParam: 42
108
109   # Foo description
110   # Type: string
111   FooParam: foo
112
113 ''',
114           }),
115         ('basic-one-param',
116          {'template': basic_template,
117           'exception': None,
118           'input_file': '''environments:
119   -
120     name: basic
121     title: Basic Environment
122     description: Basic description
123     files:
124       foo.yaml:
125         parameters:
126           - FooParam
127 ''',
128           'expected_output': '''# title: Basic Environment
129 # description: |
130 #   Basic description
131 parameter_defaults:
132   # Foo description
133   # Type: string
134   FooParam: foo
135
136 ''',
137           }),
138         ('basic-static-param',
139          {'template': basic_template,
140           'exception': None,
141           'input_file': '''environments:
142   -
143     name: basic
144     title: Basic Environment
145     description: Basic description
146     files:
147       foo.yaml:
148         parameters: all
149     static:
150       - BarParam
151 ''',
152           'expected_output': '''# title: Basic Environment
153 # description: |
154 #   Basic description
155 parameter_defaults:
156   # Foo description
157   # Type: string
158   FooParam: foo
159
160   # ******************************************************
161   # Static parameters - these are values that must be
162   # included in the environment but should not be changed.
163   # ******************************************************
164   # Bar description
165   # Type: number
166   BarParam: 42
167
168   # *********************
169   # End static parameters
170   # *********************
171 ''',
172           }),
173         ('basic-static-param-sample',
174          {'template': basic_template,
175           'exception': None,
176           'input_file': '''environments:
177   -
178     name: basic
179     title: Basic Environment
180     description: Basic description
181     files:
182       foo.yaml:
183         parameters: all
184     static:
185       - BarParam
186     sample_values:
187       BarParam: 1
188       FooParam: ''
189 ''',
190           'expected_output': '''# title: Basic Environment
191 # description: |
192 #   Basic description
193 parameter_defaults:
194   # Foo description
195   # Type: string
196   FooParam: ''
197
198   # ******************************************************
199   # Static parameters - these are values that must be
200   # included in the environment but should not be changed.
201   # ******************************************************
202   # Bar description
203   # Type: number
204   BarParam: 1
205
206   # *********************
207   # End static parameters
208   # *********************
209 ''',
210           }),
211         ('basic-private',
212          {'template': basic_private_template,
213           'exception': None,
214           'input_file': '''environments:
215   -
216     name: basic
217     title: Basic Environment
218     description: Basic description
219     files:
220       foo.yaml:
221         parameters: all
222 ''',
223           'expected_output': '''# title: Basic Environment
224 # description: |
225 #   Basic description
226 parameter_defaults:
227   # Foo description
228   # Type: string
229   FooParam: foo
230
231 ''',
232           }),
233         ('mandatory',
234          {'template': mandatory_template,
235           'exception': None,
236           'input_file': '''environments:
237   -
238     name: basic
239     title: Basic Environment
240     description: Basic description
241     files:
242       foo.yaml:
243         parameters: all
244 ''',
245           'expected_output': '''# title: Basic Environment
246 # description: |
247 #   Basic description
248 parameter_defaults:
249   # Mandatory param
250   # Mandatory. This parameter must be set by the user.
251   # Type: string
252   FooParam: <None>
253
254 ''',
255           }),
256         ('basic-sample',
257          {'template': basic_template,
258           'exception': None,
259           'input_file': '''environments:
260   -
261     name: basic
262     title: Basic Environment
263     description: Basic description
264     files:
265       foo.yaml:
266         parameters: all
267     sample_values:
268       FooParam: baz
269 ''',
270           'expected_output': '''# title: Basic Environment
271 # description: |
272 #   Basic description
273 parameter_defaults:
274   # Bar description
275   # Type: number
276   BarParam: 42
277
278   # Foo description
279   # Type: string
280   FooParam: baz
281
282 ''',
283           }),
284         ('basic-resource-registry',
285          {'template': basic_template,
286           'exception': None,
287           'input_file': '''environments:
288   -
289     name: basic
290     title: Basic Environment
291     description: Basic description
292     files:
293       foo.yaml:
294         parameters: all
295     resource_registry:
296       OS::TripleO::FakeResource: fake-filename.yaml
297 ''',
298           'expected_output': '''# title: Basic Environment
299 # description: |
300 #   Basic description
301 parameter_defaults:
302   # Bar description
303   # Type: number
304   BarParam: 42
305
306   # Foo description
307   # Type: string
308   FooParam: foo
309
310 resource_registry:
311   OS::TripleO::FakeResource: fake-filename.yaml
312 ''',
313           }),
314         ('basic-hidden',
315          {'template': basic_template,
316           'exception': None,
317           'input_file': '''environments:
318   -
319     name: basic
320     title: Basic Environment
321     description: Basic description
322     files:
323       foo.yaml:
324         parameters: all
325     sample_values:
326       EndpointMap: |-2
327
328             foo: bar
329 ''',
330           'expected_output': '''# title: Basic Environment
331 # description: |
332 #   Basic description
333 parameter_defaults:
334   # Bar description
335   # Type: number
336   BarParam: 42
337
338   # Parameter that should not be included by default
339   # Type: json
340   EndpointMap:
341     foo: bar
342
343   # Foo description
344   # Type: string
345   FooParam: foo
346
347 ''',
348           }),
349         ('missing-param',
350          {'template': basic_template,
351           'exception': RuntimeError,
352           'input_file': '''environments:
353   -
354     name: basic
355     title: Basic Environment
356     description: Basic description
357     files:
358       foo.yaml:
359         parameters:
360           - SomethingNonexistent
361 ''',
362           'expected_output': None,
363           }),
364         ('percent-index',
365          {'template': index_template,
366           'exception': None,
367           'input_file': '''environments:
368   -
369     name: basic
370     title: Basic Environment
371     description: Basic description
372     files:
373       foo.yaml:
374         parameters: all
375 ''',
376           'expected_output': '''# title: Basic Environment
377 # description: |
378 #   Basic description
379 parameter_defaults:
380   # Param with %index% as its default
381   # Type: string
382   FooParam: '%index%'
383
384 ''',
385           }),
386         ('multi-line-desc',
387          {'template': multiline_template,
388           'exception': None,
389           'input_file': '''environments:
390   -
391     name: basic
392     title: Basic Environment
393     description: Basic description
394     files:
395       foo.yaml:
396         parameters: all
397 ''',
398           'expected_output': '''# title: Basic Environment
399 # description: |
400 #   Basic description
401 parameter_defaults:
402   # Parameter with
403   # multi-line description
404   # Type: string
405   FooParam: ''
406
407 ''',
408           }),
409         ]
410
411     @classmethod
412     def generate_scenarios(cls):
413         cls.scenarios = testscenarios.multiply_scenarios(
414             cls.content_scenarios)
415
416     def test_generator(self):
417         fake_input = io.StringIO(six.text_type(self.input_file))
418         fake_template = io.StringIO(six.text_type(self.template))
419         _, fake_output_path = tempfile.mkstemp()
420         fake_output = open(fake_output_path, 'w')
421         with mock.patch('tripleo_heat_templates.environment_generator.open',
422                         create=True) as mock_open:
423             mock_open.side_effect = [fake_input, fake_template, fake_output]
424             if not self.exception:
425                 environment_generator.generate_environments('ignored.yaml')
426             else:
427                 self.assertRaises(self.exception,
428                                   environment_generator.generate_environments,
429                                   'ignored.yaml')
430                 return
431         expected = environment_generator._FILE_HEADER + self.expected_output
432         with open(fake_output_path) as f:
433             self.assertEqual(expected, f.read())
434
435 GeneratorTestCase.generate_scenarios()