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

« back to all changes in this revision

Viewing changes to heat/tests/test_components.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-09-08 21:51:19 UTC
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20130908215119-7tcek6gn73275x5k
Tags: upstream-2013.2~b3
ImportĀ upstreamĀ versionĀ 2013.2~b3

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 heat.engine.components import Component
 
16
from heat.engine.components import Components
 
17
from heat.tests.common import HeatTestCase
 
18
 
 
19
 
 
20
class ComponentTest(HeatTestCase):
 
21
 
 
22
    def test_init(self):
 
23
        comp = Component()
 
24
        self.assertEqual(comp.type, 'OS::Heat::SoftwareConfig')
 
25
        self.assertEqual(comp.properties, {})
 
26
        self.assertEqual(comp.scripts, {})
 
27
        self.assertEqual(comp.relations, [])
 
28
        self.assertEqual(comp.hosted_on(), None)
 
29
        self.assertEqual(comp.depends(), [])
 
30
 
 
31
    def test_hosted_on(self):
 
32
        schema = {
 
33
            'relationships': [
 
34
                {'hosted_on': 'wordpress'}
 
35
            ]
 
36
        }
 
37
        comp = Component(schema)
 
38
        self.assertEqual(comp.hosted_on(), 'wordpress')
 
39
 
 
40
    def test_depends(self):
 
41
        schema = {
 
42
            'relationships': [
 
43
                {'depends_on': 'config_mysql'}
 
44
            ]
 
45
        }
 
46
        comp = Component(schema)
 
47
        self.assertEqual(comp.depends(), ['config_mysql'])
 
48
 
 
49
        comp['relationships'].append({'depends_on': 'config_wordpress'})
 
50
        self.assertEqual(comp.depends(),
 
51
                         ['config_mysql', 'config_wordpress'])
 
52
 
 
53
 
 
54
class ComponentsTest(HeatTestCase):
 
55
 
 
56
    def test_init(self):
 
57
        schema = {}
 
58
        comps = Components(schema)
 
59
        self.assertEqual(0, len(comps))
 
60
 
 
61
        schema['config_mysql'] = {}
 
62
        comps = Components(schema)
 
63
        self.assertEquals(1, len(comps))
 
64
        comp = comps['config_mysql']
 
65
        self.assertIsInstance(comp, Component)
 
66
 
 
67
    def test_depends(self):
 
68
        schema = {
 
69
            'install_mysql': {
 
70
            },
 
71
            'config_mysql': {
 
72
                'relationships': [
 
73
                    {'depends_on': 'install_mysql'}
 
74
                ]
 
75
            },
 
76
            'start_mysql': {
 
77
                'relationships': [
 
78
                    {'depends_on': 'config_mysql'}
 
79
                ]
 
80
            }
 
81
        }
 
82
        comps = Components(schema)
 
83
        self.assertEqual(3, len(comps))
 
84
        deps = comps.depends()
 
85
        self.assertEqual(2, len(deps))
 
86
        self.assertIn('install_mysql', deps)
 
87
        self.assertIn('config_mysql', deps)
 
88
 
 
89
    def test_multi_depends(self):
 
90
        schema = {
 
91
            'install_mysql': {
 
92
            },
 
93
            'config_mysql': {
 
94
                'relationships': [
 
95
                    {'depends_on': 'install_mysql'}
 
96
                ]
 
97
            },
 
98
            'start_mysql': {
 
99
                'relationships': [
 
100
                    {'depends_on': 'config_mysql'}
 
101
                ]
 
102
            },
 
103
            'install_wordpress': {},
 
104
            'config_wordpress': {
 
105
                'relationships': [
 
106
                    {'depends_on': 'install_wordpress'}
 
107
                ]
 
108
            },
 
109
            'start_wordpress': {
 
110
                'relationships': [
 
111
                    {'depends_on': 'config_wordpress'},
 
112
                    {'depends_on': 'start_mysql'}
 
113
                ]
 
114
            }
 
115
        }
 
