import os
import time
import uuid

import psycopg2
import pytest
from docker import Client as DockerClient


def pytest_addoption(parser):
    parser.addoption(
        '--pg-tag',
        action='store',
        default=os.environ.get('PGTAG', '9.6'))


@pytest.fixture(scope='session')
def pg_tag(request):
    return request.config.getoption('--pg-tag')


@pytest.fixture(scope='session')
def session_id():
    return str(uuid.uuid4())


@pytest.fixture(scope='session')
def docker():
    return DockerClient(version='auto')


@pytest.yield_fixture(scope='session')
def pg_server(docker, pg_tag, session_id):
    docker.pull('postgres:{}'.format(pg_tag))
    container = docker.create_container(
        image='postgres:{}'.format(pg_tag),
        name='bawler-test-server-{}-{}'.format(pg_tag, session_id),
        detach=True,
    )
    docker.start(container=container['Id'])
    inspection = docker.inspect_container(container['Id'])
    host = inspection['NetworkSettings']['IPAddress']
    pg_params = dict(
        dbname='postgres',
        user='postgres',
        password='',
        host=host)

    delay = 0.001
    for i in range(100):
        try:
            conn = psycopg2.connect(**pg_params)
            cur = conn.cursor()
            cur.execute('SELECT 1;')
            cur.close()
            conn.close()
            break
        except psycopg2.Error as exc:
            time.sleep(delay)
            delay *= 2
    else:
        pytest.fail('Cannot start postgres server')

    container['host'] = host
    container['pg_params'] = pg_params

    yield container

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

@@ -1656,28 +1656,1872 @@
 _container(container%5B'Id'%5D)%0A
+%0A%0A@pytest.fixture(scope='session')%0Adef pg_server_stop(docker, pg_server):%0A    def _stop():%0A        conn = psycopg2.connect(**pg_server%5B'pg_params'%5D)%0A        docker.stop(pg_server%5B'Id'%5D)%0A        delay = 0.001%0A        for i in range(100):%0A            try:%0A                cur = conn.cursor()%0A                cur.execute('SELECT 1;')%0A                cur.close()%0A            except psycopg2.Error as exc:%0A                conn.close()%0A                break%0A            else:%0A                time.sleep(delay)%0A                delay *= 2%0A        else:%0A            pytest.fail('Cannot stop the server')%0A    return _stop%0A%0A%0A@pytest.fixture(scope='session')%0Adef pg_server_start(docker, pg_server):%0A    def _start():%0A        docker.start(pg_server%5B'Id'%5D)%0A        delay = 0.001%0A        for i in range(100):%0A            try:%0A                conn = psycopg2.connect(**pg_server%5B'pg_params'%5D)%0A                cur = conn.cursor()%0A                cur.execute('SELECT 1;')%0A                cur.close()%0A                conn.close()%0A                break%0A            except psycopg2.Error as exc:%0A                time.sleep(delay)%0A                delay *= 2%0A        else:%0A            pytest.fail('Cannot start postgres server')%0A    return _start%0A%0A%0A@pytest.fixture(scope='session')%0Adef pg_server_restart(docker, pg_server):%0A    def _restart():%0A        docker.restart(pg_server%5B'Id'%5D)%0A        delay = 0.001%0A        for i in range(100):%0A            try:%0A                conn = psycopg2.connect(**pg_server%5B'pg_params'%5D)%0A                cur = conn.cursor()%0A                cur.execute('SELECT 1;')%0A                cur.close()%0A                conn.close()%0A                break%0A            except psycopg2.Error as exc:%0A                time.sleep(delay)%0A                delay *= 2%0A        else:%0A            pytest.fail('Cannot start postgres server')%0A    return _restart%0A

Add start/stop/restart postgres container fixtures