Files @ 770551dc8c6f
Branch filter:

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

branko
MAR-148: Improve the SSH connectivity tests in backup_server role to be more reliable:

- Introduce a session-level fixture for setting permissions for client
SSH private keys (fixes errors related to SSH requesting tighter
permissions).
- Add assertions for the tests that verify the backup clients cannot
connect to the regular SSH server in case the SSH private keys do
not have correct permissions (just in case).
import os

import testinfra.utils.ansible_runner


testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-mandatory')


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

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


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

    service = host.service('mysql')

    assert service.is_enabled
    assert service.is_running


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

    login = host.run("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(host):
    """
    Tests if the root my.cnf configuration has been set-up with correct
    user/password and permissions.
    """

    with host.sudo():

        my_cnf = host.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(host):
    """
    Tets if the database server root login works using just the my.cnf
    configuration file.
    """

    with host.sudo():

        login = host.run("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(host):
    """
    Tests if UTF-8 configuration has been applied correctly to server.
    """

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

    check_server = host.run("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\n"

    check_database = host.run("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\n"

    check_database = host.run("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\n"