def setUp(self): configurator = Configurator() configurator.add_route("example", "/example", request_method="GET") configurator.add_route("trace_context", "/trace_context", request_method="GET") configurator.add_view(example_application, route_name="example", renderer="json") configurator.add_view(local_tracing_within_context, route_name="trace_context", renderer="json") configurator.add_view(render_exception_view, context=ControlFlowException, renderer="json") configurator.add_view(render_bad_exception_view, context=ControlFlowException2, renderer="json") mock_filewatcher = mock.Mock(spec=FileWatcher) mock_filewatcher.get_data.return_value = { "secrets": { "secret/authentication/public-key": { "type": "versioned", "current": AUTH_TOKEN_PUBLIC_KEY, } }, "vault": { "token": "test", "url": "http://vault.example.com:8200/" }, } secrets = SecretsStore("/secrets") secrets._filewatcher = mock_filewatcher self.observer = mock.Mock(spec=BaseplateObserver) self.server_observer = mock.Mock(spec=ServerSpanObserver) def _register_mock(context, server_span): server_span.register(self.server_observer) self.observer.on_server_span_created.side_effect = _register_mock self.baseplate = Baseplate() self.baseplate.register(self.observer) self.baseplate_configurator = BaseplateConfigurator( self.baseplate, trust_trace_headers=True, edge_context_factory=EdgeRequestContextFactory(secrets), ) configurator.include(self.baseplate_configurator.includeme) self.context_init_event_subscriber = mock.Mock() configurator.add_subscriber(self.context_init_event_subscriber, ServerSpanInitialized) app = configurator.make_wsgi_app() self.test_app = webtest.TestApp(app)
def http_server(gevent_socket): class HttpServer: def __init__(self, address): self.url = f"http://{address[0]}:{address[1]}/" self.requests = [] def handle_request(self, request): self.requests.append(request) return HTTPNoContent() server_bind_endpoint = config.Endpoint("127.0.0.1:0") listener = make_listener(server_bind_endpoint) server_address = listener.getsockname() http_server = HttpServer(server_address) baseplate = Baseplate() trust_handler = StaticTrustHandler(trust_headers=True) baseplate_configurator = BaseplateConfigurator( baseplate, header_trust_handler=trust_handler) configurator = Configurator() configurator.include(baseplate_configurator.includeme) configurator.add_route("test_view", "/") configurator.add_view(http_server.handle_request, route_name="test_view", renderer="json") wsgi_app = configurator.make_wsgi_app() server = make_server({"stop_timeout": "1 millisecond"}, listener, wsgi_app) server_greenlet = gevent.spawn(server.serve_forever) try: yield http_server finally: server_greenlet.kill()
def setUp(self): thread_patch = mock.patch("threading.Thread", autospec=True) thread_patch.start() self.addCleanup(thread_patch.stop) configurator = Configurator() configurator.add_route("example", "/example", request_method="GET") configurator.add_view(example_application, route_name="example", renderer="json") configurator.add_route("local_test", "/local_test", request_method="GET") configurator.add_view(local_parent_trace_within_context, route_name="local_test", renderer="json") self.client = make_client("test-service") self.observer = TraceBaseplateObserver(self.client) self.baseplate = Baseplate() self.baseplate.register(self.observer) self.baseplate_configurator = BaseplateConfigurator( self.baseplate, header_trust_handler=StaticTrustHandler(trust_headers=True), ) configurator.include(self.baseplate_configurator.includeme) app = configurator.make_wsgi_app() self.local_span_ids = [] self.local_span_observers = [] self.test_app = webtest.TestApp(app)
def make_wsgi_app(app_config): baseplate = Baseplate() baseplate.configure_observers(app_config) configurator = Configurator(settings=app_config) configurator.include(BaseplateConfigurator(baseplate).includeme) configurator.add_route("hello_world", "/", request_method="GET") configurator.scan() return configurator.make_wsgi_app()
def setUp(self): configurator = Configurator() configurator.add_route("example", "/example", request_method="GET") configurator.add_route("trace_context", "/trace_context", request_method="GET") configurator.add_view(example_application, route_name="example", renderer="json") configurator.add_view(local_tracing_within_context, route_name="trace_context", renderer="json") configurator.add_view(render_exception_view, context=ControlFlowException, renderer="json") configurator.add_view(render_bad_exception_view, context=ControlFlowException2, renderer="json") self.observer = mock.Mock(spec=BaseplateObserver) self.server_observer = mock.Mock(spec=ServerSpanObserver) def _register_mock(context, server_span): server_span.register(self.server_observer) self.observer.on_server_span_created.side_effect = _register_mock self.baseplate = Baseplate() self.baseplate.register(self.observer) self.baseplate_configurator = BaseplateConfigurator( self.baseplate, edge_context_factory=FakeEdgeContextFactory(), header_trust_handler=StaticTrustHandler(trust_headers=True), ) configurator.include(self.baseplate_configurator.includeme) self.context_init_event_subscriber = mock.Mock() configurator.add_subscriber(self.context_init_event_subscriber, ServerSpanInitialized) app = configurator.make_wsgi_app() self.test_app = webtest.TestApp(app)