1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
15
from testtools import skipIf
18
from heat.engine import clients
19
from heat.common import template_format
20
from heat.tests.common import HeatTestCase
21
from heat.tests.utils import setup_dummy_db
22
from heat.tests.utils import parse_stack
25
class JsonToYamlTest(HeatTestCase):
28
super(JsonToYamlTest, self).setUp()
29
self.expected_test_count = 2
30
self.longMessage = True
33
def test_convert_all_templates(self):
34
path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
37
template_test_count = 0
40
file_name) in self.convert_all_json_to_yaml(path):
42
self.compare_json_vs_yaml(json_str, yml_str, file_name)
43
template_test_count += 1
44
if template_test_count >= self.expected_test_count:
47
self.assertTrue(template_test_count >= self.expected_test_count,
48
'Expected at least %d templates to be tested, not %d' %
49
(self.expected_test_count, template_test_count))
51
def compare_json_vs_yaml(self, json_str, yml_str, file_name):
52
yml = template_format.parse(yml_str)
54
self.assertEqual(u'2012-12-12', yml[u'HeatTemplateFormatVersion'],
56
self.assertFalse(u'AWSTemplateFormatVersion' in yml, file_name)
57
del(yml[u'HeatTemplateFormatVersion'])
59
jsn = template_format.parse(json_str)
60
template_format.default_for_missing(jsn, 'AWSTemplateFormatVersion',
61
template_format.CFN_VERSIONS)
63
if u'AWSTemplateFormatVersion' in jsn:
64
del(jsn[u'AWSTemplateFormatVersion'])
66
self.assertEqual(yml, jsn, file_name)
68
def convert_all_json_to_yaml(self, dirpath):
69
for path in os.listdir(dirpath):
70
if not path.endswith('.template') and not path.endswith('.json'):
72
f = open(os.path.join(dirpath, path), 'r')
75
yml_str = template_format.convert_json_to_yaml(json_str)
76
yield (json_str, yml_str, f.name)
79
class YamlMinimalTest(HeatTestCase):
81
def test_minimal_yaml(self):
83
yaml2 = '''HeatTemplateFormatVersion: '2012-12-12'
89
tpl1 = template_format.parse(yaml1)
90
tpl2 = template_format.parse(yaml2)
91
self.assertEqual(tpl1, tpl2)
94
class YamlEnvironmentTest(HeatTestCase):
96
def test_no_template_sections(self):
101
parsed_env = template_format.parse(env, add_template_sections=False)
103
self.assertEqual('parameters' in parsed_env, True)
104
self.assertEqual('resource_registry' in parsed_env, True)
106
self.assertEqual('Parameters' in parsed_env, False)
107
self.assertEqual('Mappings' in parsed_env, False)
108
self.assertEqual('Resources' in parsed_env, False)
109
self.assertEqual('Outputs' in parsed_env, False)
112
class JsonYamlResolvedCompareTest(HeatTestCase):
115
super(JsonYamlResolvedCompareTest, self).setUp()
116
self.longMessage = True
120
def load_template(self, file_name):
121
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
122
'templates', file_name)
124
t = template_format.parse(f.read())
128
def compare_stacks(self, json_file, yaml_file, parameters):
129
t1 = self.load_template(json_file)
130
template_format.default_for_missing(t1, 'AWSTemplateFormatVersion',
131
template_format.CFN_VERSIONS)
132
del(t1[u'AWSTemplateFormatVersion'])
134
t2 = self.load_template(yaml_file)
135
del(t2[u'HeatTemplateFormatVersion'])
137
stack1 = parse_stack(t1, parameters)
138
stack2 = parse_stack(t2, parameters)
140
# compare resources separately so that resolved static data
142
t1nr = dict(stack1.t.t)
143
del(t1nr['Resources'])
145
t2nr = dict(stack2.t.t)
146
del(t2nr['Resources'])
147
self.assertEqual(t1nr, t2nr)
149
self.assertEquals(set(stack1.resources.keys()),
150
set(stack2.resources.keys()))
151
for key in stack1.resources:
152
self.assertEqual(stack1.resources[key].t, stack2.resources[key].t)
154
@skipIf(clients.quantumclient is None, 'quantumclient unavailable')
155
def test_quantum_resolved(self):
156
self.compare_stacks('Quantum.template', 'Quantum.yaml', {})
158
def test_wordpress_resolved(self):
159
self.compare_stacks('WordPress_Single_Instance.template',
160
'WordPress_Single_Instance.yaml',