From dc188ddd07b561cf107443834b2d586c938385a8 Mon Sep 17 00:00:00 2001 From: "kacper.wolkiewicz" Date: Thu, 30 Jul 2020 22:26:55 +0200 Subject: [PATCH 1/6] Removed bug that did not allowed password connection, fixes #309 --- src/pytest_postgresql/executor.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index 2be05254..7b3f0b37 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -136,19 +136,24 @@ def init_directory(self): # 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, + self.executable, 'initdb', '-D {}'.format(self.datadir) ) if self.password: - with tempfile.NamedTemporaryFile() as password_file: + with tempfile.NamedTemporaryFile(mode='w') as password_file: init_directory += ( - '--pwfile "%s"' % password_file.name + '-o "--auth=trust --username={} --pwfile={}"'.format( + self.user, password_file.name + ), ) password_file.write(self.password) + # Without seek(0), PostgreSQL will se password file as empty + password_file.seek(0) subprocess.check_output(' '.join(init_directory), shell=True) else: + init_directory += ( + '-o "--auth=trust --username={}"'.format(self.user), + ) subprocess.check_output(' '.join(init_directory), shell=True) self._directory_initialised = True From b4c5164b8165fafabedd4cb901865db03c35c75e Mon Sep 17 00:00:00 2001 From: Kacper Wolkiewicz Date: Fri, 31 Jul 2020 13:20:43 +0200 Subject: [PATCH 2/6] Added tests for fixture with password --- tests/conftest.py | 2 ++ tests/test_postgresql.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 5670a6a2..9d883b32 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,4 +18,6 @@ postgresql_rand_proc = factories.postgresql_proc(port=None) postgresql_rand = factories.postgresql('postgresql_rand_proc') +postgresql_password_proc = factories.postgresql_proc(password='test') +postgresql_password = factories.postgresql('postgresql_password_proc') # pylint:enable=invalid-name diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 83f33b8f..f9fcf339 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -50,6 +50,11 @@ def test_rand_postgres_port(postgresql_rand): assert postgresql_rand.status == psycopg2.extensions.STATUS_READY +def test_rand_postgres_port(postgresql_password): + """Check if postgres fixture can be started on random port.""" + assert postgresql_password.status == psycopg2.extensions.STATUS_READY + + @pytest.mark.parametrize('_', range(2)) def test_postgres_terminate_connection( postgresql, _): From ee7870d50c80f38df15056e7bce25705196b06d8 Mon Sep 17 00:00:00 2001 From: Kacper Wolkiewicz Date: Fri, 31 Jul 2020 15:06:01 +0200 Subject: [PATCH 3/6] Changed formats to fstrings, changed seek to flush, deleted unnecessery write mode from tempfile, changed output flag to multiple ones --- src/pytest_postgresql/executor.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index 7b3f0b37..1515d8a0 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -136,24 +136,21 @@ def init_directory(self): # remove old one if exists first. self.clean_directory() init_directory = ( - self.executable, 'initdb', '-D {}'.format(self.datadir) + self.executable, 'initdb', f'-o "--auth=trust --username={self.user}"', + f'-D {self.datadir}' ) if self.password: - with tempfile.NamedTemporaryFile(mode='w') as password_file: + password = self.password if isinstance(self.password, bytes) else self.password.encode() + with tempfile.NamedTemporaryFile() as password_file: init_directory += ( - '-o "--auth=trust --username={} --pwfile={}"'.format( - self.user, password_file.name - ), + f'-o "--pwfile={password_file.name}"', ) - password_file.write(self.password) - # Without seek(0), PostgreSQL will se password file as empty - password_file.seek(0) + password_file.write(password) + # Without flush(), PostgreSQL will se password file as empty + password_file.flush() subprocess.check_output(' '.join(init_directory), shell=True) else: - init_directory += ( - '-o "--auth=trust --username={}"'.format(self.user), - ) subprocess.check_output(' '.join(init_directory), shell=True) self._directory_initialised = True From b7d9672932b30a221a76a53fc5213450094871fe Mon Sep 17 00:00:00 2001 From: "kacper.wolkiewicz" Date: Sat, 1 Aug 2020 08:12:24 +0200 Subject: [PATCH 4/6] Changed password test name --- tests/test_postgresql.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index f9fcf339..100d3582 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -50,7 +50,7 @@ def test_rand_postgres_port(postgresql_rand): assert postgresql_rand.status == psycopg2.extensions.STATUS_READY -def test_rand_postgres_port(postgresql_password): +def test_postgres_password(postgresql_password): """Check if postgres fixture can be started on random port.""" assert postgresql_password.status == psycopg2.extensions.STATUS_READY From 9337e257c13967d7f12717631e904da97429cb9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Fri, 21 Aug 2020 12:42:20 +0200 Subject: [PATCH 5/6] leftover --- src/pytest_postgresql/executor.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index 2bdbfcef..3e5b5516 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -151,8 +151,6 @@ def init_directory(self): password_file.flush() subprocess.check_output(' '.join(init_directory), shell=True) else: - options += ['--auth=trust'] - init_directory += ['-o', ' '.join(options)] subprocess.check_output(init_directory) self._directory_initialised = True From ab6827cb5c1b47502d88dfef33d5222bab16ffcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Grzegorz=20=C5=9Aliwi=C5=84ski?= Date: Fri, 21 Aug 2020 12:52:40 +0200 Subject: [PATCH 6/6] shell true --- src/pytest_postgresql/executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytest_postgresql/executor.py b/src/pytest_postgresql/executor.py index 3e5b5516..9350ecc3 100644 --- a/src/pytest_postgresql/executor.py +++ b/src/pytest_postgresql/executor.py @@ -151,7 +151,7 @@ def init_directory(self): password_file.flush() subprocess.check_output(' '.join(init_directory), shell=True) else: - subprocess.check_output(init_directory) + subprocess.check_output(' '.join(init_directory), shell=True) self._directory_initialised = True