1 # Copyright 2015 Red Hat Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
19 from oslotest import base
23 from tripleo_heat_templates import environment_generator
25 load_tests = testscenarios.load_tests_apply_scenarios
31 description: Foo description
35 description: Bar description
39 description: Parameter that should not be included by default
44 basic_private_template = '''
48 description: Foo description
52 description: Bar description
57 mandatory_template = '''
60 description: Mandatory param
68 description: Param with %index% as its default
74 multiline_template = '''
79 multi-line description
87 class GeneratorTestCase(base.BaseTestCase):
90 {'template': basic_template,
92 'input_file': '''environments:
95 title: Basic Environment
96 description: Basic description
101 'expected_output': '''# title: Basic Environment
116 {'template': basic_template,
118 'input_file': '''environments:
121 title: Basic Environment
122 description: Basic description
128 'expected_output': '''# title: Basic Environment
138 ('basic-static-param',
139 {'template': basic_template,
141 'input_file': '''environments:
144 title: Basic Environment
145 description: Basic description
152 'expected_output': '''# title: Basic Environment
160 # ******************************************************
161 # Static parameters - these are values that must be
162 # included in the environment but should not be changed.
163 # ******************************************************
168 # *********************
169 # End static parameters
170 # *********************
173 ('basic-static-param-sample',
174 {'template': basic_template,
176 'input_file': '''environments:
179 title: Basic Environment
180 description: Basic description
190 'expected_output': '''# title: Basic Environment
198 # ******************************************************
199 # Static parameters - these are values that must be
200 # included in the environment but should not be changed.
201 # ******************************************************
206 # *********************
207 # End static parameters
208 # *********************
212 {'template': basic_private_template,
214 'input_file': '''environments:
217 title: Basic Environment
218 description: Basic description
223 'expected_output': '''# title: Basic Environment
234 {'template': mandatory_template,
236 'input_file': '''environments:
239 title: Basic Environment
240 description: Basic description
245 'expected_output': '''# title: Basic Environment
250 # Mandatory. This parameter must be set by the user.
257 {'template': basic_template,
259 'input_file': '''environments:
262 title: Basic Environment
263 description: Basic description
270 'expected_output': '''# title: Basic Environment
284 ('basic-resource-registry',
285 {'template': basic_template,
287 'input_file': '''environments:
290 title: Basic Environment
291 description: Basic description
296 OS::TripleO::FakeResource: fake-filename.yaml
298 'expected_output': '''# title: Basic Environment
311 OS::TripleO::FakeResource: fake-filename.yaml
315 {'template': basic_template,
317 'input_file': '''environments:
320 title: Basic Environment
321 description: Basic description
330 'expected_output': '''# title: Basic Environment
338 # Parameter that should not be included by default
350 {'template': basic_template,
351 'exception': RuntimeError,
352 'input_file': '''environments:
355 title: Basic Environment
356 description: Basic description
360 - SomethingNonexistent
362 'expected_output': None,
365 {'template': index_template,
367 'input_file': '''environments:
370 title: Basic Environment
371 description: Basic description
376 'expected_output': '''# title: Basic Environment
380 # Param with %index% as its default
387 {'template': multiline_template,
389 'input_file': '''environments:
392 title: Basic Environment
393 description: Basic description
398 'expected_output': '''# title: Basic Environment
403 # multi-line description
412 def generate_scenarios(cls):
413 cls.scenarios = testscenarios.multiply_scenarios(
414 cls.content_scenarios)
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')
427 self.assertRaises(self.exception,
428 environment_generator.generate_environments,
431 expected = environment_generator._FILE_HEADER + self.expected_output
432 with open(fake_output_path) as f:
433 self.assertEqual(expected, f.read())
435 GeneratorTestCase.generate_scenarios()