116
        comps = Components(schema)
 
117
        deps = comps.depends()
 
118
        self.assertEqual(5, len(deps))
 
119
        self.assertNotIn('start_wordpress', deps)
 
120
        self.assertIn('install_wordpress', deps)
 
121
        self.assertIn('config_wordpress', deps)
 
122
        self.assertIn('start_mysql', deps)
 
123
        self.assertIn('config_mysql', deps)
 
124
        self.assertIn('install_mysql', deps)
 
125
 
 
126
    def test_filter(self):
 
127
        schema = {
 
128
            'install_mysql': {
 
129
                'relationships': [
 
130
                    {'hosted_on': 'mysql'}
 
131
                ]
 
132
            },
 
133
            'config_mysql': {
 
134
                'relationships': [
 
135
                    {'hosted_on': 'mysql'},
 
136
                    {'depends_on': 'install_mysql'}
 
137
                ]
 
138
            },
 
139
            'start_mysql': {
 
140
                'relationships': [
 
141
                    {'hosted_on': 'mysql'},
 
142
                    {'depends_on': 'config_mysql'}
 
143
                ]
 
144
            },
 
145
            'install_wordpress': {
 
146
                'relationships': [
 
147
                    {'hosted_on': 'wordpress'}
 
148
                ]
 
149
            },
 
150
            'config_wordpress': {
 
151
                'relationships': [
 
152
                    {'hosted_on': 'wordpress'},
 
153
                    {'depends_on': 'install_wordpress'}
 
154
                ]
 
155
            },
 
156
            'start_wordpress': {
 
157
                'relationships': [
 
158
                    {'hosted_on': 'wordpress'},
 
159
                    {'depends_on': 'config_wordpress'},
 
160
                    {'depends_on': 'start_mysql'}
 
161
                ]
 
162
            }
 
163
        }
 
164
 
 
165
        comps = Components(schema)
 
166
        names = comps.filter('mysql')
 
167
        self.assertEqual(3, len(names))
 
168
        self.assertIn('config_mysql', names)
 
169
        self.assertIn('install_mysql', names)
 
170
        self.assertIn('start_mysql', names)
 
171
 
 
172
        names = comps.filter('wordpress')
 
173
        self.assertEqual(3, len(names))
 
174
        self.assertIn('config_wordpress', names)
 
175
        self.assertIn('install_wordpress', names)
 
176
        self.assertIn('start_wordpress', names)
 
177
 
 
178
    def test_validate(self):
 
179
        schema = {'install_mysql': {}}
 
180
        comps = Components(schema)
 
181
        self.assertTrue(comps.validate())
 
182
 
 
183
        schema = {
 
184
            'config_mysql': {
 
185
                'relationships': [
 
186
                    {'depends_on': 'config_mysql'}
 
187
                ]
 
188
            }
 
189
        }
 
190
        comps = Components(schema)
 
191
        err = self.assertRaises(ValueError, comps.validate)
 
192
        self.assertIn('component config_mysql depends on itself.', str(err))
 
193
 
 
194
        schema = {
 
195
            'config_mysql': {
 
196
                'relationships': [
 
197
                    {'depends_on': 'install_mysql'}
 
198
                ]
 
199
            }
 
200
        }
 
201
        comps = Components(schema)
 
202
        err = self.assertRaises(ValueError, comps.validate)
 
203
        self.assertIn('component install_mysql is not defined.', str(err))
 
204
 
 
205
        schema = {
 
206
            'install_mysql': {
 
207
            },
 
208
            'config_mysql': {
 
209
                'relationships': [
 
210
                    {'depends_on': 'install_mysql'},
 
211
                    {'depends_on': 'install_mysql'}
 
212
                ]
 
213
            }
 
214
        }
 
215
        comps = Components(schema)
 
216
        err = self.assertRaises(ValueError, comps.validate)
 
217
        self.assertIn('duplicated install_mysql in config_mysql depends on.',
 
218
                      str(err))