def postgresql_proc_fixture(request): """ #. Get config. #. Initialize postgresql data directory #. `Start a postgresqld server <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ #. Stop server and remove directory after tests. `See <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ :param FixtureRequest request: fixture request object :rtype: pytest_dbfixtures.executors.TCPExecutor :returns: tcp executor """ config = get_config(request) postgresql_ctl = executable or config.postgresql.postgresql_ctl # 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.postgresql.host pg_port = port or config.postgresql.port datadir = '/tmp/postgresqldata.{0}'.format(pg_port) logfile = '/tmp/postgresql.{0}.log'.format(pg_port) init_postgresql_directory(postgresql_ctl, config.postgresql.user, datadir, logfile) postgresql_executor = PostgreSQLExecutor( pg_ctl=postgresql_ctl, host=pg_host, port=pg_port, datadir=datadir, unixsocketdir=config.postgresql.unixsocketdir, logfile=logfile, startparams=config.postgresql.startparams, ) def stop_server_and_remove_directory(): subprocess.check_output( '{postgresql_ctl} stop -D {datadir} ' '-o "-p {port} -c unix_socket_directory=\'{unixsocketdir}\'"'. format(postgresql_ctl=postgresql_ctl, datadir=datadir, port=pg_port, unixsocketdir=config.postgresql.unixsocketdir), shell=True) postgresql_executor.stop() remove_postgresql_directory(logfile, datadir) request.addfinalizer(stop_server_and_remove_directory) # start server postgresql_executor.start() if '-w' in config.postgresql.startparams: wait_for_postgres(logfile, START_INFO) return postgresql_executor
def postgresql_proc_fixture(request): """ #. Get config. #. Initialize postgresql data directory #. `Start a postgresqld server <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ #. Stop server and remove directory after tests. `See <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ :param FixtureRequest request: fixture request object :rtype: pytest_dbfixtures.executors.TCPExecutor :returns: tcp executor """ config = get_config(request) postgresql_ctl = executable or config.postgresql.postgresql_ctl # 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.postgresql.host pg_port = get_port(port) or get_port(config.postgresql.port) datadir = path(gettempdir()) / 'postgresqldata.{0}'.format(pg_port) logsdir = path(request.config.getvalue('logsdir')) logfile_path = logsdir / '{prefix}postgresql.{port}.log'.format( prefix=logs_prefix, port=pg_port) init_postgresql_directory(postgresql_ctl, config.postgresql.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( pg_ctl=postgresql_ctl, host=pg_host, port=pg_port, datadir=datadir, unixsocketdir=config.postgresql.unixsocketdir, logfile=logfile_path, startparams=config.postgresql.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 config.postgresql.startparams: wait_for_postgres(logfile_path, START_INFO) return postgresql_executor
def postgresql_proc_fixture(request): """ #. Get config. #. Initialize postgresql data directory #. `Start a postgresqld server <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ #. Stop server and remove directory after tests. `See <http://www.postgresql.org/docs/9.1/static/app-pg-ctl.html>`_ :param FixtureRequest request: fixture request object :rtype: pytest_dbfixtures.executors.TCPExecutor :returns: tcp executor """ config = get_config(request) postgresql_ctl = executable or config.postgresql.postgresql_ctl # 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.postgresql.host pg_port = get_port(port or config.postgresql.port) datadir = '/tmp/postgresqldata.{0}'.format(pg_port) logsdir = path(request.config.getvalue('logsdir')) logfile_path = logsdir / '{prefix}postgresql.{port}.log'.format( prefix=logs_prefix, port=pg_port ) init_postgresql_directory( postgresql_ctl, config.postgresql.user, datadir ) postgresql_executor = PostgreSQLExecutor( pg_ctl=postgresql_ctl, host=pg_host, port=pg_port, datadir=datadir, unixsocketdir=config.postgresql.unixsocketdir, logfile=logfile_path, startparams=config.postgresql.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 config.postgresql.startparams: wait_for_postgres(logfile_path, START_INFO) return postgresql_executor