Ejemplo n.º 1
0
    def server_thread(self, server_created):
        """Ask HTTP server start processing requests

        This function is supposed to be run in separate thread.
        """

        # pylint: disable=line-too-long
        # params: https://github.com/Pylons/waitress/blob/master/waitress/adjustments.py#L79
        self._server = StopableWSGIServer(
            host=self.address,
            port=self.port,
            threads=1,
            expose_tracebacks=False,  #True,
            application=self._build_web_app())
        server_created.set()
        self._server.run()
Ejemplo n.º 2
0
    def customized_web_server_inner(overrides: dict = None) -> str:

        global _customized_web_server_port

        old_settings = app.registry.settings.copy()

        if overrides:
            app.registry.settings.update(overrides)

        port = customized_port or _customized_web_server_port
        host_base = "http://localhost:{}".format(port)
        logger.debug("Opening a test web server at %s", host_base)

        server = StopableWSGIServer.create(app, host="localhost", port=port)
        server.wait()

        _customized_web_server_port += 1

        def teardown():
            # Restore old settings
            app.registry.settings = old_settings

            # Shutdown server thread
            server.shutdown()

        request.addfinalizer(teardown)
        return host_base
Ejemplo n.º 3
0
    def customized_web_server_inner(overrides:dict =None) -> str:

        global _customized_web_server_port

        old_settings = app.registry.settings.copy()

        if overrides:
            app.registry.settings.update(overrides)

        port = customized_port or _customized_web_server_port
        host_base = "http://localhost:{}".format(port)
        logger.debug("Opening a test web server at %s", host_base)

        server = StopableWSGIServer.create(app, host="localhost", port=port)
        server.wait()

        _customized_web_server_port += 1

        def teardown():
            # Restore old settings
            app.registry.settings = old_settings

            # Shutdown server thread
            server.shutdown()

        request.addfinalizer(teardown)
        return host_base
Ejemplo n.º 4
0
    def serve(self):
        self.app = Flask('RESTConsumer')  # noqa
        try:
            handler = self.app.logger.handlers[0]
        except IndexError:
            handler = logging.StreamHandler()
        finally:
            handler.setFormatter(logging.Formatter(
                '%(asctime)s [ConsumerAPI] %(levelname)-8s %(message)s'))
            self.app.logger.addHandler(handler)
            log_level = logging.getLevelName(self.settings
                                             .get('log_level', 'DEBUG'))
            self.app.logger.setLevel(log_level)

        self.app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True

        server_ip = self.settings.get('server_ip', '0.0.0.0')
        server_port = int(self.settings.get('EXPOSE_PORT', 9013))
        self.admin_name = self.settings.get('ADMIN_USER', 'admin')
        self.admin_password = self.settings.get('ADMIN_PW', 'password')
        self.http = StopableWSGIServer.create(
            self.app.wsgi_app,
            port=server_port,
            host=server_ip
        )
        self.app.logger.debug('Http Serve start.')
        self.add_endpoints()
        self.app.logger.debug(f'Http Live on {server_ip} : {server_port}')
Ejemplo n.º 5
0
def setUpModule():
    global browser, server

    inipath = os.path.abspath(
        os.environ.get('TEST_INI', 'testing.ini'))
    if not os.path.isfile(inipath):
        raise RuntimeError(
            'Cannot find INI file to setup selenium tests. '
            'Please specify the path via the TEST_INI environment variable '
            'or by adding a testing.ini file to the current directory.')

    parser = ConfigParser()
    parser.read(inipath)

    config.update(parser.items('testconfig'))
    config['test_providers'] = splitlines(config['test_providers'])

    app = get_app(inipath)
    port = int(config['app_port'])
    server = StopableWSGIServer.create(app, port=port)

    driver = config.get('selenium.driver', 'firefox')
    browser = {
        'firefox': webdriver.Firefox,
        'chrome': webdriver.Chrome,
        'ie': webdriver.Ie,
    }[driver]()
Ejemplo n.º 6
0
 def setUp(self):
     self.server = StopableWSGIServer.create(debug_app)
     self.application_url = self.server.application_url.rstrip('/')
     self.proxy = proxies.HostProxy(self.application_url,
                                    client=self.client,
                                    **self.client_options)
     self.app = TestApp(self.proxy)
