Skip to content

Commit 4365754

Browse files
fix: postgres use psql instead of logs (#704)
logs may be dependent on a particular locale fixes: #703, #695
1 parent 8f1165d commit 4365754

File tree

2 files changed

+21
-45
lines changed

2 files changed

+21
-45
lines changed

modules/postgres/testcontainers/postgres/__init__.py

+10-44
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313
import os
14-
from time import sleep
1514
from typing import Optional
1615

17-
from testcontainers.core.config import testcontainers_config as c
1816
from testcontainers.core.generic import DbContainer
1917
from testcontainers.core.utils import raise_for_deprecated_parameter
20-
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs
18+
from testcontainers.core.waiting_utils import wait_container_is_ready
2119

2220
_UNSET = object()
2321

@@ -91,45 +89,13 @@ def get_connection_url(self, host: Optional[str] = None, driver: Optional[str] =
9189

9290
@wait_container_is_ready()
9391
def _connect(self) -> None:
94-
# postgres itself logs these messages to the standard error stream:
95-
#
96-
# $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 \
97-
# > | grep -o -a -m 1 -h 'database system is ready to accept connections'
98-
# 2024-08-03 00:13:02.799 EDT [70226] LOG: starting PostgreSQL 14.11 (Homebrew) ....
99-
# 2024-08-03 00:13:02.804 EDT [70226] LOG: listening on IPv4 address "127.0.0.1", port 5432
100-
# ...
101-
# ^C2024-08-03 00:13:04.226 EDT [70226] LOG: received fast shutdown request
102-
# ...
103-
#
104-
# $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 2>&1 \
105-
# > | grep -o -a -m 1 -h 'database system is ready to accept connections'
106-
# database system is ready to accept connections
107-
#
108-
# and the setup script inside docker library postgres
109-
# uses pg_ctl:
110-
# https://github.com/docker-library/postgres/blob/66da3846b40396249936938ee17e9684e6968a57/16/alpine3.20/docker-entrypoint.sh#L261-L282
111-
# which prints logs to stdout:
112-
# https://www.postgresql.org/docs/current/app-pg-ctl.html#:~:text=the%20server%27s%20standard%20output%20and%20standard%20error%20are%20sent%20to%20pg_ctl%27s%20standard%20output
113-
#
114-
# so we must wait for both the setup and real startup:
115-
predicate_streams_and = True
116-
117-
wait_for_logs(
118-
self,
119-
".*database system is ready to accept connections.*",
120-
c.max_tries,
121-
c.sleep_time,
122-
predicate_streams_and=predicate_streams_and,
123-
#
92+
escaped_single_password = self.password.replace("'", "'\"'\"'")
93+
result = self.exec(
94+
[
95+
"sh",
96+
"-c",
97+
f"PGPASSWORD='{escaped_single_password}' psql --username {self.username} --dbname {self.dbname} --host 127.0.0.1 -c 'select version();'",
98+
]
12499
)
125-
126-
count = 0
127-
while count < c.max_tries:
128-
status, _ = self.exec(f"pg_isready -hlocalhost -p{self.port} -U{self.username}")
129-
if status == 0:
130-
return
131-
132-
sleep(c.sleep_time)
133-
count += 1
134-
135-
raise RuntimeError("Postgres could not get into a ready state")
100+
if result.exit_code:
101+
raise ConnectionError("pg_isready is not ready yet")

modules/postgres/tests/test_postgres.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from pathlib import Path
22

33
import pytest
4+
import sqlalchemy
45

56
from testcontainers.postgres import PostgresContainer
6-
import sqlalchemy
77

88

99
# https://www.postgresql.org/support/versioning/
@@ -46,6 +46,16 @@ def test_docker_run_postgres_with_driver_pg8000():
4646
connection.execute(sqlalchemy.text("select 1=1"))
4747

4848

49+
def test_password_with_quotes():
50+
postgres_container = PostgresContainer("postgres:9.5", password="'''''")
51+
with postgres_container as postgres:
52+
engine = sqlalchemy.create_engine(postgres.get_connection_url())
53+
with engine.begin() as connection:
54+
result = connection.execute(sqlalchemy.text("select version()"))
55+
for row in result:
56+
assert row[0].lower().startswith("postgresql 9.5")
57+
58+
4959
# This is a feature in the generic DbContainer class
5060
# but it can't be tested on its own
5161
# so is tested in various database modules:

0 commit comments

Comments
 (0)