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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
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
23 class ScalarUnitPositiveTest(TestCase):
27 # tpl_snippet with mem_size given as number+space+MB
28 'mem_size_is_number_Space_MB',
31 type: tosca.nodes.Compute
41 # tpl_snippet with mem_size given as number+spaces+GB
42 'mem_size_is_number_Space_GB',
45 type: tosca.nodes.Compute
55 # tpl_snippet with mem_size given as number+tiB
56 'mem_size_is_number_NoSpace_GB',
59 type: tosca.nodes.Compute
69 # tpl_snippet with mem_size given as number+Spaces+GIB
70 'mem_size_is_number_Spaces_GB',
73 type: tosca.nodes.Compute
83 # tpl_snippet with mem_size given as number+Space+tib
84 'mem_size_is_number_Spaces_GB',
87 type: tosca.nodes.Compute
97 'cpu_frequency_is_float_Space_GHz',
100 type: tosca.nodes.Compute
104 cpu_frequency: 2.5 GHz
106 property='cpu_frequency',
110 'cpu_frequency_is_float_Space_MHz',
113 type: tosca.nodes.Compute
117 cpu_frequency: 800 MHz
119 property='cpu_frequency',
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)
137 class GetNumFromScalarUnitSizePositive(TestCase):
140 ( # Note that (1 TB) / (1 GB) = 1000
141 'Input is TB, user input is GB',
142 dict(InputMemSize='1 TB',
146 ( # Note that (1 Tib)/ (1 GB) = 1099
147 'Input is TiB, user input is GB',
148 dict(InputMemSize='1 TiB',
150 expected=1099.511627776)
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)
160 class GetNumFromScalarUnitFrequencyPositive(TestCase):
163 ( # Note that (1 GHz) / (1 Hz) = 1000000000
164 'Input is GHz, user input is Hz',
165 dict(InputMemSize='1 GHz',
170 'Input is GHz, user input is Hz',
171 dict(InputMemSize='2.4 GHz',
175 ( # Note that (1 GHz)/ (1 MHz) = 1000
176 'Input is MHz, user input is GHz',
177 dict(InputMemSize='800 MHz',
182 'Input is GHz, user input is Hz',
183 dict(InputMemSize='0.9 GHz',
188 'Input is GHz, user input is Hz',
189 dict(InputMemSize='2.7GHz',
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)
201 class GetNumFromScalarUnitTimePositive(TestCase):
204 ( # Note that (1 s) / (1 ms) = 1000
205 'Input is 500ms, user input is s',
206 dict(InputMemSize='500 ms',
210 ( # Note that (1 h)/ (1 s) = 3600
211 'Input is h, user input is s',
212 dict(InputMemSize='1 h',
216 ( # Note that (1 m)/ (1 s) = 60
217 'Input is m, user input is s',
218 dict(InputMemSize='0.5 m',
222 ( # Note that (1 d)/ (1 h) = 24
223 'Input is d, user input is h',
224 dict(InputMemSize='1 d',
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)
236 class GetNumFromScalarUnitSizeNegative(TestCase):
238 InputMemSize = '1 GB'
241 def test_get_num_from_scalar_unit_size_negative(self):
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\']".'),
253 class GetNumFromScalarUnitFrequencyNegative(TestCase):
255 InputFrequency = '2.7 GHz'
258 def test_get_num_from_scalar_unit_frequency_negative(self):
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\']".'),
269 class GetNumFromScalarUnitTimeNegative(TestCase):
274 def test_get_num_from_scalar_unit_frequency_negative(self):
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.'),
284 class ScalarUnitNegativeTest(TestCase):
286 custom_def_snippet = '''
287 tosca.my.nodes.Compute:
288 derived_from: tosca.nodes.Root
292 type: scalar-unit.frequency
294 - greater_or_equal: 0.1 GHz
297 type: scalar-unit.size
299 - greater_or_equal: 1 GB
302 type: scalar-unit.size
304 - in_range: [1 MiB, 1 GiB]
306 custom_def = yamlparser.simple_parse(custom_def_snippet)
308 # disk_size doesn't provide a value, mem_size uses an invalid unit.
309 def test_invalid_scalar_unit(self):
312 type: tosca.my.nodes.Compute
314 cpu_frequency: 50.3.6 GHZ
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)
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):
328 type: tosca.my.nodes.Compute
330 cpu_frequency: 0.05 GHz
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".'),
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)".'),