diff --git a/pytest_postgres/plugin.py b/pytest_postgres/plugin.py index f027f0b..66f031f 100644 --- a/pytest_postgres/plugin.py +++ b/pytest_postgres/plugin.py @@ -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') @@ -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 @@ -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}