Exemplo n.º 1
0
    def test_should_fail_too_empty_name(self):
        dimensions = {'': 1}
        with self.assertRaises(exceptions.HTTPUnprocessableEntity) as context:
            validation.validate_dimensions(dimensions)

        msg = 'Dimension name cannot be empty'
        self.assertEqual(context.exception.description, msg)
Exemplo n.º 2
0
    def _get_dimensions(self, log_element, global_dims=None):
        """Get the dimensions of log element.

        If global dimensions are specified and passed to this method,
        both instances are merged with each other.

        If neither is specified empty dictionary is returned.

        If only local dimensions are specified they are returned without any
        additional operations. The last statement applies also
        to global dimensions.

        :param dict log_element: raw log instance
        :param dict global_dims: global dimensions or None
        :return: local dimensions merged with global dimensions
        :rtype: dict
        """
        local_dims = log_element.get('dimensions', {})

        if not global_dims:
            global_dims = {}
        if local_dims:
            validation.validate_dimensions(local_dims)

        dimensions = global_dims.copy()
        dimensions.update(local_dims)

        return dimensions
Exemplo n.º 3
0
    def test_should_fail_ok_name_empty_value(self):
        name = 'monasca'
        dimensions = {name: ''}
        with self.assertRaises(exceptions.HTTPUnprocessableEntity) as context:
            validation.validate_dimensions(dimensions)

        msg = 'Dimension value cannot be empty'
        self.assertEqual(context.exception.description, msg)
Exemplo n.º 4
0
    def test_should_fail_underscore_at_begin(self):
        name = '_aDim'
        dimensions = {name: 1}
        with self.assertRaises(exceptions.HTTPUnprocessableEntity) as context:
            validation.validate_dimensions(dimensions)

        msg = 'Dimension name %s cannot start with underscore (_)' % name
        self.assertEqual(context.exception.description, msg)
Exemplo n.º 5
0
    def test_should_fail_too_long_name(self):
        name = testing.rand_string(256, 260)
        dimensions = {name: 1}
        with self.assertRaises(exceptions.HTTPUnprocessableEntity) as context:
            validation.validate_dimensions(dimensions)

        msg = 'Dimension name %s must be 255 characters or less' % name
        self.assertEqual(context.exception.description, msg)
Exemplo n.º 6
0
    def test_should_fail_invalid_chars(self):
        name = '<>'
        dimensions = {name: 1}
        with self.assertRaises(exceptions.HTTPUnprocessableEntity) as context:
            validation.validate_dimensions(dimensions)

        invalid_chars = '> < = { } ( ) \' " , ; &'
        msg = 'Dimension name %s may not contain: %s' % (name, invalid_chars)
        self.assertEqual(context.exception.description, msg)
Exemplo n.º 7
0
    def _get_dimensions(self, log_element, global_dims):
        """Get the dimensions in the log element."""
        local_dims = log_element.get('dimensions', {})
        if local_dims:
            validation.validate_dimensions(local_dims)
            if global_dims:
                dimensions = global_dims.copy()
                dimensions.update(local_dims)
            else:
                dimensions = local_dims
        else:
            dimensions = global_dims

        return dimensions
Exemplo n.º 8
0
    def new_log(self,
                application_type,
                dimensions,
                payload,
                content_type='application/json',
                validate=True):
        """Creates new log object.

        :param str application_type: origin of the log
        :param dict dimensions: dictionary of dimensions (any data sent to api)
        :param stream payload: stream to read log entry from
        :param str content_type: actual content type used to send data to
                                 server
        :param bool validate: by default True, marks if log should be validated
        :return: log object
        :rtype: dict

        :keyword: log_object
        """

        payload = rest_utils.read_body(payload, content_type)
        if not payload:
            return None

        # normalize_yet_again
        application_type = parse_application_type(application_type)
        dimensions = parse_dimensions(dimensions)

        if validate:
            self._log.debug('Validation enabled, proceeding with validation')
            validation.validate_application_type(application_type)
            validation.validate_dimensions(dimensions)

        self._log.debug(
            'application_type=%s,dimensions=%s' % (
                application_type, dimensions)
        )

        log_object = {}
        if content_type == 'application/json':
            log_object.update(payload)
        else:
            log_object.update({'message': payload})

        validation.validate_log_message(log_object)

        dimensions['component'] = application_type
        log_object.update({'dimensions': dimensions})

        return log_object
Exemplo n.º 9
0
 def _get_global_dimensions(self, request_body):
     """Get the top level dimensions in the HTTP request body."""
     global_dims = request_body.get('dimensions', {})
     validation.validate_dimensions(global_dims)
     return global_dims
Exemplo n.º 10
0
 def _get_global_dimensions(request_body):
     """Get the top level dimensions in the HTTP request body."""
     global_dims = request_body.get('dimensions', {})
     validation.validate_dimensions(global_dims)
     return global_dims
Exemplo n.º 11
0
 def test_should_pass_ok_name_ok_value_service_SERVICE_DIMENSIONS_as_name(
         self):
     name = 'some_name'
     value = '1'
     dimensions = {name: value}
     validation.validate_dimensions(dimensions)
Exemplo n.º 12
0
 def test_should_pass_ok_name_ok_value_empty_service(self):
     name = 'monasca'
     value = '1'
     dimensions = {name: value}
     validation.validate_dimensions(dimensions)
Exemplo n.º 13
0
 def test_should_pass_for_empty_dimensions_array(self):
     validation.validate_dimensions({})
Exemplo n.º 14
0
 def test_should_fail_pass_for_non_iterable_dimensions_number(self):
     validation.validate_dimensions(1)
Exemplo n.º 15
0
 def test_should_fail_for_none_dimensions(self):
     validation.validate_dimensions(None)