def get_spec_version(spec): if not isinstance(spec, dict): msg = _('The provided spec is not a map.') raise exception.SpecValidationFailed(message=msg) if 'type' not in spec: msg = _("The 'type' key is missing from the provided spec map.") raise exception.SpecValidationFailed(message=msg) if 'version' not in spec: msg = _("The 'version' key is missing from the provided spec map.") raise exception.SpecValidationFailed(message=msg) return (spec['type'], six.text_type(spec['version']))
def validate(self): '''Validate the schema.''' for (k, s) in self._schema.items(): try: # validate through resolve self.resolve_value(k) except (TypeError, ValueError) as err: msg = _('Spec validation error (%(key)s): %(err)s') % dict( key=k, err=six.text_type(err)) raise exception.SpecValidationFailed(message=msg) for key in self._data: if key not in self._schema: msg = _('Unrecognizable spec item "%s"') % key raise exception.SpecValidationFailed(message=msg)
def resolve(self, value): try: return int(value) except (TypeError, ValueError): msg = _('The value "%s" cannot be converted into an ' 'integer.') % value raise exception.SpecValidationFailed(message=msg)
def test_profile_create_with_spec_validation_failed(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'create', True) body = { 'profile': { 'name': 'test_profile', 'spec': { 'type': 'test_profile_type', 'version': '1.0', 'properties': { 'param': 'value' }, }, 'permission': None, 'metadata': {}, } } req = self._post('/profiles', json.dumps(body)) msg = 'Spec validation error (param): value' error = senlin_exc.SpecValidationFailed(message=msg) mock_call = self.patchobject(rpc_client.EngineClient, 'call', side_effect=error) resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.create, req, tenant_id=self.project, body=body) mock_call.assert_called_once_with(req.context, ('profile_create', body['profile'])) self.assertEqual(400, resp.json['code']) self.assertEqual('SpecValidationFailed', resp.json['error']['type']) self.assertIsNone(resp.json['error']['traceback'])
def validate(self, value, context=None): if not isinstance(value, six.string_types): msg = _('The value "%s" cannot be converted into a ' 'string.') % value raise exception.SpecValidationFailed(message=msg) self.resolve(value) self.validate_constraints(value, self, context)
def validate_constraints(self, value, context=None, skipped=None): if not skipped: skipped = [] try: for constraint in self.constraints: if type(constraint) not in skipped: constraint.validate(value, context) except ValueError as ex: raise exception.SpecValidationFailed(message=six.text_type(ex))
def test_create_with_spec_validation_failed(self, mock_call, mock_enforce): self._mock_enforce_setup(mock_enforce, 'create', True) body = { 'trigger': { 'name': 'test_trigger', 'spec': { 'type': 'alarm_type', 'param': 'value' }, 'enabled': True, } } req = self._post('/triggers', jsonutils.dumps(body)) msg = 'Spec validation error (param): value' error = senlin_exc.SpecValidationFailed(message=msg) mock_call.side_effect = error resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.create, req, tenant_id=self.project, body=body) mock_call.assert_called_once_with(req.context, ('trigger_create', { 'name': 'test_trigger', 'description': None, 'spec': { 'type': 'alarm_type', 'param': 'value' }, 'enabled': True, 'state': None, 'severity': None })) self.assertEqual(400, resp.json['code']) self.assertEqual('SpecValidationFailed', resp.json['error']['type']) self.assertIsNone(resp.json['error']['traceback'])
def test_profile_update_with_spec_validation_failed(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'update', True) pid = 'aaaa-bbbb-cccc' body = { 'profile': { 'name': 'test_profile', 'spec': { 'param4': 'value4' }, } } req = self._put('/profiles/%(profile_id)s' % {'profile_id': pid}, json.dumps(body)) msg = 'Spec validation error (param): value' error = senlin_exc.SpecValidationFailed(message=msg) mock_call = self.patchobject(rpc_client.EngineClient, 'call', side_effect=error) resp = shared.request_with_middleware(fault.FaultWrapper, self.controller.update, req, tenant_id=self.project, profile_id=pid, body=body) expected_args = body['profile'] expected_args['permission'] = None expected_args['metadata'] = None expected_args['profile_id'] = pid mock_call.assert_called_once_with(req.context, ('profile_update', expected_args)) self.assertEqual(400, resp.json['code']) self.assertEqual('SpecValidationFailed', resp.json['error']['type']) self.assertIsNone(resp.json['error']['traceback'])
def resolve(self, value): if str(value).lower() not in ('true', 'false'): msg = _('The value "%s" is not a valid Boolean') % value raise exception.SpecValidationFailed(message=msg) return strutils.bool_from_string(value, strict=True)