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 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
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_proc_with_password( postgres_with_password: PostgreSQLExecutor, ) -> None: """Check that password option to postgresql_proc factory is honored.""" assert postgres_with_password.running() is True # no assertion necessary here; we just want to make sure it connects with # the password retry( lambda: psycopg.connect( dbname=postgres_with_password.user, user=postgres_with_password.user, password=postgres_with_password.password, host=postgres_with_password.host, port=postgres_with_password.port, ), possible_exception=psycopg.OperationalError, ) with pytest.raises(psycopg.OperationalError): psycopg.connect( dbname=postgres_with_password.user, user=postgres_with_password.user, password="******", host=postgres_with_password.host, port=postgres_with_password.port, )
def test_executor_init_with_password( request: FixtureRequest, monkeypatch: pytest.MonkeyPatch, tmp_path_factory: pytest.TempPathFactory, locale: str, ) -> None: """Test whether the executor initializes properly.""" config = get_config(request) monkeypatch.setenv("LC_ALL", locale) port = get_port(config["port"]) assert port is not None tmpdir = tmp_path_factory.mktemp(f"pytest-postgresql-{request.node.name}") datadir = tmpdir / f"data-{port}" datadir.mkdir() logfile_path = tmpdir / f"postgresql.{port}.log" executor = PostgreSQLExecutor( executable=config["exec"], host=config["host"], port=port, datadir=str(datadir), unixsocketdir=config["unixsocketdir"], logfile=str(logfile_path), startparams=config["startparams"], password="******", dbname="somedatabase", ) with executor: assert executor.running() psycopg.connect( dbname=executor.user, user=executor.user, password=executor.password, host=executor.host, port=executor.port, ) with pytest.raises(psycopg.OperationalError): psycopg.connect( dbname=executor.user, user=executor.user, password="******", host=executor.host, port=executor.port, ) assert not executor.running()
def test_noproc_cached_version(postgresql_proc: PostgreSQLExecutor) -> None: """Test that the version is being cached.""" postgresql_noproc = NoopExecutor( postgresql_proc.host, postgresql_proc.port, postgresql_proc.user, postgresql_proc.options, postgresql_proc.dbname, ) ver = retry( lambda: postgresql_noproc.version, # type: ignore[no-any-return] possible_exception=psycopg.OperationalError, ) with postgresql_proc.stopped(): assert ver == postgresql_noproc.version
def test_postgresql_proc(postgresql_proc: PostgreSQLExecutor) -> None: """Test different postgresql versions.""" assert postgresql_proc.running() is True
def postgresql_proc_fixture( request: FixtureRequest, tmp_path_factory: TempPathFactory) -> Iterator[PostgreSQLExecutor]: """ Process fixture for PostgreSQL. :param request: fixture request object :param tmp_path_factory: temporary path object (fixture) :returns: tcp executor """ config = get_config(request) postgresql_ctl = executable or config["exec"] logfile_prefix = logs_prefix or config["logsprefix"] pg_dbname = dbname or config["dbname"] pg_load = load or config["load"] # 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") tmpdir = tmp_path_factory.mktemp( f"pytest-postgresql-{request.fixturename}") if logfile_prefix: warn( f"logfile_prefix and logsprefix config option is deprecated, " f"and will be dropped in future releases. All fixture related " f"data resides within {tmpdir}", DeprecationWarning, ) pg_port = get_port(port) or get_port(config["port"]) assert pg_port is not None datadir = tmpdir / f"data-{pg_port}" datadir.mkdir() logfile_path = tmpdir / f"{logfile_prefix}postgresql.{pg_port}.log" 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=host or config["host"], port=pg_port, user=user or config["user"], password=password or config["password"], dbname=pg_dbname, options=options or config["options"], datadir=str(datadir), unixsocketdir=unixsocketdir or config["unixsocketdir"], logfile=str(logfile_path), startparams=startparams or config["startparams"], postgres_options=postgres_options or config["postgres_options"], ) # start server with postgresql_executor: postgresql_executor.wait_for_postgres() template_dbname = f"{postgresql_executor.dbname}_tmpl" with DatabaseJanitor( user=postgresql_executor.user, host=postgresql_executor.host, port=postgresql_executor.port, dbname=template_dbname, version=postgresql_executor.version, password=postgresql_executor.password, ) as janitor: for load_element in pg_load: janitor.load(load_element) yield postgresql_executor