Changeset - fc2c451981c8
[Not reviewed]
3 1 6
Branko Majic (branko) - 6 years ago 2017-11-26 00:10:12
branko@majic.rs
MAR-128: Upgraded tests for database_server role:

- Switch to new Molecule configuration.
- Updated set-up playbook to use become: yes.
- Moved some preparatory steps outside of the main playbook (eases
idempotence tests).
- Updated tests to reference the yml inventory file.
- Updated tests to use new fixture (host instead of individual ones).
9 files changed with 178 insertions and 68 deletions:
0 comments (0 inline, 0 general)
roles/database_server/meta/main.yml
Show inline comments
 
---
 

	
 
dependencies:
 
  - common
 
\ No newline at end of file
 
  - common
roles/database_server/molecule.yml
Show inline comments
 
deleted file
roles/database_server/molecule/default/create.yml
Show inline comments
 
new file 100644
 
---
 
- name: Create
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
 
  vars:
 
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
 
    molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
 
    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
 
  tasks:
 
    - name: Create molecule instance(s)
 
      molecule_vagrant:
 
        instance_name: "{{ item.name }}"
 
        instance_interfaces: "{{ item.interfaces | default(omit) }}"
 
        instance_raw_config_args: "{{ item.instance_raw_config_args | default(omit) }}"
 

	
 
        platform_box: "{{ item.box }}"
 
        platform_box_version: "{{ item.box_version | default(omit) }}"
 
        platform_box_url: "{{ item.box_url | default(omit) }}"
 

	
 
        provider_name: "{{ molecule_yml.driver.provider.name }}"
 
        provider_memory: "{{ item.memory | default(omit) }}"
 
        provider_cpus: "{{ item.cpus | default(omit) }}"
 
        provider_raw_config_args: "{{ item.raw_config_args | default(omit) }}"
 

	
 
        state: up
 
      register: server
 
      with_items: "{{ molecule_yml.platforms }}"
 

	
 
    # Mandatory configuration for Molecule to function.
 

	
 
    - name: Populate instance config dict
 
      set_fact:
 
        instance_conf_dict: {
 
          'instance': "{{ item.Host }}",
 
          'address': "{{ item.HostName }}",
 
          'user': "{{ item.User }}",
 
          'port': "{{ item.Port }}",
 
          'identity_file': "{{ item.IdentityFile }}", }
 
      with_items: "{{ server.results }}"
 
      register: instance_config_dict
 
      when: server.changed | bool
 

	
 
    - name: Convert instance config dict to a list
 
      set_fact:
 
        instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
 
      when: server.changed | bool
 

	
 
    - name: Dump instance config
 
      copy:
 
        # NOTE(retr0h): Workaround for Ansible 2.2.
 
        #               https://github.com/ansible/ansible/issues/20885
 
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
 
        dest: "{{ molecule_instance_config }}"
 
      when: server.changed | bool
roles/database_server/molecule/default/destroy.yml
Show inline comments
 
new file 100644
 
---
 

	
 
- name: Destroy
 
  hosts: localhost
 
  connection: local
 
  gather_facts: False
 
  no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
 
  vars:
 
    molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
 
    molecule_instance_config: "{{ lookup('env',' MOLECULE_INSTANCE_CONFIG') }}"
 
    molecule_yml: "{{ lookup('file', molecule_file) | molecule_from_yaml }}"
 
  tasks:
 
    - name: Destroy molecule instance(s)
 
      molecule_vagrant:
 
        instance_name: "{{ item.name }}"
 
        platform_box: "{{ item.box }}"
 
        provider_name: "{{ molecule_yml.driver.provider.name }}"
 
        force_stop: "{{ item.force_stop | default(True) }}"
 

	
 
        state: destroy
 
      register: server
 
      with_items: "{{ molecule_yml.platforms }}"
 

	
 
    # Mandatory configuration for Molecule to function.
 

	
 
    - name: Populate instance config
 
      set_fact:
 
        instance_conf: {}
 

	
 
    - name: Dump instance config
 
      copy:
 
        # NOTE(retr0h): Workaround for Ansible 2.2.
 
        #               https://github.com/ansible/ansible/issues/20885
 
        content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
 
        dest: "{{ molecule_instance_config }}"
 
      when: server.changed | bool
roles/database_server/molecule/default/molecule.yml
Show inline comments
 
new file 100644
 
---
 

	
 
dependency: {}
 

	
 
driver:
 
  name: vagrant
 
  provider:
 
    name: virtualbox
 

	
 
lint:
 
  name: yamllint
 

	
 
platforms:
 

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

	
 
provisioner:
 
  name: ansible
 
  config_options:
 
    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
 
new file 100644
 
---
 

	
 
- hosts: parameters-mandatory
 
  become: yes
 
  roles:
 
    - role: database_server
 
      db_root_password: "root_password"
roles/database_server/molecule/default/prepare.yml
Show inline comments
 
new file 100644
 
---
 

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

	
 
- hosts: all
 
  become: yes
 
  tasks:
 

	
 
    - name: Update all caches to avoid errors due to missing remote archives
 
      apt:
 
        update_cache: yes
 
      changed_when: False
roles/database_server/molecule/default/tests/test_default.py
Show inline comments
 
file renamed from roles/database_server/tests/test_default.py to roles/database_server/molecule/default/tests/test_default.py
 
import testinfra.utils.ansible_runner
 

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

	
 

	
 
def test_installed_packages(Package):
 
def test_installed_packages(host):
 
    """
 
    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
 
    assert host.package('mariadb-client').is_installed
 
    assert host.package('mariadb-server').is_installed
 
    assert host.package('python-mysqldb').is_installed
 

	
 

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

	
 
    service = Service('mysql')
 
    service = host.service('mysql')
 

	
 
    assert service.is_enabled
 
    assert service.is_running
 

	
 

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

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

	
 
    assert login.rc == 0
 
    assert "information_schema" in login.stdout
 
@@ -38,15 +38,15 @@ def test_root_password(Command):
 
    assert "performance_schema" in login.stdout
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

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

	
 
        assert my_cnf.is_file
 
        assert my_cnf.user == 'root'
 
@@ -56,28 +56,28 @@ def test_root_my_cnf(File, Sudo):
 
        assert "password=root_password" in my_cnf.content
 

	
 

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

	
 
    with Sudo():
 
    with host.sudo():
 

	
 
        login = Command("mysql -BNe 'show databases'")
 
        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(File):
 
def test_utf8_configuration_file(host):
 
    """
 
    Tests if UTF-8 database server configuration file has been deployed
 
    correctly.
 
    """
 

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

	
 
    assert config.is_file
 
    assert config.user == 'root'
 
@@ -85,25 +85,25 @@ def test_utf8_configuration_file(File):
 
    assert config.mode == 0o644
 

	
 

	
 
def test_utf8_configuration(Command):
 
def test_utf8_configuration(host):
 
    """
 
    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
 
    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 = Command("mysql -uroot -proot_password test -BNe 'select @@character_set_server; select @@collation_server'")
 
    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"
 

	
 
    check_database = Command("mysql -uroot -proot_password test -BNe 'select @@character_set_database; select @@collation_database'")
 
    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"
 

	
 
    check_database = Command("mysql -uroot -proot_password -BNe 'select @@character_set_connection; select @@collation_connection'")
 
    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"
roles/database_server/playbook.yml
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)