~ubuntu-branches/ubuntu/trusty/heat/trusty

« back to all changes in this revision

Viewing changes to .pc/rename-quantumclient.patch/heat/tests/test_template_format.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Chuck Short
  • Date: 2013-08-08 01:08:42 UTC
  • Revision ID: package-import@ubuntu.com-20130808010842-77cni2v4vlib7rus
Tags: 2013.2~b2-0ubuntu4
[ Chuck Short ]
* debian/rules: Enable testsuite during builds.
* debian/patches/fix-sqlalchemy-0.8.patch: Build against sqlalchemy 0.8.
* debian/patches/rename-quantumclient.patch: quantumclient -> neutronclient.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
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
from testtools import skipIf
 
16
import os
 
17
 
 
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
 
23
 
 
24
 
 
25
class JsonToYamlTest(HeatTestCase):
 
26
 
 
27
    def setUp(self):
 
28
        super(JsonToYamlTest, self).setUp()
 
29
        self.expected_test_count = 2
 
30
        self.longMessage = True
 
31
        self.maxDiff = None
 
32
 
 
33
    def test_convert_all_templates(self):
 
34
        path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
 
35
                            'templates')
 
36
 
 
37
        template_test_count = 0
 
38
        for (json_str,
 
39
             yml_str,
 
40
             file_name) in self.convert_all_json_to_yaml(path):
 
41
 
 
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:
 
45
                break
 
46
 
 
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))
 
50
 
 
51
    def compare_json_vs_yaml(self, json_str, yml_str, file_name):
 
52
        yml = template_format.parse(yml_str)
 
53
 
 
54
        self.assertEqual(u'2012-12-12', yml[u'HeatTemplateFormatVersion'],
 
55
                         file_name)
 
56
        self.assertFalse(u'AWSTemplateFormatVersion' in yml, file_name)
 
57
        del(yml[u'HeatTemplateFormatVersion'])
 
58
 
 
59
        jsn = template_format.parse(json_str)
 
60
        template_format.default_for_missing(jsn, 'AWSTemplateFormatVersion',
 
61
                                            template_format.CFN_VERSIONS)
 
62
 
 
63
        if u'AWSTemplateFormatVersion' in jsn:
 
64
            del(jsn[u'AWSTemplateFormatVersion'])
 
65
 
 
66
        self.assertEqual(yml, jsn, file_name)
 
67
 
 
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'):
 
71
                continue
 
72
            f = open(os.path.join(dirpath, path), 'r')
 
73
            json_str = f.read()
 
74
 
 
75
            yml_str = template_format.convert_json_to_yaml(json_str)
 
76
            yield (json_str, yml_str, f.name)
 
77
 
 
78
 
 
79
class YamlMinimalTest(HeatTestCase):
 
80
 
 
81
    def test_minimal_yaml(self):
 
82
        yaml1 = ''
 
83
        yaml2 = '''HeatTemplateFormatVersion: '2012-12-12'
 
84
Parameters: {}
 
85
Mappings: {}
 
86
Resources: {}
 
87
Outputs: {}
 
88
'''
 
89
        tpl1 = template_format.parse(yaml1)
 
90
        tpl2 = template_format.parse(yaml2)
 
91
        self.assertEqual(tpl1, tpl2)
 
92
 
 
93
 
 
94
class YamlEnvironmentTest(HeatTestCase):
 
95
 
 
96
    def test_no_template_sections(self):
 
97
        env = '''
 
98
parameters: {}
 
99
resource_registry: {}
 
100
'''
 
101
        parsed_env = template_format.parse(env, add_template_sections=False)
 
102
 
 
103
        self.assertEqual('parameters' in parsed_env, True)
 
104
        self.assertEqual('resource_registry' in parsed_env, True)
 
105
 
 
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)
 
110
 
 
111
 
 
112
class JsonYamlResolvedCompareTest(HeatTestCase):
 
113
 
 
114
    def setUp(self):
 
115
        super(JsonYamlResolvedCompareTest, self).setUp()
 
116
        self.longMessage = True
 
117
        self.maxDiff = None
 
118
        setup_dummy_db()
 
119
 
 
120
    def load_template(self, file_name):
 
121
        filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
 
122
                                'templates', file_name)
 
123
        f = open(filepath)
 
124
        t = template_format.parse(f.read())
 
125
        f.close()
 
126
        return t
 
127
 
 
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'])
 
133
 
 
134
        t2 = self.load_template(yaml_file)
 
135
        del(t2[u'HeatTemplateFormatVersion'])
 
136
 
 
137
        stack1 = parse_stack(t1, parameters)
 
138
        stack2 = parse_stack(t2, parameters)
 
139
 
 
140
        # compare resources separately so that resolved static data
 
141
        # is compared
 
142
        t1nr = dict(stack1.t.t)
 
143
        del(t1nr['Resources'])
 
144
 
 
145
        t2nr = dict(stack2.t.t)
 
146
        del(t2nr['Resources'])
 
147
        self.assertEqual(t1nr, t2nr)
 
148
 
 
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)
 
153
 
 
154
    @skipIf(clients.quantumclient is None, 'quantumclient unavailable')
 
155
    def test_quantum_resolved(self):
 
156
        self.compare_stacks('Quantum.template', 'Quantum.yaml', {})
 
157
 
 
158
    def test_wordpress_resolved(self):
 
159
        self.compare_stacks('WordPress_Single_Instance.template',
 
160
                            'WordPress_Single_Instance.yaml',
 
161
                            {'KeyName': 'test'})