def postgresql_proc_fixture(request):
        """
        Process fixture for PostgreSQL.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)
        postgresql_ctl = executable or config['exec']
        # check if that executable exists, as it's no on system PATH
        # only replace if executable isn't passed manually
        if not os.path.exists(postgresql_ctl) and executable is None:
            pg_bindir = subprocess.check_output(
                ['pg_config', '--bindir'], universal_newlines=True).strip()
            postgresql_ctl = os.path.join(pg_bindir, 'pg_ctl')

        pg_host = host or config['host']
        pg_port = get_port(port) or get_port(config['port'])
        datadir = os.path.join(gettempdir(),
                               'postgresqldata.{0}'.format(pg_port))
        pg_user = user or config['user']
        pg_unixsocketdir = unixsocketdir or config['unixsocketdir']
        pg_startparams = startparams or config['startparams']
        pg_logsdir = logsdir or config['logsdir']
        logfile_path = os.path.join(
            pg_logsdir,
            '{prefix}postgresql.{port}.log'.format(prefix=logs_prefix,
                                                   port=pg_port))

        init_postgresql_directory(postgresql_ctl, pg_user, datadir)

        if 'FreeBSD' == platform.system():
            with (datadir / 'pg_hba.conf').open(mode='a') as f:
                f.write('host all all 0.0.0.0/0 trust\n')

        postgresql_executor = PostgreSQLExecutor(
            executable=postgresql_ctl,
            host=pg_host,
            port=pg_port,
            user=pg_user,
            datadir=datadir,
            unixsocketdir=pg_unixsocketdir,
            logfile=logfile_path,
            startparams=pg_startparams,
        )

        def stop_server_and_remove_directory():
            postgresql_executor.stop()
            remove_postgresql_directory(datadir)

        request.addfinalizer(stop_server_and_remove_directory)

        # start server
        postgresql_executor.start()
        if '-w' in pg_startparams:
            wait_for_postgres(logfile_path, START_INFO)

        return postgresql_executor
示例#2
0
    def postgresql_proc_fixture(request, tmpdir_factory):
        """
        Process fixture for PostgreSQL.

        :param FixtureRequest request: fixture request object
        :rtype: pytest_dbfixtures.executors.TCPExecutor
        :returns: tcp executor
        """
        config = get_config(request)
        postgresql_ctl = executable or config['exec']
        # check if that executable exists, as it's no on system PATH
        # only replace if executable isn't passed manually
        if not os.path.exists(postgresql_ctl) and executable is None:
            pg_bindir = subprocess.check_output(
                ['pg_config', '--bindir'], universal_newlines=True
            ).strip()
            postgresql_ctl = os.path.join(pg_bindir, 'pg_ctl')

        pg_host = host or config['host']
        pg_port = get_port(port) or get_port(config['port'])
        datadir = os.path.join(
            gettempdir(), 'postgresqldata.{}'.format(pg_port))
        pg_user = user or config['user']
        pg_options = options or config['options']
        pg_unixsocketdir = unixsocketdir or config['unixsocketdir']
        pg_startparams = startparams or config['startparams']
        logfile_path = tmpdir_factory.mktemp("data").join(
            '{prefix}postgresql.{port}.log'.format(
                prefix=logs_prefix,
                port=pg_port
            )
        )

        if platform.system() == 'FreeBSD':
            with (datadir / 'pg_hba.conf').open(mode='a') as conf_file:
                conf_file.write('host all all 0.0.0.0/0 trust\n')

        postgresql_executor = PostgreSQLExecutor(
            executable=postgresql_ctl,
            host=pg_host,
            port=pg_port,
            user=pg_user,
            options=pg_options,
            datadir=datadir,
            unixsocketdir=pg_unixsocketdir,
            logfile=logfile_path,
            startparams=pg_startparams,
        )
        # start server
        with postgresql_executor:
            postgresql_executor.wait_for_postgres()

            yield postgresql_executor
def test_executor_init_with_password(request):
    """Test whether the executor initializes properly."""
    config = get_config(request)
    executor = PostgreSQLExecutor(
        executable=config['exec'],
        host=config['host'],
        port=get_port(config['port']),
        datadir='/tmp/error',
        unixsocketdir=config['unixsocketdir'],
        logfile='/tmp/version.error.log',
        startparams=config['startparams'],
        password="******",
    )
    with executor:
        assert executor.running()
        psycopg2.connect(dbname=executor.user,
                         user=executor.user,
                         password=executor.password,
                         host=executor.host,
                         port=executor.port)
        with pytest.raises(psycopg2.OperationalError):
            psycopg2.connect(dbname=executor.user,
                             user=executor.user,
                             password='******',
                             host=executor.host,
                             port=executor.port)
    assert not executor.running()
def test_unsupported_version(request):
    """Check that the error gets raised on unsupported postgres version."""
    config = get_config(request)
    executor = PatchedPostgreSQLExecutor(
        executable=config['exec'],
        host=config['host'],
        port=get_port(config['port']),
        datadir='/tmp/error',
        unixsocketdir=config['unixsocketdir'],
        logfile='/tmp/version.error.log',
        startparams=config['startparams'],
    )

    with pytest.raises(PostgreSQLUnsupported):
        executor.start()