import os
import time

import psycopg2
import pytest

from docker import Client as DockerClient


def pytest_addoption(parser):
    parser.addoption("--dp", action="store_true", default=False,
                     help=("Force docker pull"))


@pytest.fixture(scope='session')
def docker_pull(request):
    return request.config.getoption("--dp")


@pytest.fixture(scope='session')
def session_id():
    """Unique session identifier, random string."""
    return 'aiohttp-admin'


@pytest.fixture(scope='session')
def docker():
    return DockerClient.from_env(assert_hostname=False)


@pytest.fixture(scope='session')
def pg_params(pg_server):
    return dict(**pg_server['pg_params'])


@pytest.yield_fixture(scope='session')
def pg_server(unused_port, docker, session_id, docker_pull):
    pg_tag = "9.5"
    print("Pulling postgress image")
    if docker_pull:
        docker.pull('postgres:{}'.format(pg_tag))
    port = unused_port()
    container = docker.create_container(
        image='postgres:{}'.format(pg_tag),
        name='aiopg-test-server-{}-{}'.format(pg_tag, session_id),
        ports=[5432],
        detach=True,
        host_config=docker.create_host_config(port_bindings={5432: port})
    )
    docker.start(container=container['Id'])
    host = os.environ.get('DOCKER_MACHINE_IP', '127.0.0.1')
    pg_params = dict(database='postgres',
                     user='postgres',
                     password='mysecretpassword',
                     host=host,
                     port=port)
    delay = 0.001
    for i in range(100):
        try:
            conn = psycopg2.connect(**pg_params)
            cur = conn.cursor()
            cur.close()
            conn.close()
            break
        except psycopg2.Error as e:
            print("Retry n = {} after e: {}".format(i, e))
            time.sleep(delay)
            delay *= 2
    else:
        pytest.fail("Cannot start postgres server")
    container['port'] = port
    container['pg_params'] = pg_params
    yield container

    docker.kill(container=container['Id'])
    docker.remove_container(container['Id'])

@@ -32,16 +32,31 @@
 sycopg2%0A
+import pymysql%0A
 import p
@@ -2096,24 +2096,1788 @@
 tainer(container%5B'Id'%5D)%0A
+%0A%0A@pytest.fixture%0Adef mysql_params(mysql_server):%0A    return dict(**mysql_server%5B'mysql_params'%5D)%0A%0A%0A@pytest.yield_fixture(scope='session')%0Adef mysql_server(unused_port, docker, session_id, docker_pull):%0A    mysql_tag = '5.7'%0A    if docker_pull:%0A        docker.pull('mysql:%7B%7D'.format(mysql_tag))%0A    port = unused_port()%0A    container = docker.create_container(%0A        image='mysql:%7B%7D'.format(mysql_tag),%0A        name='mysql-test-server-%7B%7D-%7B%7D'.format(mysql_tag, session_id),%0A        ports=%5B3306%5D,%0A        detach=True,%0A        environment=%7B'MYSQL_USER': 'aiohttp_admin',%0A                     'MYSQL_PASSWORD': 'mysecretpassword',%0A                     'MYSQL_DATABASE': 'aiohttp_admin',%0A                     'MYSQL_ROOT_PASSWORD': 'mysecretpassword'%7D,%0A        host_config=docker.create_host_config(port_bindings=%7B3306: port%7D)%0A    )%0A    docker.start(container=container%5B'Id'%5D)%0A    host = os.environ.get('DOCKER_MACHINE_IP', '127.0.0.1')%0A    mysql_params = dict(database='aiohttp_admin',%0A                        user='aiohttp_admin',%0A                        password='mysecretpassword',%0A                        host=host,%0A                        port=port)%0A    delay = 0.001%0A    for i in range(100):%0A        try:%0A            conn = pymysql.connect(**mysql_params)%0A            cur = conn.cursor()%0A            cur.execute(%22SELECT 1;%22)%0A            cur.close()%0A            conn.close()%0A            break%0A        except pymysql.Error as e:%0A            print(e)%0A            time.sleep(delay)%0A            delay *= 2%0A    else:%0A        pytest.fail(%22Cannot start postgres server: %7B%7D%22.format(e))%0A    container%5B'port'%5D = port%0A    container%5B'mysql_params'%5D = mysql_params%0A    yield container%0A%0A    docker.kill(container=container%5B'Id'%5D)%0A    docker.remove_container(container%5B'Id'%5D)%0A

add mysql fixture