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
40 basic_private_template = '''
44 description: Foo description
48 description: Bar description
53 mandatory_template = '''
56 description: Mandatory param
64 description: Param with %index% as its default
70 multiline_template = '''
75 multi-line description
83 class GeneratorTestCase(base.BaseTestCase):
86 {'template': basic_template,
88 'input_file': '''environments:
91 title: Basic Environment
92 description: Basic description
97 'expected_output': '''# title: Basic Environment
112 {'template': basic_template,
114 'input_file': '''environments:
117 title: Basic Environment
118 description: Basic description
124 'expected_output': '''# title: Basic Environment
134 ('basic-static-param',
135 {'template': basic_template,
137 'input_file': '''environments:
140 title: Basic Environment
141 description: Basic description
148 'expected_output': '''# title: Basic Environment
156 # ******************************************************
157 # Static parameters - these are values that must be
158 # included in the environment but should not be changed.
159 # ******************************************************
164 # *********************
165 # End static parameters
166 # *********************
169 ('basic-static-param-sample',
170 {'template': basic_template,
172 'input_file': '''environments:
175 title: Basic Environment
176 description: Basic description
186 'expected_output': '''# title: Basic Environment
194 # ******************************************************
195 # Static parameters - these are values that must be
196 # included in the environment but should not be changed.
197 # ******************************************************
202 # *********************
203 # End static parameters
204 # *********************
208 {'template': basic_private_template,
210 'input_file': '''environments:
213 title: Basic Environment
214 description: Basic description
219 'expected_output': '''# title: Basic Environment
230 {'template': mandatory_template,
232 'input_file': '''environments:
235 title: Basic Environment
236 description: Basic description
241 'expected_output': '''# title: Basic Environment
246 # Mandatory. This parameter must be set by the user.
253 {'template': basic_template,
255 'input_file': '''environments:
258 title: Basic Environment
259 description: Basic description
266 'expected_output': '''# title: Basic Environment
280 ('basic-resource-registry',
281 {'template': basic_template,
283 'input_file': '''environments:
286 title: Basic Environment
287 description: Basic description
292 OS::TripleO::FakeResource: fake-filename.yaml
294 'expected_output': '''# title: Basic Environment
307 OS::TripleO::FakeResource: fake-filename.yaml
311 {'template': basic_template,
312 'exception': RuntimeError,
313 'input_file': '''environments:
316 title: Basic Environment
317 description: Basic description
321 - SomethingNonexistent
323 'expected_output': None,
326 {'template': index_template,
328 'input_file': '''environments:
331 title: Basic Environment
332 description: Basic description
337 'expected_output': '''# title: Basic Environment
341 # Param with %index% as its default
348 {'template': multiline_template,
350 'input_file': '''environments:
353 title: Basic Environment
354 description: Basic description
359 'expected_output': '''# title: Basic Environment
364 # multi-line description
373 def generate_scenarios(cls):
374 cls.scenarios = testscenarios.multiply_scenarios(
375 cls.content_scenarios)
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')
388 self.assertRaises(self.exception,
389 environment_generator.generate_environments,
392 expected = environment_generator._FILE_HEADER + self.expected_output
393 with open(fake_output_path) as f:
394 self.assertEqual(expected, f.read())
396 GeneratorTestCase.generate_scenarios()