Files @ 17cf34f73ca6
Branch filter:

Location: majic-ansible-roles/roles/database_server/tests/test_default.py - annotation

branko
MAR-28: Implemented additional tests for mail_server role:

- Deploy a number of tools on clients in order to test SMTP, IMAP, and Sieve
services.
- Added one more user to LDAP directory for testing group restrictions.
- Deploy CA certificate on all testing machines for TLS validation purposes.
- Use different custom-configured cipher for mail server ciphers.
- Fixed invalid postmaster address for parameters-optional host.
- Deploy configuration files for use with Imap-CLI on client test machines.
- Updated testing of SMTP server to include checks for users that do not belong
to mail group.
- Extended some SMTP-related tests to cover both test servers.
- Some small fixes in SMTP-related tests for expected output from commands.
- Implemented tests covering Dovecot (IMAP + Sieve) functionality.
- Implemented tests for running/enabled services.
- Implemented tests for ClamAV.
- Implemented tests for firewall and connectivity.
- Implemented tests for Postfix TLS configuration.
- TODO: Tests for Sieve TLS configuration have not been written yet due to
limitation of available tools.
import testinfra.utils.ansible_runner

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


def test_installed_packages(Package):
    """
    Tests if the correct packages have been installed.
    """

    assert Package('mariadb-client').is_installed
    assert Package('mariadb-server').is_installed
    assert Package('python-mysqldb').is_installed


def test_service(Service):
    """
    Tests if the database server service is enabled on boot and running.
    """

    service = Service('mysql')

    assert service.is_enabled
    assert service.is_running


def test_root_password(Command):
    """
    Tests if the root password has been set correctly.
    """

    login = Command("mysql -uroot -proot_password -BNe 'show databases'")

    assert login.rc == 0
    assert "information_schema" in login.stdout
    assert "mysql" in login.stdout
    assert "performance_schema" in login.stdout


def test_root_my_cnf(File, Sudo):
    """
    Tests if the root my.cnf configuration has been set-up with correct
    user/password and permissions.
    """

    with Sudo():

        my_cnf = File('/root/.my.cnf')

        assert my_cnf.is_file
        assert my_cnf.user == 'root'
        assert my_cnf.group == 'root'
        assert my_cnf.mode == 0o400
        assert "user=root" in my_cnf.content
        assert "password=root_password" in my_cnf.content


def test_root_my_cnf_login(Command, Sudo):
    """
    Tets if the database server root login works using just the my.cnf
    configuration file.
    """

    with Sudo():

        login = Command("mysql -BNe 'show databases'")

        assert "information_schema" in login.stdout
        assert "mysql" in login.stdout
        assert "performance_schema" in login.stdout


def test_utf8_configuration_file(File):
    """
    Tests if UTF-8 database server configuration file has been deployed
    correctly.
    """

    config = File('/etc/mysql/conf.d/utf8.cnf')

    assert config.is_file
    assert config.user == 'root'
    assert config.group == 'root'
    assert config.mode == 0o644


def test_utf8_configuration(Command):
    """
    Tests if UTF-8 configuration has been applied correctly to server.
    """

    assert Command("mysql -uroot -proot_password -BNe 'drop database if exists test'").rc == 0
    assert Command("mysql -uroot -proot_password -BNe 'create database test'").rc == 0

    check_server = Command("mysql -uroot -proot_password test -BNe 'select @@character_set_server; select @@collation_server'")

    assert check_server.rc == 0
    assert check_server.stdout == "utf8\nutf8_general_ci"

    check_database = Command("mysql -uroot -proot_password test -BNe 'select @@character_set_database; select @@collation_database'")

    assert check_database.rc == 0
    assert check_database.stdout == "utf8\nutf8_general_ci"

    check_database = Command("mysql -uroot -proot_password -BNe 'select @@character_set_connection; select @@collation_connection'")

    assert check_database.rc == 0
    assert check_database.stdout == "utf8\nutf8_general_ci"