Changeset - 27483663f5f0
[Not reviewed]
0 4 3
Branko Majic (branko) - 5 years ago 2020-12-23 21:09:49
branko@majic.rs
MAR-168: Introduce additional machine in database_server tests for testing deprecated features:

- Updated Molecule configuration, defining an additional machine.
- Set-up the new machine with pre-installed MariaDB server instance,
root login with password, and separate Debian system maintenance
user.
- Set-up configuration files for root and Debian system maintenance
user login.
- Run the default set of tests on the deprecated machine group.
7 files changed with 85 insertions and 1 deletions:
0 comments (0 inline, 0 general)
roles/database_server/molecule/default/files/deprecated-debian.cnf
Show inline comments
 
new file 100644
 
# Automatically generated for Debian scripts. DO NOT TOUCH!
 
[client]
 
host     = localhost
 
user     = debian-sys-maint
 
password = debian-sys-maint-password
 
socket   = /var/run/mysqld/mysqld.sock
 
[mysql_upgrade]
 
host     = localhost
 
user     = debian-sys-maint
 
password = debian-sys-maint-password
 
socket   = /var/run/mysqld/mysqld.sock
 
basedir  = /usr
roles/database_server/molecule/default/files/deprecated-root-my.cnf
Show inline comments
 
new file 100644
 
[client]
 
user=root
 
password=root_password
roles/database_server/molecule/default/group_vars/deprecated.yml
Show inline comments
 
new file 100644
 
---
 

	
 
db_root_password: "root_password"
roles/database_server/molecule/default/molecule.yml
Show inline comments
 
---
 

	
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 
  provider:
 
    name: virtualbox
 

	
 
lint:
 
  name: yamllint
 
  options:
 
    config-file: ../../.yamllint.yml
 

	
 
platforms:
 

	
 
  - name: parameters-mandatory-stretch64
 
    groups:
 
      - parameters-mandatory
 
    box: debian/contrib-stretch64
 
    memory: 512
 
    cpus: 1
 

	
 
  - name: deprecated-stretch64
 
    groups:
 
      - deprecated
 
    box: debian/contrib-stretch64
 
    memory: 512
 
    cpus: 1
 

	
 
provisioner:
 
  name: ansible
 
  config_options:
 
    defaults:
 
      force_valid_group_names: "ignore"
 
      interpreter_python: "/usr/bin/python3"
 
    ssh_connection:
 
      pipelining: "True"
 
  lint:
 
    name: ansible-lint
 

	
 
scenario:
 
  name: default
 

	
 
verifier:
 
  name: testinfra
 
  lint:
 
    name: flake8
roles/database_server/molecule/default/playbook.yml
Show inline comments
 
---
 

	
 
- hosts: parameters-mandatory
 
- hosts: parameters-mandatory,deprecated
 
  become: true
 
  roles:
 
    - database_server
roles/database_server/molecule/default/prepare.yml
Show inline comments
 
---
 

	
 
- name: Prepare
 
  hosts: all
 
  gather_facts: false
 
  tasks:
 
    - name: Install python for Ansible
 
      raw: test -e /usr/bin/python3 || (apt -y update && apt install -y python3-minimal)
 
      become: true
 
      changed_when: false
 

	
 
- hosts: all
 
  become: true
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: true
 
      changed_when: false
 

	
 
- hosts: deprecated
 
  become: true
 
  tasks:
 

	
 
  - name: Install MariaDB
 
    apt:
 
      name:
 
        - mariadb-client
 
        - mariadb-server
 
        - python3-mysqldb
 
      state: present
 

	
 
  - name: Enable and start MariaDB
 
    service:
 
      name: mysql
 
      state: started
 
      enabled: true
 

	
 
  - name: Set password for the root database user (creating separate entry for different hosts)
 
    mysql_user:
 
      check_implicit_admin: true
 
      name: root
 
      host: "{{ item }}"
 
      password: "root_password"
 
    with_items:
 
      - "localhost"
 
      - "127.0.0.1"
 
      - "::1"
 
      - "{{ ansible_hostname }}"
 

	
 
  - name: Deploy username and password for the root database user
 
    copy:
 
      src: "deprecated-root-my.cnf"
 
      dest: "/root/.my.cnf"
 
      owner: root
 
      group: root
 
      mode: 0400
 

	
 
  - name: Disable use of unix socket login
 
    command: "mysql -B -e \"update mysql.user set plugin='' where user='root' and plugin='unix_socket'; flush privileges;\""
 

	
 
  - name: Create Debian system maintenance user
 
    mysql_user:
 
      name: debian-sys-maint
 
      password: debian-sys-maint-password
 

	
 
  - name: Deploy Debian system maintenance user login configuration
 
    copy:
 
      src: "deprecated-debian.cnf"
 
      dest: "/etc/mysql/debian.cnf"
 
      owner: root
 
      group: root
 
      mode: 0600
roles/database_server/molecule/default/tests/test_default.py
Show inline comments
 
import os
 

	
 
import testinfra.utils.ansible_runner
 

	
 

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

	
 
testinfra_hosts += testinfra.utils.ansible_runner.AnsibleRunner(
 
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('deprecated')
 

	
 
testinfra_hosts = sorted(set(testinfra_hosts))
 

	
 

	
 
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-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_string
 
        assert "password=root_password" in my_cnf.content_string
 

	
 

	
 
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_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.
 
    """
 

	
 
    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'")
0 comments (0 inline, 0 general)