コード例 #1
0
    def test_upload_invalid_spec_no_url(self):
        with self.assertRaises(KeyError):
            influxdb.upload({
                'type': 'InfluxDB',
                'raise-exceptions': True
            }, self.mock_collection)
            self.mock_logger.error.assert_called_once_with(
                "No url supplied to upload for InfluxDB.")

        self.mock_logger.reset_mock()
        influxdb.upload({'type': 'InfluxDB'}, self.mock_collection)
        self.mock_logger.error.assert_called_once_with(
            "No url supplied to upload for InfluxDB.")
コード例 #2
0
    def test_upload_bad_result_code_with_raise(self):
        class CustomException(Exception):
            pass

        def raise_(*args, **kwargs):
            raise CustomException('bang')

        mock_cm = mock.MagicMock()
        events = (
            ('1.log', '1'),
            ('2.log', '2'),
            ('1.log', '3'),
            ('2.log', '4'),
        )
        mock_cm.__enter__.return_value = iter(events)
        self.mock_collection.events.return_value = mock_cm

        mock_request_result = mock.Mock()
        mock_request_result.status_code = influxdb.requests.codes.not_found
        mock_request_result.raise_for_status.side_effect = raise_
        self.mock_requests.post.return_value = mock_request_result

        with mock.patch.object(influxdb,
                               'expand_vars',
                               wraps=influxdb.expand_vars):
            with self.assertRaises(CustomException):
                influxdb.upload(
                    {
                        'type': 'InfluxDB',
                        'url': '1.2.3.4:80',
                        'database': 'db-name',
                        'user': '******',
                        'password': '******',
                        'batch-size': 2,
                        'raise-exceptions': True
                    }, self.mock_collection)

            self.mock_requests.post.assert_called_once_with(
                '1.2.3.4:80/write?db=db-name&precision=u'
                '&u=a-user&p=a-password',
                data='1\n2')
            self.mock_logger.error.assert_called_once_with(
                'Batch upload failed.  status_code: %s', mock.ANY)
コード例 #3
0
    def test_upload_invalid_spec_bad_resolution(self):
        with self.assertRaises(KeyError):
            influxdb.upload(
                {
                    'type': 'InfluxDB',
                    'url': '1.2.3.4:80',
                    'database': 'db-name',
                    'timestamp-resolution': 'p',
                    'raise-exceptions': True
                }, self.mock_collection)
            self.mock_logger.error.assert_called_once_with(mock.ANY, 'p')

        self.mock_logger.reset_mock()
        influxdb.upload(
            {
                'type': 'InfluxDB',
                'url': '1.2.3.4:80',
                'database': 'db-name',
                'timestamp-resolution': 'p'
            }, self.mock_collection)
        self.mock_logger.error.assert_called_once_with(mock.ANY, 'p')
コード例 #4
0
    def test_upload_successful(self):
        mock_cm = mock.MagicMock()
        events = (
            ('1.log', '1'),
            ('2.log', '2'),
            ('1.log', '3'),
            ('2.log', '4'),
        )
        mock_cm.__enter__.return_value = iter(events)
        self.mock_collection.events.return_value = mock_cm

        mock_request_result = mock.Mock()
        mock_request_result.status_code = influxdb.requests.codes.ok
        self.mock_requests.post.return_value = mock_request_result

        with mock.patch.object(influxdb,
                               'expand_vars',
                               wraps=influxdb.expand_vars) as mock_expand_vars:
            influxdb.upload(
                {
                    'type': 'InfluxDB',
                    'url': '1.2.3.4:80',
                    'database': 'db-name',
                    'user': '******',
                    'password': '******',
                    'batch-size': 2,
                    'raise-exceptions': True
                }, self.mock_collection)
            mock_expand_vars.assert_has_calls(
                (mock.call({}, '1.2.3.4:80'), mock.call({}, 'db-name'),
                 mock.call({}, 'a-user'), mock.call({}, 'a-password')))
            self.mock_requests.post.assert_has_calls(
                (mock.call(
                    '1.2.3.4:80/write?db=db-name&precision=u'
                    '&u=a-user&p=a-password',
                    data='1\n2'),
                 mock.call(
                     '1.2.3.4:80/write?db=db-name&precision=u'
                     '&u=a-user&p=a-password',
                     data='3\n4')))
コード例 #5
0
    def test_upload_bad_result_code(self):
        mock_cm = mock.MagicMock()
        events = (
            ('1.log', '1'),
            ('2.log', '2'),
            ('1.log', '3'),
            ('2.log', '4'),
        )
        mock_cm.__enter__.return_value = iter(events)
        self.mock_collection.events.return_value = mock_cm

        mock_request_result = mock.Mock()
        mock_request_result.status_code = influxdb.requests.codes.not_found
        self.mock_requests.post.return_value = mock_request_result

        with mock.patch.object(influxdb,
                               'expand_vars',
                               wraps=influxdb.expand_vars):
            influxdb.upload(
                {
                    'type': 'InfluxDB',
                    'url': '1.2.3.4:80',
                    'database': 'db-name',
                    'user': '******',
                    'password': '******',
                    'batch-size': 2,
                    'raise-exceptions': False
                }, self.mock_collection)

            self.mock_requests.post.assert_called_once_with(
                '1.2.3.4:80/write?db=db-name&precision=u'
                '&u=a-user&p=a-password',
                data='1\n2')
            self.mock_logger.error.assert_has_calls((
                mock.call('Batch upload failed.  status_code: %s', mock.ANY),
                mock.call('Abandoning batch upload to InfluxDB'),
            ))
コード例 #6
0
    def test_upload_exception_on_post(self):
        class CustomException(Exception):
            pass

        def raise_(*args, **kwargs):
            raise CustomException('bang')

        mock_cm = mock.MagicMock()
        events = (
            ('1.log', '1'),
            ('2.log', '2'),
            ('1.log', '3'),
            ('2.log', '4'),
        )
        mock_cm.__enter__.return_value = iter(events)
        self.mock_collection.events.return_value = mock_cm
        self.mock_requests.post.side_effect = raise_

        with mock.patch.object(influxdb,
                               'expand_vars',
                               wraps=influxdb.expand_vars):
            influxdb.upload(
                {
                    'type': 'InfluxDB',
                    'url': '1.2.3.4:80',
                    'database': 'db-name',
                    'user': '******',
                    'password': '******',
                    'batch-size': 2,
                    'raise-exceptions': False
                }, self.mock_collection)

            self.mock_requests.post.assert_called_once_with(
                '1.2.3.4:80/write?db=db-name&precision=u'
                '&u=a-user&p=a-password',
                data='1\n2')
            self.mock_logger.error.assert_called_once_with(mock.ANY, 'bang')
コード例 #7
0
 def test_upload_invalid_spec_s3(self):
     with self.assertRaises(AssertionError):
         influxdb.upload({'type': 's3'}, self.mock_collection)