Support version of tosca_simple_yaml_1_1
[parser.git] / tosca2heat / tosca-parser / toscaparser / tests / test_scalarunit.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.common import exception
14 from toscaparser.elements.scalarunit import ScalarUnit_Frequency
15 from toscaparser.elements.scalarunit import ScalarUnit_Size
16 from toscaparser.elements.scalarunit import ScalarUnit_Time
17 from toscaparser.nodetemplate import NodeTemplate
18 from toscaparser.tests.base import TestCase
19 from toscaparser.utils.gettextutils import _
20 from toscaparser.utils import yamlparser
21
22
23 class ScalarUnitPositiveTest(TestCase):
24
25     scenarios = [
26         (
27             # tpl_snippet with mem_size given as number+space+MB
28             'mem_size_is_number_Space_MB',
29             dict(tpl_snippet='''
30                  server:
31                    type: tosca.nodes.Compute
32                    capabilities:
33                      host:
34                        properties:
35                          mem_size: 1024 MB
36                  ''',
37                  property='mem_size',
38                  expected='1024 MB')
39         ),
40         (
41             # tpl_snippet with mem_size given as number+spaces+GB
42             'mem_size_is_number_Space_GB',
43             dict(tpl_snippet='''
44                  server:
45                    type: tosca.nodes.Compute
46                    capabilities:
47                      host:
48                        properties:
49                          mem_size: 1     GB
50                  ''',
51                  property='mem_size',
52                  expected='1 GB')
53         ),
54         (
55             # tpl_snippet with mem_size given as number+tiB
56             'mem_size_is_number_NoSpace_GB',
57             dict(tpl_snippet='''
58                  server:
59                    type: tosca.nodes.Compute
60                    capabilities:
61                      host:
62                        properties:
63                          mem_size: 1tiB
64                  ''',
65                  property='mem_size',
66                  expected='1 TiB')
67         ),
68         (
69             # tpl_snippet with mem_size given as number+Spaces+GIB
70             'mem_size_is_number_Spaces_GB',
71             dict(tpl_snippet='''
72                  server:
73                    type: tosca.nodes.Compute
74                    capabilities:
75                      host:
76                        properties:
77                          mem_size: 1     GIB
78                  ''',
79                  property='mem_size',
80                  expected='1 GiB')
81         ),
82         (
83             # tpl_snippet with mem_size given as number+Space+tib
84             'mem_size_is_number_Spaces_GB',
85             dict(tpl_snippet='''
86                  server:
87                    type: tosca.nodes.Compute
88                    capabilities:
89                      host:
90                        properties:
91                          mem_size: 1 tib
92                  ''',
93                  property='mem_size',
94                  expected='1 TiB')
95         ),
96         (
97             'cpu_frequency_is_float_Space_GHz',
98             dict(tpl_snippet='''
99                  server:
100                    type: tosca.nodes.Compute
101                    capabilities:
102                      host:
103                        properties:
104                          cpu_frequency: 2.5 GHz
105                  ''',
106                  property='cpu_frequency',
107                  expected='2.5 GHz')
108         ),
109         (
110             'cpu_frequency_is_float_Space_MHz',
111             dict(tpl_snippet='''
112                  server:
113                    type: tosca.nodes.Compute
114                    capabilities:
115                      host:
116                        properties:
117                          cpu_frequency: 800 MHz
118                  ''',
119                  property='cpu_frequency',
120                  expected='800 MHz')
121         ),
122     ]
123
124     def test_scenario_scalar_unit_positive(self):
125         tpl = self.tpl_snippet
126         nodetemplates = yamlparser.simple_parse(tpl)
127         nodetemplate = NodeTemplate('server', nodetemplates)
128         props = nodetemplate.get_capability('host').get_properties()
129         prop_name = self.property
130         if props and prop_name in props.keys():
131             prop = props[prop_name]
132             self.assertIsNone(prop.validate())
133             resolved = prop.value
134         self.assertEqual(resolved, self.expected)
135
136
137 class GetNumFromScalarUnitSizePositive(TestCase):
138
139     scenarios = [
140         (   # Note that (1 TB) / (1 GB) = 1000
141             'Input is TB, user input is GB',
142             dict(InputMemSize='1   TB',
143                  UserInputUnit='gB',
144                  expected=1000)
145         ),
146         (   # Note that (1 Tib)/ (1 GB) = 1099
147             'Input is TiB, user input is GB',
148             dict(InputMemSize='1   TiB',
149                  UserInputUnit='gB',
150                  expected=1099.511627776)
151         ),
152     ]
153
154     def test_scenario_get_num_from_scalar_unit_size(self):
155         resolved = (ScalarUnit_Size(self.InputMemSize).
156                     get_num_from_scalar_unit(self.UserInputUnit))
157         self.assertEqual(resolved, self.expected)
158
159
160 class GetNumFromScalarUnitFrequencyPositive(TestCase):
161
162     scenarios = [
163         (   # Note that (1 GHz) / (1 Hz) = 1000000000
164             'Input is GHz, user input is Hz',
165             dict(InputMemSize='1   GHz',
166                  UserInputUnit='Hz',
167                  expected=1000000000)
168         ),
169         (
170             'Input is GHz, user input is Hz',
171             dict(InputMemSize='2.4   GHz',
172                  UserInputUnit='Hz',
173                  expected=2400000000)
174         ),
175         (   # Note that (1 GHz)/ (1 MHz) = 1000
176             'Input is MHz, user input is GHz',
177             dict(InputMemSize='800   MHz',
178                  UserInputUnit='GHz',
179                  expected=0.8)
180         ),
181         (
182             'Input is GHz, user input is Hz',
183             dict(InputMemSize='0.9  GHz',
184                  UserInputUnit='MHz',
185                  expected=900)
186         ),
187         (
188             'Input is GHz, user input is Hz',
189             dict(InputMemSize='2.7GHz',
190                  UserInputUnit='MHz',
191                  expected=2700)
192         ),
193     ]
194
195     def test_scenario_get_num_from_scalar_unit_frequency(self):
196         resolved = (ScalarUnit_Frequency(self.InputMemSize).
197                     get_num_from_scalar_unit(self.UserInputUnit))
198         self.assertEqual(resolved, self.expected)
199
200
201 class GetNumFromScalarUnitTimePositive(TestCase):
202
203     scenarios = [
204         (   # Note that (1 s) / (1 ms) = 1000
205             'Input is 500ms, user input is s',
206             dict(InputMemSize='500   ms',
207                  UserInputUnit='s',
208                  expected=0.5)
209         ),
210         (   # Note that (1 h)/ (1 s) = 3600
211             'Input is h, user input is s',
212             dict(InputMemSize='1   h',
213                  UserInputUnit='s',
214                  expected=3600)
215         ),
216         (   # Note that (1 m)/ (1 s) = 60
217             'Input is m, user input is s',
218             dict(InputMemSize='0.5   m',
219                  UserInputUnit='s',
220                  expected=30)
221         ),
222         (   # Note that (1 d)/ (1 h) = 24
223             'Input is d, user input is h',
224             dict(InputMemSize='1   d',
225                  UserInputUnit='h',
226                  expected=24)
227         ),
228     ]
229
230     def test_scenario_get_num_from_scalar_unit_time(self):
231         resolved = (ScalarUnit_Time(self.InputMemSize).
232                     get_num_from_scalar_unit(self.UserInputUnit))
233         self.assertEqual(resolved, self.expected)
234
235
236 class GetNumFromScalarUnitSizeNegative(TestCase):
237
238     InputMemSize = '1 GB'
239     UserInputUnit = 'qB'
240
241     def test_get_num_from_scalar_unit_size_negative(self):
242         try:
243             (ScalarUnit_Size(self.InputMemSize).
244              get_num_from_scalar_unit(self.UserInputUnit))
245         except Exception as error:
246             self.assertIsInstance(error, ValueError)
247             self.assertEqual(_('The unit "qB" is not valid. Valid units are '
248                                '"[\'B\', \'GB\', \'GiB\', \'KiB\', \'MB\', '
249                                '\'MiB\', \'TB\', \'TiB\', \'kB\']".'),
250                              error.__str__())
251
252
253 class GetNumFromScalarUnitFrequencyNegative(TestCase):
254
255     InputFrequency = '2.7 GHz'
256     UserInputUnit = 'Jz'
257
258     def test_get_num_from_scalar_unit_frequency_negative(self):
259         try:
260             (ScalarUnit_Frequency(self.InputFrequency).
261              get_num_from_scalar_unit(self.UserInputUnit))
262         except Exception as error:
263             self.assertIsInstance(error, ValueError)
264             self.assertEqual(_('The unit "Jz" is not valid. Valid units are '
265                                '"[\'GHz\', \'Hz\', \'MHz\', \'kHz\']".'),
266                              error.__str__())
267
268
269 class GetNumFromScalarUnitTimeNegative(TestCase):
270
271     InputTime = '5 ms'
272     UserInputUnit = 'D'
273
274     def test_get_num_from_scalar_unit_frequency_negative(self):
275         try:
276             (ScalarUnit_Time(self.InputTime).
277              get_num_from_scalar_unit(self.UserInputUnit))
278         except Exception as error:
279             self.assertIsInstance(error, ValueError)
280             self.assertEqual(_('"Jz" is not a valid scalar-unit.'),
281                              error.__str__())
282
283
284 class ScalarUnitNegativeTest(TestCase):
285
286     custom_def_snippet = '''
287     tosca.my.nodes.Compute:
288       derived_from: tosca.nodes.Root
289       properties:
290         cpu_frequency:
291           required: false
292           type: scalar-unit.frequency
293           constraints:
294             - greater_or_equal: 0.1 GHz
295         disk_size:
296           required: false
297           type: scalar-unit.size
298           constraints:
299             - greater_or_equal: 1 GB
300         mem_size:
301           required: false
302           type: scalar-unit.size
303           constraints:
304             - in_range: [1 MiB, 1 GiB]
305     '''
306     custom_def = yamlparser.simple_parse(custom_def_snippet)
307
308     # disk_size doesn't provide a value, mem_size uses an invalid unit.
309     def test_invalid_scalar_unit(self):
310         tpl_snippet = '''
311         server:
312           type: tosca.my.nodes.Compute
313           properties:
314             cpu_frequency: 50.3.6 GHZ
315             disk_size: MB
316             mem_size: 1 QB
317         '''
318         nodetemplates = yamlparser.simple_parse(tpl_snippet)
319         nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def)
320         for p in nodetemplate.get_properties_objects():
321             self.assertRaises(ValueError, p.validate)
322
323     # disk_size is less than 1 GB, mem_size is not in the required range.
324     # Note: in the spec, the minimum value of mem_size is 1 MiB (> 1 MB)
325     def test_constraint_for_scalar_unit(self):
326         tpl_snippet = '''
327         server:
328           type: tosca.my.nodes.Compute
329           properties:
330             cpu_frequency: 0.05 GHz
331             disk_size: 500 MB
332             mem_size: 1 MB
333         '''
334         nodetemplates = yamlparser.simple_parse(tpl_snippet)
335         nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def)
336         props = nodetemplate.get_properties()
337         if 'cpu_frequency' in props.keys():
338             error = self.assertRaises(exception.ValidationError,
339                                       props['cpu_frequency'].validate)
340             self.assertEqual(_('The value "0.05 GHz" of property '
341                                '"cpu_frequency" must be greater than or equal '
342                                'to "0.1 GHz".'), error.__str__())
343         if 'disk_size' in props.keys():
344             error = self.assertRaises(exception.ValidationError,
345                                       props['disk_size'].validate)
346             self.assertEqual(_('The value "500 MB" of property "disk_size" '
347                                'must be greater than or equal to "1 GB".'),
348                              error.__str__())
349
350         if 'mem_size' in props.keys():
351             error = self.assertRaises(exception.ValidationError,
352                                       props['mem_size'].validate)
353             self.assertEqual(_('The value "1 MB" of property "mem_size" is '
354                                'out of range "(min:1 MiB, max:1 GiB)".'),
355                              error.__str__())