Fix error parsing scalar unit contraints

Return the correct error in some cases where the unit is not set.

Related-Bug #1897268

Change-Id: I83cfa7b8a26949026a6c51c50a5aec22eadd3725
This commit is contained in:
Miguel Caballer 2020-11-18 11:34:03 +01:00
parent b598d9cfe4
commit 0653750dd2
4 changed files with 29 additions and 9 deletions

View File

@ -278,8 +278,8 @@ class GreaterOrEqual(Constraint):
'comparable values.')))
def _is_valid(self, value):
if toscaparser.functions.is_function(value) or \
value >= self.constraint_value:
if value is not None and (toscaparser.functions.is_function(value) or
value >= self.constraint_value):
return True
return False

View File

@ -76,13 +76,16 @@ class ScalarUnit(object):
self.validate_scalar_unit()
regex = re.compile(r'([0-9.]+)\s*(\w+)')
result = regex.match(str(self.value)).groups()
converted = (float(validateutils.str_to_num(result[0]))
* self.SCALAR_UNIT_DICT[result[1]]
/ self.SCALAR_UNIT_DICT[unit])
if converted - int(converted) < 0.0000000000001:
converted = int(converted)
return converted
if regex.match(str(self.value)):
result = regex.match(str(self.value)).groups()
converted = (float(validateutils.str_to_num(result[0]))
* self.SCALAR_UNIT_DICT[result[1]]
/ self.SCALAR_UNIT_DICT[unit])
if converted - int(converted) < 0.0000000000001:
converted = int(converted)
return converted
else:
return None
class ScalarUnit_Size(ScalarUnit):

View File

@ -0,0 +1,10 @@
tosca_definitions_version: tosca_simple_yaml_1_0
topology_template:
node_templates:
my_block_storage:
type: BlockStorage
properties:
size: 10

View File

@ -1921,3 +1921,10 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml
exception.MissingRequiredFieldError,
lambda: Reservation(reservation[name]))
self.assertEqual(expectedmessage, err.__str__())
def test_scalar_unit_without_unit(self):
tpl_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/test_scalar_unit_without_unit.yaml")
self.assertRaises(exception.ValidationError,
lambda: ToscaTemplate(tpl_path))