Пример #1
0
    def get(self,
            namespace,
            start=None,
            end=None,
            method=None,
            fill=None,
            group=None):
        """Make a query to retrieve something in the database."""
        if _validate_namespace(namespace):
            namespace, field = _extract_field(namespace)

        if not self._namespace_exists(namespace):
            error = (f'Error to get values because namespace \'{namespace}\''
                     'does not exist.')
            raise NamespaceError(error)

        if start is None and end is None:
            error = 'Start and end value should not be \'None\'.'
            raise ValueError(error)

        if iso_format_validation(start) is False and start is not None:
            start = convert_to_iso(start)
        if iso_format_validation(end) is False and end is not None:
            end = convert_to_iso(end)

        if validate_timestamp(start, end) is False:
            error = 'Error to get values due end value is smaller than start.'
            raise ValueError(error)

        points = self._get_points(namespace, start, end, field, method, fill,
                                  group)
        return points
Пример #2
0
    def test_event_delete_failed_namespace_wrong_prefix(
            self, mock_influx_del, mock_callback):
        """Test fail case in method event_delete with an invalid namespace."""
        namespace = 'telemetry.switches.1.interfaces.232.bytes_in'
        start = 123456
        end = 123457

        event = mock.MagicMock()
        event.content = {'namespace': namespace, 'start': start, 'end': end}

        # Values expected to call '_execute_callback'
        exc = NamespaceError()
        error = (exc.__class__.__name__, str(exc))
        result = None

        # Set a exception to mocked function return value
        mock_influx_del.side_effect = NamespaceError()

        self.napp.event_delete(event)
        mock_callback.assert_called_with(event, result, error)
Пример #3
0
def _validate_namespace(namespace):
    if not isinstance(namespace, str) or not re.match(r'\S+', namespace):
        error = 'Error. Namespace should be a string.'
        raise TypeError(error)

    if 'kytos.kronos' not in namespace:
        error = (f'Error. Namespace \'{namespace}\' must have the format '
                 '\'kytos.kronos.*\'')
        raise NamespaceError(error)

    return True
Пример #4
0
    def test_rest_delete_failed_namespace_wrong_prefix(self, mock_influx_del):
        """Test fail case in rest_delete with an invalid namespace."""
        namespace = 'telemetry.switches.1.interfaces.232.bytes_in'
        start = 123456
        end = 123457

        mock_influx_del.side_effect = NamespaceError()

        app = Flask(__name__)
        with app.app_context():
            response = self.napp.rest_delete(namespace, start, end)
            exception_name = response.json['exc_name']
            self.assertEqual(exception_name, 'NamespaceError')
Пример #5
0
    def test_rest_save_failed_namespace_without_prefix(self, mock_influx_save):
        """Test fail case in method rest_save with an invalid namespace."""
        namespace = 'telemetry.switches.1.interfaces.232.bytes_in'
        value = '123'
        timestamp = None

        mock_influx_save.side_effect = NamespaceError()

        app = Flask(__name__)
        with app.app_context():
            response = self.napp.rest_save(namespace, value, timestamp)
            exception_name = response.json['exc_name']
            self.assertEqual(exception_name, 'NamespaceError')
Пример #6
0
    def test_event_save_fail_with_invalid_namespace(self, mock_influx_save,
                                                    mock_callback):
        """Test fail case in method event_save with an invalid namespace."""
        namespace = 'telemetry.switches.1.interfaces.232.bytes_in'
        value = '123'
        timestamp = None

        event = mock.MagicMock()
        event.content = {'namespace': namespace,
                         'value': value,
                         'timestamp': timestamp}

        # Values expected to call '_execute_callback'
        exc = NamespaceError()
        error = (exc.__class__.__name__, str(exc))
        result = None

        # Set a exception to mocked function return value
        mock_influx_save.side_effect = NamespaceError()

        self.napp.event_save(event)
        mock_callback.assert_called_with(event, result, error)
Пример #7
0
    def test_rest_get_fail_invalid_namespace(self, mock_influx_get):
        """Test fail case in method rest_get passing an invalid namespace."""
        namespace = 'kronos.telemetry.switches.1.interfaces.232.bytes_in.12'
        start = 123456
        end = 123457

        mock_influx_get.side_effect = NamespaceError()

        app = Flask(__name__)
        with app.app_context():
            response = self.napp.rest_get(namespace, start, end)
            exception_name = response.json['exc_name']
            self.assertEqual(exception_name, 'NamespaceError')
Пример #8
0
    def delete(self, namespace, start=None, end=None):
        """Delete data in influxdb. Start and end must be a timestamp."""
        if iso_format_validation(start) is False and start is not None:
            start = convert_to_iso(start)
        if iso_format_validation(end) is False and end is not None:
            end = convert_to_iso(end)

        if _validate_namespace(namespace):
            namespace, _ = _extract_field(namespace)

        if not self._namespace_exists(namespace):
            error = (f'Error deleting because namespace \'{namespace}\' does'
                     'not exist.')
            raise NamespaceError(error)

        if validate_timestamp(start, end) is False:
            error = 'Error to get values due end value is smaller than start.'
            raise ValueError(error)

        return self._delete_points(namespace, start, end)