예제 #1
0
    def __init__(self, addr):
        import django
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict['ENGINE'] == 'django.db.backends.sqlite3'
                    and conn.settings_dict['NAME'] == ':memory:'):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs = {'connections_override': connections_override}
        from django.conf import settings
        if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler
            liveserver_kwargs['static_handler'] = StaticFilesHandler
        else:
            from django.test.testcases import _StaticFilesHandler
            liveserver_kwargs['static_handler'] = _StaticFilesHandler

        if django.VERSION < (1, 11):
            host, possible_ports = parse_addr(addr)
            self.thread = LiveServerThread(host, possible_ports,
                                           **liveserver_kwargs)
        elif django.VERSION > (1, 11, 2):
            host, possible_ports = parse_addr(addr)
            self.thread = LiveServerThread(host,
                                           port=possible_ports[0],
                                           **liveserver_kwargs)
        else:
            try:
                host, port = addr.split(':')
            except ValueError:
                host = addr
            else:
                liveserver_kwargs['port'] = int(port)
            self.thread = LiveServerThread(host, **liveserver_kwargs)

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={'append': host}, )
        self._live_server_modified_settings.enable()

        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
    def __init__(self, addr):
        try:
            from django.test.testcases import LiveServerThread
        except ImportError:
            pytest.skip('live_server tests not supported in Django < 1.4')
        from django.db import connections

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict['ENGINE'] == 'django.db.backends.sqlite3'
                    and conn.settings_dict['NAME'] == ':memory:'):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs = {'connections_override': connections_override}
        try:
            from django.test.testcases import _StaticFilesHandler
            liveserver_kwargs['static_handler'] = _StaticFilesHandler
        except ImportError:
            pass

        host, possible_ports = parse_addr(addr)
        self.thread = LiveServerThread(host, possible_ports,
                                       **liveserver_kwargs)
        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
    def server_thread_start(self):
        try:
            conn = connections[self._instance.connection_alias]
            conn.creation.create_test_db(
                self._instance.verbosity,
                autoclobber=False,
                keepdb=False,
                serialize=conn.settings_dict['TEST'].get('SERIALIZE', True))
            conn.inc_thread_sharing()
            self._instance.connections_override[conn.alias] = conn
            if self._instance.fixtures:
                call_command(
                    'loaddata', *self._instance.fixtures, **{
                        'verbosity': self._instance.verbosity,
                        'database': conn.alias
                    })

            self._instance.server_thread = LiveServerThread(
                self._instance.host, StaticFilesHandler,
                self._instance.connections_override, self._instance.port)
            self._instance.server_thread.daemon = True
            self._instance.server_thread.start()
            self._instance.server_thread.is_ready.wait()

            if self._instance.server_thread.error:
                self.server_thread_stop()
                raise self._instance.server_thread.error

        except Exception as e:
            print(e)
            self.server_thread_stop()
예제 #4
0
    def handle(self, *args, **options):
        try:
            conn = connections['default']
            conn.creation.create_test_db(2, autoclobber=False, keepdb=False,
                                         serialize=conn.settings_dict['TEST'].get('SERIALIZE', True))
            conn.inc_thread_sharing()
            self.connections_override = {}
            self.connections_override[conn.alias] = conn
            fixt = ['mainsite/fixtures/initial-data.json']
            call_command('loaddata', *fixt,
                         **{'verbosity': 2, 'database': conn.alias})

            self.server_thread = LiveServerThread('127.0.0.1', StaticFilesHandler,
                                                            self.connections_override,
                                                            8090)
            self.server_thread.daemon = True
            self.server_thread.start()
            self.server_thread.is_ready.wait()

            if self.server_thread.error:
                self.server_thread.terminate()
                for conn in self.connections_override.values():
                    conn.dec_thread_sharing()
                    conn.close()
                raise self.server_thread.error

            time.sleep(120)

        except Exception as e:
            print(e)
            self.server_thread.terminate()
            for conn in self.connections_override.values():
                conn.dec_thread_sharing()
                conn.close()
예제 #5
0
    def start_server_thread(self):
        self.server_thread = LiveServerThread('localhost', _StaticFilesHandler)
        self.server_thread.daemon = True
        self.server_thread.start()

        # Wait for the live server to be ready
        self.server_thread.is_ready.wait()
        if self.server_thread.error:
            # Clean up behind ourselves, since tearDownClass won't get called in
            # case of errors.
            self.stop_server_thread()
            raise self.server_thread.error
        return 'http://%s:%s' % (self.server_thread.host,
                                 self.server_thread.port)
    def __init__(self, addr: str) -> None:
        from django.db import connections
        from django.test.testcases import LiveServerThread
        from django.test.utils import modify_settings

        liveserver_kwargs = {}  # type: Dict[str, Any]

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict["ENGINE"] == "django.db.backends.sqlite3"
                    and conn.settings_dict["NAME"] == ":memory:"):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs["connections_override"] = connections_override
        from django.conf import settings

        if "django.contrib.staticfiles" in settings.INSTALLED_APPS:
            from django.contrib.staticfiles.handlers import StaticFilesHandler

            liveserver_kwargs["static_handler"] = StaticFilesHandler
        else:
            from django.test.testcases import _StaticFilesHandler

            liveserver_kwargs["static_handler"] = _StaticFilesHandler

        try:
            host, port = addr.split(":")
        except ValueError:
            host = addr
        else:
            liveserver_kwargs["port"] = int(port)
        self.thread = LiveServerThread(host, **liveserver_kwargs)

        self._live_server_modified_settings = modify_settings(
            ALLOWED_HOSTS={"append": host})

        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
    def __init__(self, host, possible_ports):

        connections_override = {}

        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict['ENGINE'] == 'django.db.backends.sqlite3'
                    and conn.settings_dict['NAME'] == ':memory:'):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        self.thread = LiveServerThread(host, possible_ports,
                                       connections_override)
        self.thread.daemon = True
        self.thread.start()

        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
예제 #8
0
    def __init__(self, addr):
        from django.db import connections
        from django.test.testcases import LiveServerThread

        connections_override = {}
        for conn in connections.all():
            # If using in-memory sqlite databases, pass the connections to
            # the server thread.
            if (conn.settings_dict['ENGINE'] == 'django.db.backends.sqlite3'
                    and conn.settings_dict['NAME'] == ':memory:'):
                # Explicitly enable thread-shareability for this connection
                conn.allow_thread_sharing = True
                connections_override[conn.alias] = conn

        liveserver_kwargs = {'connections_override': connections_override}
        from django.conf import settings
        if ('django.contrib.staticfiles' in settings.INSTALLED_APPS
                and get_django_version() >= (1, 7)):
            from django.contrib.staticfiles.handlers import (StaticFilesHandler
                                                             )
            liveserver_kwargs['static_handler'] = StaticFilesHandler
        else:
            try:
                from django.test.testcases import _StaticFilesHandler
            except ImportError:
                pass
            else:
                liveserver_kwargs['static_handler'] = _StaticFilesHandler

        host, possible_ports = parse_addr(addr)
        self.thread = LiveServerThread(host, possible_ports,
                                       **liveserver_kwargs)
        self.thread.daemon = True
        self.thread.start()
        self.thread.is_ready.wait()

        if self.thread.error:
            raise self.thread.error
예제 #9
0
def start_django():
    server = LiveServerThread("localhost", SAUCE_PORTS, _StaticFilesHandler)
    server.daemon = True
    server.start()
    return server