Skip to content
Merged
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
15 changes: 13 additions & 2 deletions pytest_postgres/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def pytest_addoption(parser):
help='Save database container at the end')
parser.addoption('--pg-local', action='store_true',
help='Check postgresql container on localhost')
parser.addoption('--pg-network', action='store', default=None,
help='Specify docker network for the PostgreSQL container')


@pytest.fixture(scope='session')
Expand Down Expand Up @@ -43,6 +45,10 @@ def pg_server(docker, request):
pg_image = request.config.getoption('--pg-image')
pg_reuse = request.config.getoption('--pg-reuse')
pg_local = request.config.getoption('--pg-local')
pg_network = request.config.getoption('--pg-network')

if pg_local and pg_network:
pytest.fail('--pg-local and --pg-network are mutually exclusive.')

container = None
port = None
Expand All @@ -61,18 +67,23 @@ def pg_server(docker, request):
image=pg_image,
name=pg_name,
ports={'5432/tcp': None},
network=pg_network if pg_network else None,
detach=True
)

container.start()
container.reload()

host = container.attrs['NetworkSettings']['IPAddress']
port = 5432
ports = container.attrs['NetworkSettings']['Ports']
if pg_local:
host = '127.0.0.1'
ports = container.attrs['NetworkSettings']['Ports']
port = ports['5432/tcp'][0]['HostPort']
elif pg_network:
net = container.attrs['NetworkSettings']['Networks'][pg_network]
host = net['IPAddress']
else:
host = container.attrs['NetworkSettings']['IPAddress']

pg_params = {'database': 'postgres', 'user': 'postgres',
'password': 'postgres', 'host': host, 'port': port}
Expand Down