Skip to content

Commit

Permalink
Get rid of pg-local option, slightly rewrite create test container logic
Browse files Browse the repository at this point in the history
  • Loading branch information
clayman083 committed May 8, 2018
1 parent ff59a25 commit 802fde2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ htmlcov/
.coverage
.coverage.*
.cache
**/coverage/*
nosetests.xml
coverage.xml
*,cover
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test: ## run tests quickly with the default Python
py.test

test-all: ## run tests on every Python version with tox
tox
tox -- --pg-image=postgres:alpine tests

coverage: ## check code coverage quickly with the default Python
coverage run --source pytest_postgres -m pytest
Expand Down
77 changes: 44 additions & 33 deletions pytest_postgres/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ def pytest_addoption(parser):
help='Specify database container name to use.')
parser.addoption('--pg-reuse', action='store_true',
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')

Expand All @@ -39,58 +37,71 @@ def check_connection(params):
pytest.fail('Cannot start postgres server')


def create_container(docker, image, name, ports, network=None):
container = None

if name:
for item in docker.containers.list(all=True):
if name in item.name:
container = item
break

if not container:
try:
docker.images.pull(image)
except docker_client.errors.APIError:
pass

container_params = {'image': image, 'name': name, 'detach': True}

if network:
container_params['network'] = network
else:
container_params['ports'] = ports

try:
container = docker.containers.create(**container_params)
except docker_client.errors.ImageNotFound:
pytest.fail('Image `{0}` not found'.format(image))

return container


@pytest.yield_fixture(scope='session')
def pg_server(docker, request):
pg_name = request.config.getoption('--pg-name')
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
if not pg_name:
pg_name = 'db-{}'.format(str(uuid.uuid4()))

if pg_name:
for item in docker.containers.list(all=True):
if pg_name in item.name:
container = item
break

if not container:
docker.images.pull(pg_image)
container = docker.containers.create(
image=pg_image,
name=pg_name,
ports={'5432/tcp': None},
network=pg_network if pg_network else None,
detach=True
)

container = create_container(docker, pg_image, pg_name, {'5432/tcp': None},
pg_network)
container.start()
container.reload()

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

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

try:
check_connection(pg_params)
yield {'network': container.attrs['NetworkSettings'],
'params': pg_params}
yield {
'network': container.attrs['NetworkSettings'],
'params': pg_params
}
finally:
if not pg_reuse:
container.kill()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='pytest-postgres',
version='0.4.0',
version='0.5.0',
packages=find_packages(),
url='https://github.com/clayman74/pytest-postgres',
licence='MIT',
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ deps=
commands =
coverage erase
coverage run --source pytest_postgres -m py.test \
tests
{posargs}
coverage report -m
coverage html
coverage xml
Expand Down

0 comments on commit 802fde2

Please sign in to comment.