From 18f93a9a8b05f4d492061db632f17e84103a99e0 2017-06-13 12:49:26 From: Branko Majic Date: 2017-06-13 12:49:26 Subject: [PATCH] MAR-24: Implemented tests for 'database_server' role: - Added Molecule configuration for single platform and instance. - Added test playbook that deploys database_server role for testing. - Fixed mode setting by appending leading zero. - Ignore task that immediatelly restarts MariaDB database server. - Implemented tests for the role covering full functionality. --- diff --git a/roles/database_server/molecule.yml b/roles/database_server/molecule.yml new file mode 100644 index 0000000000000000000000000000000000000000..243d6c61de3fc210d30d930d5c9c62631a4e119e --- /dev/null +++ b/roles/database_server/molecule.yml @@ -0,0 +1,27 @@ +--- + +dependency: + name: galaxy + +driver: + name: vagrant + +vagrant: + + platforms: + - name: debian-jessie64 + box: debian/contrib-jessie64 + + providers: + - name: virtualbox + type: virtualbox + options: + memory: 512 + cpus: 1 + + instances: + + - name: parameters-mandatory + +verifier: + name: testinfra diff --git a/roles/database_server/playbook.yml b/roles/database_server/playbook.yml new file mode 100644 index 0000000000000000000000000000000000000000..c2559a1b56acea0ecf088518ecfabd2a4bd4944b --- /dev/null +++ b/roles/database_server/playbook.yml @@ -0,0 +1,13 @@ +--- + +- hosts: all + tasks: + + - name: Update all caches to avoid errors due to missing remote archives + apt: + update_cache: yes + +- hosts: parameters-mandatory + roles: + - role: database_server + db_root_password: "root_password" diff --git a/roles/database_server/tasks/main.yml b/roles/database_server/tasks/main.yml index 9b180bc396647e56e1b5903e8a1e65d24ec4de6f..e2a738162c0d1b66007640ec881cf7b3d4aa74a0 100644 --- a/roles/database_server/tasks/main.yml +++ b/roles/database_server/tasks/main.yml @@ -20,19 +20,25 @@ - name: Deploy username and password for the root database user template: src="root_my.cnf.j2" dest="/root/.my.cnf" - owner=root group=root mode=400 + owner=root group=root mode=0400 - name: Set UTF-8 encoding as default for MariaDB copy: src="utf8.cnf" dest="/etc/mysql/conf.d/utf8.cnf" - owner=root group=root mode=644 + owner=root group=root mode=0644 register: mariadb_utf8_configuration - name: Restart MariaDB in order to use UTF-8 as default character set service: name=mysql state=restarted when: mariadb_utf8_configuration.changed + tags: + # [ANSIBLE0016] Tasks that run when changed should likely be handlers + # UTF-8 configuration must be applied immediatelly in order to ensure that + # subsequent tasks that create databases will end-up with correct (UTF-8) + # encoding. Otherwise they will be created using default latin1. + - skip_ansible_lint - name: Explicitly run all handlers include: ../handlers/main.yml when: "handlers | default(False) | bool() == True" tags: - - handlers \ No newline at end of file + - handlers diff --git a/roles/database_server/tests/test_default.py b/roles/database_server/tests/test_default.py new file mode 100644 index 0000000000000000000000000000000000000000..65d3ad3533f317c9e10751a693e110526ab1e2a4 --- /dev/null +++ b/roles/database_server/tests/test_default.py @@ -0,0 +1,109 @@ +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"