Exemplo n.º 1
0
    def test_config_django_available_main_settings_preferred(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        django_conf = mock.MagicMock()
        django_conf.settings.DEBUG = True
        django_conf.settings.METRICS = {
            'version':
            2,
            'publishers': [
                {
                    'path':
                    'tests.unit.recorders.test_default:MockPublisherExtra'
                },
            ]
        }
        django_conf.settings.SOA_SERVER_SETTINGS = {
            'metrics': {
                'kwargs': {
                    'config': {
                        'version':
                        2,
                        'publishers': [
                            {
                                'path':
                                'tests.unit.recorders.test_default:MockPublisher'
                            },
                        ]
                    }
                }
            }
        }

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }):
            recorder = DefaultMetricsRecorder('me')

            assert recorder.is_configured is True
            assert recorder._configuration is not None
            assert len(recorder._configuration.publishers) == 1
            assert recorder._configuration.publishers[0] is mock_publisher_extra
            assert recorder._configuration.version == 2

            recorder = DefaultMetricsRecorder('you')

            assert recorder.is_configured is True
            assert recorder._configuration is not None
            assert len(recorder._configuration.publishers) == 1
            assert recorder._configuration.publishers[0] is mock_publisher_extra
            assert recorder._configuration.version == 2
Exemplo n.º 2
0
    def test_publish_no_config(self, mock_publish_metrics):
        recorder = DefaultMetricsRecorder('oops')
        recorder.counter('foo.bar').increment()
        recorder.timer('baz.qux').set(15)

        recorder.publish_all()

        assert recorder.get_all_metrics() == []

        assert mock_publish_metrics.call_count == 0
Exemplo n.º 3
0
    def test_config_explicit(self):
        recorder = DefaultMetricsRecorder(
            'me',
            config={
                'version':
                2,
                'publishers': [
                    {
                        'path':
                        'tests.unit.recorders.test_default:MockPublisher'
                    },
                ],
            })
        assert recorder.is_configured is True

        assert recorder._configuration is not None
        assert len(recorder._configuration.publishers) == 1
        assert recorder._configuration.publishers[0] is mock_publisher

        # re-config should do nothing
        recorder.configure({'bad_config': 'bad_value'})
Exemplo n.º 4
0
 def _recorder(
     prefix,
     meta=False
 ):  # type: (Optional[six.text_type], bool) -> DefaultMetricsRecorder
     return DefaultMetricsRecorder(
         prefix,
         config={
             'version':
             2,
             'enable_meta_metrics':
             meta,
             'publishers': [
                 {
                     'path':
                     'tests.unit.recorders.test_default:MockPublisher'
                 },
             ],
         })
Exemplo n.º 5
0
    def test_config_django_available_but_settings_missing(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        django_conf = mock.MagicMock()
        django_conf.settings.DEBUG = True
        del django_conf.settings.METRICS
        del django_conf.settings.SOA_SERVER_SETTINGS

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }):
            recorder = DefaultMetricsRecorder('me')

        assert recorder.is_configured is False
Exemplo n.º 6
0
    def test_config_django_available_but_settings_false(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        django_conf = mock.MagicMock()
        if six.PY2:
            django_conf.settings.__nonzero__.return_value = False
        else:
            django_conf.settings.__bool__.return_value = False

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }):
            recorder = DefaultMetricsRecorder('me')

        assert recorder.is_configured is False
Exemplo n.º 7
0
    def test_config_django_available_but_settings_broken2(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        class S(object):
            def __getattr__(self, item):
                raise FakeImproperlyConfigured()

        django_conf = mock.MagicMock()
        django_conf.settings = S()

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }):
            recorder = DefaultMetricsRecorder('me')

        assert recorder.is_configured is False
Exemplo n.º 8
0
    def test_config_django_causes_conformity_import_error(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        django_conf = mock.MagicMock()
        e = ValidationError(
            "Invalid keyword arguments:\n  - middleware.0.path: ImportError: cannot import name 'baz' from 'foo.bar' "
        )
        if six.PY2:
            django_conf.settings.__nonzero__.side_effect = e
        else:
            django_conf.settings.__bool__.side_effect = e

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }):
            recorder = DefaultMetricsRecorder('me')

        assert recorder.is_configured is False
Exemplo n.º 9
0
    def test_config_django_causes_conformity_other_error(self):
        django_exceptions = mock.MagicMock()
        django_exceptions.ImproperlyConfigured = FakeImproperlyConfigured

        django_conf = mock.MagicMock()
        e = ValidationError(
            "Invalid keyword arguments:\n  - middleware.0.path: Some other error that isn't an import error "
        )
        if six.PY2:
            django_conf.settings.__nonzero__.side_effect = e
        else:
            django_conf.settings.__bool__.side_effect = e

        with mock.patch.dict(
                'sys.modules', {
                    'django': mock.MagicMock(),
                    'django.conf': django_conf,
                    'django.core': mock.MagicMock(),
                    'django.core.exceptions': django_exceptions,
                }), pytest.raises(ValidationError) as error_context:
            DefaultMetricsRecorder('me')

        assert error_context.value is e
Exemplo n.º 10
0
 def test_config_no_config(self):
     recorder = DefaultMetricsRecorder('me')
     assert recorder.is_configured is False