Ejemplo n.º 7
0
def setUpModule():
    global browser, server

    inipath = os.path.abspath(os.environ.get('TEST_INI', 'test.ini'))
    if not os.path.isfile(inipath):
        raise RuntimeError(
            'Cannot find INI file to setup selenium tests. '
            'Please specify the path via the TEST_INI environment variable '
            'or by adding a test.ini file to the current directory.')

    parser = ConfigParser()
    parser.read(inipath)

    config.update(parser.items('testconfig'))
    config['test_providers'] = splitlines(config['test_providers'])

    app = get_app(inipath)
    port = int(config['app_port'])
    server = StopableWSGIServer.create(app, port=port)

    driver = config.get('selenium.driver', 'firefox')
    browser = {
        'firefox': webdriver.Firefox,
        'chrome': webdriver.Chrome,
        'ie': webdriver.Ie,
    }[driver]()
Ejemplo n.º 8
0
 def setUp(self):
     self.server = StopableWSGIServer.create(debug_app)
     self.application_url = self.server.application_url.rstrip('/')
     self.proxy = proxies.HostProxy(self.application_url,
                                    client=self.client,
                                    **self.client_options)
     self.app = TestApp(self.proxy)
Ejemplo n.º 9
0
 def __init__(self, app=None, url=None, timeout=30000,
              extra_environ=None, relative_to=None, **kwargs):
     super(TestApp, self).__init__(app, relative_to=relative_to)
     self.server = StopableWSGIServer.create(app)
     self.server.wait()
     self.application_url = self.server.application_url
     os.environ['APPLICATION_URL'] = self.application_url
     self.extra_environ = extra_environ or {}
     self.timeout = timeout
     self.test_app = self
Ejemplo n.º 10
0
def server_for_test_app(app):
    try:
        from paste.exceptions.errormiddleware import ErrorMiddleware
        error_app = ErrorMiddleware(app.app, debug=True, error_log="errors.log")
        server = StopableWSGIServer.create(error_app)
    except ImportError:
        # paste.exceptions not available for Python 3.
        error_app = app.app
        server = StopableWSGIServer.create(error_app)
    try:
        server.wait()
        yield server
    finally:
        server.shutdown()
    # There seem to be persistent transient problems with the testing, sleeping
    # between creation of test app instances for greater than .5 seconds seems
    # to help (async loop length in code is .5 so this maybe makes some sense?)
    if "TEST_WEBAPP_POST_SHUTDOWN_SLEEP" in environ:
        time.sleep(int(environ.get("TEST_WEBAPP_POST_SHUTDOWN_SLEEP")))
Ejemplo n.º 11
0
def server_for_test_app(app):
    try:
        from paste.exceptions.errormiddleware import ErrorMiddleware
        error_app = ErrorMiddleware(app.app, debug=True, error_log="errors.log")
        server = StopableWSGIServer.create(error_app)
    except ImportError:
        # paste.exceptions not available for Python 3.
        error_app = app.app
        server = StopableWSGIServer.create(error_app)
    try:
        server.wait()
        yield server
    finally:
        server.shutdown()
    # There seem to be persistent transient problems with the testing, sleeping
    # between creation of test app instances for greater than .5 seconds seems
    # to help (async loop length in code is .5 so this maybe makes some sense?)
    if "TEST_WEBAPP_POST_SHUTDOWN_SLEEP" in environ:
        time.sleep(int(environ.get("TEST_WEBAPP_POST_SHUTDOWN_SLEEP")))
Ejemplo n.º 12
0
def wsgi_server(settings, db, mock_dossiers, mock_organes_acteurs,
                amendements_repository):
    from zam_repondeur import make_app

    settings = {**settings, "zam.auth_cookie_secure": False}
    wsgi_app = make_app(None, **settings)
    server = StopableWSGIServer.create(wsgi_app)
    server.settings = settings
    yield server
    server.shutdown()
Ejemplo n.º 13
0
def server_for_test_app(app):
    try:
        from paste.exceptions.errormiddleware import ErrorMiddleware
        error_app = ErrorMiddleware(app.app, debug=True, error_log="errors.log")
    except ImportError:
        # paste.exceptions not available for Python 3.
        error_app = app
    server = StopableWSGIServer.create(error_app)
    try:
        server.wait()
        yield server
    finally:
        server.shutdown()
Ejemplo n.º 14
0
 def __init__(self,
              app=None,
              url=None,
              timeout=30000,
              extra_environ=None,
              relative_to=None,
              **kwargs):
     super(TestApp, self).__init__(app, relative_to=relative_to)
     self.server = StopableWSGIServer.create(app)
     self.server.wait()
     self.application_url = self.server.application_url
     os.environ['APPLICATION_URL'] = self.application_url
     self.extra_environ = extra_environ or {}
     self.timeout = timeout
     self.test_app = self
