Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/pytest_postgresql/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,25 @@ def init_directory(self):
return
# remove old one if exists first.
self.clean_directory()
init_directory = (
self.executable, 'initdb',
'-o "--auth=trust --username=%s"' % self.user,
'-D %s' % self.datadir,
)
init_directory = [self.executable, 'initdb', '--pgdata', self.datadir]
options = ['--username=%s' % self.user]

if self.password:
with tempfile.NamedTemporaryFile() as password_file:
init_directory += (
'--pwfile "%s"' % password_file.name,
)
password_file.write(self.password)
subprocess.check_output(' '.join(init_directory), shell=True)
options += ['--auth=password',
'--pwfile=%s' % password_file.name]
if hasattr(self.password, 'encode'):
password = self.password.encode('utf-8')
else:
password = self.password
password_file.write(password)
password_file.flush()
init_directory += ['-o', ' '.join(options)]
subprocess.check_output(init_directory)
else:
subprocess.check_output(' '.join(init_directory), shell=True)
options += ['--auth=trust']
init_directory += ['-o', ' '.join(options)]
subprocess.check_output(init_directory)

self._directory_initialised = True

Expand Down
28 changes: 28 additions & 0 deletions tests/test_executor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Test various executor behaviours."""
import psycopg2
import pytest
from pkg_resources import parse_version

from pytest_postgresql.executor import PostgreSQLExecutor, PostgreSQLUnsupported
from pytest_postgresql.factories import postgresql_proc
from pytest_postgresql.factories import get_config
from pytest_postgresql.port import get_port

Expand Down Expand Up @@ -32,3 +34,29 @@ def test_unsupported_version(request):

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


postgres_with_password = postgresql_proc(password='hunter2')


def test_proc_with_password(
postgres_with_password): # pylint: disable=redefined-outer-name
"""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
psycopg2.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)

with pytest.raises(psycopg2.OperationalError):
psycopg2.connect(
dbname=postgres_with_password.user,
user=postgres_with_password.user,
password='bogus',
host=postgres_with_password.host,
port=postgres_with_password.port)