Skip to content
Closed
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
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
- mongodb.py
- neo4j.py
- nginx.py
- postgresql.py
- rabbitmq.py
- redis.py
- selenium.py
Expand Down
4 changes: 2 additions & 2 deletions release-process.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Steps::
tox -esphinx-docs


Decleare package version
Declare package version
------------------------

In setup.py bump version to the next::
Expand Down Expand Up @@ -51,7 +51,7 @@ Steps::
python setup.py bdist_wheel


Check install capability for the whell
Check install capability for the wheel
--------------------------------------

Steps::
Expand Down
29 changes: 25 additions & 4 deletions testcontainers/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,48 @@ class PostgresContainer(DbContainer):
POSTGRES_USER = os.environ.get("POSTGRES_USER", "test")
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD", "test")
POSTGRES_DB = os.environ.get("POSTGRES_DB", "test")
POSTGRES_INITDB_ARGS = os.environ.get("POSTGRES_INITDB_ARGS", None)
POSTGRES_INITDB_WALDIR = os.environ.get("POSTGRES_INITDB_WALDIR", None)
POSTGRES_HOST_AUTH_METHOD = os.environ.get("POSTGRES_HOST_AUTH_METHOD", None)
PGDATA = os.environ.get("PGDATA", None)

env_names = [
"POSTGRES_USER",
"POSTGRES_PASSWORD",
"POSTGRES_DB",
"POSTGRES_INITDB_ARGS",
"POSTGRES_INITDB_WALDIR",
"POSTGRES_HOST_AUTH_METHOD",
"PGDATA"
]

def __init__(self,
image="postgres:latest",
port=5432, user=None,
password=None,
dbname=None,
driver="psycopg2",
initdb_args=None,
initdb_waldir=None,
host_auth_method=None,
pgdata=None,
**kwargs):
super(PostgresContainer, self).__init__(image=image, **kwargs)
self.POSTGRES_USER = user or self.POSTGRES_USER
self.POSTGRES_PASSWORD = password or self.POSTGRES_PASSWORD
self.POSTGRES_DB = dbname or self.POSTGRES_DB
self.port_to_expose = port
self.driver = driver

self.POSTGRES_INITDB_ARGS = initdb_args or self.POSTGRES_INITDB_ARGS
self.POSTGRES_INITDB_WALDIR = initdb_waldir or self.POSTGRES_INITDB_WALDIR
self.POSTGRES_HOST_AUTH_METHOD = host_auth_method or self.POSTGRES_HOST_AUTH_METHOD
self.PGDATA = pgdata or self.PGDATA
self.with_exposed_ports(self.port_to_expose)

def _configure(self):
self.with_env("POSTGRES_USER", self.POSTGRES_USER)
self.with_env("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD)
self.with_env("POSTGRES_DB", self.POSTGRES_DB)
for env_name in self.env_names:
if getattr(self, env_name) is not None:
self.with_env(env_name, getattr(self, env_name))

def get_connection_url(self, host=None):
return super()._create_connection_url(dialect="postgresql+{}".format(self.driver),
Expand Down
39 changes: 39 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest
import sqlalchemy
# import time

from testcontainers.postgres import PostgresContainer

# >= v14 use scram-sha-256 by default, so test changing to MD5
@pytest.mark.parametrize('version', ['14.5', '15.1'])
def test_docker_run_postgresql_md5(version):
_docker_run_postgresql_test(version, "md5")


# >= v10 to <v14 use md5 by default, so test changing to scram-sha-256.
@pytest.mark.parametrize('version', ['12.8', '13.5'])
def test_docker_run_postgresql_scram(version):
_docker_run_postgresql_test(version, "scram-sha-256")


def _docker_run_postgresql_test(version, method):
test_user = 'bob' + method[:3]
with PostgresContainer(
image=f'postgres:{version}',
user=test_user,
password='hi bob',
initdb_args=f'--auth-host={method}',
host_auth_method=f'{method}'
).with_bind_ports(5432, 45432) as postgres:
engine = sqlalchemy.create_engine(postgres.get_connection_url())
with engine.connect() as conn:
test_query = "SELECT rolname, rolpassword FROM pg_authid "\
f" WHERE rolname = '{test_user}';"
result = conn.execute(test_query)
name, password = result.fetchone()

assert name == test_user

# password_method = str(password[:len(method)])
# password_method = password_method.lower()
assert method == str(password[:len(method)]).lower()