def test_get_middleware_returns_desired_object_and_setting(self, version, expected_setting): expected_middleware = ['Middlware'] settings_stub = Config() setattr(settings_stub, expected_setting, expected_middleware) with mock.patch('django.VERSION', version): with mock.patch('django.conf.settings', settings_stub): middleware, setting = get_middleware_and_setting_name() assert middleware == expected_middleware assert setting == expected_setting
def test_uninstrument_django_removes_configured_settings(self): instrument(django=True) uninstrument('django') assert not hasattr(settings, 'OPENTRACING_TRACED_ATTRIBUTES') assert not hasattr(settings, 'OPENTRACING_TRACER_CALLABLE') assert not hasattr(settings, 'OPENTRACING_TRACER_PARAMETERS') assert not hasattr(settings, 'OPENTRACING_TRACE_ALL') assert not hasattr(settings, 'OPENTRACING_SET_GLOBAL_TRACER') assert not hasattr(settings, 'OPENTRACING_TRACING') _, setting = get_middleware_and_setting_name() assert config.middleware_class not in getattr(settings, setting)
def test_instrument_django_set_global_tracer_with_provided_callable(self): config.set_global_tracer = True config.tracer_callable = 'opentracing.mocktracer.MockTracer' instrument(django=True) assert settings.OPENTRACING_SET_GLOBAL_TRACER is True assert settings.OPENTRACING_TRACER_CALLABLE == 'opentracing.mocktracer.MockTracer' assert settings.OPENTRACING_TRACER_PARAMETERS == {} assert not hasattr(settings, 'OPENTRACING_TRACING') assert settings.OPENTRACING_TRACE_ALL == config.trace_all assert settings.OPENTRACING_TRACED_ATTRIBUTES == config.traced_attributes _, setting = get_middleware_and_setting_name() assert config.middleware_class in getattr(settings, setting)
def test_instrument_django_unset_global_tracer_configures_desired_settings(self): tracer = MockTracer() config.set_global_tracer = False instrument(tracer, django=True) assert settings.OPENTRACING_SET_GLOBAL_TRACER is False assert isinstance(settings.OPENTRACING_TRACING._tracer, opentracing.Tracer) assert not hasattr(settings, 'OPENTRACING_TRACER_CALLABLE') assert not hasattr(settings, 'OPENTRACING_TRACER_PARAMETERS') assert settings.OPENTRACING_TRACE_ALL == config.trace_all assert settings.OPENTRACING_TRACED_ATTRIBUTES == config.traced_attributes _, setting = get_middleware_and_setting_name() assert config.middleware_class in getattr(settings, setting)
def test_instrument_django_set_global_tracer_with_provided_tracer_arg(self): tracer = MockTracer() config.set_global_tracer = True instrument(tracer, django=True) assert settings.OPENTRACING_SET_GLOBAL_TRACER is True assert not hasattr(settings, 'OPENTRACING_TRACER_CALLABLE') assert not hasattr(settings, 'OPENTRACING_TRACER_PARAMETERS') assert settings.OPENTRACING_TRACING.tracer is tracer assert settings.OPENTRACING_TRACE_ALL == config.trace_all assert settings.OPENTRACING_TRACED_ATTRIBUTES == config.traced_attributes _, setting = get_middleware_and_setting_name() assert config.middleware_class in getattr(settings, setting)
def test_middleware_untraced(self): client = Client() with mock.patch('opentracing.span.Span.set_tag') as set_tag: client.get('/one/') assert set_tag.called is False assert not hasattr(settings, 'OPENTRACING_TRACED_ATTRIBUTES') assert not hasattr(settings, 'OPENTRACING_TRACER_CALLABLE') assert not hasattr(settings, 'OPENTRACING_TRACER_PARAMETERS') assert not hasattr(settings, 'OPENTRACING_TRACE_ALL') assert not hasattr(settings, 'OPENTRACING_SET_GLOBAL_TRACER') assert not hasattr(settings, 'OPENTRACING_TRACING') assert not hasattr(settings, 'OPENTRACING_TRACER') _, setting = get_middleware_and_setting_name() assert config.middleware_class not in getattr(settings, setting)
def test_unset_global_tracer_installed_app_sanity(self): assert 'signalfx_tracing' in settings.INSTALLED_APPS assert config.middleware_class in get_middleware_and_setting_name()[0] assert config.tracer_callable == 'opentracing.mocktracer.MockTracer' assert config.tracer_parameters == dict(scope_manager=None) client = Client() client.get('/one/') tracer = settings.OPENTRACING_TRACING.tracer span = tracer.finished_spans().pop() span.tags['path'] = '/one/' span.tags['method'] = 'GET' assert settings.OPENTRACING_TRACER_CALLABLE == 'opentracing.mocktracer.MockTracer' assert settings.OPENTRACING_TRACER_PARAMETERS == dict(scope_manager=None) assert opentracing.tracer is not tracer
def test_django_config_determines_instrumented_setting_values_without_global_tracer(self): config.set_global_tracer = False desired_attributes = ['some', 'attributes'] config.traced_attributes = desired_attributes desired_trace_all = 123 config.trace_all = desired_trace_all desired_middleware_class = 'some.middleware' config.middleware_class = desired_middleware_class tracer = 'SomeTracer' instrument(tracer, django=True) assert settings.OPENTRACING_TRACING._tracer is tracer assert settings.OPENTRACING_TRACED_ATTRIBUTES == desired_attributes assert settings.OPENTRACING_TRACE_ALL == desired_trace_all _, setting = get_middleware_and_setting_name() assert desired_middleware_class in getattr(settings, setting) uninstrument('django') assert not hasattr(settings, 'OPENTRACING_TRACED_ATTRIBUTES') assert not hasattr(settings, 'OPENTRACING_TRACER_CALLABLE') assert not hasattr(settings, 'OPENTRACING_TRACE_ALL') assert desired_middleware_class not in getattr(settings, setting)