"""
Role tests
"""

from testinfra.utils.ansible_runner import AnsibleRunner

testinfra_hosts = AnsibleRunner('.molecule/ansible_inventory').get_hosts('all')

PG_VERSION = {
    'debian': {
        'jessie': '9.4',
    },
    'ubuntu': {
        'trusty': '9.3',
        'xenial': '9.5',
    },
}


def test_packages(host):
    """
    Check if packages are installed
    """

    packages = []
    os_distribution = host.system_info.distribution
    os_codename = host.system_info.codename
    pg_version = PG_VERSION[os_distribution][os_codename]

    if host.system_info.distribution in ('debian', 'ubuntu'):
        packages = [
            'ca-certificates',
            'locales',
            'postgresql-{}'.format(pg_version),
            'postgresql-common',
            'postgresql-client-{}'.format(pg_version),
            'postgresql-client-common',
            'postgresql-contrib-{}'.format(pg_version),
            'python-psycopg2',
        ]

    for package in packages:
        assert host.package(package).is_installed


def test_service(host):
    """
    Check if database service is started and enabled
    """

    service = ''

    if host.system_info.distribution in ('debian', 'ubuntu'):
        service = 'postgresql'

    assert host.service(service).is_enabled

    # Systemctl not available with Docker images
    if 'docker' != host.backend.NAME:
        assert host.service(service).is_running


def test_user(host):
    """
    Check if database user exists
    """

    if host.system_info.distribution in ('debian', 'ubuntu'):
        assert host.user('postgres').exists
        assert host.user('postgres').group == 'postgres'
        assert host.user('postgres').home == '/var/lib/postgresql'
        assert host.user('postgres').shell == '/bin/bash'


def test_config_files(host):
    """
    Check if configuration files exists
    """

    config_files = []
    os_distribution = host.system_info.distribution
    os_codename = host.system_info.codename
    pg_version = PG_VERSION[os_distribution][os_codename]

    if host.system_info.distribution in ('debian', 'ubuntu'):
        config_files = [
            '/etc/postgresql/{}/main/environment'.format(pg_version),
            '/etc/postgresql/{}/main/pg_ctl.conf'.format(pg_version),
            '/etc/postgresql/{}/main/pg_hba.conf'.format(pg_version),
            '/etc/postgresql/{}/main/pg_ident.conf'.format(pg_version),
            '/etc/postgresql/{}/main/postgresql.conf'.format(pg_version),
            '/etc/postgresql/{}/main/start.conf'.format(pg_version),
        ]

    for config_file in config_files:
        assert host.file(config_file).exists
        assert host.file(config_file).user == 'postgres'
        assert host.file(config_file).group == 'postgres'
        if 'ident' in config_file or 'hba' in config_file:
            assert host.file(config_file).mode == 0o640
        else:
            assert host.file(config_file).mode == 0o644

@@ -1432,16 +1432,23 @@
 ef test_
+system_
 user(hos
@@ -1472,24 +1472,22 @@
 heck if 
-database
+system
  user ex
@@ -2958,8 +2958,1266 @@
 = 0o644%0A
+%0A%0Adef test_databases_and_privileges(host):%0A    %22%22%22%0A    Check databases exists%0A    %22%22%22%0A%0A    expected_databases = %5B'foo', 'bar', 'foobar'%5D%0A    existing_databases = host.check_output(%22psql -U postgres -c '%5Cl'%22)%0A%0A    # Test databases%0A    for expected_database in expected_databases:%0A        assert ' %7B%7D '.format(expected_database) in existing_databases%0A%0A    # Test privileges%0A    assert 'bar_user' in existing_databases%0A    assert 'foo_user' not in existing_databases%0A    assert 'foobar_user' not in existing_databases%0A%0A%0Adef test_databases_extensions(host):%0A    %22%22%22%0A    Check databases extensions exists%0A    %22%22%22%0A%0A    databases = %5B'foo', 'bar', 'foobar'%5D%0A    psql_command = %22psql -U postgres -c '%5Cdx' %7B%7D%22%0A%0A    for database in databases:%0A        database_extensions = host.check_output(psql_command.format(database))%0A        if database == 'foo':%0A            assert ' cube ' in database_extensions%0A        else:%0A            assert ' cube ' not in database_extensions%0A%0A%0Adef test_users(host):%0A    %22%22%22%0A    Check users exists%0A    %22%22%22%0A%0A    expected_users = %5B'foo_user', 'bar_user', 'foobar_user'%5D%0A    existing_users = host.check_output(%22psql -U postgres -c '%5Cdu'%22)%0A%0A    for expected_user in expected_users:%0A        assert ' %7B%7D '.format(expected_user) in existing_users%0A

Add more tests