Skip to content

Commit

Permalink
move docker creation into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vangheem committed Apr 10, 2017
1 parent 837b194 commit 224a1f6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 73 deletions.
1 change: 1 addition & 0 deletions buildout.cfg
Expand Up @@ -65,3 +65,4 @@ eggs =
[versions]
pycodestyle = 2.2.0
flake8 = 3.2.1
aiohttp = 2.0.5
76 changes: 3 additions & 73 deletions guillotina/tests/conftest.py
Expand Up @@ -5,16 +5,15 @@
from guillotina.interfaces import IApplication
from guillotina.testing import ADMIN_TOKEN
from guillotina.testing import TESTING_SETTINGS
from guillotina.tests.utils import cleanup_postgres_docker
from guillotina.tests.utils import get_mocked_request
from time import sleep
from guillotina.tests.utils import run_docker_postgresql

import aiohttp
import asyncio
import copy
import docker
import json
import os
import psycopg2
import pytest


Expand Down Expand Up @@ -44,68 +43,6 @@
DUMMY_SETTINGS['databases'][0]['db']['dsn'] = {}


def run_docker_postgresql():
docker_client = docker.from_env(version='1.23')

# Clean up possible other docker containers
test_containers = docker_client.containers.list(
all=True,
filters={'label': CONTAINERS_FOR_TESTING_LABEL})
for test_container in test_containers:
test_container.stop()
test_container.remove(v=True, force=True)

# Create a new one
container = docker_client.containers.run(
image=IMAGE,
labels=[CONTAINERS_FOR_TESTING_LABEL],
detach=True,
ports={
'5432/tcp': 5432
},
cap_add=['IPC_LOCK'],
mem_limit='1g',
environment={
'POSTGRES_PASSWORD': '',
'POSTGRES_DB': 'guillotina',
'POSTGRES_USER': 'postgres'
},
privileged=True
)
ident = container.id
count = 1

container_obj = docker_client.containers.get(ident)

opened = False
host = ''

while count < 30 and not opened:
count += 1
container_obj = docker_client.containers.get(ident)
print(container_obj.status)
sleep(2)
if container_obj.attrs['NetworkSettings']['IPAddress'] != '':
if os.environ.get('TESTING', '') == 'jenkins':
host = container_obj.attrs['NetworkSettings']['IPAddress']
else:
host = 'localhost'

if host != '':
try:
conn = psycopg2.connect("dbname=guillotina user=postgres host=%s port=5432" % host) # noqa
cur = conn.cursor()
cur.execute("SELECT 1;")
cur.fetchone()
cur.close()
conn.close()
opened = True
except: # noqa
conn = None
cur = None
return host


@pytest.fixture(scope='session')
def postgres():
"""
Expand All @@ -121,14 +58,7 @@ def postgres():
yield host # provide the fixture value

if not IS_TRAVIS:
docker_client = docker.from_env(version='1.23')
# Clean up possible other docker containers
test_containers = docker_client.containers.list(
all=True,
filters={'label': CONTAINERS_FOR_TESTING_LABEL})
for test_container in test_containers:
test_container.kill()
test_container.remove(v=True, force=True)
cleanup_postgres_docker()


class GuillotinaDBRequester(object):
Expand Down
79 changes: 79 additions & 0 deletions guillotina/tests/utils.py
Expand Up @@ -3,9 +3,12 @@
from guillotina.interfaces import IDefaultLayer
from guillotina.interfaces import IRequest
from guillotina.security.policy import Interaction
from time import sleep
from zope.interface import alsoProvides
from zope.interface import implementer

import docker
import os
import uuid


Expand Down Expand Up @@ -75,3 +78,79 @@ def _p_register(ob):
if ob._p_jar is None:
conn = FakeConnection()
conn._p_register(ob)


POSTGRESQL_IMAGE = 'postgres:9.6'


def run_docker_postgresql(label='testingaiopg'):
docker_client = docker.from_env(version='1.23')

# Clean up possible other docker containers
test_containers = docker_client.containers.list(
all=True,
filters={'label': label})
for test_container in test_containers:
test_container.stop()
test_container.remove(v=True, force=True)

# Create a new one
container = docker_client.containers.run(
image=POSTGRESQL_IMAGE,
labels=[label],
detach=True,
ports={
'5432/tcp': 5432
},
cap_add=['IPC_LOCK'],
mem_limit='1g',
environment={
'POSTGRES_PASSWORD': '',
'POSTGRES_DB': 'guillotina',
'POSTGRES_USER': 'postgres'
},
privileged=True
)
ident = container.id
count = 1

container_obj = docker_client.containers.get(ident)

opened = False
host = ''

while count < 30 and not opened:
count += 1
container_obj = docker_client.containers.get(ident)
print(container_obj.status)
sleep(2)
if container_obj.attrs['NetworkSettings']['IPAddress'] != '':
if os.environ.get('TESTING', '') == 'jenkins':
host = container_obj.attrs['NetworkSettings']['IPAddress']
else:
host = 'localhost'

if host != '':
try:
conn = psycopg2.connect("dbname=guillotina user=postgres host=%s port=5432" % host) # noqa
cur = conn.cursor()
cur.execute("SELECT 1;")
cur.fetchone()
cur.close()
conn.close()
opened = True
except: # noqa
conn = None
cur = None
return host


def cleanup_postgres_docker(label='testingaiopg'):
docker_client = docker.from_env(version='1.23')
# Clean up possible other docker containers
test_containers = docker_client.containers.list(
all=True,
filters={'label': label})
for test_container in test_containers:
test_container.kill()
test_container.remove(v=True, force=True)

0 comments on commit 224a1f6

Please sign in to comment.