Ejemplo n.º 15
0
 def connect(self, cfg, customAppSettings={}, wrapper='StopableWSGIServer'):
     self.cfg = cfg
     # clear old connections
     # try:
     # DBSession.close()
     # DBSession.remove()
     # os.remove(self.cfg['app']['db'])
     # except:
     # pass
     # create appConfig from ini
     self.appSettings = appconfig(
         'config:' + os.path.join(
             os.path.dirname(__file__), '../../', self.cfg['app']['ini']
         )
     )
     # store some derived variables
     self.appSettings['sqlalchemy.url'] = 'sqlite:///'+self.cfg['app']['db']
     # merge/override appConfig with custom settings in cfg
     self.appSettings.update(self.cfg['app']['appSettings'])
     # merge/override appConfig with individual custom settings
     self.appSettings.update(customAppSettings)
     # app
     engine = engine_from_config(self.appSettings)
     DBSession.configure(bind=engine)
     Base.metadata.create_all(engine)
     from c3smembership import main
     app = main({}, **self.appSettings)
     # create srv
     if wrapper == 'StopableWSGIServer':
         self.srv = StopableWSGIServer.create(
             app,
             host=self.cfg['app']['host'],
             port=self.cfg['app']['port']
         )
         # check srv
         if not self.srv.wait():
             raise Exception('Server could not be fired up. Exiting ...')
     elif wrapper == 'TestApp':
         self.srv = TestApp(app)
     else:
         raise Exception('Wrapper could not be found. Exiting ...')
     # store some variables
     self.srv.db = DBSession
     self.srv.url = 'http://' + self.cfg['app']['host'] + ':' \
                    + self.cfg['app']['port'] + '/'
     # self.srv.lu = 'lu/' + self.cfg['member']['token'] + '/' \
     #              + self.cfg['member']['email']
     return self.srv
Ejemplo n.º 16
0
 def setUpServer(self):
     """Create a WSGI server instance and save it in self.server.
     """
     app = self.make_wsgi_app()
     kwargs = {}
     if self.host is not None:
         kwargs['host'] = self.host
     if self.port is not None:
         kwargs['port'] = int(self.port)
     self.server = StopableWSGIServer.create(app, **kwargs)
     # If we dynamically set the host/port, we want to reset it to localhost
     # Otherwise this will depend on, for example, the local network setup
     if self.host in (None, '0.0.0.0', '127.0.0.1', 'localhost'):
         self.server.effective_host = 'localhost'
     # Refresh the hostname and port in case we dynamically picked them
     self['host'] = self.host = self.server.effective_host
     self['port'] = self.port = int(self.server.effective_port)
Ejemplo n.º 17
0
 def setUpServer(self):
     """Create a WSGI server instance and save it in self.server.
     """
     app = self.make_wsgi_app()
     kwargs = {}
     if self.host is not None:
         kwargs['host'] = self.host
     if self.port is not None:
         kwargs['port'] = int(self.port)
     self.server = StopableWSGIServer.create(app, **kwargs)
     # If we dynamically set the host/port, we want to reset it to localhost
     # Otherwise this will depend on, for example, the local network setup
     if self.host in (None, '0.0.0.0', '127.0.0.1', 'localhost'):
         self.server.effective_host = 'localhost'
     # Refresh the hostname and port in case we dynamically picked them
     self['host'] = self.host = self.server.effective_host
     self['port'] = self.port = int(self.server.effective_port)
Ejemplo n.º 18
0
def wsgi_server(request, wsgi_server_app, wsgi_server_host_port):
    from webtest.http import StopableWSGIServer
    host, port = wsgi_server_host_port

    server = StopableWSGIServer.create(
        wsgi_server_app,
        host=host,
        port=port,
        threads=1,
        channel_timeout=60,
        cleanup_interval=10,
        expose_tracebacks=True,
    )
    assert server.wait()

    yield 'http://%s:%s' % wsgi_server_host_port

    server.shutdown()
