Files @ 2e3f14bc1505
Branch filter:

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

branko
MAR-192: Added support for Debian 12 Bookworm to database_server role:

- Newer versions of MariaDB are planning on making the utf8 become a
default alias for utf8mb4. Stick to utf8mb3 currently for
compatibility purposes.
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('python3-pymysql').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_utf8_configuration_file(host):
    """
    Tests if UTF-8 database server configuration file has been deployed
    correctly.
    """

    config = host.file('/etc/mysql/mariadb.conf.d/90-utf8.cnf')

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


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

    expected_character_set_and_collation = {
        "bullseye": "utf8\nutf8_general_ci\n",
        "bookworm": "utf8mb3\nutf8mb3_general_ci\n",
    }

    distribution_release = host.ansible("setup")["ansible_facts"]["ansible_distribution_release"]

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

        check_server = host.run("mysql -uroot test -BNe 'select @@character_set_server; select @@collation_server'")

        assert check_server.rc == 0
        assert check_server.stdout == expected_character_set_and_collation[distribution_release]

        check_database = host.run("mysql -uroot test -BNe 'select @@character_set_database; select @@collation_database'")

        assert check_database.rc == 0
        assert check_database.stdout == expected_character_set_and_collation[distribution_release]

        check_database = host.run("mysql -uroot -BNe 'select @@character_set_connection; select @@collation_connection'")

        assert check_database.rc == 0
        assert check_database.stdout == expected_character_set_and_collation[distribution_release]