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