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,
93 'input_file': '''environments:
96 title: Basic Environment
97 description: Basic description
102 'expected_output': '''# title: Basic Environment
117 {'template': basic_template,
120 'input_file': '''environments:
123 title: Basic Environment
124 description: Basic description
130 'expected_output': '''# title: Basic Environment
140 ('basic-static-param',
141 {'template': basic_template,
144 'input_file': '''environments:
147 title: Basic Environment
148 description: Basic description
155 'expected_output': '''# title: Basic Environment
163 # ******************************************************
164 # Static parameters - these are values that must be
165 # included in the environment but should not be changed.
166 # ******************************************************
171 # *********************
172 # End static parameters
173 # *********************
176 ('basic-static-param-sample',
177 {'template': basic_template,
180 'input_file': '''environments:
183 title: Basic Environment
184 description: Basic description
194 'expected_output': '''# title: Basic Environment
202 # ******************************************************
203 # Static parameters - these are values that must be
204 # included in the environment but should not be changed.
205 # ******************************************************
210 # *********************
211 # End static parameters
212 # *********************
216 {'template': basic_private_template,
219 'input_file': '''environments:
222 title: Basic Environment
223 description: Basic description
228 'expected_output': '''# title: Basic Environment
239 {'template': mandatory_template,
242 'input_file': '''environments:
245 title: Basic Environment
246 description: Basic description
251 'expected_output': '''# title: Basic Environment
256 # Mandatory. This parameter must be set by the user.
263 {'template': basic_template,
266 'input_file': '''environments:
269 title: Basic Environment
270 description: Basic description
277 'expected_output': '''# title: Basic Environment
291 ('basic-resource-registry',
292 {'template': basic_template,
295 'input_file': '''environments:
298 title: Basic Environment
299 description: Basic description
304 OS::TripleO::FakeResource: fake-filename.yaml
306 'expected_output': '''# title: Basic Environment
319 OS::TripleO::FakeResource: fake-filename.yaml
323 {'template': basic_template,
326 'input_file': '''environments:
329 title: Basic Environment
330 description: Basic description
339 'expected_output': '''# title: Basic Environment
347 # Parameter that should not be included by default
359 {'template': basic_template,
360 'exception': RuntimeError,
362 'input_file': '''environments:
365 title: Basic Environment
366 description: Basic description
370 - SomethingNonexistent
372 'expected_output': None,
375 {'template': index_template,
378 'input_file': '''environments:
381 title: Basic Environment
382 description: Basic description
387 'expected_output': '''# title: Basic Environment
391 # Param with %index% as its default
398 {'template': multiline_template,
400 'input_file': '''environments:
403 title: Basic Environment
404 description: Basic description
410 title: Nested Environment
411 description: Nested description
415 'expected_output': '''# title: Basic Environment
420 # multi-line description
425 'nested_output': '''# title: Nested Environment
430 # multi-line description
437 {'template': multiline_template,
440 'input_file': '''environments:
443 title: Basic Environment
444 description: Basic description
449 'expected_output': '''# title: Basic Environment
454 # multi-line description
463 def generate_scenarios(cls):
464 cls.scenarios = testscenarios.multiply_scenarios(
465 cls.content_scenarios)
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')
485 self.assertRaises(self.exception,
486 environment_generator.generate_environments,
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 +
496 self.assertEqual(expected, f.read())
498 GeneratorTestCase.generate_scenarios()