ClearcodeHQ / pytest-postgresql

This is a pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database. It allows you to specify fixtures for PostgreSQL process and client.
https://pypi.python.org/pypi/pytest-postgresql/
GNU Lesser General Public License v3.0
415 stars 44 forks source link

Problem calling pg_ctl with "--auth=trust" #343

Closed BrunoCoimbra closed 2 months ago

BrunoCoimbra commented 3 years ago

Hello,

I noticed the changes in 2.5.1 seem to break pytest_postgresql fixtures. The problem happens when pg_ctl tries to call initdb adding the "--auth=trust" parameter:

subprocess.CalledProcessError: Command '['/usr/bin/pg_ctl', 'initdb', '--pgdata', '/tmp/postgresqldata.15455', '-o', '--username=postgres --auth=trust']' returned non-zero exit status 1.

Executing the same command directly on terminal I get:

/usr/bin/pg_ctl initdb --pgdata /tmp/postgresqldata.15455 -o --username=postgres --auth=trust
/usr/bin/pg_ctl: unrecognized option '--auth=trust'
Try "pg_ctl --help" for more information.

Are there new configuration requirements I'm missing, or is it an actual bug?

Thanks, Bruno

fizyk commented 3 years ago

Could you check v2.5.2 if it still occurs? What system are you on? And did that worked for you before?

BrunoCoimbra commented 3 years ago

Ok v2.5.2 seems to have solved the problem. We can close this issue. Thanks for the help.

michael-burt commented 3 years ago

I am still experiencing this issue in v2.5.2.

$ python
Python 3.8.6 (default, Oct 13 2020, 20:49:19)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest_postgresql
>>> pytest_postgresql.__version__
'2.5.2'
>>> exit()
$ /usr/lib/postgresql/11/bin/pg_ctl initdb --pgdata /tmp/postgresqldata.25110 -o --username=postgres --auth=trust
/usr/lib/postgresql/11/bin/pg_ctl: unrecognized option '--auth=trust'
Try "pg_ctl --help" for more information.
$ /usr/lib/postgresql/11/bin/pg_ctl --version
pg_ctl (PostgreSQL) 11.9 (Debian 11.9-0+deb10u1)
fizyk commented 3 years ago

If anyone is experiencing this issue, I'll accept tested solution, review pull request, but actually have to time to actively work on it.

davidlee-ca commented 3 years ago

I ran into this problem on Mac.

Turns out it was a stray copy of libpq that didn't support the --auth flag. My solution: remove libpq, remove and reinstall postgres.

blelia commented 8 months ago

I am using the same version, and I have the same problem, reinstalling did not solve the problem.
Any ideas?

> pg_ctl --version           
pg_ctl (PostgreSQL) 16.1 (Homebrew)
> python3.11 -m pip show pytest-postgresql
Name: pytest-postgresql
Version: 5.0.0
Summary: Postgresql fixtures and fixture factories for Pytest.
Home-page: 
Author: 
Author-email: Grzegorz Śliwiński <fizyk+pypi@fizyk.dev>
License: 
Location: /opt/homebrew/lib/python3.11/site-packages
Requires: mirakuru, port-for, psycopg, pytest, setuptools
Required-by: 
> pg_ctl initdb --pgdata /XXX/YYY/ZZZ/pytest-postgresql-postgresql_proc0/data-65432 -o --username=myusername --auth=trust                
pg_ctl: unrecognized option `--auth=trust'
Try "pg_ctl --help" for more information.
nachonavarro commented 4 months ago

Same here, auth is not recognized

realDragonium commented 4 months ago

I experienced the same issues, the subprocess.CalledProcessError: Command error, in my main terminal application, but in some other terminals it worked for some reason.

After a bit of digging, it got my tests working again after adding LANG=en_US.UTF-8 in front of my pytest run command.

Adding this env variable in front of the actual command being ran doesn't resolve the pg_ctl: unrecognised option--auth=trust'` issue.

If you find this comment, I hope this might be useful.

fizyk commented 4 months ago

I'm reopening it, @realDragonium that's the first lead I got in this case. I wonder why does it work when using before all pytest command and not if it's used for just pg_ctl 🤔

fizyk commented 4 months ago

Okay, we use envvars for main process start, but here https://github.com/ClearcodeHQ/pytest-postgresql/blob/main/pytest_postgresql/executor.py#L180 it's not used, @realDragonium have you tried it there? (line 180 or line 184?

realDragonium commented 4 months ago

Glad to see this issue being reopened!

If i run my test WITH the env var, the subcommand being ran logs the following:

initdb: could not find suitable text search configuration for locale "UTF-8"

If i run my test WITHOUT the env var, i get:

initdb: error: invalid locale settings; check LANG and LC_* environment variables
pg_ctl: database system initialization failed
subprocess.CalledProcessError: Command '['/opt/homebrew/Cellar/postgresql@16/16.3/bin/pg_ctl', 'initdb', '--pgdata', '/private/var/folders/6q/g8ntf_8555lc6m3xzcsjzt4r0000gn/T/pytest-of-drago/pytest-148/pytest-postgresql-postgresql_proc0/data-30269', '-o', '--username=postgres --auth=trust']' returned non-zero exit status 1.

I have confirmed that there is no LANG env var in environment of the subprocess command, when running the test without setting the env var. I did this by printing out the result of running env cmd in subprocess.check_output.

Is this what you're looking for?

fizyk commented 4 months ago

okay, but the first is only in logs and the whole test suite runs fine, right?

Would you be able to modify your copy of pytest-postgresql with such diff and see the results?

diff --git a/pytest_postgresql/executor.py b/pytest_postgresql/executor.py
index 2579749..d61acc1 100644
--- a/pytest_postgresql/executor.py
+++ b/pytest_postgresql/executor.py
@@ -177,11 +177,11 @@ def init_directory(self) -> None:
                 password_file.write(password)
                 password_file.flush()
                 init_directory += ["-o", " ".join(options)]
-                subprocess.check_output(init_directory)
+                subprocess.check_output(init_directory, env=self._envvars)
         else:
             options += ["--auth=trust"]
             init_directory += ["-o", " ".join(options)]
-            subprocess.check_output(init_directory)
+            subprocess.check_output(init_directory, env=self._envvars)

         self._directory_initialised = True
realDragonium commented 4 months ago

okay, but the first is only in logs and the whole test suite runs fine, right?

Yes it does

Would you be able to modify your copy of pytest-postgresql with such diff and see the results?

Yes, it works with the provided modification. (And it stops working when i remove the change)

Awesome!

fizyk commented 4 months ago

Okay, got pull request, now to think a bit how to test it.