def test_get_num_from_scalar_unit_size_negative(self): try: Constraint.get_num_from_scalar_unit_size(self.InputMemSize, self.UserInputUnit) except Exception as error: self.assertTrue(isinstance(error, ValueError)) self.assertEqual('input unit "qB" is not a valid unit', error.__str__())
def _validate_datatype(self): dtype = self.schema['type'] if dtype == self.STRING: return Constraint.validate_string(self.value) elif dtype == self.INTEGER: return Constraint.validate_integer(self.value) elif dtype == self.NUMBER: return Constraint.validate_number(self.value) elif dtype == self.LIST: return Constraint.validate_list(self.value)
def test_validvalues_validate_fail(self): schema = {'valid_values': [2, 4, 6, 8]} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 5) self.assertEqual('prop: 5 is not an valid value "[2, 4, 6, 8]".', str(error))
def test_pattern_validate_fail(self): schema = {'pattern': '[0-9]*'} constraint = Constraint('prop', Schema.STRING, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 'abc') self.assertEqual('prop: "abc" does not match pattern "[0-9]*".', str(error))
def test_max_length_validate_fail(self): schema = {'max_length': 4} constraint = Constraint('prop', Schema.STRING, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 'abcde') self.assertEqual('length of prop: abcde must be no greater than "4".', str(error))
def handle_properties(self): tosca_props = self._get_tosca_props( self.nodetemplate.get_properties_objects()) objectstore_props = {} container_quota = {} skip_check = False for key, value in tosca_props.items(): if key == "store_name": objectstore_props["name"] = value elif key == "store_size" or key == "store_maxsize": #currently heat is not supporting dynamically increase #the container quota-size. #if both defined in tosca template, consider store_maxsize. if skip_check: continue quota_size = None if "store_maxsize" in tosca_props.keys(): quota_size = tosca_props["store_maxsize"] else: quota_size = tosca_props["store_size"] container_quota["Quota-Bytes"] = \ Constraint.get_num_from_scalar_unit_size(quota_size) objectstore_props["X-Container-Meta"] = container_quota skip_check = True objectstore_props["X-Container-Read"] = '".r:*"' self.properties = objectstore_props
def test_greater_than_validate_fail(self): schema = {'greater_than': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 3) self.assertEqual('prop: 3 must be greater than "4".', str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, 4) self.assertEqual('prop: 4 must be greater than "4".', str(error))
def test_greater_or_equal_validate_fail(self): schema = {'greater_or_equal': 3.9} constraint = Constraint('prop', Schema.FLOAT, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 3.0) self.assertEqual('prop: 3.0 must be greater or equal to "3.9".', str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, 3.8) self.assertEqual('prop: 3.8 must be greater or equal to "3.9".', str(error))
def test_less_than_validate_fail(self): schema = {'less_than': datetime.date(2014, 0o7, 25)} constraint = Constraint('prop', Schema.TIMESTAMP, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, datetime.date(2014, 0o7, 25)) self.assertEqual('prop: 2014-07-25 must be ' 'less than "2014-07-25".', str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, datetime.date(2014, 0o7, 27)) self.assertEqual('prop: 2014-07-27 must be ' 'less than "2014-07-25".', str(error))
def test_in_range_validate(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(2)) self.assertIsNone(constraint.validate(4)) self.assertIsNone(constraint.validate(6))
def test_equal_validate_fail(self): schema = {'equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 8) self.assertEqual('prop: 8 is not equal to "4".', str(error))
def test_less_or_equal_validate_fail(self): schema = {'less_or_equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 5) self.assertEqual('prop: 5 must be less or equal to "4".', str(error))
def test_validvalues_validate(self): schema = {'valid_values': [2, 4, 6, 8]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(4))
def test_max_length_validate(self): schema = {'max_length': 4} constraint = Constraint('prop', Schema.STRING, schema) self.assertIsNone(constraint.validate('abcd')) self.assertIsNone(constraint.validate('abc'))
def test_less_than_validate(self): schema = {'less_than': datetime.date(2014, 0o7, 25)} constraint = Constraint('prop', Schema.TIMESTAMP, schema) self.assertIsNone(constraint.validate(datetime.date(2014, 0o7, 20))) self.assertIsNone(constraint.validate(datetime.date(2014, 0o7, 24)))
def test_greater_than_validate(self): schema = {'greater_than': 4} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(6))
def test_pattern_validate(self): schema = {'pattern': '[0-9]*'} constraint = Constraint('prop', Schema.STRING, schema) self.assertIsNone(constraint.validate('123'))
def test_greater_or_equal_validate(self): schema = {'greater_or_equal': 3.9} constraint = Constraint('prop', Schema.FLOAT, schema) self.assertIsNone(constraint.validate(3.9)) self.assertIsNone(constraint.validate(4.0))
def test_in_range_validate_fail(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 8) self.assertEqual('prop: 8 is out of range (min:2, max:6).', str(error))
def test_less_or_equal_validate(self): schema = {'less_or_equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(4)) self.assertIsNone(constraint.validate(3))
def test_scenario_get_num_from_scalar_unit_size(self): resolved = Constraint.get_num_from_scalar_unit_size(self.InputMemSize, self.UserInputUnit) self.assertEqual(resolved, self.expected)
def test_scenario_get_num_from_scalar_unit_size(self): resolved = Constraint.get_num_from_scalar_unit_size( self.InputMemSize, self.UserInputUnit) self.assertEqual(resolved, self.expected)
def validate_datatype(type, value, entry_schema=None, custom_def=None): '''Validate value with given type. If type is list or map, validate its entry by entry_schema(if defined) If type is a user-defined complex datatype, custom_def is required. ''' if type == Schema.STRING: return Constraint.validate_string(value) elif type == Schema.INTEGER: return Constraint.validate_integer(value) elif type == Schema.FLOAT: return Constraint.validate_float(value) elif type == Schema.NUMBER: return Constraint.validate_number(value) elif type == Schema.BOOLEAN: return Constraint.validate_boolean(value) elif type == Schema.TIMESTAMP: return Constraint.validate_timestamp(value) elif type == Schema.LIST: Constraint.validate_list(value) if entry_schema: DataEntity.validate_entry(value, entry_schema, custom_def) return value elif type == Schema.SCALAR_UNIT_SIZE: return Constraint.validate_scalar_unit_size(value) elif type == Schema.MAP: Constraint.validate_map(value) if entry_schema: DataEntity.validate_entry(value, entry_schema, custom_def) return value else: data = DataEntity(type, value, custom_def) return data.validate()
def _validate_constraints(self): constraints = self.constraints if constraints: for constraint in constraints: Constraint(self.name, self.value, constraint).validate()
def test_in_range_min_max(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertEqual(2, constraint.min) self.assertEqual(6, constraint.max)