Ejemplo n.º 19
0
def wsgi_server(request, wsgi_server_app, wsgi_server_host_port):
    from webtest.http import StopableWSGIServer
    host, port = wsgi_server_host_port

    server = StopableWSGIServer.create(
        wsgi_server_app,
        host=host,
        port=port,
        threads=1,
        channel_timeout=60,
        cleanup_interval=10,
        expose_tracebacks=True,
    )
    assert server.wait()

    yield 'http://%s:%s' % wsgi_server_host_port

    server.shutdown()
Ejemplo n.º 20
0
def _server(request, app, server_host_port):
    from webtest.http import StopableWSGIServer
    host, port = server_host_port

    server = StopableWSGIServer.create(
        app,
        host=host,
        port=port,
        threads=1,
        channel_timeout=60,
        cleanup_interval=10,
        expose_tracebacks=True,
    )
    assert server.wait()

    @request.addfinalizer
    def shutdown():
        server.shutdown()

    return 'http://%s:%s' % server_host_port
Ejemplo n.º 21
0
def _server(request, authenticated_app, server_host_port):
    from webtest.http import StopableWSGIServer
    host, port = server_host_port

    server = StopableWSGIServer.create(
        authenticated_app,
        host=host,
        port=port,
        threads=1,
        channel_timeout=60,
        cleanup_interval=10,
        expose_tracebacks=True,
    )
    assert server.wait()

    @request.addfinalizer
    def shutdown():
        server.shutdown()

    return 'http://%s:%s' % server_host_port
Ejemplo n.º 22
0
 def connect(self, cfg, customAppSettings=None,
             wrapper='StopableWSGIServer'):
     if customAppSettings is None:
         customAppSettings = {}
     self.cfg = cfg
     self.app_settings = appconfig(
         'config:' + os.path.join(
             os.path.dirname(__file__), '../../', self.cfg['app']['ini']
         )
     )
     # store some derived variables
     self.app_settings['sqlalchemy.url'] = 'sqlite:///'+self.cfg['app']['db']
     # merge/override appConfig with custom settings in cfg
     self.app_settings.update(self.cfg['app']['appSettings'])
     # merge/override appConfig with individual custom settings
     self.app_settings.update(customAppSettings)
     # app
     engine = engine_from_config(self.app_settings)
     DBSession.configure(bind=engine)
     Base.metadata.create_all(engine)
     from c3smembership import main
     app = main({}, **self.app_settings)
     # create srv
     if wrapper == 'StopableWSGIServer':
         self.srv = StopableWSGIServer.create(
             app,
             host=self.cfg['app']['host'],
             port=self.cfg['app']['port']
         )
         # check srv
         if not self.srv.wait():
             raise Exception('Server could not be fired up. Exiting ...')
     elif wrapper == 'TestApp':
         self.srv = TestApp(app)
     else:
         raise Exception('Wrapper could not be found. Exiting ...')
     # store some variables
     self.srv.db = DBSession
     self.srv.url = 'http://' + self.cfg['app']['host'] + ':' \
                    + self.cfg['app']['port'] + '/'
     return self.srv
Ejemplo n.º 23
0
def web_server(request, app:Router) -> str:
    """py.test fixture to create a WSGI web server for functional tests.

    The default web server address is localhost:8521. The port can be changed with ``websauna.test_web_server_port``.

    :param app: py.test fixture for constructing a WSGI application

    :return: localhost URL where the web server is running.
    """

    host = "localhost"
    port = int(app.initializer.config.registry.settings.get("websauna.test_web_server_port", 8521))

    server = StopableWSGIServer.create(app, host=host, port=port)
    server.wait()

    def teardown():
        server.shutdown()

    request.addfinalizer(teardown)
    return "http://{}:{}".format(host, port)
Ejemplo n.º 24
0
 def setUp(self):
     super(TestClient, self).setUp()
     settings = {'elasticsearch.host': self.es_cluster.urls}
     app = main({}, **settings)
     self.server = StopableWSGIServer.create(app)
     docs = []
     for i in range(1, 32):
         docs.append({
             'date': '2012-01-%.2d' % i,
             'downloads_count': i,
             'add_on': str(i % 2 + 1),
         })
     self.es_client.bulk_index('time_2012-01', 'downloads', docs)
     for j in range(2, 6):
         self.es_client.index('time_2012-%.2d' % j, 'downloads', {
             'date': '2012-%.2d-01' % j,
             'downloads_count': j,
             'add_on': str(j % 2),
         })
     self.es_client.refresh()
     self.server.wait()
Ejemplo n.º 25
0
def web_server(request: FixtureRequest, app: Router) -> str:
    """py.test fixture to create a WSGI web server for functional tests.

    The default web server address is localhost:8521. The port can be changed with ``websauna.test_web_server_port``.

    :param app: py.test fixture for constructing a WSGI application
    :return: localhost URL where the web server is running.
    """
    host = "localhost"
    port = int(
        app.initializer.config.registry.settings.get(
            "websauna.test_web_server_port", 8521))

    server = StopableWSGIServer.create(app, host=host, port=port)
    server.wait()

    def teardown():
        server.shutdown()

    request.addfinalizer(teardown)
    return "http://{0}:{1}".format(host, port)
Ejemplo n.º 26
0
def wsgi_server(application):
    """Return (running) WebTest's StopableWSGIServer for application."""
    server = StopableWSGIServer.create(application)
    server.wait()
    return server
Ejemplo n.º 27
0
 def setUp(self):
     self.server = StopableWSGIServer.create(debug_app)
     self.app = TestApp(Proxy(self.server.application_url))
Ejemplo n.º 28
0
def pytest_namespace():
    return dict(server=StopableWSGIServer.create(SimpleApp()))
Ejemplo n.º 29
0
def pytest_namespace():
    return dict(server=StopableWSGIServer.create(SimpleApp()))
Ejemplo n.º 30
0
 def http_server(self):
     if self._http_server is None:
         self._http_server = StopableWSGIServer.create(
             self.application, clear_untrusted_proxy_headers=True)
     return self._http_server
Ejemplo n.º 31
0
def webtest_server(application):
    """Return (running) WebTest's StopableWSGIServer for application."""
    server = StopableWSGIServer.create(application)
    server.wait()
    return server
Ejemplo n.º 32
0
class TestServer(object):
    def __init__(self, port=0, address='127.0.0.1'):
        self.request = {}
        self.response = {}
        self.response_once = {}
        self.port = port
        self.address = address
        self._handler = None
        self._thread = None
        self._server = None
        self._started = Event()
        self.config = {}
        self.config.update({
            'port': self.port,
        })
        self.reset()

    def reset(self):
        self.request.clear()
        self.request.update({
            'args': {},
            'args_binary': {},
            'headers': {},
            'cookies': None,
            'path': None,
            'method': None,
            'data': None,
            'files': {},
            'client_ip': None,
            'done': False,
            'charset': 'utf-8',
        })
        self.response.clear()
        self.response.update({
            'code': 200,
            'data': '',
            'headers': [],
            'cookies': [],
            'callback': None,
            'sleep': None,
            'charset': 'utf-8',
        })
        self.response_once.clear()

    def _build_web_app(self):
        """Build bottle web application that is served by HTTP server"""
        app = WebApplication(self)
        app.route('<path:re:.*>', method='ANY')(app.handle_any_request)
        return app

    def server_thread(self, server_created):
        """Ask HTTP server start processing requests

        This function is supposed to be run in separate thread.
        """

        # pylint: disable=line-too-long
        # params: https://github.com/Pylons/waitress/blob/master/waitress/adjustments.py#L79
        self._server = StopableWSGIServer(
            host=self.address,
            port=self.port,
            threads=1,
            expose_tracebacks=False,  #True,
            application=self._build_web_app())
        server_created.set()
        self._server.run()

    def start(self, daemon=True):
        """Start the HTTP server."""
        server_created = Event()
        self._thread = Thread(target=self.server_thread, args=[server_created])
        self._thread.daemon = daemon
        self._thread.start()
        if not server_created.wait(2):
            raise Exception('Could not create test server app instance')
        self._server.wait()

    def stop(self):
        """Stop tornado loop and wait for thread finished it work."""
        self._server.shutdown()
        self._thread.join()

    def get_url(self, path='', port=None):
        """Build URL that is served by HTTP server."""
        if port is None:
            port = self.port
        return urljoin('http://%s:%d/' % (self.address, port), path)

    def wait_request(self, timeout):
        """Stupid implementation that eats CPU."""
        start = time.time()
        while True:
            if self.request['done']:
                break
            time.sleep(0.01)
            if time.time() - start > timeout:
                raise WaitTimeoutError('No request processed in %d seconds' %
                                       timeout)
Ejemplo n.º 33
0
def setup_test(test):
    test.globs['server'] = StopableWSGIServer.create(debug_app)
    test.globs['application_url'] = test.globs['server'].application_url
    for example in test.examples:
        example.options.setdefault(ELLIPSIS, 1)